簡體   English   中英

R 我怎樣才能從立交橋 API 獲得唯一的方法並減少數據量

[英]R how can I get only ways from Overpass API and reduce the amount of data

我試圖減少查詢跨越服務器所需的數據量和時間。 我只對方法和使用 osmdata Package 感興趣,這是我目前的方法:

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並轉換為sfc object(參見github的討論):

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 提取。 如果您在德國使用數據,那么我建議您檢查geofabrikbbbike提供商:

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返回的提取小得多; 因此,我將使用bbbike返回的 OSM 數據運行以下步驟。

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

請注意,function 識別出您已經下載了 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)

代表 package (v1.0.0) 於 2021 年 3 月 31 日創建

概括:

  1. 如果你需要多次導入 OSM 數據,那么你應該有一個持久的下載目錄 這也意味着您無需在每次運行新查詢時都下載 OSM 數據提取(除非請求的數據不包含在任何現有數據提取中)。
  2. 如果您需要導入覆蓋中/小型地理區域的 OSM 線路,我建議采用“查詢”方法(即lines_v1)。
  3. 第二種方法有幾個好處(即它比另一種方法更快,尤其是對於較大的提取物,並且從前面的 plot 中可以看出,它剪裁線而不是選擇與框相交的道路)。 另一方面,從頭開始編寫vectortranslate選項非常困難(我們正在開發更直觀的API,但目前還處於開發階段)。 此外,該選項會修改.gpkg文件的底層結構(可能會產生相關后果)。 我們正在努力解決這兩個問題,但您需要等到版本 0.3 或 0.4。

查看此處此處此處了解 osmextract 背后的更多詳細信息。

隨時在此處添加任何問題或評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2025 STACKOOM.COM