首页 > 解决方案 > 使用开放街道地图数据进行位置分析

问题描述

我正在尝试使用 Open Street Map 提供的功能进行一些简单的位置分析。特别是,我想构建与靠近主要道路和每小时交通计数相关的功能。使用位置列表,我需要找到到最近的主要道路的距离以及该道路上的交通估计。到目前为止,我无法从 OSM 中提取 Key:traffic:hourly,也没有找到一种有效的方法来计算到某个位置的最近主要道路。

下面的示例是亚特兰大所有麦当劳和主要道路的简单设置。找到离每个位置最近的主要道路以及该道路的 OSM 交通估计的最佳方法是什么?

#~~~~~~~~~~Example~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#load packages
library(tidyverse)
library(osmdata)
library(osmar)
library(sf)

#Query McDonald's in Atlanta
q.ATL<- getbb("Atlanta") %>%
  opq() %>%
  add_osm_feature("amenity", "fast_food")%>%
  add_osm_feature("name", "McDonald's",value_exact = FALSE, match_case = FALSE)%>%
  osmdata_sf()

MDs.info<-as.data.frame(q.ATL[["osm_points"]]) # actually make this a dataframe

# Find Major Roads
MajorRds <- getbb("Atlanta Georgia")%>%
  opq()%>%
  add_osm_feature(key = "highway", value = c("motorway", "primary", "motorway_link", "primary_link")) %>%
  #add_osm_feature("traffic", "hourly")%>%  # This does not grab the appropriate information
  osmdata_sf()

标签: rgeolocationopenstreetmapoverpass-api

解决方案


您正在寻找的可能是st_nearest_feature()功能:

library(tidyverse)
library(osmdata)
library(sf)

#Query McDonald's in Atlanta
q.ATL<- getbb("Atlanta") %>%
  opq() %>%
  add_osm_feature("amenity", "fast_food")%>%
  add_osm_feature("name", "McDonald's",value_exact = FALSE, match_case = FALSE)%>%
  osmdata_sf()

MDs.info <- as.data.frame(q.ATL[["osm_points"]]) |>
  select(osm_id, geometry) |>
  st_as_sf()

# Find Major Roads
MajorRds <- getbb("Atlanta Georgia")%>%
  opq()%>%
  add_osm_feature(key = "highway", value = c("motorway", "primary", "motorway_link", "primary_link")) %>%
  #add_osm_feature("traffic", "hourly")%>%  # This does not grab the appropriate information
  osmdata_sf()

现在,我们必须合并osm_linesosm_multilines获得所有高速公路:

r <- MajorRds$osm_lines |>
  select(osm_id, name, highway, geometry)

r <- MajorRds$osm_multilines |>
  select(osm_id, name, highway, geometry) |>
  add_row(r)

让我们绘制它以进行可视化

plot(r$geometry)
plot(MDs.info, col = "blue", add = TRUE)

现在我们将使用st_nearest_feature()找到最近的方式并st_distance计算它的距离:

nearest <- st_nearest_feature(MDs.info, r)
dist = st_distance(MDs.info, r[nearest,], by_element=TRUE)
MDs.info <- cbind(MDs.info, st_drop_geometry(r)[nearest,])
MDs.info$dist <- dist
head(MDs.info)
#> Simple feature collection with 6 features and 5 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: -84.3696 ymin: 33.7743 xmax: -84.29572 ymax: 33.79026
#> Geodetic CRS:  WGS 84
#>              osm_id osm_id.1                name       highway
#> 497203850 497203850 40861481      Clairmont Road       primary
#> 534028686 534028686  9249238                <NA> motorway_link
#> 544125305 544125305 74927688 East College Avenue       primary
#> 654308248 654308248 74927688 East College Avenue       primary
#> 654308249 654308249 74927688 East College Avenue       primary
#> 654308250 654308250 74927688 East College Avenue       primary
#>                             geometry           dist
#> 497203850 POINT (-84.30741 33.79026)   86.16797 [m]
#> 534028686   POINT (-84.3696 33.7743) 1586.02491 [m]
#> 544125305 POINT (-84.29574 33.77833)  856.44432 [m]
#> 654308248 POINT (-84.29574 33.77831)  854.60091 [m]
#> 654308249 POINT (-84.29572 33.77831)  854.08773 [m]
#> 654308250 POINT (-84.29573 33.77804)  826.30178 [m]

reprex 包于 2022-02-10 创建(v2.0.1)

问题key=traffic:hourly是,它不受欢迎且很少使用,请参阅https://taginfo.openstreetmap.org/keys/?key=traffic%3Ahourly


推荐阅读