Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gdal_footprint support to gdal_utils #2305

Merged
merged 7 commits into from Jan 2, 2024

Conversation

goergen95
Copy link
Contributor

Hi,
as discussed in #2304 this PR adds support for the gdal_footprint utility requiring GDAL >= 3.8.0.
I am very new to C++, so please double-check if everything is ok. I used the script below to check the functionality:

# remotes::install_github("https://github.com/goergen95/sf", ref = "gdalfootprint")
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.8.1, PROJ 9.1.1; sf_use_s2() is TRUE
tif <- system.file("tif/geomatrix.tif", package = "sf")
t1 <- tempfile(fileext = ".gpkg")
t2 <- tempfile(fileext = ".json")
t3 <- tempfile(fileext = ".gpkg")
t4 <- tempfile(fileext = ".gpkg")
t5 <- tempfile(fileext = ".gpkg")
t6 <- tempfile(fileext = ".gpkg")

# GPKG output
gdal_utils("gdalfootprint", tif, t1)
(s1 <- st_read(t1))
#> Reading layer `footprint' from data source `/tmp/RtmpS1pM0Y/file27bf77437ef6.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#>                             geom
#> 1 MULTIPOLYGON (((1841002 114...
# GeoJSON output
gdal_utils("gdalfootprint", tif, t2, options = c("-of", "GeoJSON"))
(s2 <- st_read(t2))
#> Reading layer `footprint' from data source `/tmp/RtmpS1pM0Y/file27bf1a8c155b.json' using driver `GeoJSON'
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#>                         geometry
#> 1 MULTIPOLYGON (((1841002 114...
# gdal_footprint options
gdal_utils("gdalfootprint", tif, t3, options = c("-t_srs", "EPSG:4326"))
(s3 <- st_read(t3))
#> Reading layer `footprint' from data source `/tmp/RtmpS1pM0Y/file27bf33b7643b.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -104.8474 ymin: 10.11931 xmax: -104.8463 ymax: 10.12043
#> Geodetic CRS:  WGS 84
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -104.8474 ymin: 10.11931 xmax: -104.8463 ymax: 10.12043
#> Geodetic CRS:  WGS 84
#>                             geom
#> 1 MULTIPOLYGON (((-104.8465 1...
# opening options
gdal_utils("gdalfootprint", tif, t4, options = c("-oo", "NUM_THREADS=1"))
(s4 <- st_read(t4))
#> Reading layer `footprint' from data source `/tmp/RtmpS1pM0Y/file27bf1086b216.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#>                             geom
#> 1 MULTIPOLYGON (((1841002 114...
# data set creation options
gdal_utils("gdalfootprint", tif, t5, options = c("-dsco", "CRS_WKT_EXTENSION=Yes"))
(s5 <- st_read(t5))
#> Reading layer `footprint' from data source `/tmp/RtmpS1pM0Y/file27bf1d7b91f7.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: 1840902 ymin: 1143873 xmax: 1841032 ymax: 1144003
#> Projected CRS: WGS 84 / UTM zone 11N
#>                             geom
#> 1 MULTIPOLYGON (((1841002 114...
# remote file with overview option
tif2 <- paste0("/vsicurl/https://deafrica-input-datasets.s3.af-south-1.amazonaws.com/", 
               "cci_landcover/1992/cci_landcover_1992_v2.0.7cds.tif")
gdal_utils("gdalfootprint", tif2, t6, options = c("-ovr", "4"))
(s6 <- st_read(t6))
#> Reading layer `footprint' from data source `/tmp/RtmpS1pM0Y/file27bf6fbdb9e1.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -26.36111 ymin: -47.96944 xmax: 64.5 ymax: 38.35
#> Geodetic CRS:  WGS 84
#> Simple feature collection with 1 feature and 0 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -26.36111 ymin: -47.96944 xmax: 64.5 ymax: 38.35
#> Geodetic CRS:  WGS 84
#>                             geom
#> 1 MULTIPOLYGON (((-26.36111 3...

Created on 2024-01-01 with reprex v2.0.2

#if GDAL_VERSION_NUM < 3080000
Rcpp::CharacterVector ret;
Rcpp::stop("gdalfootprint util requires GDAL >= 3.8.0");
#endif
Copy link
Member

@edzer edzer Jan 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be an #else, and the #endif should go after line 284.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I think I now use #if properly.

R/gdal_utils.R Outdated
@@ -112,6 +112,7 @@ gdal_utils = function(util = "info", source, destination, options = character(0)
"-te", "-tr", "-tap", "-ts", "-ot")) # https://gdal.org/programs/gdal_rasterize.html
CPL_gdalrasterize(source, destination, options, oo, doo, config_options, overwrite, quiet)
}, # nocov end
gdalfootprint = CPL_gdalfootprint(source, destination, options, oo, config_options, quiet),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest to call the util footprint, rather than gdalfootprint, in line with the others. Also above in the doc section.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

@edzer edzer merged commit 26ebfea into r-spatial:main Jan 2, 2024
6 of 7 checks passed
@edzer
Copy link
Member

edzer commented Jan 2, 2024

Great, thanks!

@edzer
Copy link
Member

edzer commented Jan 2, 2024

I added a few more commits to clean up.

@goergen95
Copy link
Contributor Author

Cool, thanks for the help. I'll close the issue then.

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.

None yet

2 participants