Converting a coordinate table to a form file using R

I have a point coordinate dataset in the UTM 48 zone.

xy 615028.3 2261614 615016.3 2261635 614994.4 2261652 

The CSV file is here .

I would like to load a CSV and create a form file using R. My code:

 library(maptools) library(rgdal) library(sp) UTMcoor=read.csv(file="https://dl.dropboxusercontent.com/u/549234/s1.csv") coordinates(UTMcoor)=~X+Y proj4string(UTMcoor)=CRS("++proj=utm +zone=48") # set it to UTM LLcoor<-spTransform(UTMcoor,CRS("+proj=longlat")) #set it to Lat Long plot(LLcoor) points(LLcoor$X,LLcoor$Y,pch=19,col="blue",cex=0.8) #to test if coordinate can be plot as point map writeOGR(UTMcoor, dsn="c:/todel" ,layer="tsb",driver="ESRI Shapefile") writeSpatialShape("LLcoor","test") 

The last command (writeSpatialShape) R produces the following error:

 Error in writeSpatialShape("LL2", "test") : x is acharacterobject, not a compatible Spatial*DataFrame 

When I read LLcoor from the console, it seems that this is already a spatial DataFrame. Writing a form file using writeOGR (RGdal package) also gives a similar error. Any hints are welcome.

+6
source share
2 answers

Something is wrong in your example. Failure of the second and last line.

In any case, your mistake is pretty clear. You specify the variable name LL2 instead of the variable itself. But in your example, neither LLcoor nor UTMcoor are in the correct format for use with writeOGR or writeSpatialShape . You need to convert them to SpatialDataframe , for example:

 UTMcoor.df <- SpatialPointsDataFrame(UTMcoor, data.frame(id=1:length(UTMcoor))) 
+3
source

Following @Matthew Plourde's suggestion, I used the SpatialPointsDataFrame function to convert UMTcoor to a spatial data frame. This solved my problem.

There are also small details in writeOGR that were incorrect in my original script, then in the 1st argument there is no need to enter data in the 1st argument.

 library(maptools) library(rgdal) library(sp) filePath="https://dl.dropboxusercontent.com/u/549234/s1.csv" UTMcoor=read.csv(file=filePath) coordinates(UTMcoor)=~X+Y proj4string(UTMcoor)=CRS("++proj=utm +zone=48") # set it to UTM UTMcoor.df <- SpatialPointsDataFrame(UTMcoor, data.frame(id=1:length(UTMcoor))) LLcoor<-spTransform(UTMcoor.df,CRS("+proj=longlat")) LLcoor.df=SpatialPointsDataFrame(LLcoor, data.frame(id=1:length(LLcoor))) writeOGR(LLcoor.df, dsn="c:/todel" ,layer="t1",driver="ESRI Shapefile") writeSpatialShape(LLcoor.df, "t2") 
+1
source

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


All Articles