使用 R 将坐标表转换为形状文件 [英] Convert table of coordinate to shape file using R

查看:49
本文介绍了使用 R 将坐标表转换为形状文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UTM 区域 48 中有一个点坐标数据集.

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

  x           y       
615028.3  2261614    
615016.3  2261635    
614994.4  2261652    

CSV 文件此处.

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

I would like to load the CSV and create shapefile using R. My code is:

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

在最后一个命令 (writeSpatialShape) R 中给出以下错误:

In the last command (writeSpatialShape) R give the following error:

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

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

As I read the LLcoor from the console it seem that it already a Spatial DataFrame. Writing shape file using writeOGR (RGdal package) also give similar error. Any hint is much appreciated.

推荐答案

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

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

无论如何,您的错误很明显.您提供的是变量LL2"的名称而不是变量本身.但是在您的示例中,LLcoorUTMcoor 都不是与 writeOGRwriteSpatialShape 一起使用的正确格式.您需要先将它们转换为 SpatialDataframe,例如:

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

这篇关于使用 R 将坐标表转换为形状文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