geopackage 0.6.0

Read and write OGC GeoPackage (.gpkg) files: pure-Rust container handling over bundled SQLite, with spec-correct spatial indexing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! `GeoPackage::validate`: the checks, their severities, and what every
//! committed fixture reports.

#![expect(
    clippy::unwrap_used,
    reason = "clippy's allow-*-in-tests covers #[test] fns but not the free helper fns in an integration-test crate; the unwraps in these helpers are the intended failure mechanism"
)]

use std::path::{Path, PathBuf};

use geopackage::core::types::{ColumnType, GeometryType};
use geopackage::{ColumnSpec, Finding, GeoPackage, GeometrySpec, Severity, TableSchemaBuilder};
use tempfile::TempDir;

fn fixtures_dir() -> PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
}

fn open_fixture(name: &str) -> (TempDir, GeoPackage) {
    let dir = tempfile::tempdir().unwrap();
    let dst = dir.path().join(name);
    std::fs::copy(fixtures_dir().join(name), &dst).unwrap();
    let gpkg = GeoPackage::open_lenient(&dst).unwrap();
    (dir, gpkg)
}

fn gpkg() -> (TempDir, GeoPackage) {
    let dir = tempfile::tempdir().unwrap();
    let gpkg = GeoPackage::create(dir.path().join("v.gpkg")).unwrap();
    gpkg.add_epsg_srs(4326).unwrap();
    (dir, gpkg)
}

fn with_layer() -> (TempDir, GeoPackage) {
    let (dir, gpkg) = gpkg();
    gpkg.create_layer(
        &TableSchemaBuilder::new("roads")
            .column(ColumnSpec::new("name", ColumnType::Text(None)))
            .geometry(GeometrySpec::new(GeometryType::LineString, 4326)),
    )
    .unwrap();
    (dir, gpkg)
}

#[test]
fn a_file_this_crate_just_wrote_has_nothing_to_report() {
    let (_dir, gpkg) = with_layer();
    assert_eq!(gpkg.validate().unwrap(), Vec::new());
}

#[test]
fn an_unindexed_layer_is_an_advisory_rather_than_a_defect() {
    let (_dir, gpkg) = gpkg();
    gpkg.create_layer(
        &TableSchemaBuilder::new("roads")
            .geometry(GeometrySpec::new(GeometryType::LineString, 4326))
            .spatial_index(false),
    )
    .unwrap();

    let findings = gpkg.validate().unwrap();
    assert_eq!(
        findings,
        vec![Finding::NoSpatialIndex {
            table_name: "roads".to_owned()
        }]
    );
    assert_eq!(findings[0].severity(), Severity::Advisory);
    assert!(findings[0].repair().is_some());
}

#[test]
fn a_contents_row_with_no_table_is_an_error_that_names_a_repair() {
    let (_dir, gpkg) = with_layer();
    gpkg.connection()
        .execute(
            "INSERT INTO gpkg_contents (table_name, data_type, identifier) \
             VALUES ('ghost', 'attributes', 'ghost')",
            [],
        )
        .unwrap();

    let findings = gpkg.validate().unwrap();
    let finding = findings
        .iter()
        .find(|f| matches!(f, Finding::MissingContentsTable { .. }))
        .expect("the dangling row");
    assert_eq!(finding.severity(), Severity::Error);
    assert_eq!(finding.table_name(), Some("ghost"));
    assert!(finding.repair().is_some());
}

