r - 使用 ggmap 和 geom_path 绘制街道

标签 r ggplot2 ggmap overpass-api

我喜欢用 ggmap 绘制街道在特定区域。我通过立交桥 api 从 osm 获取数据。它适用于大多数街道,geom_path .然而,有些街道是乱七八糟的。任何提示表示赞赏。

请看 http://overpass-turbo.eu/对于所需的输出。您可以在下面的 R 代码中找到查询。

library(httr)
library(tidyverse)
#> ── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 2.2.1     ✔ purrr   0.2.4
#> ✔ tibble  1.4.2     ✔ dplyr   0.7.4
#> ✔ tidyr   0.8.0     ✔ stringr 1.3.0
#> ✔ readr   1.1.1     ✔ forcats 0.3.0
#> ── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter() masks stats::filter()
#> ✖ dplyr::lag()    masks stats::lag()
library(ggmap)
library(rlist)


# Get data from overpass api
query <- paste('[out:json];',
  '(way["highway"~"primary|residential"](53.5970, 9.9010, 53.6050, 9.9080););',
  '(._;>;);',
  'out body;')
url <- 'http://overpass-api.de/api/interpreter'
r <- POST(url = url, body = query, encode = 'json')


# Tidy data
nodes <- content(r)$elements %>% list.filter(type == 'node')
ways <- content(r)$elements %>% list.filter(type == 'way')

df_nodes <- nodes %>% 
  list.select(type, id, lat, lon) %>%
  bind_rows()

df_ways <- ways %>% 
  lapply(function(x) list.append(x, street = x$tags$name)) %>%
  list.select(street, nodes)
df_ways <- map(df_ways, function(x) x %>% as_tibble) %>% 
  bind_rows() %>% 
  mutate(id = unlist(nodes))

df <- df_ways %>% left_join(df_nodes, by = 'id')
head(df)
#> # A tibble: 6 x 6
#>   street           nodes            id type    lat   lon
#>   <chr>            <list>        <dbl> <chr> <dbl> <dbl>
#> 1 Reichsbahnstraße <int [1]>  38893884 node   53.6  9.91
#> 2 Reichsbahnstraße <int [1]>  55079985 node   53.6  9.91
#> 3 Reichsbahnstraße <int [1]>  38893882 node   53.6  9.91
#> 4 Reichsbahnstraße <int [1]>  38893881 node   53.6  9.91
#> 5 Reichsbahnstraße <int [1]> 380820539 node   53.6  9.91
#> 6 Reichsbahnstraße <int [1]>  38893879 node   53.6  9.91


# Get map
lat <- (max(df$lat)+min(df$lat))/2
lon <- (max(df$lon)+min(df$lon))/2
hamburg <- get_map(location = c(lon = lon, lat = lat), zoom = 16)
#> Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=53.601726,9.90531&zoom=16&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false


# Plot
ggmap(hamburg) +
  geom_path(data = df, aes(x = lon, y = lat, color = street), size = 2)
#> Warning: Removed 3 rows containing missing values (geom_path).

'Actual Output'
'Desired Output'

最佳答案

每当街道有许多分支时,您就会遇到此问题,因为 geom_path()将简单地将每两个连续点与一条直线连接起来。

让我们以“Reichsbahnstraße”为例:

lon <- df %>% filter(street == "Reichsbahnstraße") %>% .$lon
lat <- df %>% filter(street == "Reichsbahnstraße") %>% .$lat
lat[1] == lat[41] & lon[1] == lon[41]
# returns TRUE
geom_path()从点 1(交叉点,见下文)开始,绘制街道的一部分(走向东北),然后再次回到交叉点(索引 41)以绘制下一个分支。

为了避免这种情况,您可以做的是在绘制另一个分支之前通过同一条道路返回交叉点(例如,而不是 c(7, 8, 9, 10, 7, 6)c(7, 8, 9, 10, 9, 8, 7, 6) )。有点像如果你试图用铅笔画街道而不把它从纸上取下来你会怎么做。

enter image description here

关于r - 使用 ggmap 和 geom_path 绘制街道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50086143/

相关文章:

r - 实三次多项式的最快数值解?

r - 将一条线拟合到热图上

r - 热图中的密度计数

r - 使用注释时的ggmap和错误(geom = "text")

r - 基于值通过R中的ggmap生成空间热图

r - 在 R 中查找长度大于 1 的向量元素

R:是否可以将 2000 万多行 CSV 的读取并行化/加速到 R 中?

r - 在 R 中指定网格箭头的 gpar 设置

r - 如何使用 str_detect() 选择行,然后根据其匹配的模式创建一个新变量?

r - 仅在 geom_bar 和 geom_errorbar 中的一组条上绘制误差线