我如何将我的特定位置分配给他们的社区?
How do I assign my specific locations to their neighborhoods?
我有一个特定位置的数据集,上面有经度和纬度数据。我想创建一个新列,告诉我这些点落在哪些街区。
我的数据集 (df) 有超过 50,000 个位置,有些是重复的。
例如:
Location Longitude Latitude Neighborhood
Fenway Park -71.0972 42.3467 Fenway-Kenmore
我还有附近的 shapefile,你可以在这里找到
https://data.boston.gov/group/geospatial
您正在寻找空间连接。由于您的数据在 lat/long 中,您需要投影到邻域坐标,以便您的数据对齐。 Lat/long 在处理度量单位时不是很好。
然后您需要使用 st_within 对点进行空间连接,如果它们落在邻域多边形内,这将合并属性。从那里您可以将列子集化为您想要的输出。
请注意,默认情况下是左连接,因此请确保您的数据框排在第一位。您没有提供任何数据,所以我不知道这是否对您数据中的所有情况都 100% 有效。
编辑:我发现该站点的邻域 shapefile 有一些无效的几何图形。您可能想通过 st_make_valid()
修复该问题。我还添加了一个带有一些虚拟数据和邻域 shapefile 的示例。重复项无关紧要,您可以随心所欲地处理它们。
library(sf)
# read in neighborhoods shapefile
neighborhoods <- st_read('Boston_Neighborhoods.shp')%>%
st_make_valid()
# convert df to spatial dataframe and reproject
spdf <- st_as_sf(df, coords=c("Longitude", "Latitude"), crs=st_crs("EPSG:4326")) %>%
st_transform(crs=st_crs(neighborhoods))
# do the spatial join
output <- st_join(spdf, neighborhoods, join=st_within)
这是一个包含一些虚拟数据的示例。
plist <- as.data.frame(do.call(rbind, list(c(756363.463294525, 2926358.57093617),
c(756363.463294525, 2926358.57093617),
c(761610.797283232, 2937282.14778728),
c(763204.448665894, 2946825.83819385),
c(763085.235486707, 2950007.83174181))))
pts <- st_as_sf(plist,
crs=st_crs(neighborhoods),
coords=c(1,2))
st_join(pts, neighborhoods, join=st_within)
# Simple feature collection with 5 features and 7 fields
# geometry type: POINT
# dimension: XY
# bbox: xmin: 756363.5 ymin: 2926359 xmax: 763204.4 ymax: 2950008
# projected CRS: NAD83 / Massachusetts Mainland (ftUS)
# OBJECTID Name Acres Neighborho SqMiles ShapeSTAre ShapeSTLen geometry
# 1 27 Roslindale 1605.5682 15 2.51 69938273 53563.91 POINT (756363.5 2926359)
# 2 27 Roslindale 1605.5682 15 2.51 69938273 53563.91 POINT (756363.5 2926359)
# 3 28 Jamaica Plain 2519.2454 11 3.94 109737890 56349.94 POINT (761610.8 2937282)
# 4 29 Mission Hill 350.8536 13 0.55 15283120 17918.72 POINT (763204.4 2946826)
# 5 30 Longwood 188.6119 28 0.29 8215904 11908.76 POINT (763085.2 2950008)
我有一个特定位置的数据集,上面有经度和纬度数据。我想创建一个新列,告诉我这些点落在哪些街区。
我的数据集 (df) 有超过 50,000 个位置,有些是重复的。
例如:
Location Longitude Latitude Neighborhood
Fenway Park -71.0972 42.3467 Fenway-Kenmore
我还有附近的 shapefile,你可以在这里找到 https://data.boston.gov/group/geospatial
您正在寻找空间连接。由于您的数据在 lat/long 中,您需要投影到邻域坐标,以便您的数据对齐。 Lat/long 在处理度量单位时不是很好。
然后您需要使用 st_within 对点进行空间连接,如果它们落在邻域多边形内,这将合并属性。从那里您可以将列子集化为您想要的输出。
请注意,默认情况下是左连接,因此请确保您的数据框排在第一位。您没有提供任何数据,所以我不知道这是否对您数据中的所有情况都 100% 有效。
编辑:我发现该站点的邻域 shapefile 有一些无效的几何图形。您可能想通过 st_make_valid()
修复该问题。我还添加了一个带有一些虚拟数据和邻域 shapefile 的示例。重复项无关紧要,您可以随心所欲地处理它们。
library(sf)
# read in neighborhoods shapefile
neighborhoods <- st_read('Boston_Neighborhoods.shp')%>%
st_make_valid()
# convert df to spatial dataframe and reproject
spdf <- st_as_sf(df, coords=c("Longitude", "Latitude"), crs=st_crs("EPSG:4326")) %>%
st_transform(crs=st_crs(neighborhoods))
# do the spatial join
output <- st_join(spdf, neighborhoods, join=st_within)
这是一个包含一些虚拟数据的示例。
plist <- as.data.frame(do.call(rbind, list(c(756363.463294525, 2926358.57093617),
c(756363.463294525, 2926358.57093617),
c(761610.797283232, 2937282.14778728),
c(763204.448665894, 2946825.83819385),
c(763085.235486707, 2950007.83174181))))
pts <- st_as_sf(plist,
crs=st_crs(neighborhoods),
coords=c(1,2))
st_join(pts, neighborhoods, join=st_within)
# Simple feature collection with 5 features and 7 fields
# geometry type: POINT
# dimension: XY
# bbox: xmin: 756363.5 ymin: 2926359 xmax: 763204.4 ymax: 2950008
# projected CRS: NAD83 / Massachusetts Mainland (ftUS)
# OBJECTID Name Acres Neighborho SqMiles ShapeSTAre ShapeSTLen geometry
# 1 27 Roslindale 1605.5682 15 2.51 69938273 53563.91 POINT (756363.5 2926359)
# 2 27 Roslindale 1605.5682 15 2.51 69938273 53563.91 POINT (756363.5 2926359)
# 3 28 Jamaica Plain 2519.2454 11 3.94 109737890 56349.94 POINT (761610.8 2937282)
# 4 29 Mission Hill 350.8536 13 0.55 15283120 17918.72 POINT (763204.4 2946826)
# 5 30 Longwood 188.6119 28 0.29 8215904 11908.76 POINT (763085.2 2950008)