如何将csv转换为R中的shp [英] How to convert csv to shp in R
问题描述
我过去几天一直在尝试将csv转换为shapefile。我知道我可以很容易地在QGIS或Arc中做,但是想将这个过程添加到现有的R代码中。
所以我可以在没有问题的情况下阅读csv p>
MyData< - read.csv(file =c:/TheDataIWantToReadIn.csv,header = TRUE,sep =, )
我从Packages Shapefile帮助指南中找到了下面的代码。然而,我似乎找不到一种方法来处理我的代码。我的行是每一个点,因此我要创建的shapefile将是所有的点。我没有Id列,但是我有两个单独的列中的x和y数据。
dd< - data.frame(Id = c(1,2),X = c(3,5) Y = c(9,6))
/ pre>
ddTable < - data.frame(Id = c(1,2),Name = c(Item1,Item2))
ddShapefile< - convert.to.shapefile(dd,ddTable,Id,1)
write.shapefile(ddShapefile,c:/ test,arcgis = T)
任何帮助将不胜感激。
解决方案将建议使用
rgdal
而不是shapefiles
。为了使用rgdal
,您必须从 http://cran.revolutionanalytics.com/web/packages/rgdal/ 。
以下代码应该让你在正确的方向:
install.packages(c(rgdal,sp))
库rgdal)
库(sp)
MyData< - read.csv(file =c:/TheDataIWantToReadIn.csv,header = TRUE,sep =,)
以下代码片段来自使用ggplot2包在R中映射。
class(MyData)#data.frame
coordinates(MyData)< - 〜longitude + latitude#无论等价于
#data.frame
class MyData)#[1]SpatialPointsDataFrame
#attr(,package)
#[1]sp
以下代码片段来自如何用投影编写shapefile - 解决问题
writeOGR(crest.sp,c:/ test,layer name,driver =ESRI Shapefile)
I have been trying for the past couple of days to convert a csv to shapefile. I know I can easily do in QGIS or Arc but would like to add this process into my existing R code.
So i can read in the csv with no issues
MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")
I found the code below from the Packages Shapefile help guide. However I can't seem to find a way for it to work on my code. My rows are each a point, therefore my shapefile I am trying to create will be all points. I don't have an Id column, however I do have x and y data in two separate columns.
dd <- data.frame(Id=c(1,2),X=c(3,5),Y=c(9,6)) ddTable <- data.frame(Id=c(1,2),Name=c("Item1","Item2")) ddShapefile <- convert.to.shapefile(dd, ddTable, "Id", 1) write.shapefile(ddShapefile, "c:/test", arcgis=T)
Any help would be greatly appreciated.
解决方案I would suggest using
rgdal
rather thanshapefiles
. In order to usergdal
, you'll have to check out the system requirements from http://cran.revolutionanalytics.com/web/packages/rgdal/.The following code should get you in the right direction:
install.packages(c("rgdal", "sp")) library(rgdal) library(sp) MyData <- read.csv(file="c:/TheDataIWantToReadIn.csv", header=TRUE, sep=",")
The following code snippet comes from Mapping in R using the ggplot2 package.
class(MyData) # data.frame coordinates(MyData)<-~longitude+latitude # whatever the equivalent is in your # data.frame class(MyData) # [1] "SpatialPointsDataFrame" # attr(,"package") # [1] "sp"
The following code snippet comes How to write a shapefile with projection - problem solved
writeOGR(crest.sp, "c:/test", "layer name", driver = "ESRI Shapefile")
这篇关于如何将csv转换为R中的shp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!