使用R将坐标表转换为形状文件

使用R将坐标表转换为形状文件,r,shapefile,R,Shapefile,我有一个UTM 48区的点坐标数据集 x y 615028.3 2261614 615016.3 2261635 614994.4 2261652 CSV文件 我想加载CSV并使用R创建shapefile。我的代码是: library(maptools) library(rgdal) library(sp) UTMcoor=read.csv(file="https://dl.dropboxusercontent.co

我有一个UTM 48区的点坐标数据集

  x           y       
615028.3  2261614    
615016.3  2261635    
614994.4  2261652    
CSV文件

我想加载CSV并使用R创建shapefile。我的代码是:

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")

当我从控制台读取LLcoor时,它似乎已经是一个空间数据帧。使用writeOGR(RGdal包)写入形状文件也会出现类似的错误。非常感谢您的任何提示。

您的示例有问题。倒数第二行也失败了

无论如何,你的错误很明显。您提供的是变量“LL2”的名称,而不是变量本身。但在您的示例中,无论是
LLcoor
还是
UTMcoor
都不是与
writeOGR
writespaceshape
一起使用的正确格式。您需要首先将它们转换为
SpatialDataframe
,例如:

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

UTMcoor.df根据@Matthew Plourde的建议,我使用函数SpatialPointsDataFrame将UMTCOR转换为空间数据帧。这解决了我的问题

writeOGR中还有一些小细节在我的原始脚本中是不正确的,第一个参数中的dataframe不应该放在双括号中

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")
库(maptools)
图书馆(rgdal)
图书馆(sp)
文件路径=”https://dl.dropboxusercontent.com/u/549234/s1.csv"
UTMcoor=read.csv(文件=文件路径)
坐标(UTMcoor)=~X+Y
proj4string(UTMcoor)=CRS(“++proj=utm+zone=48”)#将其设置为utm

df一件小事,你在代码开头漏掉了一个“l”,谢谢你的提问,这对我帮助很大!
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")