我有一个包含11589个类为"polygons"的对象的SpatialPolygonsDataFrame。其中10699个对象仅由一个多边形组成,但其余的对象由多个多边形(2到22个)组成。
如果一个对象由多个多边形组成,则可能存在以下三种情况:
- 有时,这些附加多边形描述了类别“polygons”中第一个多边形所描述地理区域中的一个“洞”。
- 有时,这些附加多边形描述了其他地理区域,即该地区的形状相当复杂,并通过结合多个部分来描述。
- 有时,可能是1和2的混合。
Stackoverflow帮助我正确地绘制此类空间对象(Plot spatial area defined by multiple polygons)。
然而,我仍然不知道如何确定一个点(由经度/纬度定义)是否在一个多边形内。
以下是我的代码。 我尝试应用sp包中的point.in.polygon函数,但找不到如何处理由多个多边形/洞组成的对象的方法。
# Load packages
# ---------------------------------------------------------------------------
library(maptools)
library(rgdal)
library(rgeos)
library(ggplot2)
library(sp)
# 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
map <- readShapeSpatial(shp, proj4string = CRS("+init=epsg:25832"))
# Transform the geocoding from UTM to Longitude/Latitude
map <- spTransform(map, CRS("+proj=longlat +datum=WGS84"))
# Pick an geographic area which consists of multiple polygons
# ---------------------------------------------------------------------------
# Output a frequency table of areas with N polygons
nPolys <- sapply(map@polygons, function(x)length(x@Polygons))
# Get geographic area with the most polygons
polygon.with.max.polygons <- which(nPolys==max(nPolys))
# Get shape for the geographic area with the most polygons
Poly.coords <- map[which(nPolys==max(nPolys)),]
# Plot
# ---------------------------------------------------------------------------
# Plot region without Google maps (ggplot2)
plot(Poly.coords, col="lightgreen")
# Find if a point is in a polygon
# ---------------------------------------------------------------------------
# Define points
points_of_interest <- data.frame(long=c(10.5,10.51,10.15,10.4),
lat =c(51.85,51.72,51.81,51.7),
id =c("A","B","C","D"), stringsAsFactors=F)
# Plot points
points(points_of_interest$long, points_of_interest$lat, pch=19)
point.in.polygon
能够处理多个多边形,您知道在这种情况下如何为point.in.polygon(point.x, point.y, pol.x, pol.y, mode.checked=FALSE)
提供必要的参数吗? - majomread.csv("bear-$hit-sightings.csv")
和readOGR(".", "The_Woods")
来回答人们多年来一直在问的另一个问题! - geneorama