#[test]
fn an_out_of_step_spatial_index_is_an_error() {
    let (_dir, gpkg) = with_layer();
    let layer = gpkg.layer("roads").unwrap();
    let mut writer = layer.writer().unwrap();
    writer
        .insert(
            None,
            &geo_types::Line::new(
                geo_types::Coord { x: 0.0, y: 0.0 },
                geo_types::Coord { x: 1.0, y: 1.0 },
            ),
            &[geopackage::ValueRef::Text("a")],
        )
        .unwrap();
    writer.commit().unwrap();
    assert_eq!(gpkg.validate().unwrap(), Vec::new());

    // Empty the index behind the triggers' back, as a tool writing rows
    // without maintaining it would.
    gpkg.connection()
        .execute("DELETE FROM rtree_roads_geom", [])
        .unwrap();

    let findings = gpkg.validate().unwrap();
    let finding = findings
        .iter()
        .find(|f| matches!(f, Finding::SpatialIndexOutOfStep { .. }))
        .expect("the emptied index");
    assert_eq!(finding.severity(), Severity::Error);
    assert_eq!(finding.table_name(), Some("roads"));
    assert!(finding.repair().unwrap().contains("rebuild_spatial_index"));
}

#[test]
fn a_removed_extension_is_reported_as_a_warning() {
    let (_dir, gpkg) = with_layer();
    gpkg.connection()
        .execute(
            "INSERT INTO gpkg_extensions \
             (table_name, column_name, extension_name, definition, scope) \
             VALUES ('roads', 'geom', 'gpkg_srs_id_trigger', 'x', 'read-write')",
            [],
        )
        .unwrap();

    let findings = gpkg.validate().unwrap();
    let finding = findings
        .iter()
        .find(|f| matches!(f, Finding::RemovedExtension { .. }))
        .expect("the 2016 removal");
    assert_eq!(finding.severity(), Severity::Warning);
    // Nothing this crate can do: the row belongs to whoever wrote it.
    assert_eq!(finding.repair(), None);
}

#[test]
fn an_unrecognised_extension_is_reported_rather_than_ignored() {
    let (_dir, gpkg) = with_layer();
    gpkg.connection()
        .execute(
            "INSERT INTO gpkg_extensions \
             (table_name, column_name, extension_name, definition, scope) \
             VALUES ('roads', 'geom', 'acme_secret_sauce', 'x', 'read-write')",
            [],
        )
        .unwrap();

    assert!(
        gpkg.validate()
            .unwrap()
            .iter()
            .any(|f| matches!(f, Finding::UnrecognisedExtension { .. }))
    );
}

#[test]
fn a_dangling_metadata_reference_is_an_error() {
    use geopackage::core::metadata::MetadataScope;
    use geopackage::{MetadataTarget, NewMetadata};

    let (_dir, gpkg) = with_layer();
    let id = gpkg
        .add_metadata(&NewMetadata::new(
            MetadataScope::Dataset,
            "http://example.invalid/std",
            "{}",
        ))
        .unwrap();
    gpkg.add_metadata_reference(
        id,
        &MetadataTarget::GeoPackage,
        geopackage::core::datetime::DateTime::parse_strict("2020-01-01T00:00:00.000Z").unwrap(),
        None,
    )
    .unwrap();
    assert_eq!(gpkg.validate().unwrap(), Vec::new());

    // Point it at a record that is not there. The DDL's foreign key forbids
    // this, so it takes turning enforcement off, which is how such a file
    // arrives in practice: SQLite defaults foreign keys off, and the writers
    // that produce GeoPackages do not turn them on.
    gpkg.connection()
        .execute_batch(
            "PRAGMA foreign_keys = OFF; \
             UPDATE gpkg_metadata_reference SET md_file_id = 404; \
             PRAGMA foreign_keys = ON;",
        )
        .unwrap();
    let findings = gpkg.validate().unwrap();
    assert!(findings.contains(&Finding::DanglingMetadataReference { md_id: 404 }));
    assert_eq!(findings[0].severity(), Severity::Error);
}

