在R中创建Shapefile [英] Creating Shapefiles in R
问题描述
我正在尝试在R中创建一个shapefile,稍后将其导入Fusion Table或某些其他GIS应用程序中.
I'm trying to create a shapefile in R that I will later import to either Fusion Table or some other GIS application.
首先,我导入了一个空白的shapefile,其中包含加拿大的所有人口普查区域.我已经基于CT的唯一ID将其他数据(表格格式)附加到shapefile,并且已经映射了结果.目前,我只需要在Vancouver中的文件,并且我想导出一个仅包含Vancouver CT和我新附加的属性数据的shapefile.
To start,I imported a blank shapefile containing all the census tracts in Canada. I have attached other data (in tabular format) to the shapefile based on the unique ID of the CTs, and I have mapped my results. At the moment, I only need the ones in Vancouver and I would like to export a shapefile that contains only the Vancouver CTs as well as my newly attached attribute data.
这是我的代码(由于隐私原因,某些部分被省略了):
Here is my code (some parts omitted due to privacy reasons):
shape <- readShapePoly('C:/TEST/blank_ct.shp') #Load blank shapefile
shape@data = data.frame(shape@data, data2[match(shape@data$CTUID, data2$CTUID),]) #data2 is my created attributes that I'm attaching to blank file
shape1 <-shape[shape$CMAUID == 933,] #selecting the Vancouver CTs
我看过其他使用此示例: writePolyShape 创建shapefile.我尝试了一下,并且在一定程度上起作用了.它创建了.shp,.dbf和.shx文件.我缺少.prj文件,而且不确定如何创建它.有没有更好的方法来创建shapefile?
I've seen other examples using this: writePolyShape to create the shapefile. I tried it, and it worked to an extent. It created the .shp, .dbf, and .shx files. I'm missing the .prj file and I'm not sure how to go about creating it. Are there better methods out there for creating shapefiles?
在此问题上的任何帮助将不胜感激.
Any help on this matter would be greatly appreciated.
推荐答案
使用rgdal
和writeOGR
. rgdal
将保留投影信息
Use rgdal
and writeOGR
. rgdal
will preserve the projection information
类似
library(rdgal)
shape <- readOGR(dsn = 'C:/TEST', layer = 'blank_ct')
# do your processing
shape@data = data.frame(shape@data, data2[match(shape@data$CTUID, data2$CTUID),]) #data2 is my created attributes that I'm attaching to blank file
shape1 <-shape[shape$CMAUID == 933,]
writeOGR(shape1, dsn = 'C:/TEST', layer ='newstuff', driver = 'ESRI Shapefile')
请注意,dsn
是包含.shp
文件的文件夹,而layer
是没有.shp
扩展名的shapefile的名称.它将读取(readOGR
)和写入(writeOGR
)所有组件文件(.dbf
,.shp
,.prj
等)
Note that the dsn
is the folder containing the .shp
file, and the layer
is the name of the shapefile without the .shp
extension. It will read (readOGR
) and write (writeOGR
) all the component files (.dbf
, .shp
, .prj
etc)
这篇关于在R中创建Shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!