sf icon indicating copy to clipboard operation
sf copied to clipboard

`st_read` reads compressed GPKG (>= GDAL 3.7) - should this be advertised?

Open rsbivand opened this issue 1 year ago • 4 comments

@Nowosad I've been looking at reading compressed vector files, such as this: GB_election_2024_sim.gpkg.zip

In terra:

> xx <- terra::vect("GB_election_2024_sim.gpkg.zip")
> xx
 class       : SpatVector 
 geometry    : polygons 
 dimensions  : 632, 19  (geometries, attributes)
 extent      : 5512.998, 655970.4, 5342.9, 1220287  (xmin, xmax, ymin, ymax)
 source      : GB_election_2024_sim.gpkg.zip (GB_election_2024_sim)
 coord. ref. : OSGB36 / British National Grid (EPSG:27700) 
 names       :     Constituency            Name Area_Code Area_Description
 type        :            <chr>           <chr>     <chr>            <chr>
 values      : Aberafan Maesteg Aberafan Maest~       WMC  Westminster Co~
                 Aberdeen North Aberdeen North~       WMC  Westminster Co~
                 Aberdeen South Aberdeen South~       WMC  Westminster Co~
       File_Name Feature_Serial_Number Collection_Serial_Number
           <chr>                 <int>                    <int>
 ABERAFAN_MAEST~                     1                        1
 ABERDEEN_NORTH~                   472                      472
 ABERDEEN_SOUTH~                   473                      473
 Global_Polygon_ID Admin_Unit_ID Census_Code (and 9 more)
             <int>         <int>       <chr>             
            146759        187003   W07000081             
            147374        187618   S14000060             
            147375        187619   S14000061        

In sf:

> sim <- st_read("GB_election_2024_sim.gpkg.zip")
Reading layer `GB_election_2024_sim' from data source 
  `/home/rsb/topics/packages/github-r-spatial/LICD/GB_election_2024_sim.gpkg.zip' 
  using driver `GPKG'
Simple feature collection with 632 features and 19 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 5512.998 ymin: 5342.9 xmax: 655970.4 ymax: 1220287
Projected CRS: OSGB36 / British National Grid
> all.equal(st_geometry(sim), st_geometry(st_cast(st_as_sf(xx), "MULTIPOLYGON")))
[1] TRUE
> all.equal(st_drop_geometry(sim), st_drop_geometry(st_as_sf(xx)))
[1] TRUE

Could we compress the GPKG in spData see https://github.com/Nowosad/spData/issues/62 ? Or are too many using GDAL < 3.7 ? This actually also works from GDAL >= 3.1 for ESRI Shapefiles too.

rsbivand avatar Sep 04 '24 09:09 rsbivand

@rsbivand GDAL 3.7 was just released less than 1.5 years ago. Thus, my feeling is that it is too early to expect the majority of the users to have it...

Nowosad avatar Sep 09 '24 15:09 Nowosad

OK, it does save a lot (~50%) on installed size. CRAN Windows and macOS are OK, current Fedora also, Ubuntu GH actions OK, CRAN Fedora devel checks not OK, because Fedora is still 36, so GDAL 3.4. CRAN macOS checks not OK 3.5 on both architectures. I'll see how to move CRAN forward, at least for feasible check platforms, with the benefit of smaller installed sizes for vector files.

rsbivand avatar Sep 09 '24 18:09 rsbivand

GDAL37 <- as.numeric_version(unname(sf_extSoftVersion()["GDAL"])) >= "3.7.0"
file <- "aa.gpkg.zip"
zipfile <- system.file(file, package="aaa")
if (GDAL37) {
    out <- st_read(zipfile)
} else {
    td <- tempdir()
    bn <- sub(".zip", "", basename(file))
    target <- unzip(zipfile, files=bn, exdir=td)
    out <- st_read(target)
}

rsbivand avatar Sep 10 '24 08:09 rsbivand

Reading zipped geopackage is supported by GDAL since forever. GDAL 3.7 just brings a bit of syntaxic sugar on opening (and direct creation of a .gpkg.zip). For all versions, you can open "/vsizip/GB_election_2024_sim.gpkg.zip/GB_election_2024_sim.gpkg" . Cf logic at https://github.com/OSGeo/gdal/blob/d09bdd06b5b0753b0ae743f531a672259eb9dae5/ogr/ogrsf_frmts/gpkg/ogrgeopackagedriver.cpp#L63 and https://github.com/OSGeo/gdal/blob/d09bdd06b5b0753b0ae743f531a672259eb9dae5/ogr/ogrsf_frmts/gpkg/ogrgeopackagedatasource.cpp#L1438

rouault avatar Oct 05 '24 15:10 rouault