#[test]
fn a_relation_whose_mapping_table_is_gone_is_an_error() {
    use geopackage::NewRelation;
    use geopackage::core::related::RelationName;

    let (_dir, gpkg) = with_layer();
    gpkg.create_attributes_table(
        &TableSchemaBuilder::new("notes").column(ColumnSpec::new("note", ColumnType::Text(None))),
    )
    .unwrap();
    gpkg.add_relation(&NewRelation::new(
        "roads",
        "notes",
        RelationName::Media,
        "roads_notes",
    ))
    .unwrap();
    assert_eq!(gpkg.validate().unwrap(), Vec::new());

    gpkg.connection()
        .execute("DROP TABLE roads_notes", [])
        .unwrap();
    let findings = gpkg.validate().unwrap();
    assert!(findings.contains(&Finding::MissingMappingTable {
        mapping_table_name: "roads_notes".to_owned()
    }));
}

#[test]
fn findings_come_back_most_severe_first() {
    let (_dir, gpkg) = gpkg();
    // An advisory (unindexed layer) and an error (dangling contents row).
    gpkg.create_layer(
        &TableSchemaBuilder::new("roads")
            .geometry(GeometrySpec::new(GeometryType::LineString, 4326))
            .spatial_index(false),
    )
    .unwrap();
    gpkg.connection()
        .execute(
            "INSERT INTO gpkg_contents (table_name, data_type, identifier) \
             VALUES ('ghost', 'attributes', 'ghost')",
            [],
        )
        .unwrap();

    let findings = gpkg.validate().unwrap();
    assert!(findings.len() >= 2);
    let severities: Vec<Severity> = findings.iter().map(Finding::severity).collect();
    let mut sorted = severities.clone();
    sorted.sort_by(|a, b| b.cmp(a));
    assert_eq!(severities, sorted, "{findings:?}");
}

// --- the corpus ---------------------------------------------------------------
//
// Every committed fixture, with its findings pinned. A change in what this
// crate detects shows up here as a diff rather than passing quietly, which is
// the phase 7 item asking for known findings on known files to be the expected
// output.

#[test]
fn every_committed_fixture_reports_what_it_is_expected_to() {
    let expected: &[(&str, &[&str])] = &[
        ("attributes_spread.gpkg", &[]),
        // A GDAL file whose catalogue name differs from the physical table
        // only in case, which is what the fixture exists to carry.
        ("case_mismatch.gpkg", &["TableNameCaseMismatch"]),
        // Written without indexes: the fixture is about relations.
        ("gdal_related.gpkg", &["NoSpatialIndex"]),
        ("gdal_curves.gpkg", &[]),
        (
            "gdal_multilayer_1_4.gpkg",
            &["NoSpatialIndex", "NoSpatialIndex", "NoSpatialIndex"],
        ),
        // A 1.2 file, so it carries the pre-1.4 RTree trigger set.
        ("gdal_points_1_2.gpkg", &["LegacySpatialIndexTriggers"]),
        ("gdal_tiles.gpkg", &[]),
        // GP10 in the header, which is the point of this one.
        ("legacy_gp10.gpkg", &["LegacyApplicationId"]),
        ("qgis_lines.gpkg", &[]),
    ];

    for (name, want) in expected {
        if !fixtures_dir().join(name).exists() {
            // The QGIS fixture only regenerates where QGIS is installed.
            continue;
        }
        let (_dir, gpkg) = open_fixture(name);
        let mut got: Vec<String> = gpkg
            .validate()
            .unwrap()
            .iter()
            .map(|finding| {
                let debug = format!("{finding:?}");
                debug
                    .split_once(' ')
                    .map_or(debug.clone(), |(head, _)| head.to_owned())
                    .trim_end_matches('{')
                    .trim()
                    .to_owned()
            })
            .collect();
        got.sort();
        let mut want: Vec<String> = want.iter().map(|s| (*s).to_owned()).collect();
        want.sort();
        assert_eq!(got, want, "{name}");
    }
}

