Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R : How to write an XYZ file from a SpatialPointsDataFrame?

Tags:

r

spatial

I have a SpatialPointsDataFrame which has one attribute (let's call it z for convenience) as well as lat/long coordinates.

I want to write this out to an XYZ file (i.e. an ASCII file with three columns).

Initially I tried

write.table(spdf, filename, row.names=FALSE)

but this wrote the z value first, followed by the coordinates, on each row. So it was ZXY format rather than XYZ. Not a big deal, perhaps, but annoying for other people who have to use the file.

At present I am using what feels like a really horrible bodge to do this (given below), but my question is: is there a good and straightforward way to write a SPDF out as XYZ, with the columns in the right order? It seems as though it ought to be easy!

Thanks for any advice.

Bodge:

dfOutput <- data.frame(x = coordinates(spdf)[,1], y = coordinates(spdf)[,2])
dfOutput$z <- data.frame(spdf)[,1]
write.table(dfOutput, filename, row.names=FALSE)
like image 564
Flyto Avatar asked Jun 27 '13 11:06

Flyto


People also ask

How does xyz file look like?

xyz files contain molecular model descriptions, including atom numbers, element symbols and X, Y, and Z coordinates. These . xyz files are usually created and saved in the plain text format.

What are xyz files used for?

xyz files are used as storage of information like timestamps and a list of positions.


1 Answers

Why not just

library(sp)
spdf <- SpatialPointsDataFrame(coords=matrix(rnorm(30), ncol = 2), 
                               data=data.frame(z = rnorm(15)))


write.csv(cbind(coordinates(spdf), spdf@data), file = "example.csv", 
          row.names = FALSE)
like image 108
Noah Avatar answered Sep 25 '22 04:09

Noah