【问题标题】:Plot spatial area defined by multiple polygons绘制由多个多边形定义的空间区域
【发布时间】:2014-03-24 14:55:13
【问题描述】:

我有一个 SpatialPolygonsDataFrame,其中包含 11589 个“多边形”类空间对象。这些对象中有 10699 个正好由 1 个多边形组成。但是,这些空间对象的其余部分由多个多边形(2 到 22 个)组成。

如果一个对象由多个多边形组成,则可能出现三种情况:

1) 额外的多边形可以在第一个多边形描述的空间区域中描述一个“洞”。 2)额外的多边形也可以描述额外的地理区域,即区域的形状非常复杂,并且通过将多个部分放在一起来描述。 3) 通常是 1) 和 2) 的混合。

我的问题是:如何绘制这样一个基于多个多边形的空间对象?

我已经能够提取和绘制第一个多边形的信息,但我还没有弄清楚如何一次绘制如此复杂的空间对象的所有多边形。

您可以在下面找到我的代码。问题出在倒数第 15 行。

# Load packages
# ---------------------------------------------------------------------------
library(maptools)
library(rgdal)
library(ggmap)
library(rgeos)


# Get data
# ---------------------------------------------------------------------------
# Download shape information from the internet
URL <- "http://www.geodatenzentrum.de/auftrag1/archiv/vektor/vg250_ebenen/2012/vg250_2012-01-01.utm32s.shape.ebenen.zip"
td <- tempdir()
setwd(td)
temp <- tempfile(fileext = ".zip")
download.file(URL, temp)
unzip(temp)

# Get shape file
shp <- file.path(tempdir(),"vg250_0101.utm32s.shape.ebenen/vg250_ebenen/vg250_gem.shp")

# Read in shape file
x <- readShapeSpatial(shp, proj4string = CRS("+init=epsg:25832"))

# Transform the geocoding from UTM to Longitude/Latitude
x <- spTransform(x, CRS("+proj=longlat +datum=WGS84"))

# Extract relevant information 
att <- attributes(x)
Poly <- att$polygons


# Pick an geographic area which consists of multiple polygons
# ---------------------------------------------------------------------------
# Output a frequency table of areas with N polygons 
order.of.polygons.in.shp <- sapply(x@polygons, function(x) x@plotOrder)
length.vector <- unlist(lapply(order.of.polygons.in.shp, length))
table(length.vector) 

# Get geographic area with the most polygons
polygon.with.max.polygons <- which(length.vector==max(length.vector))
# Check polygon
# x@polygons[polygon.with.max.polygons]

# Get shape for the geographic area with the most polygons
### HERE IS THE PROBLEM ###
### ONLY information for the first polygon is extracted ###
Poly.coords <- data.frame(slot(Poly[[polygon.with.max.polygons ]]@Polygons[[1]], "coords"))

# Plot
# ---------------------------------------------------------------------------
## Calculate centroid for the first polygon of the specified area
coordinates(Poly.coords) <- ~X1+X2
proj4string(Poly.coords) <- CRS("+proj=longlat +datum=WGS84")
center <- gCentroid(Poly.coords)

# Download a map which is centered around this centroid
al1 = get_map(location = c(lon=center@coords[1], lat=center@coords[2]), zoom = 10, maptype = 'roadmap')

# Plot map
ggmap(al1) + 
  geom_path(data=as.data.frame(Poly.coords), aes(x=X1, y=X2))

【问题讨论】:

    标签: r plot polygon geospatial ggmap


    【解决方案1】:

    我可能误解了您的问题,但您可能使这个问题变得比必要的困难得多。

    (注意:我在处理 R 中的 .zip 文件时遇到了麻烦,所以我只是在操作系统中下载并解压缩了它)。

    library(rgdal)
    library(ggplot2)
    
    setwd("< directory with shapefiles >")
    map <- readOGR(dsn=".", layer="vg250_gem", p4s="+init=epsg:25832")
    map <- spTransform(map, CRS("+proj=longlat +datum=WGS84"))
    
    nPolys <- sapply(map@polygons, function(x)length(x@Polygons))
    region <- map[which(nPolys==max(nPolys)),]
    plot(region, col="lightgreen")
    

    # using ggplot...
    region.df <- fortify(region)
    ggplot(region.df, aes(x=long,y=lat,group=group))+
      geom_polygon(fill="lightgreen")+
      geom_path(colour="grey50")+
      coord_fixed()
    

    请注意,ggplot 不能正确处理漏洞:geom_path(...) 工作正常,但geom_polygon(...) 填补了漏洞。我以前遇到过这个问题(请参阅this question),并且由于缺乏响应,它可能是 ggplot 中的错误。由于您没有使用geom_polygon(...),这不会影响您...

    在上面的代码中,您将替换以下行:

    ggmap(al1) + geom_path(data=as.data.frame(Poly.coords), aes(x=X1, y=X2))
    

    与:

    ggmap(al1) + geom_path(data=region.df, aes(x=long,y=lat,group=group))
    

    【讨论】:

    • 谢谢,这对我帮助很大。现在,我只需要弄清楚如何检查某些地理点是否在该区域内。不幸的是,当您有多个多边形/孔时,这似乎也更难(请参阅我的问题:stackoverflow.com/questions/21971447/…)。
    猜你喜欢
    • 1970-01-01
    • 2015-09-06
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-04-17
    • 2018-08-05
    • 2014-08-17
    • 1970-01-01
    相关资源
    最近更新 更多