SPB Git

Repository Public License

FormatTests.swift 25.0 KB 325 lines swift

1// ORIX Convert — Packages/EngineGeo/Tests/EngineGeoTests/FormatTests.swift2// Description: Readers and writers format by format, against fixtures built independently of the engine's writers:3//              byte-assembled shapefiles of every shape type, a GeoPackage created with raw SQL, real-shaped KML and4//              GPX, KMZ and zipped shapefiles, GeoJSON variants, CSV detection rules.5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import SQLite38import Testing9import ConvertCore10@testable import EngineGeo1112func read(_ url: URL, _ format: FormatID, _ options: GeoReadOptions = GeoReadOptions()) throws -> GeoDataset {13    try GeoReader.read(url, format: format, options: options, check: {})14}1516@Suite struct ShapefileTests {17    static func write(_ dir: URL, _ base: String, type: Int32, _ shapes: [ShapefileBytes.Shape]) -> URL {18        let u = dir.appendingPathComponent("\(base).shp")19        try? ShapefileBytes.shp(type: type, shapes: shapes).write(to: u)20        return u21    }2223    @Test func readsEveryShapeTypeFromIndependentBytes() throws {24        let temp = TempDir()25        let dir = temp.sub("types")26        // Point, PointM, PointZ with and without the optional measure.27        var g = try read(Self.write(dir, "p", type: 1, [.point(x: 1, y: 2), .null]), .shapefile).layers[0].features.map(\.geometry)28        #expect(g == [.point(Coordinate(1, 2)), nil])29        g = try read(Self.write(dir, "pm", type: 21, [.pointM(x: 1, y: 2, m: 7), .pointM(x: 3, y: 4, m: -1e39)]), .shapefile).layers[0].features.map(\.geometry)30        #expect(g == [.point(Coordinate(1, 2, m: 7)), .point(Coordinate(3, 4))])31        g = try read(Self.write(dir, "pz", type: 11, [.pointZ(x: 1, y: 2, z: 3, m: 4), .pointZ(x: 5, y: 6, z: 7, m: nil)]), .shapefile).layers[0].features.map(\.geometry)32        #expect(g == [.point(Coordinate(1, 2, z: 3, m: 4)), .point(Coordinate(5, 6, z: 7))])33        // MultiPoint.34        g = try read(Self.write(dir, "mp", type: 8, [.multiPoint([(1, 2), (3, 4)])]), .shapefile).layers[0].features.map(\.geometry)35        #expect(g == [.multiPoint([Coordinate(1, 2), Coordinate(3, 4)])])36        // PolyLine: one part → LineString, two parts → MultiLineString.37        g = try read(Self.write(dir, "l", type: 3, [.poly(type: 3, parts: [[(0, 0), (1, 1)]]), .poly(type: 3, parts: [[(0, 0), (1, 1)], [(5, 5), (6, 6), (7, 5)]])]), .shapefile).layers[0].features.map(\.geometry)38        #expect(g == [.lineString([Coordinate(0, 0), Coordinate(1, 1)]), .multiLineString([[Coordinate(0, 0), Coordinate(1, 1)], [Coordinate(5, 5), Coordinate(6, 6), Coordinate(7, 5)]])])39        // PolyLineZ with measures, PolyLineM.40        g = try read(Self.write(dir, "lz", type: 13, [.polyZ(type: 13, parts: [[(0, 0), (1, 1)]], z: [10, 11], m: [100, 101])]), .shapefile).layers[0].features.map(\.geometry)41        #expect(g == [.lineString([Coordinate(0, 0, z: 10, m: 100), Coordinate(1, 1, z: 11, m: 101)])])42        g = try read(Self.write(dir, "lz2", type: 13, [.polyZ(type: 13, parts: [[(0, 0), (1, 1)]], z: [10, 11], m: nil)]), .shapefile).layers[0].features.map(\.geometry)43        #expect(g == [.lineString([Coordinate(0, 0, z: 10), Coordinate(1, 1, z: 11)])])44        g = try read(Self.write(dir, "lm", type: 23, [.polyM(type: 23, parts: [[(0, 0), (1, 1)]], m: [5, 6])]), .shapefile).layers[0].features.map(\.geometry)45        #expect(g == [.lineString([Coordinate(0, 0, m: 5), Coordinate(1, 1, m: 6)])])46    }4748    // Clockwise outer rings and counter-clockwise holes, as the specification requires.49    static let outerA: [(Double, Double)] = [(0, 0), (0, 10), (10, 10), (10, 0), (0, 0)]50    static let holeA: [(Double, Double)] = [(2, 2), (4, 2), (4, 4), (2, 4), (2, 2)]51    static let outerB: [(Double, Double)] = [(20, 0), (20, 10), (30, 10), (30, 0), (20, 0)]52    static let holeB: [(Double, Double)] = [(22, 2), (24, 2), (24, 4), (22, 4), (22, 2)]53    static func ring(_ r: [(Double, Double)]) -> [Coordinate] { r.map { Coordinate($0.0, $0.1) } }5455    @Test func assignsHolesToTheirOuterRings() throws {56        let temp = TempDir()57        let dir = temp.sub("poly")58        // Rings deliberately interleaved: outer A, outer B, hole of B, hole of A.59        let url = Self.write(dir, "poly", type: 5, [60            .poly(type: 5, parts: [Self.outerA, Self.holeA]),61            .poly(type: 5, parts: [Self.outerA, Self.outerB, Self.holeB, Self.holeA]),62            .poly(type: 5, parts: [Self.holeA]),63        ])64        let d = try read(url, .shapefile)65        let g = d.layers[0].features.map(\.geometry)66        #expect(g[0] == .polygon([Self.ring(Self.outerA), Self.ring(Self.holeA)]))67        #expect(g[1] == .multiPolygon([[Self.ring(Self.outerA), Self.ring(Self.holeA)], [Self.ring(Self.outerB), Self.ring(Self.holeB)]]))68        // A polygon made only of a counter-clockwise ring is kept and reported.69        #expect(g[2] == .polygon([Self.ring(Self.holeA)]))70        #expect(d.warnings.contains { $0.code == "shp-ring-orientation" && $0.message.hasPrefix("1 polygon(s)") })71        #expect(d.warnings.contains { $0.code == "shp-no-dbf" })72        // PolygonZ.73        let z = try read(Self.write(dir, "polyz", type: 15, [.polyZ(type: 15, parts: [Self.outerA], z: [1, 2, 3, 4, 1], m: nil)]), .shapefile).layers[0].features[0].geometry74        #expect(coordinates(z).map(\.z) == [1, 2, 3, 4, 1])75    }7677    @Test func writerProducesSpecConformingBytes() async throws {78        // GeoJSON polygons (counter-clockwise exterior per RFC 7946) → shapefile; the bytes are then parsed here,79        // independently of the engine's reader.80        let temp = TempDir()81        let source = temp.write("areas.geojson", """82        {"type":"FeatureCollection","features":[83        {"type":"Feature","properties":{"id":1},"geometry":{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[2,2],[2,4],[4,4],[4,2],[2,2]]]}},84        {"type":"Feature","properties":{"id":2},"geometry":{"type":"MultiPolygon","coordinates":[[[[20,0],[30,0],[30,10],[20,10],[20,0]]],[[[40,0],[50,0],[50,10],[40,10],[40,0]]]]}},85        {"type":"Feature","properties":{"id":3},"geometry":null}86        ]}87        """)88        let r = try await convert(source, .geojson, to: .shapefile, in: temp)89        let folder = r.output.files[0]90        let shp = [UInt8](try Data(contentsOf: folder.appendingPathComponent("areas.shp")))91        let shx = [UInt8](try Data(contentsOf: folder.appendingPathComponent("areas.shx")))92        func be(_ b: [UInt8], _ o: Int) -> Int { Int(b[o]) << 24 | Int(b[o + 1]) << 16 | Int(b[o + 2]) << 8 | Int(b[o + 3]) }93        func le(_ b: [UInt8], _ o: Int) -> Int { Int(b[o]) | Int(b[o + 1]) << 8 | Int(b[o + 2]) << 16 | Int(b[o + 3]) << 24 }94        func dbl(_ b: [UInt8], _ o: Int) -> Double { Double(bitPattern: (0..<8).reduce(UInt64(0)) { $0 | UInt64(b[o + $1]) << (8 * UInt64($1)) }) }95        #expect(be(shp, 0) == 9994 && le(shp, 28) == 1000 && le(shp, 32) == 5)96        #expect(be(shp, 24) * 2 == shp.count && be(shx, 24) * 2 == shx.count)97        #expect(shx.count == 100 + 3 * 8)98        #expect([dbl(shp, 36), dbl(shp, 44), dbl(shp, 52), dbl(shp, 60)] == [0, 0, 50, 10])99        // Record 1 through the index: offset and length in 16-bit words.100        let offset = be(shx, 100) * 2101        #expect(offset == 100 && be(shp, offset) == 1 && be(shp, offset + 4) == be(shx, 104))102        let c = offset + 8103        #expect(le(shp, c) == 5 && le(shp, c + 36) == 2 && le(shp, c + 40) == 10)104        #expect(le(shp, c + 44) == 0 && le(shp, c + 48) == 5)105        func ring(_ start: Int, _ n: Int) -> [(Double, Double)] { (0..<n).map { (dbl(shp, start + 16 * $0), dbl(shp, start + 16 * $0 + 8)) } }106        func area2(_ r: [(Double, Double)]) -> Double { (0..<(r.count - 1)).reduce(0.0) { $0 + r[$1].0 * r[$1 + 1].1 - r[$1 + 1].0 * r[$1].1 } }107        let outer = ring(c + 52, 5), hole = ring(c + 52 + 80, 5)108        #expect(area2(outer) < 0, "the outer ring must be clockwise")109        #expect(area2(hole) > 0, "the hole must be counter-clockwise")110        // Record 3 is a null shape.111        let third = be(shx, 100 + 16) * 2112        #expect(be(shp, third) == 3 && be(shp, third + 4) == 2 && le(shp, third + 8) == 0)113        #expect(r.warnings.first("shp-null-shapes")?.message.hasPrefix("1 feature(s)") == true)114        // dBASE header: version 3, 3 records, one N field named id.115        let dbf = [UInt8](try Data(contentsOf: folder.appendingPathComponent("areas.dbf")))116        #expect(dbf[0] == 3 && le(dbf, 4) == 3 && dbf[32 + 11] == UInt8(ascii: "N") && dbf.last == 0x1A)117        #expect(String(decoding: dbf[32..<34], as: UTF8.self) == "id")118        #expect(try String(contentsOf: folder.appendingPathComponent("areas.cpg"), encoding: .utf8) == "UTF-8")119        #expect(try String(contentsOf: folder.appendingPathComponent("areas.prj"), encoding: .utf8) == Cities.wgs84ESRI)120        #expect(r.features.map(\.geometry?.typeName) == ["Polygon", "MultiPolygon", nil])121    }122123    @Test func readsAttributeTypesAndCodePages() throws {124        let temp = TempDir()125        let dir = temp.sub("dbf")126        let shp = Self.write(dir, "t", type: 1, [.point(x: 0, y: 0), .point(x: 1, y: 1)])127        let columns: [ShapefileBytes.Column] = [.init(name: "TXT", type: "C", length: 8), .init(name: "INT", type: "N", length: 6), .init(name: "DEC", type: "N", length: 8, decimals: 3),128                                                .init(name: "FLT", type: "F", length: 12, decimals: 4), .init(name: "OK", type: "L", length: 1), .init(name: "DAY", type: "D", length: 8), .init(name: "MEMO", type: "M", length: 10)]129        // "Évêché" in Windows-1252.130        let latin: [UInt8] = [0xC9, 0x76, 0xEA, 0x63, 0x68, 0xE9]131        let rows: [[[UInt8]]] = [132            [latin, Array("42".utf8), Array("-1.250".utf8), Array("1.5e3".utf8), [UInt8(ascii: "Y")], Array("20240229".utf8), Array("7".utf8)],133            [[], Array("******".utf8), [], [], [UInt8(ascii: "?")], [], []],134        ]135        try ShapefileBytes.dbf(columns: columns, rows: rows, ldid: 0x57).write(to: dir.appendingPathComponent("t.dbf"))136        var d = try read(shp, .shapefile)137        var f = d.layers[0].features138        #expect(f[0].attributes.map(\.name) == ["TXT", "INT", "DEC", "FLT", "OK", "DAY", "MEMO"])139        #expect(f[0]["TXT"] == .string("Évêché"), "language driver id 0x57 = Windows-1252")140        #expect(f[0]["INT"] == .integer(42) && f[0]["DEC"] == .real(-1.25) && f[0]["FLT"] == .real(1500))141        #expect(f[0]["OK"] == .bool(true) && f[0]["DAY"] == .date("2024-02-29") && f[0]["MEMO"] == .string("7"))142        #expect(f[1].attributes.allSatisfy { $0.value == .null })143        #expect(d.warnings.contains { $0.code == "dbf-field-types" && $0.message.contains("M") })144        // No language driver id and no .cpg: guessed and reported.145        try ShapefileBytes.dbf(columns: columns, rows: rows, ldid: 0).write(to: dir.appendingPathComponent("t.dbf"))146        d = try read(shp, .shapefile)147        #expect(d.layers[0].features[0]["TXT"] == .string("Évêché"))148        #expect(d.warnings.contains { $0.code == "dbf-encoding-assumed" && $0.message.contains("Windows-1252") })149        // A .cpg file wins over the language driver id; found case-insensitively.150        let utf: [UInt8] = Array("Évêché".utf8.prefix(8))151        try ShapefileBytes.dbf(columns: [.init(name: "TXT", type: "C", length: 9)], rows: [[Array("Évêché".utf8)], [utf]], ldid: 0x57).write(to: dir.appendingPathComponent("t.dbf"))152        try Data("UTF-8\n".utf8).write(to: dir.appendingPathComponent("T.CPG"))153        f = try read(shp, .shapefile).layers[0].features154        #expect(f[0]["TXT"] == .string("Évêché"))155        // The explicit option wins over everything.156        var options = GeoReadOptions()157        options.shapefileEncoding = "850"158        try ShapefileBytes.dbf(columns: [.init(name: "TXT", type: "C", length: 3)], rows: [[[0x82, 0x74, 0x82]], [[]]]).write(to: dir.appendingPathComponent("t.dbf"))159        #expect(try read(shp, .shapefile, options).layers[0].features[0]["TXT"] == .string("été"))160        options.shapefileEncoding = "klingon"161        #expect(throws: ConversionError.self) { _ = try read(shp, .shapefile, options) }162    }163164    @Test func readsZippedShapefiles() async throws {165        let temp = TempDir()166        let dir = temp.sub("z")167        _ = Cities.writeShapefile(in: dir)168        _ = Cities.writeShapefile(in: dir, base: "second")169        var entries: [(String, Data, Bool)] = []170        for name in try FileManager.default.contentsOfDirectory(atPath: dir.path).sorted() {171            entries.append(("data/" + name.uppercased().replacingOccurrences(of: ".SHP", with: ".shp"), try Data(contentsOf: dir.appendingPathComponent(name)), name.hasSuffix("dbf")))172        }173        let zip = temp.write("cities-zipped.shp", ZipBytes.archive(entries))174        let d = try read(zip, .shapefile)175        #expect(d.layers.map(\.name) == ["CITIES", "SECOND"])176        #expect(d.layers[0].features.count == 3 && d.layers[0].crs.epsg == 4326)177        #expect(d.layers[1].features[0]["name"] == .string("Montréal"))178        // Two layers → two GeoJSON files.179        let r = try await convert(zip, .shapefile, to: .geojson, in: temp)180        #expect(r.output.files.map(\.lastPathComponent) == ["cities-zipped-CITIES.geojson", "cities-zipped-SECOND.geojson"])181        let one = try await convert(zip, .shapefile, to: .geojson, in: temp, options: ["layer": .string("SECOND")])182        #expect(one.output.files.count == 1 && one.features.count == 3)183    }184185    @Test func writesZAndMVariants() async throws {186        let temp = TempDir()187        let csv = temp.write("zm.csv", "id,wkt\n1,\"LINESTRING ZM (0 0 5 100, 1 1 6 101)\"\n2,\"LINESTRING ZM (2 2 7 102, 3 3 8 103)\"\n")188        let r = try await convert(csv, .csv, to: .shapefile, in: temp)189        let shp = [UInt8](try Data(contentsOf: r.output.files[0].appendingPathComponent("zm.shp")))190        #expect(shp[32] == 13, "PolyLineZ")191        #expect(r.features[0].geometry == .lineString([Coordinate(0, 0, z: 5, m: 100), Coordinate(1, 1, z: 6, m: 101)]))192        let m = temp.write("m.csv", "id,wkt\n1,\"MULTIPOINT M ((0 0 9), (1 1 8))\"\n")193        let rm = try await convert(m, .csv, to: .shapefile, in: temp)194        #expect([UInt8](try Data(contentsOf: rm.output.files[0].appendingPathComponent("m.shp")))[32] == 28, "MultiPointM")195        #expect(rm.features[0].geometry == .multiPoint([Coordinate(0, 0, m: 9), Coordinate(1, 1, m: 8)]))196        // And back out to WKT with everything intact.197        let back = try await convert(r.output.files[0].appendingPathComponent("zm.shp"), .shapefile, to: .csv, in: temp)198        #expect(try String(contentsOf: back.output.files[0], encoding: .utf8) == "WKT,id\r\n\"LINESTRING ZM (0 0 5 100,1 1 6 101)\",1\r\n\"LINESTRING ZM (2 2 7 102,3 3 8 103)\",2\r\n")199    }200}201202@Suite struct GeoPackageTests {203    @Test func readsRawSQLGeoPackage() throws {204        let temp = TempDir()205        let url = temp.file("raw.gpkg")206        func xdr(_ type: UInt32, _ doubles: [Double]) -> [UInt8] { [0] + RawGeoPackage.be32(type) + doubles.flatMap(RawGeoPackage.be) }207        // Big-endian LineString ZM (3002) with 2 points, behind a big-endian header with an XYZM envelope.208        let line: [UInt8] = RawGeoPackage.header(srs: 32618, envelope: 4, bigEndian: true) + [0] + RawGeoPackage.be32(3002) + RawGeoPackage.be32(2) + [1.0, 2, 3, 4, 5, 6, 7, 8].flatMap(RawGeoPackage.be)209        // Little-endian Polygon with a hole, XY envelope.210        var polygon: [UInt8] = RawGeoPackage.header(srs: 32618, envelope: 1) + [1] + RawGeoPackage.le32(3) + RawGeoPackage.le32(2)211        polygon += RawGeoPackage.le32(4) + [0.0, 0, 4, 0, 0, 4, 0, 0].flatMap(RawGeoPackage.le) + RawGeoPackage.le32(4) + [1.0, 1, 1, 2, 2, 1, 1, 1].flatMap(RawGeoPackage.le)212        // MultiPoint Z (1004) of two points, and a GeometryCollection (7) of a point and a line.213        var multi: [UInt8] = RawGeoPackage.header(srs: 32618) + [1] + RawGeoPackage.le32(1004) + RawGeoPackage.le32(2)214        multi += [1] + RawGeoPackage.le32(1001) + [1.0, 2, 3].flatMap(RawGeoPackage.le) + [1] + RawGeoPackage.le32(1001) + [4.0, 5, 6].flatMap(RawGeoPackage.le)215        var collection: [UInt8] = RawGeoPackage.header(srs: 32618) + [1] + RawGeoPackage.le32(7) + RawGeoPackage.le32(2)216        collection += xdr(1, [9, 8]) + [1] + RawGeoPackage.le32(2) + RawGeoPackage.le32(2) + [0.0, 0, 1, 1].flatMap(RawGeoPackage.le)217        let emptyPoint: [UInt8] = RawGeoPackage.header(srs: 32618, empty: true) + [1] + RawGeoPackage.le32(1) + RawGeoPackage.le(.nan) + RawGeoPackage.le(.nan)218        RawGeoPackage.create(url, sql: """219        INSERT INTO gpkg_spatial_ref_sys VALUES ('WGS 84 / UTM zone 18N', 32618, 'EPSG', 32618, 'PROJCS["whatever"]', NULL);220        CREATE TABLE "mixed things" (id INTEGER PRIMARY KEY, shape GEOMETRY, label TEXT, n INTEGER, v REAL, ok BOOLEAN, seen DATETIME, raw BLOB);221        INSERT INTO gpkg_contents (table_name, data_type, identifier, srs_id) VALUES ('mixed things', 'features', 'Mixed', 32618);222        INSERT INTO gpkg_geometry_columns VALUES ('mixed things', 'shape', 'GEOMETRY', 32618, 2, 2);223        CREATE TABLE notes (id INTEGER PRIMARY KEY, text TEXT);224        INSERT INTO notes (text) VALUES ('plain row');225        INSERT INTO gpkg_contents (table_name, data_type, identifier) VALUES ('notes', 'attributes', 'notes');226        CREATE TABLE imagery (id INTEGER PRIMARY KEY, zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_data BLOB);227        INSERT INTO gpkg_contents (table_name, data_type, identifier, srs_id) VALUES ('imagery', 'tiles', 'imagery', 4326);228        """, inserts: [229            ("INSERT INTO \"mixed things\" (shape, label, n, v, ok, seen, raw) VALUES (?, 'line', 1, 1.5, 1, '2024-06-01T10:00:00Z', x'00ff')", line),230            ("INSERT INTO \"mixed things\" (shape, label, n, v, ok) VALUES (?, 'polygon', 2, 2.5, 0)", polygon),231            ("INSERT INTO \"mixed things\" (shape, label) VALUES (?, 'multi')", multi),232            ("INSERT INTO \"mixed things\" (shape, label) VALUES (?, 'collection')", collection),233            ("INSERT INTO \"mixed things\" (shape, label) VALUES (?, 'empty')", emptyPoint),234            ("INSERT INTO \"mixed things\" (label) VALUES ('no geometry')", nil),235        ])236        let d = try read(url, .geopackage)237        #expect(d.layers.map(\.name) == ["mixed things", "notes"])238        let layer = d.layers[0]239        #expect(layer.crs.epsg == 32618 && layer.crs.definition != nil)240        let g = layer.features.map(\.geometry)241        #expect(g[0] == .lineString([Coordinate(1, 2, z: 3, m: 4), Coordinate(5, 6, z: 7, m: 8)]))242        #expect(g[1] == .polygon([[Coordinate(0, 0), Coordinate(4, 0), Coordinate(0, 4), Coordinate(0, 0)], [Coordinate(1, 1), Coordinate(1, 2), Coordinate(2, 1), Coordinate(1, 1)]]))243        #expect(g[2] == .multiPoint([Coordinate(1, 2, z: 3), Coordinate(4, 5, z: 6)]))244        #expect(g[3] == .collection([.point(Coordinate(9, 8)), .lineString([Coordinate(0, 0), Coordinate(1, 1)])]))245        #expect(g[4] == nil && g[5] == nil)246        #expect(layer.features[0].attributes == [Attribute("label", .string("line")), Attribute("n", .integer(1)), Attribute("v", .real(1.5)), Attribute("ok", .bool(true)), Attribute("seen", .dateTime("2024-06-01T10:00:00Z"))])247        #expect(layer.features[1]["ok"] == .bool(false) && layer.features[1]["seen"] == .null)248        #expect(d.layers[1].features.count == 1 && d.layers[1].features[0]["text"] == .string("plain row") && d.layers[1].features[0].geometry == nil)249        #expect(d.warnings.contains { $0.code == "gpkg-non-vector" && $0.message.contains("1 table(s) of type \"tiles\"") })250        #expect(d.warnings.contains { $0.code == "gpkg-blob-attributes" && $0.message.hasPrefix("1 binary") })251        // Layer selection by table name; an unknown name lists the choices.252        var options = GeoReadOptions()253        options.layer = "notes"254        #expect(try read(url, .geopackage, options).layers.map(\.name) == ["notes"])255        options.layer = "nope"256        #expect(throws: ConversionError.invalidOption("The GeoPackage has no layer named \"nope\". Layers: mixed things, notes.")) { _ = try read(url, .geopackage, options) }257        // Preview lists the layers.258        #expect(try GeoPreview.preview(url: url, format: .geopackage, limit: 1).layerNames == ["mixed things", "notes"])259    }260261    @Test func reportsTilesOnlyPackages() throws {262        let temp = TempDir()263        let url = temp.file("tiles.gpkg")264        RawGeoPackage.create(url, sql: """265        CREATE TABLE imagery (id INTEGER PRIMARY KEY, zoom_level INTEGER, tile_column INTEGER, tile_row INTEGER, tile_data BLOB);266        INSERT INTO gpkg_contents (table_name, data_type, identifier, srs_id) VALUES ('imagery', 'tiles', 'imagery', 4326);267        """)268        #expect(throws: ConversionError.malformedInput("The GeoPackage holds no vector layer (only tiles content, which is not supported).")) { _ = try read(url, .geopackage) }269    }270271    @Test func writesAConformingPackage() async throws {272        let temp = TempDir()273        let source = temp.write("My places.geojson", """274        {"type":"FeatureCollection","features":[275        {"type":"Feature","properties":{"fid":"a","Name":"one","name":"dup","when":"2024-06-01","at":"2024-06-01T10:00:00Z","tags":["x",{"y":1}],"n":1},"geometry":{"type":"Point","coordinates":[1,2,3]}},276        {"type":"Feature","properties":{"fid":"b","Name":"two","n":2.5},"geometry":{"type":"LineString","coordinates":[[0,0],[5,5]]}},277        {"type":"Feature","properties":{"fid":"c"},"geometry":null}278        ]}279        """)280        let r = try await convert(source, .geojson, to: .geopackage, in: temp)281        let url = r.output.files[0]282        // Inspect the file with plain SQLite, independently of the engine's reader.283        var db: OpaquePointer?284        #expect(sqlite3_open_v2(url.path, &db, SQLITE_OPEN_READONLY, nil) == SQLITE_OK)285        defer { sqlite3_close(db) }286        func rows(_ sql: String) -> [[String]] {287            var stmt: OpaquePointer?288            guard sqlite3_prepare_v2(db, sql, -1, &stmt, nil) == SQLITE_OK else { Issue.record("\(sql): \(String(cString: sqlite3_errmsg(db)))"); return [] }289            defer { sqlite3_finalize(stmt) }290            var out: [[String]] = []291            while sqlite3_step(stmt) == SQLITE_ROW {292                out.append((0..<sqlite3_column_count(stmt)).map { sqlite3_column_text(stmt, $0).map { String(cString: $0) } ?? "NULL" })293            }294            return out295        }296        #expect(rows("PRAGMA application_id") == [["1196444487"]])297        #expect(rows("PRAGMA user_version") == [["10200"]])298        #expect(rows("PRAGMA integrity_check") == [["ok"]])299        #expect(rows("PRAGMA foreign_key_check").isEmpty)300        #expect(rows("SELECT srs_id, organization, organization_coordsys_id FROM gpkg_spatial_ref_sys ORDER BY srs_id") == [["-1", "NONE", "-1"], ["0", "NONE", "0"], ["4326", "EPSG", "4326"]])301        #expect(rows("SELECT table_name, data_type, identifier, min_x, min_y, max_x, max_y, srs_id FROM gpkg_contents") == [["My_places", "features", "My_places", "0.0", "0.0", "5.0", "5.0", "4326"]])302        #expect(rows("SELECT table_name, column_name, geometry_type_name, srs_id, z, m FROM gpkg_geometry_columns") == [["My_places", "geom", "GEOMETRY", "4326", "2", "0"]])303        #expect(rows("SELECT name, type FROM pragma_table_info('My_places')") == [["fid", "INTEGER"], ["geom", "GEOMETRY"], ["fid_2", "TEXT"], ["Name", "TEXT"], ["name_2", "TEXT"], ["when", "TEXT"], ["at", "TEXT"], ["tags", "TEXT"], ["n", "DOUBLE"]])304        #expect(rows("SELECT extension_name, scope FROM gpkg_extensions") == [["gpkg_rtree_index", "write-only"]])305        #expect(rows("SELECT id, minx, maxx, miny, maxy FROM rtree_My_places_geom ORDER BY id") == [["1", "1.0", "1.0", "2.0", "2.0"], ["2", "0.0", "5.0", "0.0", "5.0"]])306        #expect(rows("SELECT COUNT(*) FROM sqlite_master WHERE type = 'trigger'") == [["6"]])307        // Geometry blob of the point: header "GP", version 0, flags 0x01 (little-endian, no envelope), srs 4326, then WKB Point Z.308        #expect(rows("SELECT hex(geom) FROM My_places WHERE fid = 1") == [["47500001E6100000" + "01E9030000" + "000000000000F03F" + "0000000000000040" + "0000000000000840"]])309        // Line: envelope code 1 (flags 0x03) with minx, maxx, miny, maxy.310        #expect(rows("SELECT substr(hex(geom), 1, 16), length(geom) FROM My_places WHERE fid = 2") == [["47500003E6100000", "\(8 + 32 + 9 + 32)"]])311        #expect(rows("SELECT geom IS NULL, tags FROM My_places WHERE fid = 3") == [["1", "NULL"]])312        #expect(rows("SELECT tags FROM My_places WHERE fid = 1") == [["[\"x\",{\"y\":1}]"]])313        #expect(r.warnings.first("gpkg-columns-renamed")?.message.contains("fid → fid_2; name → name_2") == true)314        #expect(r.warnings.first("gpkg-tables-renamed") != nil)315        #expect(r.output.facts["geo.spatial-index"] == "rtree")316        // Without the index: no extension table rows, no rtree.317        let plain = try await convert(source, .geojson, to: .geopackage, in: temp, options: ["gpkg.spatial-index": .bool(false)])318        #expect(plain.output.facts["geo.spatial-index"] == "none")319        #expect(FileTypeDetector.detect(plain.output.files[0]).format == .geopackage)320        // GeoPackage has no nested type: the JSON text stays text on the way back.321        let back = try await convert(url, .geopackage, to: .geojson, in: temp)322        #expect(try String(contentsOf: back.output.files[0], encoding: .utf8).contains("\"tags\":\"[\\\"x\\\",{\\\"y\\\":1}]\"") )323    }324}325
Files main
Menu

Theme