在这里的帖子的启发下,用R开发地理专题地图,我正在考虑构建一个基于邮政编码的等值区域图.我从http://www.census.gov/geo/www/cob/z52000.html下载了新罕布什尔州和缅因州的形状文件,但我有兴趣合并或合并这两个州的.shp文件.
maptools在使用中读取它们之后,包中是否有一种机制用于执行这种合并或串联两个.shp文件readShapeSpatial()?如果使用RgoogleMaps包装会更容易,也欢迎输入.
我跟进了 Roman Lu\xc5\xa1trik 发布的链接,以下答案是对http://r-sig-geo.2731867.n2.nabble.com/suggestion-to-MERGE-or-的轻微修改UNION-3-shapefiles-td5914413.html#a5916751。
\n\n以下代码将允许您合并.shp从2000 年人口普查 5 位邮政编码制表区域 (ZCTA) 制图边界文件获得的文件并绘制它们。
在本例中,我下载了马萨诸塞州、新罕布什尔州和缅因州的文件和.shp关联文件。.dbf.shx
library(\'maptools\')\nlibrary(\'rgdal\')\n\nsetwd(\'c:/location.of.shp.files\')\n\n# this location has the shapefiles for zt23_d00 (Maine), zt25_d00 (Mass.), and zt33_d00 (New Hampshire).\n\n# columns.to.keep\n# allows the subsequent spRbind to work properly\n\ncolumns.to.keep <- c(\'AREA\', \'PERIMETER\', \'ZCTA\', \'NAME\', \'LSAD\', \'LSAD_TRANS\')\n\nfiles <- list.files(pattern="*.shp$", recursive=TRUE, full.names=TRUE) \n\nuid <-1 \n\n# get polygons from first file\n\npoly.data<- readOGR(files[1], gsub("^.*/(.*).shp$", "\\\\1", files[1])) \nn <- length(slot(poly.data, "polygons"))\npoly.data <- spChFIDs(poly.data, as.character(uid:(uid+n-1))) \nuid <- uid + n \npoly.data <- poly.data[columns.to.keep]\n\n# combine remaining polygons with first polygon\n\nfor (i in 2:length(files)) {\n temp.data <- readOGR(files[i], gsub("^.*/(.*).shp$", "\\\\1",files[i]))\n n <- length(slot(temp.data, "polygons")) \n temp.data <- spChFIDs(temp.data, as.character(uid:(uid+n-1))) \n temp.data <- temp.data[columns.to.keep]\n uid <- uid + n \n poly.data <- spRbind(poly.data,temp.data) \n}\n\nplot(poly.data)\n\n# save new shapefile\n\ncombined.shp <- \'combined.shp\'\nwriteOGR(poly.data, dsn=combined.shp, layer=\'combined1\', driver=\'ESRI Shapefile\') \nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
2196 次 |
| 最近记录: |