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.
在过去的几天里,我一直在尝试将csv转换为shapefile。我知道我可以在QGIS或Arc中轻松完成,但是想将此过程添加到我现有的R代码中。
So i can read in the csv with no issues
所以我可以在csv中读到没有任何问题
MyData <- read.csv(file="c:/TheDa*tToReadIn.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.
我在Packages Shapefile帮助指南中找到了以下代码。但是,我似乎无法找到一种方法来处理我的代码。我的行都是一个点,因此我想创建的shapefile将是所有点。我没有Id列,但是我在两个单独的列中有x和y数据。
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.
任何帮助将不胜感激。
1 个解决方案
#1
I would suggest using rgdal
rather than shapefiles
. In order to use rgdal
, you'll have to check out the system requirements from http://cran.revolutionanalytics.com/web/packages/rgdal/.
我建议使用rgdal而不是shapefile。要使用rgdal,您必须从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:/TheDa*tToReadIn.csv", header=TRUE, sep=",")
The following code snippet comes from Mapping in R using the ggplot2 package.
以下代码片段来自使用ggplot2包的R中的Mapping。
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
以下代码片段如何编写带有投影的shapefile - 问题已解决
writeOGR(crest.sp, "c:/test", "layer name", driver = "ESRI Shapefile")
#1
I would suggest using rgdal
rather than shapefiles
. In order to use rgdal
, you'll have to check out the system requirements from http://cran.revolutionanalytics.com/web/packages/rgdal/.
我建议使用rgdal而不是shapefile。要使用rgdal,您必须从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:/TheDa*tToReadIn.csv", header=TRUE, sep=",")
The following code snippet comes from Mapping in R using the ggplot2 package.
以下代码片段来自使用ggplot2包的R中的Mapping。
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
以下代码片段如何编写带有投影的shapefile - 问题已解决
writeOGR(crest.sp, "c:/test", "layer name", driver = "ESRI Shapefile")