17

クラス「ポリゴン」の11589個のオブジェクトを持つSpatialPolygonsDataFrameがあります。これらのオブジェクトの 10699 は正確に 1 つのポリゴンで構成されていますが、残りのオブジェクトは複数のポリゴン (2 ~ 22) で構成されています。

のオブジェクトが複数のポリゴンで構成されている場合、次の 3 つのシナリオが考えられます。

  1. 場合によっては、これらの追加のポリゴンは、クラス「ポリゴン」のオブジェクトの最初のポリゴンによって記述される地理的アラの「穴」を記述します。
  2. 場合によっては、これらの追加のポリゴンが追加の地理的エリアを記述します。つまり、地域の形状は非常に複雑で、複数のパーツを組み合わせて記述されます。
  3. 場合によっては、1) と 2) の両方が混在している可能性があります。

Stackoverflow は、そのような空間オブジェクトを適切にプロットするのに役立ちました (複数のポリゴンで定義された空間領域をプロットする)。

ただし、ポイント(経度/緯度で定義)がポリゴン内にあるかどうかを判断する方法にはまだ答えられません。

以下は私のコードです。パッケージ内の関数を適用しようとしましたが、複数のポリゴン/穴で構成されるこのようなオブジェクトを処理する方法が見つかりませんでした。point.in.polygonsp

# 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)

ここに画像の説明を入力

4

2 に答える 2

5

「ポリゴン内のポイント」ルーチンを使用できますが、これはRでマルチポリゴンのケースを処理するように適切に設計されていないようです(実際には少し奇妙です)。複数のポリゴン。ここでの秘訣は、奇数のポリゴンの内側にいる場合、マルチポリゴンの内側にいるということです。偶数個のポリゴンの内側にいる場合、実際にはシェイプの外側にいます。

すべての頂点を元の point.in.polygon テストに渡すことを確認するだけで、光線交差を使用する Point in Polygon テストでは、すでにこれを処理できるはずですが、R がどのメカニズムを使用しているかはわかりません。上記の偶数/奇数のアドバイスしかできません。

このコードも見つけましたが、役立つかどうかはわかりません:

require(sp)
require(rgdal)
require(maps)

# read in bear data, and turn it into a SpatialPointsDataFrame
bears <- read.csv("bear-sightings.csv")
coordinates(bears) <- c("longitude", "latitude")

# read in National Parks polygons
parks <- readOGR(".", "10m_us_parks_area")

# tell R that bear coordinates are in the same lat/lon reference system
# as the parks data -- BUT ONLY BECAUSE WE KNOW THIS IS THE CASE!
proj4string(bears) <- proj4string(parks)

# combine is.na() with over() to do the containment test; note that we
# need to "demote" parks to a SpatialPolygons object first
inside.park <- !is.na(over(bears, as(parks, "SpatialPolygons")))

# what fraction of sightings were inside a park?
mean(inside.park)
## [1] 0.1720648

# use 'over' again, this time with parks as a SpatialPolygonsDataFrame
# object, to determine which park (if any) contains each sighting, and
# store the park name as an attribute of the bears data
bears$park <- over(bears, parks)$Unit_Name

# draw a map big enough to encompass all points (but don't actually plot
# the points yet), then add in park boundaries superimposed upon a map
# of the United States
plot(coordinates(bears), type="n")
map("world", region="usa", add=TRUE)
plot(parks, border="green", add=TRUE)
legend("topright", cex=0.85,
    c("Bear in park", "Bear not in park", "Park boundary"),
    pch=c(16, 1, NA), lty=c(NA, NA, 1),
    col=c("red", "grey", "green"), bty="n")
title(expression(paste(italic("Ursus arctos"),
    " sightings with respect to national parks")))

# now plot bear points with separate colors inside and outside of parks
points(bears[!inside.park, ], pch=1, col="gray")
points(bears[inside.park, ], pch=16, col="red")

# write the augmented bears dataset to CSV
write.csv(bears, "bears-by-park.csv", row.names=FALSE)

# ...or create a shapefile from the points
writeOGR(bears, ".", "bears-by-park", driver="ESRI Shapefile")
于 2014-02-23T17:15:58.220 に答える