测试 R 中的 shapefile 中是否存在 lat/lon 对
Test whether lat/lon pairs exist within a shapefile in R
我已经阅读了许多关于这个主题的论坛,但我似乎无法根据我的特定问题调整我读过的任何内容。基本上,我有一个 lat/lon 值的数据框,我想做的就是测试这些坐标是否存在于加利福尼亚州。
这是一些示例数据:
library(tidyverse)
library(sf)
coords <- tribble(
~city, ~lon, ~lat,
LA, -118.2437, 34.0522,
SF, -122.4194, 37.7749,
SAC, -121.4944, 38.5816,
CHI, -87.6298, 41.8781,
NY, -74.0060, 40.7128
)
这是来自国家网站的形状文件的 link:CA Shape Files。
我想我很接近...
# read in shape data
cali <- read_sf("CA_State_TIGER2016.shp")
# convert coordinates to spatial point compatible data
coords_sf <- st_as_sf(coords, coords = c("lon", "lat"), crs = st_crs(cali))
从那里,我假设我使用 st_contains
来测试我的 cali
对象是否包含在 coords_sf
中找到的坐标,但我无法让它工作。
有什么建议吗?
感谢您的帮助!
在您的代码中,您的点数据集 coords
的原始坐标参考系与您要应用的 crs 之间存在混淆。
请注意,名为 coords
的数据集不是空间数据集。您需要将其设为 st_as_sf()
的空间数据集。您在此数据框中输入的坐标的 crs 是 "geographical coordinates".
一旦这是一个数据集,您就可以将其转换为目标 crs。
在您的代码中,您尝试同时执行这两项操作。
因此您正在寻找的答案是:
library(tidyverse)
library(sf)
coords <- tribble(
~city, ~lon, ~lat,
"LA", -118.2437, 34.0522,
"SF", -122.4194, 37.7749,
"SAC", -121.4944, 38.5816,
"CHI", -87.6298, 41.8781,
"NY", -74.0060, 40.7128
)
file <- tempfile(fileext = ".zip")
download.file("https://data.ca.gov/dataset/e212e397-1277-4df3-8c22-40721b095f33/resource/3db1e426-fb51-44f5-82d5-a54d7c6e188b/download/ca-state-boundary.zip", destfile = file)
unzip(zipfile = file)
# read in shape data
cali <- read_sf("CA_State_TIGER2016.shp")
# Your data are originally geographical coordinates which has EPSG=4326
coords_sf <- st_as_sf(coords, coords = c("lon", "lat"), crs = 4326)
# Then you can transform them to the system you want
coords_cali <- coords_sf %>% st_transform(crs = st_crs(cali))
cali %>% st_contains(coords_cali)
如果您想在点数据集中添加 cali
shapefile 的信息,您可以:
- 保留整个点数据集并放入
NA
coords_cali %>%
st_join(cali)
- 仅保留
cali
多边形内的点
coords_cali %>%
st_intersection(cali)
我已经阅读了许多关于这个主题的论坛,但我似乎无法根据我的特定问题调整我读过的任何内容。基本上,我有一个 lat/lon 值的数据框,我想做的就是测试这些坐标是否存在于加利福尼亚州。
这是一些示例数据:
library(tidyverse)
library(sf)
coords <- tribble(
~city, ~lon, ~lat,
LA, -118.2437, 34.0522,
SF, -122.4194, 37.7749,
SAC, -121.4944, 38.5816,
CHI, -87.6298, 41.8781,
NY, -74.0060, 40.7128
)
这是来自国家网站的形状文件的 link:CA Shape Files。
我想我很接近...
# read in shape data
cali <- read_sf("CA_State_TIGER2016.shp")
# convert coordinates to spatial point compatible data
coords_sf <- st_as_sf(coords, coords = c("lon", "lat"), crs = st_crs(cali))
从那里,我假设我使用 st_contains
来测试我的 cali
对象是否包含在 coords_sf
中找到的坐标,但我无法让它工作。
有什么建议吗?
感谢您的帮助!
在您的代码中,您的点数据集 coords
的原始坐标参考系与您要应用的 crs 之间存在混淆。
请注意,名为 coords
的数据集不是空间数据集。您需要将其设为 st_as_sf()
的空间数据集。您在此数据框中输入的坐标的 crs 是 "geographical coordinates".
一旦这是一个数据集,您就可以将其转换为目标 crs。
在您的代码中,您尝试同时执行这两项操作。
因此您正在寻找的答案是:
library(tidyverse)
library(sf)
coords <- tribble(
~city, ~lon, ~lat,
"LA", -118.2437, 34.0522,
"SF", -122.4194, 37.7749,
"SAC", -121.4944, 38.5816,
"CHI", -87.6298, 41.8781,
"NY", -74.0060, 40.7128
)
file <- tempfile(fileext = ".zip")
download.file("https://data.ca.gov/dataset/e212e397-1277-4df3-8c22-40721b095f33/resource/3db1e426-fb51-44f5-82d5-a54d7c6e188b/download/ca-state-boundary.zip", destfile = file)
unzip(zipfile = file)
# read in shape data
cali <- read_sf("CA_State_TIGER2016.shp")
# Your data are originally geographical coordinates which has EPSG=4326
coords_sf <- st_as_sf(coords, coords = c("lon", "lat"), crs = 4326)
# Then you can transform them to the system you want
coords_cali <- coords_sf %>% st_transform(crs = st_crs(cali))
cali %>% st_contains(coords_cali)
如果您想在点数据集中添加 cali
shapefile 的信息,您可以:
- 保留整个点数据集并放入
NA
coords_cali %>%
st_join(cali)
- 仅保留
cali
多边形内的点
coords_cali %>%
st_intersection(cali)