ポイントがポリゴンシェープファイル内にあるかどうかを確認する


19

Zillowには、米国の主要都市のさまざまな地域のシェープファイルのセットがあります。Rを使用して、特定の近隣に特定の建物が存在するかどうかを確認したかったのです。

library(rgeos)
library(sp)
library(rgdal)

df <- data.frame(Latitude =c(47.591351, 47.62212,47.595152),
                 Longitude = c(-122.332271,-122.353985,-122.331639),
                 names = c("Safeco Field", "Key Arena", "Century Link"))
coordinates(df) <- ~ Latitude + Longitude

wa.map <- readOGR("ZillowNeighborhoods-WA.shp", layer="ZillowNeighborhoods-WA")

sodo <- wa.map[wa.map$CITY == "Seattle"  & wa.map$NAME == "Industrial District", ]

問題なくプロットできます

plot(sodo)
points(df$Latitude ~ df$Longitude, col = "red", cex = 1)

ここに画像の説明を入力してください

proj4シェープファイルの文字列をdata.frame に一致させます

CRSobj <- CRS("+proj=longlat +datum=NAD83 +no_defs +ellps=GRS80 +towgs84=0,0,0 ")
df@proj4string <- CRSobj

over(df, sodo)

これにより、多くのNA価値が得られます。私はこの答えを試しまし

spp <- SpatialPoints(df)
spp@proj4string <- CRSobj
over(spp, sodo)

ただし、NA値のみを取得します。他に何を試してみるべきですか?

回答:


20

空間data.frameが正しく形成されていません。これはうまくいくかもしれません:

library(rgeos)
library(sp)
library(rgdal)

wa.map <- readOGR("ZillowNeighborhoods-WA.shp", layer="ZillowNeighborhoods-WA")

sodo <- wa.map[wa.map$CITY == "Seattle"  & wa.map$NAME == "Industrial District", ]

# Don't use df as name, it is an R function
# Better to set longitudes as the first column and latitudes as the second
dat <- data.frame(Longitude = c(-122.332271,-122.353985,-122.331639),
                  Latitude =c(47.591351, 47.62212,47.595152),
                  names = c("Safeco Field", "Key Arena", "Century Link"))
# Assignment modified according
coordinates(dat) <- ~ Longitude + Latitude
# Set the projection of the SpatialPointsDataFrame using the projection of the shapefile
proj4string(dat) <- proj4string(sodo)

over(dat, sodo)
#  STATE COUNTY    CITY                NAME REGIONID
#1    WA   King Seattle Industrial District   271892
#2  <NA>   <NA>    <NA>                <NA>       NA
#3  <NA>   <NA>    <NA>                <NA>       NA

over(sodo, dat)
#           names
#122 Safeco Field

7

私はちょうど同じことをやっています。パスカルの答えはほとんどそれをカバーしていますが、以下のように2つの追加ステップが必要になる場合があります。

#After you create your list of latlongs you must set the proj4string to longlat
proj4string(dat) <- CRS("+proj=longlat")

#Before you re-set the proj4string to the one from sodo you must actually convert #your coordinates to the new projection
dat <- spTransform(dat, proj4string(sodo))

どの場合にこれらの追加の手順が必要かは明確ではありません。私にとって、user32309による答えの解決策は十分でした。
djhurio

3
データの形式によって異なります。プロジェクションAにあり、proj4stringを使用することを宣言したいだけであれば、問題ないはずです。ただし、投影Bで、実際に投影Aへの変換を行う必要がある場合は、spTransformを使用する必要があります。
ジョンカリー

2

私はこの投稿で受け入れられた答えに同様のアプローチを使用しましたが、実際には満足していなかったため、代替案を調べてsfライブラリを見つけました。

このライブラリを使用すると、次のようなコードを記述できます。

library(sf)
# Shapefile from ABS: 
# https://www.abs.gov.au/AUSSTATS/abs@.nsf/DetailsPage/1270.0.55.004July%202016?OpenDocument
map = read_sf("data/ABS/shapes/SUA_2016_AUST.shp")

pnts_sf <- st_as_sf(pnts, coords = c('y', 'x'), crs = st_crs(map))

pnts <- pnts_sf %>% mutate(
  intersection = as.integer(st_intersects(geometry, map))
  , area = if_else(is.na(intersection), '', map$SUA_NAME16[intersection])
) 

pnts

出力:

         geometry intersection area    
*     <POINT [°]>        <int> <chr>   
1 (138.62 -34.92)           79 Adelaide
2 (138.58 -34.93)           79 Adelaide
3 (138.52 -34.95)           79 Adelaide
4 (152.71 -27.63)           60 Brisbane
5 (153.01 -27.57)           60 Brisbane
6  (150.73 -33.9)           31 Sydney  
7 (150.99 -33.92)           31 Sydney 

同様の質問であった別の投稿にこのコードを投稿しました、ここ: R sfパッケージでポイントを含むポリゴンを特定する

弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.