SPB Git

Repository Public License

Support.swift 13.1 KB 244 lines swift

1// ORIX Convert — Packages/EngineGeo/Tests/EngineGeoTests/Support.swift2// Description: Test support: temporary folders, a conversion runner that collects warnings and runs validation, and3//              fixture builders written independently of the engine's writers (shapefile bytes per the ESRI4//              specification, GeoPackage through raw SQL per the OGC specification, a ZIP assembler).5// Author: Simon-Pierre Boucher — contact@spboucher.ai6import Foundation7import SQLite38import Compression9import Testing10import ConvertCore11@testable import EngineGeo1213final class TempDir: @unchecked Sendable {14    let url: URL15    init() {16        url = FileManager.default.temporaryDirectory.appendingPathComponent("orix-geo-tests-\(UUID().uuidString)", isDirectory: true)17        try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)18    }19    deinit { try? FileManager.default.removeItem(at: url) }20    func file(_ name: String) -> URL { url.appendingPathComponent(name) }21    func write(_ name: String, _ text: String) -> URL { let u = file(name); try? Data(text.utf8).write(to: u); return u }22    func write(_ name: String, _ data: Data) -> URL { let u = file(name); try? data.write(to: u); return u }23    func sub(_ name: String) -> URL {24        let u = url.appendingPathComponent(name, isDirectory: true)25        try? FileManager.default.createDirectory(at: u, withIntermediateDirectories: true)26        return u27    }28}2930final class WarningBox: @unchecked Sendable {31    private let lock = NSLock()32    private var items: [ConversionWarning] = []33    func add(_ w: ConversionWarning) { lock.lock(); items.append(w); lock.unlock() }34    var all: [ConversionWarning] { lock.lock(); defer { lock.unlock() }; return items }35    func first(_ code: String) -> ConversionWarning? { all.first { $0.code == code } }36    var codes: [String] { all.map(\.code) }37}3839struct Converted {40    let output: ConversionOutput41    let warnings: WarningBox42    let checks: [ValidationCheck]43    let layers: [Layer]44    var features: [Feature] { layers.flatMap(\.features) }45}4647/// Converts, validates (every check must pass) and reads the output back.48func convert(_ source: URL, _ from: FormatID, to: FormatID, in temp: TempDir, options: [String: OptionValue] = [:], name: String? = nil) async throws -> Converted {49    let engine = GeoEngine()50    let work = temp.sub("work-\(UUID().uuidString.prefix(8))")51    let box = WarningBox()52    let request = ConversionRequest(source: source, sourceFormat: from, destinationFormat: to, options: ConversionOptions(options), outputBaseName: name)53    #expect(engine.path(for: request) != nil, "path \(from) → \(to) is not declared")54    let context = ConversionContext(workDirectory: work, warn: { box.add($0) })55    let output = try await engine.convert(request, context: context)56    let generic = OutputValidator.validate(output, destinationFormat: to, workDirectory: work)57    let checks = generic + (try await engine.validate(output, request: request, source: nil))58    for c in checks { #expect(c.passed, "\(from)→\(to) check \(c.name): \(c.detail)") }59    return Converted(output: output, warnings: box, checks: checks, layers: try GeoEngine.readBack(output, format: to))60}6162func coordinates(_ g: Geometry?) -> [Coordinate] {63    var out: [Coordinate] = []64    g?.forEachCoordinate { out.append($0) }65    return out66}6768// MARK: - Independent shapefile assembler (ESRI Shapefile Technical Description, July 1998)6970struct ShapefileBytes {71    enum Shape {72        case null73        case point(x: Double, y: Double)74        case pointZ(x: Double, y: Double, z: Double, m: Double?)75        case pointM(x: Double, y: Double, m: Double)76        case multiPoint([(Double, Double)])77        /// PolyLine (3) or Polygon (5): parts of (x, y).78        case poly(type: Int32, parts: [[(Double, Double)]])79        /// PolyLineZ (13) or PolygonZ (15) with Z and optional M arrays.80        case polyZ(type: Int32, parts: [[(Double, Double)]], z: [Double], m: [Double]?)81        case polyM(type: Int32, parts: [[(Double, Double)]], m: [Double])82    }8384    static func le<T: FixedWidthInteger>(_ v: T) -> [UInt8] { withUnsafeBytes(of: v.littleEndian) { Array($0) } }85    static func be<T: FixedWidthInteger>(_ v: T) -> [UInt8] { withUnsafeBytes(of: v.bigEndian) { Array($0) } }86    static func d(_ v: Double) -> [UInt8] { le(v.bitPattern) }8788    static func record(_ s: Shape) -> [UInt8] {89        func box(_ pts: [(Double, Double)]) -> [UInt8] {90            d(pts.map(\.0).min() ?? 0) + d(pts.map(\.1).min() ?? 0) + d(pts.map(\.0).max() ?? 0) + d(pts.map(\.1).max() ?? 0)91        }92        func polyBody(_ type: Int32, _ parts: [[(Double, Double)]]) -> [UInt8] {93            let all = parts.flatMap { $0 }94            var b = le(type) + box(all) + le(Int32(parts.count)) + le(Int32(all.count))95            var start: Int32 = 096            for p in parts { b += le(start); start += Int32(p.count) }97            for p in all { b += d(p.0) + d(p.1) }98            return b99        }100        func range(_ v: [Double]) -> [UInt8] { d(v.min() ?? 0) + d(v.max() ?? 0) + v.flatMap(d) }101        switch s {102        case .null: return le(Int32(0))103        case .point(let x, let y): return le(Int32(1)) + d(x) + d(y)104        case .pointZ(let x, let y, let z, let m): return le(Int32(11)) + d(x) + d(y) + d(z) + (m.map(d) ?? [])105        case .pointM(let x, let y, let m): return le(Int32(21)) + d(x) + d(y) + d(m)106        case .multiPoint(let pts): return le(Int32(8)) + box(pts) + le(Int32(pts.count)) + pts.flatMap { d($0.0) + d($0.1) }107        case .poly(let type, let parts): return polyBody(type, parts)108        case .polyZ(let type, let parts, let z, let m): return polyBody(type, parts) + range(z) + (m.map(range) ?? [])109        case .polyM(let type, let parts, let m): return polyBody(type, parts) + range(m)110        }111    }112113    /// Main file: 100-byte header, then records (big-endian record number and length in 16-bit words).114    static func shp(type: Int32, shapes: [Shape]) -> Data {115        var body: [UInt8] = []116        for (i, s) in shapes.enumerated() {117            let content = record(s)118            body += be(Int32(i + 1)) + be(Int32(content.count / 2)) + content119        }120        var header = be(Int32(9994)) + [UInt8](repeating: 0, count: 20) + be(Int32((100 + body.count) / 2)) + le(Int32(1000)) + le(type)121        header += [UInt8](repeating: 0, count: 64)122        return Data(header + body)123    }124125    struct Column {126        var name: String127        var type: Character128        var length: Int129        var decimals: Int = 0130    }131132    /// dBASE III table. `rows` hold the already formatted field bytes (padded here).133    static func dbf(columns: [Column], rows: [[[UInt8]]], ldid: UInt8 = 0) -> Data {134        let recordSize = 1 + columns.reduce(0) { $0 + $1.length }135        var out: [UInt8] = [0x03, 95, 7, 26] + le(UInt32(rows.count)) + le(UInt16(32 + 32 * columns.count + 1)) + le(UInt16(recordSize))136        out += [UInt8](repeating: 0, count: 17) + [ldid] + [0, 0]137        for c in columns {138            var name = Array(c.name.utf8.prefix(10))139            name += [UInt8](repeating: 0, count: 11 - name.count)140            out += name + [c.type.asciiValue ?? 67] + [0, 0, 0, 0] + [UInt8(c.length), UInt8(c.decimals)] + [UInt8](repeating: 0, count: 14)141        }142        out.append(0x0D)143        for row in rows {144            out.append(0x20)145            for (i, c) in columns.enumerated() {146                let bytes = Array(row[i].prefix(c.length))147                let pad = [UInt8](repeating: 0x20, count: c.length - bytes.count)148                out += (c.type == "N" || c.type == "F") ? pad + bytes : bytes + pad149            }150        }151        out.append(0x1A)152        return Data(out)153    }154}155156// MARK: - Independent ZIP assembler157158enum ZipBytes {159    static func le<T: FixedWidthInteger>(_ v: T) -> [UInt8] { withUnsafeBytes(of: v.littleEndian) { Array($0) } }160161    static func deflate(_ data: Data) -> Data {162        var out = Data(count: data.count + 1024)163        let capacity = out.count164        let n = out.withUnsafeMutableBytes { dst in165            data.withUnsafeBytes { src in166                compression_encode_buffer(dst.bindMemory(to: UInt8.self).baseAddress!, capacity, src.bindMemory(to: UInt8.self).baseAddress!, data.count, nil, COMPRESSION_ZLIB)167            }168        }169        out.count = n170        return out171    }172173    /// `entries`: name, content, deflate? `declaredSize` lets a test lie about the uncompressed size.174    static func archive(_ entries: [(String, Data, Bool)], declaredSize: UInt32? = nil) -> Data {175        var out: [UInt8] = []176        var central: [UInt8] = []177        for (name, content, compress) in entries {178            let payload = compress ? deflate(content) : content179            let method: UInt16 = compress ? 8 : 0180            let offset = UInt32(out.count)181            let usize = declaredSize ?? UInt32(content.count)182            let common = le(UInt16(20)) + le(UInt16(0)) + le(method) + le(UInt16(0)) + le(UInt16(0)) + le(UInt32(0)) + le(UInt32(payload.count)) + le(usize) + le(UInt16(name.utf8.count)) + le(UInt16(0))183            out += le(UInt32(0x0403_4B50)) + common + Array(name.utf8) + Array(payload)184            central += le(UInt32(0x0201_4B50)) + le(UInt16(20)) + common + le(UInt16(0)) + le(UInt16(0)) + le(UInt16(0)) + le(UInt32(0)) + le(offset) + Array(name.utf8)185        }186        let centralOffset = UInt32(out.count)187        out += central188        out += le(UInt32(0x0605_4B50)) + le(UInt16(0)) + le(UInt16(0)) + le(UInt16(entries.count)) + le(UInt16(entries.count)) + le(UInt32(central.count)) + le(centralOffset) + le(UInt16(0))189        return Data(out)190    }191}192193// MARK: - GeoPackage through raw SQL (OGC 12-128r15)194195enum RawGeoPackage {196    static func be(_ v: Double) -> [UInt8] { withUnsafeBytes(of: v.bitPattern.bigEndian) { Array($0) } }197    static func le(_ v: Double) -> [UInt8] { withUnsafeBytes(of: v.bitPattern.littleEndian) { Array($0) } }198    static func be32(_ v: UInt32) -> [UInt8] { withUnsafeBytes(of: v.bigEndian) { Array($0) } }199    static func le32(_ v: UInt32) -> [UInt8] { withUnsafeBytes(of: v.littleEndian) { Array($0) } }200201    /// GeoPackageBinary header. `envelope`: 0 none, 1 XY, 2 XYZ, 3 XYM, 4 XYZM (values are not checked by readers).202    static func header(srs: Int32, envelope: UInt8 = 0, bigEndian: Bool = false, empty: Bool = false) -> [UInt8] {203        let flags: UInt8 = (bigEndian ? 0 : 1) | (envelope << 1) | (empty ? 0x10 : 0)204        let id = bigEndian ? withUnsafeBytes(of: srs.bigEndian) { Array($0) } : withUnsafeBytes(of: srs.littleEndian) { Array($0) }205        let doubles = [0, 4, 6, 6, 8][Int(envelope)]206        return [0x47, 0x50, 0x00, flags] + id + [UInt8](repeating: 0, count: 8 * doubles)207    }208209    static func exec(_ db: OpaquePointer?, _ sql: String) {210        var error: UnsafeMutablePointer<CChar>?211        if sqlite3_exec(db, sql, nil, nil, &error) != SQLITE_OK {212            Issue.record("SQL failed: \(error.map { String(cString: $0) } ?? "?") — \(sql.prefix(120))")213        }214    }215216    static let coreTables = """217    PRAGMA application_id = 1196444487; PRAGMA user_version = 10300;218    CREATE TABLE gpkg_spatial_ref_sys (srs_name TEXT NOT NULL, srs_id INTEGER PRIMARY KEY, organization TEXT NOT NULL, organization_coordsys_id INTEGER NOT NULL, definition TEXT NOT NULL, description TEXT);219    CREATE TABLE gpkg_contents (table_name TEXT NOT NULL PRIMARY KEY, data_type TEXT NOT NULL, identifier TEXT UNIQUE, description TEXT DEFAULT '', last_change DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')), min_x DOUBLE, min_y DOUBLE, max_x DOUBLE, max_y DOUBLE, srs_id INTEGER);220    CREATE TABLE gpkg_geometry_columns (table_name TEXT NOT NULL, column_name TEXT NOT NULL, geometry_type_name TEXT NOT NULL, srs_id INTEGER NOT NULL, z TINYINT NOT NULL, m TINYINT NOT NULL, CONSTRAINT pk_geom_cols PRIMARY KEY (table_name, column_name));221    INSERT INTO gpkg_spatial_ref_sys VALUES ('Undefined Cartesian SRS', -1, 'NONE', -1, 'undefined', NULL);222    INSERT INTO gpkg_spatial_ref_sys VALUES ('Undefined geographic SRS', 0, 'NONE', 0, 'undefined', NULL);223    INSERT INTO gpkg_spatial_ref_sys VALUES ('WGS 84 geodetic', 4326, 'EPSG', 4326, 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]]', NULL);224    """225226    /// Creates a database, runs `sql`, then inserts `blobs` with `insert` (one `?` for the geometry blob per row).227    static func create(_ url: URL, sql: String, inserts: [(String, [UInt8]?)] = []) {228        var db: OpaquePointer?229        guard sqlite3_open(url.path, &db) == SQLITE_OK else { Issue.record("cannot create \(url.path)"); return }230        defer { sqlite3_close(db) }231        exec(db, coreTables)232        exec(db, sql)233        for (statement, blob) in inserts {234            var stmt: OpaquePointer?235            guard sqlite3_prepare_v2(db, statement, -1, &stmt, nil) == SQLITE_OK else { Issue.record("prepare failed: \(statement)"); continue }236            if let blob {237                _ = blob.withUnsafeBytes { sqlite3_bind_blob(stmt, 1, $0.baseAddress, Int32(blob.count), unsafeBitCast(-1, to: sqlite3_destructor_type.self)) }238            }239            if sqlite3_step(stmt) != SQLITE_DONE { Issue.record("insert failed: \(String(cString: sqlite3_errmsg(db)))") }240            sqlite3_finalize(stmt)241        }242    }243}244
Files main
Menu

Theme