r - R我如何才能从Overpass API获得唯一的方法并减少数据量
问题描述
我试图减少查询跨越服务器所需的数据量和时间。我只对方式感兴趣,通过使用 osmdata 包,这是我目前的方法:
library(osmdata)
bbox_dimensions <-c(xmin=11.2360151977671, ymin= 47.8047832575026, xmax= 11.8886729361838, ymax=48.2426118570748)
my_osm_data <- opq(bbox = bbox_dimensions,timeout = 180,memsize = 104857600) %>%
add_osm_feature(
key = 'highway',
value = c("primary","secondary", "tertiary")
) %>%
osmdata_sf(quiet = FALSE)
是否可以减少此查询的数据量?我只对沿途的节点不感兴趣。
解决方案
正如我在评论中所写,如果您需要对属于同一地理区域的 OSM 数据运行多个查询,我建议您采用以下方法。
首先,加载包
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(osmextract)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright.
#> Check the package website, https://docs.ropensci.org/osmextract/, for more details.
library(tmap)
tmap_mode("view")
#> tmap mode set to interactive viewing
定义bbox
并转换为对象(参见githubsfc
上的讨论):
my_bbox <- st_bbox(
c(xmin = 11.2360151977671, ymin = 47.8047832575026, xmax = 11.8886729361838, ymax = 48.2426118570748),
crs = 4326
)
my_bbox_poly <- st_as_sfc(my_bbox)
然后,我们需要为应该涵盖所有查询的特定地理区域下载 OSM 提取。如果您在德国使用数据,那么我建议您检查geofabrik
和bbbike
提供者:
oe_match(my_bbox_poly, provider = "geofabrik")
#> The input place was matched with multiple geographical areas.
#> Selecting the smallest administrative unit. Check ?oe_match for more details.
#> $url
#> [1] "https://download.geofabrik.de/europe/germany/bayern/oberbayern-latest.osm.pbf"
#>
#> $file_size
#> [1] 185338670
oe_match(my_bbox_poly, provider = "bbbike")
#> $url
#> [1] "https://download.bbbike.org/osm/bbbike/Muenchen/Muenchen.osm.pbf"
#>
#> $file_size
#> [1] 58400897
提供者返回的提取比;bbbike
返回的提取小得多。geofabrik
因此,我将使用返回的 OSM 数据运行以下步骤bbbike
。
oe_get("Muenchen", provider = "bbbike", download_only = TRUE, skip_vectortranslate = TRUE)
#> The input place was matched with: Muenchen
#> File downloaded!
#> [1] "C:\\Users\\Utente\\Documents\\osm-data\\bbbike_Muenchen.osm.pbf"
然后,如果您想读入属于特定 bbox 并具有某些特征的行数据,那么我建议采用以下方法:
lines_v1 <- oe_get(
place = "Muenchen", # or place = my_bbox_poly
layer = "lines",
provider = "bbbike",
query = "SELECT * FROM lines WHERE highway IN ('primary', 'secondary', 'tertiary')",
wkt_filter = st_as_text(my_bbox_poly)
)
#> The input place was matched with: Muenchen
#> The chosen file was already detected in the download directory. Skip downloading.
#> Start with the vectortranslate operations on the input file!
#> 0...10...20...30...40...50...60...70...80...90...100 - done.
#> Finished the vectortranslate operations on the input file!
#> Reading layer `lines' from data source `C:\Users\Utente\Documents\osm-data\bbbike_Muenchen.gpkg' using driver `GPKG'
#> Simple feature collection with 13032 features and 9 fields
#> Geometry type: LINESTRING
#> Dimension: XY
#> Bounding box: xmin: 11.19608 ymin: 47.80002 xmax: 11.89542 ymax: 48.25359
#> Geodetic CRS: WGS 84
请注意,该功能会识别您已经下载了 OSM 提取并跳过再次下载相同的文件。如果您设置永久下载目录,则可以优化此过程。有关更多详细信息,请参见此处。
# Check result
tm_shape(my_bbox_poly) +
tm_borders(col = "darkred") +
tm_shape(lines_v1) +
tm_lines(lwd = 2)
一种更有效(但更棘手)的方法如下:
lines_v2 <- oe_get(
place = "Muenches",
layer = "lines",
provider = "bbbike",
vectortranslate_options = c(
"-f", "GPKG",
"-overwrite",
"-where", "highway IN ('primary', 'secondary', 'tertiary')",
"-clipsrc", st_as_text(my_bbox_poly),
"-nlt", "PROMOTE_TO_MULTI",
"lines"
)
)
#> The input place was matched with: Muenchen
#> The chosen file was already detected in the download directory. Skip downloading.
#> Start with the vectortranslate operations on the input file!
#> 0...10...20...30...40...50...60...70...80...90...100 - done.
#> Finished the vectortranslate operations on the input file!
#> Reading layer `lines' from data source `C:\Users\Utente\Documents\osm-data\bbbike_Muenchen.gpkg' using driver `GPKG'
#> Simple feature collection with 13027 features and 9 fields
#> Geometry type: MULTILINESTRING
#> Dimension: XY
#> Bounding box: xmin: 11.23602 ymin: 47.80478 xmax: 11.88867 ymax: 48.24261
#> Geodetic CRS: WGS 84
图形检查
# Check result
tm_shape(my_bbox_poly) +
tm_borders(col = "darkred") +
tm_shape(lines_v2) +
tm_lines(lwd = 2)
由reprex 包于 2021-03-31 创建(v1.0.0)
概括:
- 如果你需要多次导入 OSM 数据,那么你应该有一个持久的下载目录。这也意味着您无需在每次运行新查询时都下载 OSM 数据提取(除非请求的数据不包含在任何现有数据提取中)。
- 如果您需要导入覆盖中/小型地理区域的 OSM 线路,我建议采用“查询”方法(即lines_v1)。
- 第二种方法有几个好处(即它比另一种更快,特别是对于较大的提取物,并且,正如您从上图中看到的那样,它剪裁线而不是选择与框相交的道路)。另一方面,从头开始编写 vectortranslate 选项非常困难(我们正在开发一个更直观的 API,但目前还处于开发阶段)。此外,该选项会修改
.gpkg
文件的底层结构(这可能会产生相关后果)。我们正在努力解决这两个问题,但您需要等到版本 0.3 或 0.4。
查看此处、此处和此处了解 osmextract 背后的更多详细信息。
随时在此处添加任何问题或评论。
推荐阅读
- python - 有关将图像添加到视频的信息
- java - 如何防止 Escape 键关闭 JFace 对话框
- java - Mockito - 未完成的存根
- javascript - 在 click 事件中将 javascript 对象分成具有 4 个项目的组
- python - Fortran 子例程不返回 python
- git - 从源分支移动提交,但在目标分支将被合并回来时保留它们?
- json - 计算一个特定的 json/word 出现了多少次
- ionic-framework - Ionic 2 去除离子清新剂下的线
- c# - 如何从 CosmosDB 中的特定类型获取所有项目
- c# - MVC如何将GUID转换为模型中的字符串