2014-02-23 110 views
1

我有一个具有11589个“多边形”类空间对象的SpatialPolygonsDataFrame。这些对象中的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([email protected], function(x) [email protected]) 
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 
# [email protected][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([email protected][1], [email protected][2]), zoom = 10, maptype = 'roadmap') 

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

回答

2

我可能会误解你的问题,但有可能你做得比所需要的难得多。

(注意:我在R中处理.zip文件时遇到了问题,所以我只是将它下载并解压缩到OS中)。

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([email protected], function(x)length([email protected])) 
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)) 
+0

谢谢,这帮助我很多。现在,我只需要弄清楚如何检查某个地理点是否在这个区域内。不幸的是,当你有多个多边形/孔时,这似乎也更难(请参阅我的问题:http://stackoverflow.com/questions/21971447/check-if-point-is-in-spatial-object-which-consists -of-多个多边形孔)。 – majom