Convert table of coordinate to shape file using R

本秂侑毒 提交于 2019-12-04 06:20:19

There's something wrong with your example. The second to last line fails, too.

In any case, your error is pretty clear. You're supplying the name of the variable "LL2" instead of the variable itself. But in your example neither LLcoor nor UTMcoor are in the proper format to use with writeOGR or writeSpatialShape. You need to first convert them to SpatialDataframe, e.g., :

UTMcoor.df <- SpatialPointsDataFrame(UTMcoor, data.frame(id=1:length(UTMcoor)))
anh.hv

After suggestion by @Matthew Plourde, I have used the function SpatialPointsDataFrame to convert the UMTcoor to a spatial dataframe. That solved my problem.

There are also small detail in writeOGR that wasn't correct in my original script, the dataframe in the 1st argument should not be put in double bracket.

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")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!