使用ggmap和geom_path绘制街道

12

我喜欢用ggmap在特定区域绘制街道地图。我通过overpass 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'

1个回答

1
每当街道有许多分支时,您将遇到此问题,因为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


谢谢你的回答。能自动化吗?每个项目都是不同的领域,因此手动调整并不实际。还是说这归结为“ggmap不是适合该工作的正确工具”? - Birger
我会遍历每个“街道”的坐标值x。每当一个点重复出现时,例如x[i] == x[j],我会使用append(x, x[(j-1):i], after = j)将反转的序列x[(j-1):i] = x[j-1], ..., x[i]插入到x[j]和下一个点x[j+1]之间(我从j-1开始以避免在序列中出现c(..., x[j], x[j], x[j-1], ...))。 这里我使用的索引是针对单个向量的,在您的情况下,您需要考虑(lat, lon)值。 希望这很清楚,如果您认为我应该在答案中添加这一点,请告诉我 :) - byouness
这对我不起作用。我已经绘制了Reichsbahnstraße的值,并从开始到结束进行了着色。请看这里:https://imgur.com/a/Uv0XMaI 在到达第一个交叉口之前存在一个跳跃。 - Birger
我认为自动化这个任务并不容易,特别是如果每条街道都有自己的问题... 对于 Reichsbahnstrasse 街道,在观察了 15 分钟的点并画出部分街道后,这是我得到的代码: geom_path(data = df %>% filter(street == 'Reichsbahnstraße') %>% .[c(1:4, 41:5, 58:42),], aes(...))问题如我的答案所述(即数据包含一段接一段的街道。在某些部分之后,我们跳到另一条以远离前一个起点的位置开始的街道,因此形成了一条直线)。 - byouness
所以我做的是将一些部分反转,以便每个部分的结尾对应下一个部分的开头:1:4,然后是41:5,最后是58:42。希望这可以帮到你! - byouness

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接