[英]Extracting Long/Lat from a shapefile and calculating nearest distance between two sets of coordinates
我有兩組正在使用的數據。 第一個是地址列表及其各自的經度和緯度坐標:
首先是地址列表及其長/緯度坐標:
library(dplyr)
library(tidygeocoder)
library(tigris)
library(rgeos)
library(sf)
addresses <-
structure(
list(
id = c(
107234063L,
106950145L,
107256562L,
107277550L,
106952865L,
106858955L,
104019143L,
102264960L,
101690658L,
107259458L
),
streetno = c(12700L, 2016L, 311L, 3405L,
2400L, 711L, 2400L, 406L, 14002L, 1502L),
streetname = c(
"Stafford",
"Dunlavy",
"Branard",
"Shepherd",
"Fountain View",
"William",
"Braeswood",
"Hawthorne",
"Hempstead",
"Quitman"
),
city = c(
"Stafford",
"Houston",
"Houston",
"Houston",
"Houston",
"Houston",
"Houston",
"Houston",
"Houston",
"Houston"
),
state = c("TX", "TX", "TX",
"TX", "TX", "TX", "TX", "TX", "TX", "TX"),
zip5 = c(
77477L,
77006L,
77006L,
77018L,
77057L,
77002L,
77030L,
77006L,
77040L,
77009L
),
vaddress = c(
"12700 Stafford Rd",
"2016 Dunlavy St",
"311 Branard St",
"3405 N Shepherd Dr",
"2400 Fountain View Dr",
"711 William St",
"2400 N Braeswood Blvd",
"406 Hawthorne St",
"14002 Hempstead Rd",
"1502 Quitman St"
),
countyname = c(
"Harris",
"Harris",
"Harris",
"Harris",
"Harris",
"Harris",
"Harris",
"Harris",
"Harris",
"Harris"
),
hdname = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
complete_address = c(
"12700 Stafford Rd, Stafford, TX 77477",
"2016 Dunlavy St, Houston, TX 77006",
"311 Branard St, Houston, TX 77006",
"3405 N Shepherd Dr, Houston, TX 77018",
"2400 Fountain View Dr, Houston, TX 77057",
"711 William St, Houston, TX 77002",
"2400 N Braeswood Blvd, Houston, TX 77030",
"406 Hawthorne St, Houston, TX 77006",
"14002 Hempstead Rd, Houston, TX 77040",
"1502 Quitman St, Houston, TX 77009"
),
longlat = structure(
list(
singlelineaddress = c(
"12700 Stafford Rd, Stafford, TX 77477",
"2016 Dunlavy St, Houston, TX 77006",
"311 Branard St, Houston, TX 77006",
"3405 N Shepherd Dr, Houston, TX 77018",
"2400 Fountain View Dr, Houston, TX 77057",
"711 William St, Houston, TX 77002",
"2400 N Braeswood Blvd, Houston, TX 77030",
"406 Hawthorne St, Houston, TX 77006",
"14002 Hempstead Rd, Houston, TX 77040",
"1502 Quitman St, Houston, TX 77009"
),
lat = c(
29.634542,
29.74779,
29.737116,
29.817532,
29.742098,
29.76709,
29.698315,
29.742435,
29.850826,
29.783348
),
long = c(
-95.545334,
-95.40214,-95.383736,
-95.41044,
-95.48542,
-95.35345,
-95.415306,-95.38407,
-95.52886,
-95.35297
)
),
row.names = c(NA,-10L),
class = c("tbl_df", "tbl", "data.frame")
)
),
row.names = 353:362,
class = "data.frame"
)
下一組坐標來自下載以下 shapefile:
texas_hd <- state_legislative_districts("TX", house = "lower")
所以我想做的是雙重的:
從texas_hd
文件中提取長/緯度坐標
在addresses
中創建一個新列,將每行的長/緯度坐標映射到texas_hd
中最近的SLDLST
代碼
例如, 2016 Dunlavy St, Houston, TX 77006
的地址應該是來自SLDLST
的 134
請在下面找到一種可能的解決方案來回答您的兩個請求。
代表
library(sf)
library(tigris)
library(tidyr)
library(dplyr)
texas_hd <- state_legislative_districts("TX", house = "lower")
texas_hd_coords <- texas_hd %>%
sf::st_coordinates() %>%
as.data.frame() %>%
dplyr::select(X,Y) %>%
dplyr::rename(long = X, lat = Y)
head(texas_hd_coords)
#> long lat
#> 1 -97.81123 30.23370
#> 2 -97.81115 30.23384
#> 3 -97.81105 30.23400
#> 4 -97.81096 30.23416
#> 5 -97.81037 30.23516
#> 6 -97.80993 30.23587
注意:不知道您希望最終結果是什么,最后一行代碼刪除了texas_hd
的所有列,除了SLDLST
列。 因此,如有必要,您可以通過刪除select(-c(...))
中包含的列輕松地在最終結果中添加所需的列
addresses <- addresses %>%
dplyr::as_tibble() %>%
tidyr::unnest(., cols = c(longlat)) %>%
sf::st_as_sf(., coords = c("long", "lat"), crs = 4326) %>%
sf::st_transform(., crs = 4269) %>%
sf::st_join(., texas_hd, join = st_nearest_feature) %>%
dplyr::select(-c(GEOID, NAMELSAD, LSAD, LSY, MTFCC, FUNCSTAT, ALAND, AWATER, INTPTLAT, INTPTLON))
addresses
#> Simple feature collection with 10 features and 13 fields
#> Geometry type: POINT
#> Dimension: XY
#> Bounding box: xmin: -95.54533 ymin: 29.63454 xmax: -95.35297 ymax: 29.85083
#> Geodetic CRS: NAD83
# A tibble: 10 x 14
#> id streetno streetname city state zip5 vaddress countyname hdname complete_address singlelineaddre~ geometry STATEFP SLDLST
#> <int> <int> <chr> <chr> <chr> <int> <chr> <chr> <int> <chr> <chr> <POINT [°]> <chr> <chr>
#> 1 107234063 12700 Stafford Stafford TX 77477 12700 Stafford Rd Harris 0 12700 Stafford Rd, Staffor~ 12700 Stafford ~ (-95.54533 29.63454) 48 027
#> 2 106950145 2016 Dunlavy Houston TX 77006 2016 Dunlavy St Harris 0 2016 Dunlavy St, Houston, ~ 2016 Dunlavy St~ (-95.40214 29.74779) 48 134
#> 3 107256562 311 Branard Houston TX 77006 311 Branard St Harris 0 311 Branard St, Houston, T~ 311 Branard St,~ (-95.38374 29.73712) 48 147
#> 4 107277550 3405 Shepherd Houston TX 77018 3405 N Shepherd Dr Harris 0 3405 N Shepherd Dr, Housto~ 3405 N Shepherd~ (-95.41044 29.81753) 48 148
#> 5 106952865 2400 Fountain View Houston TX 77057 2400 Fountain View Dr Harris 0 2400 Fountain View Dr, Hou~ 2400 Fountain V~ (-95.48542 29.7421) 48 133
#> 6 106858955 711 William Houston TX 77002 711 William St Harris 0 711 William St, Houston, T~ 711 William St,~ (-95.35345 29.76709) 48 142
#> 7 104019143 2400 Braeswood Houston TX 77030 2400 N Braeswood Blvd Harris 0 2400 N Braeswood Blvd, Hou~ 2400 N Braeswoo~ (-95.41531 29.69832) 48 134
#> 8 102264960 406 Hawthorne Houston TX 77006 406 Hawthorne St Harris 0 406 Hawthorne St, Houston,~ 406 Hawthorne S~ (-95.38407 29.74244) 48 147
#> 9 101690658 14002 Hempstead Houston TX 77040 14002 Hempstead Rd Harris 0 14002 Hempstead Rd, Housto~ 14002 Hempstead~ (-95.52886 29.85083) 48 139
#> 10 107259458 1502 Quitman Houston TX 77009 1502 Quitman St Harris 0 1502 Quitman St, Houston, ~ 1502 Quitman St~ (-95.35297 29.78335) 48 148
由代表 package (v2.0.1) 於 2022 年 2 月 2 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.