R: How to write an XYZ file from SpatialPointsDataFrame?

I have a SpatialPointsDataFrame that has one attribute (let it be called z for convenience), as well as lat / long coordinates.

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

I tried first

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

but first it wrote down the z value, and then the coordinates on each line. So it was ZXY format, not XYZ. This may not be a big problem, but annoying other people who should use the file.

I am currently using what seems really terrible to do this (see below), but my question is: is there a good and easy way to write SPDF as XYZ with columns in the correct order? It seems like it should be easy!

Thanks for any advice.

mend:

 dfOutput <- data.frame(x = coordinates(spdf)[,1], y = coordinates(spdf)[,2]) dfOutput$z <- data.frame(spdf)[,1] write.table(dfOutput, filename, row.names=FALSE) 
+4
source share
2 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) 
+3
source

You can write to a .shp file using the writeOGR package from rgdal . Alternatively, you can fortify (from ggplot2 ) your data and write it as a csv file.

+2
source

Source: https://habr.com/ru/post/1488479/


All Articles