#[test]
fn every_finding_renders_as_a_sentence_naming_its_subject() {
    // One of each variant, so a new variant without a Display arm fails to
    // compile here rather than printing its debug form to a user.
    //
    // `SpatialIndexAudit` is `#[non_exhaustive]`, so the audit comes from a
    // real layer rather than a literal: one row inserted through the writer,
    // then the index emptied behind the triggers' back, which is the same
    // divergence `an_out_of_step_spatial_index_is_an_error` builds.
    let (_dir, gpkg) = with_layer();
    let layer = gpkg.layer("roads").unwrap();
    let mut writer = layer.writer().unwrap();
    writer
        .insert(
            None,
            &geo_types::Line::new(
                geo_types::Coord { x: 0.0, y: 0.0 },
                geo_types::Coord { x: 1.0, y: 1.0 },
            ),
            &[geopackage::ValueRef::Text("a")],
        )
        .unwrap();
    writer.commit().unwrap();
    gpkg.connection()
        .execute("DELETE FROM rtree_roads_geom", [])
        .unwrap();
    let audit = layer.audit_spatial_index().unwrap();
    assert_eq!(
        audit.missing, 1,
        "the emptied index should lose its one row"
    );

    let cases: Vec<(Finding, &str)> = vec![
        (
            Finding::LegacyApplicationId {
                version: geopackage::GpkgVersion::V1_0,
                application_id: 0x4750_3130,
            },
            "GP10",
        ),
        (
            Finding::MissingContentsTable {
                table_name: "roads".into(),
            },
            "roads",
        ),
        (
            Finding::TableNameCaseMismatch {
                declared: "Roads".into(),
                actual: "roads".into(),
            },
            "case",
        ),
        (
            Finding::RemovedExtension {
                extension_name: "gpkg_geom_CIRCULARSTRING".into(),
                table_name: Some("roads".into()),
            },
            "roads",
        ),
        (
            Finding::UnrecognisedExtension {
                extension_name: "acme_thing".into(),
                table_name: None,
                scope: geopackage::ExtensionScope::ReadWrite,
            },
            "acme_thing",
        ),
        (
            Finding::SpatialIndexOutOfStep {
                table_name: "roads".into(),
                audit,
            },
            "1 missing",
        ),
        (
            Finding::LegacySpatialIndexTriggers {
                table_name: "roads".into(),
            },
            "pre-1.4",
        ),
        (
            Finding::NoSpatialIndex {
                table_name: "roads".into(),
            },
            "no spatial index",
        ),
        (
            Finding::TilePyramidInconsistent {
                table_name: "basemap".into(),
                detail: "zoom 3 missing".into(),
            },
            "zoom 3 missing",
        ),
        (Finding::DanglingMetadataReference { md_id: 7 }, "7"),
        (
            Finding::MissingMappingTable {
                mapping_table_name: "map".into(),
            },
            "map",
        ),
        (
            Finding::NonConformantRelationName {
                relation_name: "sideways".into(),
            },
            "sideways",
        ),
    ];

    for (finding, expected_fragment) in cases {
        let rendered = finding.to_string();
        assert!(
            rendered.contains(expected_fragment),
            "{finding:?} rendered as {rendered:?}, which does not mention {expected_fragment:?}"
        );
        // A finding's own line carries neither its severity nor its repair:
        // those are separate accessors so a caller arranges them itself.
        assert!(!rendered.contains("Severity"), "{rendered:?}");
        assert!(!rendered.is_empty());
    }
}

#[test]
fn an_optional_table_name_reads_as_a_sentence_either_way() {
    let with = Finding::RemovedExtension {
        extension_name: "gpkg_geom_CIRCULARSTRING".into(),
        table_name: Some("roads".into()),
    };
    let without = Finding::RemovedExtension {
        extension_name: "gpkg_geom_CIRCULARSTRING".into(),
        table_name: None,
    };
    assert!(with.to_string().contains(r#"on "roads""#), "{with:?}");
    assert!(!without.to_string().contains(" on "), "{without:?}");
}

#[test]
fn severity_renders_as_a_lowercase_word() {
    assert_eq!(Severity::Advisory.to_string(), "advisory");
    assert_eq!(Severity::Warning.to_string(), "warning");
    assert_eq!(Severity::Error.to_string(), "error");
}