【问题标题】:Creating new polygons from a detailed shapefile从详细的 shapefile 创建新多边形
【发布时间】:2014-10-13 15:01:21
【问题描述】:

有没有什么快速的方法可以使用更详细的形状文件来构建新的形状文件?下面的例子会说明问题。

使用此link 中提供的数据,可以获得圣保罗市。

if(require(rgdal) == F){install.packages('rgdal'); require(rgdal)}
if(require(ggplot2) == F){install.packages('ggplot2'); require(ggplot2)}
setwd("..data directory..")
sp4.rg <- readOGR(".", "35MUE250GC_SIR")
plot(sp4.rg, axes=TRUE, border="gray")

在这个地块中有 645 个自治市。我想做同样的情节,但现在我想在一个新的区域(多边形)中聚集一些这个城市。例如:我们称这个新区域为“SPABCD”。该地区由以下市镇组成:“SãO PAULO”、“SãO BERNARDO DO CAMPO”、“SANTO ANDRÉ”、“SãO CAETANO”和“DIADEMA”。

sp <- fortify(sp4.rg, region = "NM_MUNICIP")
spabcd <- c("SÃO PAULO", "SÃO BERNARDO DO CAMPO",  "SANTO ANDRÉ", "SÃO CAETANO DO SUL", "DIADEMA")
sp$region <- ifelse(sp$id %in% spabcd, "SPABCD", sp$id)

我的问题是:有一种快速的方法(一个已经创建的函数)来选择这个新区域周围的纬度和经度,并且可以绘制它?

【问题讨论】:

  • 这个maptools vignette 可能会帮助您入门。
  • 谢谢@hrbrmstr!我会看到的!
  • +1 @hrbrmstr 间接建议使用maptools::unionSpatialPolygons()。此外,作为一般规则,您需要对Spatial* 对象进行所有操作之前 fortify()'ing 它们...

标签: r map plot subset shapefile


【解决方案1】:

一个快速/toy 示例,其中包含您的数据(因为我相信这需要您做更多的工作):

library(maptools)

regs <- c("S\xc3O PAULO", "S\xc3O BERNARDO DO CAMPO", 
          "SANTO ANDR\xc9", "S\xc3O CAETANO DO SUL", "DIADEMA")

# make a copy of all the IDs
sp2 <- sp$ID

# filter out only the ones we want dissolved:
sp2[which(!sp$NM_MUNICIP %in% regs)] <- NA
sp.u <- unionSpatialPolygons(sp, sp2)

# sample new plot
plot(sp.u)

# showing this region on the whole map

plot(sp, axes=TRUE, border="gray")
plot(sp.u, col="red", add=TRUE)

现在,只有边框:

# make one "bin"
sp.u.s <- getSpPPolygonsLabptSlots(sp.u)

outline <- cut(sp.u.s[,1], range(sp.u.s[,1]), include.lowest=TRUE)

# one more dissolve
dissolved <- unionSpatialPolygons(sp.u ,outline)

plot(sp, axes=TRUE, border="gray")
plot(dissolved, add=TRUE)

【讨论】:

猜你喜欢
  • 2015-08-29
  • 2020-01-09
  • 2014-10-25
  • 2019-10-07
  • 1970-01-01
  • 2011-06-29
  • 2013-04-19
  • 2019-03-21
  • 1970-01-01
相关资源
最近更新 更多