簡體   English   中英

我如何 map 到 R 中我的多邊形/形狀文件的最近點的緯度/經度

[英]How can I map a lat/long to the nearest point of my polygon/shapefile in R

我正在嘗試對我的形狀文件 map 一組緯度/經度以獲得 CSDUID/人口普查部門信息。

但是,有一些異常值或緯度/經度在 shapefile 之外。 我想知道是否有可能將這些點 map 到 shapefile 的最近點,以便我可以在后面的分析中獲得划分信息?

可重現的例子

假設我有一個像gr這樣的 shapefile

nc <- st_transform(st_read(system.file("shape/nc.shp", package="sf")), 2264)                
gr = st_sf(
    label = apply(expand.grid(1:10, LETTERS[10:1])[,2:1], 1, paste0, collapse = " "),
    geom = st_make_grid(nc))

# gr
# Simple feature collection with 100 features and 1 field
# geometry type:  POLYGON
# dimension:      XY
# bbox:           xmin: 406262.2 ymin: 48374.87 xmax: 3052887 ymax: 1044158
# epsg (SRID):    2264
# proj4string:    +proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.2192024384 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=us-ft +no_defs
# First 10 features:
#    label                           geom
# 1   J  1 POLYGON ((406262.2 48374.87...
# 2   J  2 POLYGON ((670924.7 48374.87...
# 3   J  3 POLYGON ((935587.2 48374.87...
# 4   J  4 POLYGON ((1200250 48374.87,...
# 5   J  5 POLYGON ((1464912 48374.87,...
# 6   J  6 POLYGON ((1729575 48374.87,...
# 7   J  7 POLYGON ((1994237 48374.87,...
# 8   J  8 POLYGON ((2258900 48374.87,...
# 9   J  9 POLYGON ((2523562 48374.87,...
# 10  J 10 POLYGON ((2788225 48374.87,...

我有一點在邊界框之外:

point = st_point(c(106160,28370))

library(ggplot2)
ggplot() + geom_sf(data = gr) + geom_sf(data = point)

在此處輸入圖像描述

我怎樣才能 map 指向最近的多邊形?

sf package 中,您具有出色的空間分析功能,尤其是空間連接。 假設您的點數據被命名為point並且您的多邊形被命名為...... polygons (兩個sf對象),您可以使用sf::st_join進行空間連接。

st_join接受不同類型的空間映射。 我認為您對st_nearest_feature合並感興趣(首先刪除屬於您的多邊形內的點,例如通過第一個st_join使用st_within

library(sf)

st_join(
  points,
  polygons,
  join = st_nearest_feature
)

在編輯后的示例中,這將給出:

point = st_sf(st_sfc(point), crs = st_crs(gr))
st_join(point, gr, join = st_nearest_feature)

請參閱?st_join文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM