Skip to content

set value of boundary if place is sf/sfc/bbox - #314

Merged
agila5 merged 7 commits into
ropensci:masterfrom
juanfonsecaLS1:add-wkt-filter-oe-get
Mar 13, 2026
Merged

set value of boundary if place is sf/sfc/bbox#314
agila5 merged 7 commits into
ropensci:masterfrom
juanfonsecaLS1:add-wkt-filter-oe-get

Conversation

@juanfonsecaLS1

@juanfonsecaLS1 juanfonsecaLS1 commented Mar 10, 2026

Copy link
Copy Markdown
Contributor
  • set boundary if place is sf/sfc/bbox
  • refined validation
  • added change to documentation

Description

Added code so the boundary takes the value of place if it is a sf/sfc POLYGON or a BBOX, reducing the processing time if the available pbf is bigger than the target area. Also reduces the need to provide redundant inputs.

Related Issue

Addresses #313

Example

devtools::load_all()
#> ℹ Loading 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(sf)
#> Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library(tmap)

sample_coordinates <- st_point(x = c( 31.221185559514872   , 30.061534928785676 ))

sfc_point <- st_sfc(sample_coordinates,crs = 4326)

buffer <- st_buffer(sfc_point,dist = 1000)
bbox <- st_bbox(buffer)

my_filter <- buffer |> st_as_text()


# Extracting the data using osmextract
osmext_net_buffer <- oe_get_network(buffer, mode = "driving")
#> The input place was matched with Egypt. 
#> The chosen file was already detected in the download directory. Skip downloading.
#> Starting 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:\temp\osmextract\geofabrik_egypt-latest.gpkg' using driver `GPKG'
#> Simple feature collection with 674 features and 14 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 31.19957 ymin: 30.03999 xmax: 31.23587 ymax: 30.09277
#> Geodetic CRS:  WGS 84
nrow(osmext_net_buffer)
#> [1] 674

osmext_net_bbox <- oe_get_network(bbox, mode = "driving")
#> The input place was matched with Egypt. 
#> The chosen file was already detected in the download directory. Skip downloading.
#> Starting 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:\temp\osmextract\geofabrik_egypt-latest.gpkg' using driver `GPKG'
#> Simple feature collection with 674 features and 14 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 31.19957 ymin: 30.03999 xmax: 31.23587 ymax: 30.09277
#> Geodetic CRS:  WGS 84
nrow(osmext_net_bbox)
#> [1] 674

osmext_net_wkt <- oe_get_network("Egypt", mode = "driving",wkt_filter = my_filter)
#> The input place was matched with: Egypt
#> The chosen file was already detected in the download directory. Skip downloading.
#> Starting 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:\temp\osmextract\geofabrik_egypt-latest.gpkg' using driver `GPKG'
#> Simple feature collection with 447 features and 14 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 31.19957 ymin: 30.03999 xmax: 31.23587 ymax: 30.09277
#> Geodetic CRS:  WGS 84
nrow(osmext_net_wkt)
#> [1] 447

## Results of the original code
# nrow(osmext_net_full)
# [1] 1509041


qtm(osmext_net_buffer, col = "green",col_alpha = 0.5)+
  qtm(osmext_net_bbox, col = "red", col_alpha = 0.5)+
  qtm(osmext_net_wkt, col = "blue", col_alpha = 0.5)

Created on 2026-03-10 with reprex v2.1.1

Comment thread R/get.R Outdated
@juanfonsecaLS1 juanfonsecaLS1 changed the title set value of boundary if place is sf/sfc/bbox set value of boundary if place is sf/sfc/bbox Mar 12, 2026
As suggested by @Robinlovelace in ropensci#314: adds a message explaining
that boundary=place is being used to geographically subset the output,
and how to override with boundary=NULL to import the full extract.
@elifclaus

elifclaus commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

I've implemented your message suggestion in a PR to @juanfonsecaLS1's branch: juanfonsecaLS1#1

The message reads:

Setting boundary = place to geographically subset the output.
Use boundary = NULL to import full extract.

The two-line format makes the behaviour clear and immediately tells users how to opt out if they want the full extract.

Add informative message when boundary is auto-set from place
@juanfonsecaLS1

Copy link
Copy Markdown
Contributor Author

@agila5 I have just updated NEWS.md

@agila5

agila5 commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Thanks!

Setting boundary = place to geographically subset the output. Use boundary = NULL to import full extract.

I believe that's not going to work since the default value of boundary is NULL. Another idea might be to slightly modify the logic behind the new approach so that boundary = place is not run if, for example, boundary = NA (instead of NULL). If you agree, I'll apply the change after merging.

@Robinlovelace

Copy link
Copy Markdown
Member

Thanks!

Setting boundary = place to geographically subset the output. Use boundary = NULL to import full extract.

I believe that's not going to work since the default value of boundary is NULL. Another idea might be to slightly modify the logic behind the new approach so that boundary = place is not run if, for example, boundary = NA (instead of NULL). If you agree, I'll apply the change after merging.

Oh yes of course, I forgot that the default is place = NULL doh 🤦

@Robinlovelace

Copy link
Copy Markdown
Member

place = FALSE should also work...

@agila5
agila5 merged commit a0ed569 into ropensci:master Mar 13, 2026
3 checks passed
@agila5

agila5 commented Mar 13, 2026

Copy link
Copy Markdown
Contributor

Ok, merged! I'll apply final changes and then we can also close the issue.

@juanfonsecaLS1
juanfonsecaLS1 deleted the add-wkt-filter-oe-get branch March 13, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants