Skip to content

Commit

Permalink
Implement gpkg_validate() first phase
Browse files Browse the repository at this point in the history
  • Loading branch information
brownag committed Mar 3, 2024
1 parent 5617b3b commit 37029a1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 36 deletions.
11 changes: 11 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# gpkg 0.0.9

- Implemented GDAL driver detection for file paths via {vapour} for #15

- Implemented functions for creating, adding, and removing spatial reference systems from `gpkg_spatial_ref_sys`:
- `gpkg_create_spatial_ref_sys()`, `gpkg_add_spatial_ref_sys()` `gpkg_delete_spatial_ref_sys()`, `gpkg_list_srs()`

- Implemented basic `gpkg_validate()` routine (will be expanded)
- Checks that `gpkg_contents` and `gpkg_spatial_ref_sys` exist
- Checks that at least one tile or vector dataset (with at least 0 rows) is in the database and `gpkg_contents`

# gpkg 0.0.8

- Added `gpkg_create_spatial_view()` for creating spatial views, which are dynamic layers accessible as if they were typical static geometry layers (for #6).
Expand Down
20 changes: 19 additions & 1 deletion R/gpkg-validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,23 @@
#' @return `TRUE` if valid. `FALSE` if one or more problems are found. For full diagnostics run with `diagnostics = TRUE` to return a list containing results for each input GeoPackage.
#' @export
gpkg_validate <- function(x, diagnostics = FALSE) {
stop("This is not implemented yet")
x <- .gpkg_connection_from_x(x)
lt <- gpkg_list_tables(x)
cg <- gpkg_contents(x)
res <- list(
required_tables = all(c("gpkg_contents","gpkg_spatial_ref_sys") %in% lt),
has_contents = (nrow(cg) > 0),
has_spatial_tables = all(cg$table_name %in% lt) &&
sum(cg$data_type %in%
c("features", "2d-gridded-coverage")) > 0
)
if (is.null(diagnostics) ||
length(diagnostics) == 0 ||
nchar(diagnostics) == 0) {
diagnostics <- names(res)
}
if (is.character(diagnostics) || isTRUE(diagnostics)) {
return(res[diagnostics])
}
all(sapply(res[diagnostics], isTRUE))
}
67 changes: 34 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ start by adding two DEM (GeoTIFF) files.
``` r
library(gpkg)
library(terra)
#> terra 1.7.55
#> terra 1.7.73

dem <- system.file("extdata", "dem.tif", package = "gpkg")
stopifnot(nchar(dem) > 0)
Expand All @@ -85,6 +85,7 @@ gpkg_write(
RASTER_TABLE = "DEM1",
FIELD_NAME = "Elevation"
)
#> Loading required namespace: vapour

gpkg_write(
dem,
Expand Down Expand Up @@ -139,7 +140,7 @@ g
#> rtree_bbox_geom_parent, rtree_bbox_geom_rowid, sqlite_sequence
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#> Path: /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg
#> Path: /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg
#> Extensions: TRUE
class(g)
#> [1] "geopackage"
Expand All @@ -163,14 +164,14 @@ g2
#> rtree_bbox_geom_parent, rtree_bbox_geom_rowid, sqlite_sequence
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#> Path: /tmp/RtmpQZ3zKV/Rgpkg304b329e168a3.gpkg
#> Path: /tmp/RtmpucSQbZ/Rgpkg150d0280f06f3.gpkg
#> Extensions: TRUE
class(g2)
#> [1] "geopackage"
```

Note that a temporary GeoPackage
(/tmp/RtmpQZ3zKV/Rgpkg304b329e168a3.gpkg) is automatically created when
(/tmp/RtmpucSQbZ/Rgpkg150d0280f06f3.gpkg) is automatically created when
using the `geopackage(<list>)` constructor.

You also may have a *DBIConnection* to a GeoPackage database already
Expand Down Expand Up @@ -209,8 +210,8 @@ gpkg_tables(g)
#> resolution : 0.008333333, 0.008333333 (x, y)
#> extent : 6.008333, 6.266667, 49.69167, 49.94167 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : file304b339abcc96.gpkg:DEM1
#> varname : file304b339abcc96
#> source : file150d056e9f7be.gpkg:DEM1
#> varname : file150d056e9f7be
#> name : DEM1
#> min value : 195
#> max value : 500
Expand All @@ -221,15 +222,15 @@ gpkg_tables(g)
#> resolution : 0.008333333, 0.008333333 (x, y)
#> extent : 6.008333, 6.266667, 49.69167, 49.94167 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> source : file304b339abcc96.gpkg:DEM2
#> varname : file304b339abcc96
#> source : file150d056e9f7be.gpkg:DEM2
#> varname : file150d056e9f7be
#> name : DEM2
#> min value : 195
#> max value : 500
#>
#> $myattr
#> # Source: table<myattr> [10 x 2]
#> # Database: sqlite 3.41.2 [/tmp/RtmpQZ3zKV/file304b339abcc96.gpkg]
#> # Database: sqlite 3.45.0 [/tmp/RtmpucSQbZ/file150d056e9f7be.gpkg]
#> a b
#> <int> <chr>
#> 1 1 A
Expand All @@ -248,7 +249,7 @@ gpkg_tables(g)
#> geometry : polygons
#> dimensions : 1, 0 (geometries, attributes)
#> extent : 6.008333, 6.266667, 49.69167, 49.94167 (xmin, xmax, ymin, ymax)
#> source : file304b339abcc96.gpkg (bbox)
#> source : file150d056e9f7be.gpkg (bbox)
#> layer : bbox
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)

Expand Down Expand Up @@ -293,13 +294,13 @@ returns a *tbl_SQLiteConnection*.
``` r
gpkg_tbl(g, "gpkg_contents")
#> # Source: table<gpkg_contents> [4 x 10]
#> # Database: sqlite 3.41.2 [/tmp/RtmpQZ3zKV/file304b339abcc96.gpkg]
#> # Database: sqlite 3.45.0 [/tmp/RtmpucSQbZ/file150d056e9f7be.gpkg]
#> table_name data_type identifier description last_change min_x min_y max_x
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 DEM1 2d-gridded… DEM1 "" 2023-11-19… 6.01 49.7 6.27
#> 2 DEM2 2d-gridded… DEM2 "" 2023-11-19… 6.01 49.7 6.27
#> 3 bbox features bbox "" 2023-11-19… 6.01 49.7 6.27
#> 4 myattr attributes myattr "" 2023-11-19… -180 -90 180
#> 1 DEM1 2d-gridded… DEM1 "" 2024-03-03… 6.01 49.7 6.27
#> 2 DEM2 2d-gridded… DEM2 "" 2024-03-03… 6.01 49.7 6.27
#> 3 bbox features bbox "" 2024-03-03… 6.01 49.7 6.27
#> 4 myattr attributes myattr "" 2024-03-03… -180 -90 180
#> # ℹ 2 more variables: max_y <dbl>, srs_id <int>
```

Expand All @@ -322,12 +323,12 @@ analysis.
``` r
head(gpkg_table_pragma(g))
#> dsn table_name nrow table_info.cid
#> 1 /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg DEM1 1 0
#> 2 /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg DEM1 1 1
#> 3 /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg DEM1 1 2
#> 4 /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg DEM1 1 3
#> 5 /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg DEM1 1 4
#> 6 /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg DEM2 1 0
#> 1 /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg DEM1 1 0
#> 2 /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg DEM1 1 1
#> 3 /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg DEM1 1 2
#> 4 /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg DEM1 1 3
#> 5 /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg DEM1 1 4
#> 6 /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg DEM2 1 0
#> table_info.name table_info.type table_info.notnull table_info.dflt_value
#> 1 id INTEGER 0 <NA>
#> 2 zoom_level INTEGER 1 <NA>
Expand Down Expand Up @@ -355,7 +356,7 @@ gpkg_vect(g, 'bbox')
#> geometry : polygons
#> dimensions : 1, 0 (geometries, attributes)
#> extent : 6.008333, 6.266667, 49.69167, 49.94167 (xmin, xmax, ymin, ymax)
#> source : file304b339abcc96.gpkg (bbox)
#> source : file150d056e9f7be.gpkg (bbox)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
```

Expand All @@ -369,7 +370,7 @@ gpkg_vect(g, 'gpkg_ogr_contents')
#> geometry : none
#> dimensions : 0, 2 (geometries, attributes)
#> extent : 0, 0, 0, 0 (xmin, xmax, ymin, ymax)
#> source : file304b339abcc96.gpkg (SELECT)
#> source : file150d056e9f7be.gpkg (SELECT)
#> coord. ref. :
#> names : table_name feature_count
#> type : <chr> <int>
Expand Down Expand Up @@ -419,10 +420,10 @@ gpkg_rast(g)
#> resolution : 0.008333333, 0.008333333 (x, y)
#> extent : 6.008333, 6.266667, 49.69167, 49.94167 (xmin, xmax, ymin, ymax)
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> sources : file304b339abcc96.gpkg:DEM1
#> file304b339abcc96.gpkg:DEM2
#> varnames : file304b339abcc96
#> file304b339abcc96
#> sources : file150d056e9f7be.gpkg:DEM1
#> file150d056e9f7be.gpkg:DEM2
#> varnames : file150d056e9f7be
#> file150d056e9f7be
#> names : DEM1, DEM2
#> min values : 195, 195
#> max values : 500, 500
Expand All @@ -444,13 +445,13 @@ contains critical information on the data contained in a GeoPackage.
``` r
gpkg_table(g, "gpkg_contents")
#> # Source: table<gpkg_contents> [4 x 10]
#> # Database: sqlite 3.41.2 [/tmp/RtmpQZ3zKV/file304b339abcc96.gpkg]
#> # Database: sqlite 3.45.0 [/tmp/RtmpucSQbZ/file150d056e9f7be.gpkg]
#> table_name data_type identifier description last_change min_x min_y max_x
#> <chr> <chr> <chr> <chr> <chr> <dbl> <dbl> <dbl>
#> 1 DEM1 2d-gridded… DEM1 "" 2023-11-19… 6.01 49.7 6.27
#> 2 DEM2 2d-gridded… DEM2 "" 2023-11-19… 6.01 49.7 6.27
#> 3 bbox features bbox "" 2023-11-19… 6.01 49.7 6.27
#> 4 myattr attributes myattr "" 2023-11-19… -180 -90 180
#> 1 DEM1 2d-gridded… DEM1 "" 2024-03-03… 6.01 49.7 6.27
#> 2 DEM2 2d-gridded… DEM2 "" 2024-03-03… 6.01 49.7 6.27
#> 3 bbox features bbox "" 2024-03-03… 6.01 49.7 6.27
#> 4 myattr attributes myattr "" 2024-03-03… -180 -90 180
#> # ℹ 2 more variables: max_y <dbl>, srs_id <int>
```

Expand Down Expand Up @@ -503,7 +504,7 @@ gpkg_connect(g)
#> rtree_bbox_geom_parent, rtree_bbox_geom_rowid, sqlite_sequence
#> --------------------------------------------------------------------------------
#> <SQLiteConnection>
#> Path: /tmp/RtmpQZ3zKV/file304b339abcc96.gpkg
#> Path: /tmp/RtmpucSQbZ/file150d056e9f7be.gpkg
#> Extensions: TRUE

# disconnect
Expand Down
3 changes: 1 addition & 2 deletions inst/tinytest/test_gpkg.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ expect_equal(gpkg_add_relatedtables_extension(gempty), 0)
gpkg_disconnect(gempty)
unlink(gempty$dsn)

# TODO: validator
expect_error(gpkg_validate(g))
expect_true(gpkg_validate(g))

# checking ability to clean up corrupted contents
RSQLite::dbRemoveTable(g$env$con, "gpkg_contents")
Expand Down

0 comments on commit 37029a1

Please sign in to comment.