SPB Git

Repository Public License

TextFormatTests.swift 32.5 KB 416 lines swift

1// ORIX Convert — Packages/EngineGeo/Tests/EngineGeoTests/TextFormatTests.swift2// Description: KML / KMZ, GPX, GeoJSON and CSV readers and writers against real-shaped documents, and the exact3//              counts of what each destination cannot carry.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineGeo910@Suite struct KMLTests {11    @Test func readsFoldersStylesAndExtendedData() throws {12        let temp = TempDir()13        let d = try read(temp.write("trip.kml", Samples.kml), .kml)14        #expect(d.layers.map(\.name) == ["Sites", "Sites/Parks", "Routes"])15        #expect(d.layers.allSatisfy { $0.crs.epsg == 4326 })16        let sites = d.layers[0].features17        #expect(sites[0].attributes == [18            Attribute("name", .string("Old Port")), Attribute("description", .string("<b>Vieux-Port</b> &amp; quays")),19            Attribute("visitors", .integer(6_000_000)), Attribute("rating", .real(4.5)), Attribute("open", .bool(true)), Attribute("phone", .string("514-555-0100")),20        ])21        #expect(sites[0].geometry == .point(Coordinate(-73.554, 45.5075, z: 12)))22        #expect(sites[1].attributes == [Attribute("name", .string("Summit")), Attribute("timestamp", .dateTime("2024-06-01T10:00:00Z"))])23        #expect(sites[1].geometry == .point(Coordinate(-73.5878, 45.5048)))24        guard case .polygon(let rings)? = d.layers[1].features[0].geometry else { Issue.record("not a polygon"); return }25        #expect(rings.count == 2 && rings[0].count == 5 && rings[1].count == 5)26        #expect(rings[0][2] == Coordinate(-73.58, 45.52, z: 0) && rings[1][1] == Coordinate(-73.595, 45.515, z: 0))27        let routes = d.layers[2].features28        #expect(routes[0].geometry == .lineString([Coordinate(-73.554, 45.5075), Coordinate(-73.56, 45.506), Coordinate(-73.5878, 45.5048)]))29        #expect(routes[1].geometry == .collection([.point(Coordinate(-73.55, 45.51)), .lineString([Coordinate(-73.55, 45.51), Coordinate(-73.56, 45.52)])]))30        #expect(routes[2].geometry == .lineString([Coordinate(-73.55, 45.51, z: 20), Coordinate(-73.56, 45.52, z: 22)]))31        // What was dropped, counted exactly: 2 style definitions + 2 style references.32        let byCode = Dictionary(grouping: d.warnings, by: \.code)33        #expect(byCode["kml-styles-dropped"]?.first?.message.hasPrefix("4 style definition(s)") == true)34        #expect(byCode["kml-styles-dropped"]?.first?.severity == .dataLoss)35        #expect(Set(byCode["kml-dropped"]?.map(\.message) ?? []) == ["1 image overlay(s) in the KML file were not carried over.", "1 network link(s) (never fetched) in the KML file were not carried over."])36        #expect(byCode["kml-track-times"]?.first?.message.hasPrefix("2 timestamp(s)") == true)37    }3839    @Test func layerSelectionAndMultiLayerTargets() async throws {40        let temp = TempDir()41        let source = temp.write("trip.kml", Samples.kml)42        // Every layer → one GeoPackage with three tables.43        let gpkg = try await convert(source, .kml, to: .geopackage, in: temp)44        #expect(gpkg.layers.map(\.name) == ["Sites", "Sites_Parks", "Routes"])45        #expect(gpkg.layers.map(\.features.count) == [2, 1, 3])46        #expect(gpkg.features[5].geometry == .lineString([Coordinate(-73.55, 45.51, z: 20), Coordinate(-73.56, 45.52, z: 22)]))47        // GeoJSON: one file per layer.48        let geojson = try await convert(source, .kml, to: .geojson, in: temp)49        #expect(geojson.output.files.map(\.lastPathComponent) == ["trip-Sites.geojson", "trip-Sites_Parks.geojson", "trip-Routes.geojson"])50        // One chosen layer.51        let one = try await convert(source, .kml, to: .csv, in: temp, options: ["layer": .string("Sites")])52        #expect(try String(contentsOf: one.output.files[0], encoding: .utf8) == "longitude,latitude,elevation,name,description,visitors,rating,open,phone,timestamp\r\n-73.554,45.5075,12,Old Port,<b>Vieux-Port</b> &amp; quays,6000000,4.5,true,514-555-0100,\r\n-73.5878,45.5048,,Summit,,,,,,2024-06-01T10:00:00Z\r\n")53        await #expect(throws: ConversionError.invalidOption("The source has no layer named \"Nope\". Layers: Sites, Sites/Parks, Routes.")) {54            _ = try await convert(source, .kml, to: .csv, in: temp, options: ["layer": .string("Nope")])55        }56        // Shapefile: collections are refused, then split on request; mixed geometry types give several shapefiles.57        do {58            _ = try await convert(source, .kml, to: .shapefile, in: temp)59            Issue.record("a geometry collection was written to a shapefile")60        } catch let e as ConversionError {61            guard case .invalidOption(let m) = e else { Issue.record("wrong error \(e)"); return }62            #expect(m.hasPrefix("1 feature(s) are geometry collections"))63        }64        let shp = try await convert(source, .kml, to: .shapefile, in: temp, options: ["geometry.explode-collections": .bool(true)])65        let names = try FileManager.default.contentsOfDirectory(atPath: shp.output.files[0].path).filter { $0.hasSuffix(".shp") }.sorted()66        #expect(names == ["Routes_line.shp", "Routes_point.shp", "Sites.shp", "Sites_Parks.shp"])67        #expect(shp.warnings.first("collections-split")?.message == "1 geometry collection(s) were split into 2 separate feature(s), each repeating the attributes of its collection.")68        #expect(shp.warnings.first("shp-split-by-geometry")?.message.contains("2 shapefiles were written") == true)69        #expect(shp.features.count == 7)70        // "Sites" mixes a point with Z and one without: the second gets Z = 0, and that is said.71        #expect(shp.warnings.first("shp-z-filled")?.message.hasPrefix("1 geometr(ies)") == true)72    }7374    @Test func writesTypedSchemaAndNameMapping() async throws {75        let temp = TempDir()76        let source = temp.write("places.geojson", """77        {"type":"FeatureCollection","features":[78        {"type":"Feature","properties":{"Title":"A & B <1>","notes":"line1\\nline2","count":3,"ratio":0.5,"ok":false,"empty":null,"day":"2024-06-01"},"geometry":{"type":"MultiPolygon","coordinates":[[[[0,0],[0,1],[1,1],[1,0],[0,0]]],[[[5,5],[6,5],[6,6],[5,6],[5,5]]]]}}79        ]}80        """)81        let r = try await convert(source, .geojson, to: .kml, in: temp, options: ["kml.name-field": .string("title"), "kml.description-field": .string("notes")])82        let text = try String(contentsOf: r.output.files[0], encoding: .utf8)83        #expect(text.contains("<name>A &amp; B &lt;1&gt;</name><description>line1\nline2</description>"))84        #expect(text.contains("<SimpleField name=\"count\" type=\"int\"/>") && text.contains("<SimpleField name=\"ratio\" type=\"double\"/>") && text.contains("<SimpleField name=\"ok\" type=\"bool\"/>"))85        #expect(!text.contains("SimpleData name=\"empty\""))86        // KML wants counter-clockwise outer rings: the clockwise source ring was reversed, the other kept.87        #expect(text.contains("<coordinates>0,0 1,0 1,1 0,1 0,0</coordinates>") && text.contains("<coordinates>5,5 6,5 6,6 5,6 5,5</coordinates>"))88        let f = r.features[0]89        #expect(f["name"] == .string("A & B <1>") && f["description"] == .string("line1\nline2"))90        #expect(f["count"] == .integer(3) && f["ratio"] == .real(0.5) && f["ok"] == .bool(false) && f["day"] == .string("2024-06-01"))91        #expect(f.geometry?.typeName == "MultiPolygon")92    }9394    @Test func controlCharactersAreRemovedAndCounted() async throws {95        let temp = TempDir()96        let source = temp.write("c.geojson", "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"name\":\"a\\u0001b\",\"note\":\"line\\nbreak\\ttab\"},\"geometry\":{\"type\":\"Point\",\"coordinates\":[1,2]}}]}")97        for destination in [FormatID.kml, .gpx] {98            let r = try await convert(source, .geojson, to: destination, in: temp)99            #expect(r.features[0]["name"] == .string("ab"))100            #expect(r.warnings.first("xml-control-characters")?.message.hasPrefix("1 text value(s)") == true)101            #expect(r.warnings.first("xml-control-characters")?.severity == .dataLoss)102        }103        // Formats that can carry them keep them.104        let gpkg = try await convert(source, .geojson, to: .geopackage, in: temp)105        #expect(gpkg.features[0]["name"] == .string("a\u{1}b") && gpkg.warnings.first("xml-control-characters") == nil)106    }107108    @Test func readsKMZ() throws {109        let temp = TempDir()110        let kmz = temp.write("trip.kml", ZipBytes.archive([("files/icon.png", Data([1, 2, 3]), false), ("doc.kml", Data(Samples.kml.utf8), true)]))111        let d = try read(kmz, .kml)112        #expect(d.featureCount == 6)113        #expect(d.warnings.contains { $0.code == "kmz-resources" && $0.severity == .dataLoss })114        // Archives lying about their sizes are refused, never inflated beyond the declaration.115        let bomb = temp.write("bomb.kml", ZipBytes.archive([("doc.kml", Data(repeating: 0x20, count: 200_000), true)], declaredSize: 10))116        #expect(throws: ConversionError.self) { _ = try read(bomb, .kml) }117        let empty = temp.write("empty.kml", ZipBytes.archive([("readme.txt", Data("x".utf8), false)]))118        #expect(throws: ConversionError.malformedInput("The KMZ archive contains no KML document.")) { _ = try read(empty, .kml) }119    }120}121122@Suite struct GPXTests {123    @Test func readsWaypointsRoutesTracksAndExtensions() throws {124        let temp = TempDir()125        let url = temp.write("ride.gpx", Samples.gpx)126        let d = try read(url, .gpx)127        #expect(d.layers.map(\.name) == ["waypoints", "routes", "tracks"])128        let w = d.layers[0].features[0]129        #expect(w.geometry == .point(Coordinate(-73.5673, 45.5017, z: 36.5)))130        #expect(w.attributes == [131            Attribute("time", .dateTime("2024-06-01T10:00:00Z")), Attribute("name", .string("Start")), Attribute("cmt", .string("Meet here")), Attribute("desc", .string("Parking")),132            Attribute("link", .string("https://example.invalid/start")), Attribute("sym", .string("Flag")), Attribute("type", .string("poi")), Attribute("sat", .integer(9)), Attribute("hdop", .real(0.9)), Attribute("color", .string("blue")),133        ])134        #expect(d.layers[1].features[0].geometry == .lineString([Coordinate(-73.57, 45.5), Coordinate(-73.58, 45.51)]))135        #expect(d.layers[1].features[0].attributes == [Attribute("name", .string("Plan")), Attribute("number", .integer(2))])136        #expect(d.layers[2].features[0].geometry == .multiLineString([[Coordinate(-73.57, 45.5, z: 30), Coordinate(-73.571, 45.501, z: 31.5)], [Coordinate(-73.572, 45.502, z: 33), Coordinate(-73.573, 45.503, z: 34)]]))137        #expect(d.layers[2].features[0].attributes == [Attribute("name", .string("Ride")), Attribute("type", .string("cycling"))])138        // 1 route point with a name + 3 track points with a time (one of them also with extensions).139        #expect(d.warnings.contains { $0.code == "gpx-point-data" && $0.message.hasPrefix("4 route or track point(s)") && $0.severity == .dataLoss })140        #expect(d.warnings.contains { $0.code == "gpx-metadata" })141        // With the option, every point becomes a feature and nothing is dropped.142        var options = GeoReadOptions()143        options.gpxTrackPoints = true144        let p = try read(url, .gpx, options)145        #expect(p.layers.map(\.name) == ["waypoints", "routes", "tracks", "route_points", "track_points"])146        #expect(!p.warnings.contains { $0.code == "gpx-point-data" })147        let first = p.layers[4].features[0]148        #expect(first.attributes == [Attribute("track_fid", .integer(0)), Attribute("track_seg_id", .integer(0)), Attribute("track_seg_point_id", .integer(0)),149                                     Attribute("time", .dateTime("2024-06-01T10:00:00Z")), Attribute("hr", .integer(120)), Attribute("cad", .integer(80))])150        #expect(p.layers[4].features[2].attributes.prefix(3).map(\.value) == [.integer(0), .integer(1), .integer(0)])151        #expect(p.layers[4].features.count == 4 && p.layers[3].features[0]["name"] == .string("A"))152    }153154    @Test func readsGPX10() throws {155        let temp = TempDir()156        let url = temp.write("old.gpx", "<?xml version=\"1.0\"?><gpx version=\"1.0\" creator=\"x\" xmlns=\"http://www.topografix.com/GPX/1/0\"><wpt lat=\"1.5\" lon=\"2.5\"><ele>3</ele><name>P</name><url>http://example.invalid</url></wpt><trk><trkseg><trkpt lat=\"1\" lon=\"2\"/><trkpt lat=\"3\" lon=\"4\"/></trkseg></trk></gpx>")157        let d = try read(url, .gpx)158        #expect(d.layers[0].features[0].geometry == .point(Coordinate(2.5, 1.5, z: 3)))159        #expect(d.layers[0].features[0]["link"] == .string("http://example.invalid"))160        #expect(d.layers[1].features[0].geometry == .lineString([Coordinate(2, 1), Coordinate(4, 3)]))161    }162163    @Test func trackPointsToCSV() async throws {164        let temp = TempDir()165        let r = try await convert(temp.write("ride.gpx", Samples.gpx), .gpx, to: .csv, in: temp, options: ["gpx.track-points": .bool(true), "layer": .string("track_points")])166        #expect(try String(contentsOf: r.output.files[0], encoding: .utf8) == "longitude,latitude,elevation,track_fid,track_seg_id,track_seg_point_id,time,hr,cad\r\n-73.57,45.5,30,0,0,0,2024-06-01T10:00:00Z,120,80\r\n-73.571,45.501,31.5,0,0,1,2024-06-01T10:00:05Z,,\r\n-73.572,45.502,33,0,1,0,2024-06-01T10:05:00Z,,\r\n-73.573,45.503,34,0,1,1,,,\r\n")167    }168169    @Test func writingKeepsOnlyFixedFieldsAndCountsTheRest() async throws {170        let temp = TempDir()171        let source = temp.write("mixed.geojson", """172        {"type":"FeatureCollection","features":[173        {"type":"Feature","properties":{"Name":"Cabin","Description":"Hut","elevation":812.5,"time":"2024-06-01T10:00:00Z","sat":"many","owner":"SP","beds":4},"geometry":{"type":"Point","coordinates":[-73.5,45.5]}},174        {"type":"Feature","properties":{"name":"Trail","number":7,"length_km":12.5},"geometry":{"type":"LineString","coordinates":[[-73.5,45.5,100],[-73.6,45.6,110]]}},175        {"type":"Feature","properties":{"name":"Loop","owner":null},"geometry":{"type":"MultiLineString","coordinates":[[[0,0],[1,1]],[[2,2],[3,3]]]}},176        {"type":"Feature","properties":{"name":"Pair"},"geometry":{"type":"MultiPoint","coordinates":[[1,1],[2,2]]}},177        {"type":"Feature","properties":{"name":"Nowhere"},"geometry":null}178        ]}179        """)180        let r = try await convert(source, .geojson, to: .gpx, in: temp)181        let text = try String(contentsOf: r.output.files[0], encoding: .utf8)182        #expect(text.contains("<wpt lat=\"45.5\" lon=\"-73.5\"><ele>812.5</ele><time>2024-06-01T10:00:00Z</time><name>Cabin</name><desc>Hut</desc></wpt>"))183        #expect(text.contains("<trk><name>Trail</name><number>7</number>\n<trkseg>\n<trkpt lat=\"45.5\" lon=\"-73.5\"><ele>100</ele></trkpt>"))184        // Dropped: sat ("many" is not a number), owner, beds on the cabin; length_km on the trail = 4 values.185        let w = try #require(r.warnings.first("gpx-attributes-dropped"))186        #expect(w.message == "4 attribute value(s) were dropped because GPX only has fixed fields (name, desc, cmt, src, sym, type, time, link…). Attributes concerned: beds, length_km, owner, sat.")187        #expect(r.warnings.first("gpx-no-geometry")?.message.hasPrefix("1 feature(s)") == true)188        #expect(r.warnings.first("gpx-multipoints-split")?.message.hasPrefix("1 multi-point") == true)189        #expect(r.layers.map(\.name) == ["waypoints", "tracks"])190        #expect(r.layers[0].features.count == 3 && r.layers[1].features.count == 2)191        // Routes on request; a multi-part line still has to be a track.192        let routes = try await convert(source, .geojson, to: .gpx, in: temp, options: ["gpx.lines-as": .string("routes")])193        #expect(routes.layers.map(\.name) == ["waypoints", "routes", "tracks"])194        #expect(routes.warnings.first("gpx-multiline-tracks")?.message.hasPrefix("1 multi-part line(s)") == true)195    }196197    @Test func polygonsNeedAnExplicitChoice() async throws {198        let temp = TempDir()199        let source = temp.write("area.geojson", "{\"type\":\"Feature\",\"properties\":{\"name\":\"Lot\"},\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[1,2],[2,2],[2,1],[1,1]]]}}")200        do {201            _ = try await convert(source, .geojson, to: .gpx, in: temp)202            Issue.record("a polygon was written to GPX without asking")203        } catch let e as ConversionError {204            guard case .invalidOption(let m) = e else { Issue.record("wrong error \(e)"); return }205            #expect(m.hasPrefix("1 feature(s) are polygons, and GPX can only hold points and lines."))206        }207        let r = try await convert(source, .geojson, to: .gpx, in: temp, options: ["gpx.polygons-as-tracks": .bool(true)])208        #expect(r.warnings.first("gpx-polygons-as-tracks")?.severity == .dataLoss)209        guard case .multiLineString(let segments)? = r.features[0].geometry else { Issue.record("not a two-segment track"); return }210        #expect(segments.count == 2 && segments[0].first == segments[0].last && segments[0].count == 5)211        // Collections: refused, or split on request.212        let collection = temp.write("c.geojson", "{\"type\":\"GeometryCollection\",\"geometries\":[{\"type\":\"Point\",\"coordinates\":[1,2]},{\"type\":\"LineString\",\"coordinates\":[[0,0],[1,1]]}]}")213        await #expect(throws: ConversionError.self) { _ = try await convert(collection, .geojson, to: .gpx, in: temp) }214        let split = try await convert(collection, .geojson, to: .gpx, in: temp, options: ["geometry.explode-collections": .bool(true)])215        #expect(split.features.count == 2)216    }217}218219@Suite struct GeoJSONAndCSVTests {220    @Test func geoJSONVariants() throws {221        let temp = TempDir()222        let url = temp.write("v.geojson", """223        {"type":"FeatureCollection","name":"Named","generator":"x","bbox":[0,0,1,1],"features":[224        {"type":"Feature","id":17,"extra":true,"properties":{"big":9007199254740993,"neg":-0.0,"exp":1e3,"s":"a\\u00e9\\ud83d\\ude00\\n","obj":{"k":[1,2,{"z":null}]},"nil":null},"geometry":{"type":"Point","coordinates":[1,2,3,4]}},225        {"type":"Feature","properties":null,"geometry":{"type":"GeometryCollection","geometries":[{"type":"MultiPoint","coordinates":[[1,2]]},{"type":"GeometryCollection","geometries":[]}]}},226        {"type":"Feature","properties":{},"geometry":{"type":"Point","coordinates":[]}}227        ]}228        """)229        let d = try read(url, .geojson)230        #expect(d.layers[0].name == "Named")231        let f = d.layers[0].features232        #expect(f[0].attributes == [Attribute("id", .integer(17)), Attribute("big", .integer(9_007_199_254_740_993)), Attribute("neg", .real(-0.0)), Attribute("exp", .real(1000)),233                                    Attribute("s", .string("aé😀\n")), Attribute("obj", .json("{\"k\":[1,2,{\"z\":null}]}")), Attribute("nil", .null)])234        #expect(f[0].geometry == .point(Coordinate(1, 2, z: 3)))235        #expect(f[1].geometry == .collection([.multiPoint([Coordinate(1, 2)]), .collection([])]))236        #expect(f[2].geometry == nil)237        #expect(d.warnings.contains { $0.code == "geojson-extra-ordinates" && $0.message.hasPrefix("1 position(s)") })238        // "generator" at the top and "extra" on the feature.239        #expect(d.warnings.contains { $0.code == "geojson-foreign-members" && $0.message.hasPrefix("2 non-standard member(s)") })240        #expect(d.warnings.contains { $0.code == "geojson-feature-id" && $0.message.hasPrefix("1 feature identifier(s)") })241    }242243    @Test func newlineDelimitedGeoJSON() async throws {244        let temp = TempDir()245        let url = temp.write("lines.geojson", "{\"type\":\"Feature\",\"properties\":{\"n\":1},\"geometry\":{\"type\":\"Point\",\"coordinates\":[1,2]}}\n\u{1E}{\"type\":\"Feature\",\"properties\":{\"n\":2},\"geometry\":{\"type\":\"Point\",\"coordinates\":[3,4]}}\r\n\n{\"type\":\"LineString\",\"coordinates\":[[0,0],[1,1]]}\n")246        let d = try read(url, .geojson)247        #expect(d.layers[0].features.count == 3)248        #expect(d.warnings.contains { $0.code == "geojson-lines" && $0.message.contains("3 records") })249        let r = try await convert(url, .geojson, to: .geopackage, in: temp)250        #expect(r.features.map(\.geometry?.typeName) == ["Point", "Point", "LineString"])251    }252253    @Test func geoJSONWriterFollowsRFC7946() async throws {254        let temp = TempDir()255        // A clockwise square with a clockwise hole, and measures.256        let csv = temp.write("p.csv", "id,note,wkt\n1,\"say \"\"hi\"\"\",\"POLYGON ((0 0, 0 4, 4 4, 4 0, 0 0), (1 1, 1 2, 2 2, 2 1, 1 1))\"\n2,,\"POINT ZM (1 2 3 4)\"\n3,x,\n")257        let r = try await convert(csv, .csv, to: .geojson, in: temp, options: ["crs.source": .string("EPSG:4326")])258        let text = try String(contentsOf: r.output.files[0], encoding: .utf8)259        #expect(text == """260        {"type":"FeatureCollection","name":"p","features":[261        {"type":"Feature","properties":{"id":1,"note":"say \\"hi\\""},"geometry":{"type":"Polygon","coordinates":[[[0,0],[4,0],[4,4],[0,4],[0,0]],[[1,1],[1,2],[2,2],[2,1],[1,1]]]}},262        {"type":"Feature","properties":{"id":2,"note":null},"geometry":{"type":"Point","coordinates":[1,2,3]}},263        {"type":"Feature","properties":{"id":3,"note":"x"},"geometry":null}264        ]}265266        """)267        #expect(r.warnings.first("measures-dropped")?.message.hasPrefix("1 geometr(ies) carry measure (M) values, which GeoJSON cannot store") == true)268        #expect(r.warnings.first("csv-missing-coordinates")?.message.hasPrefix("1 row(s)") == true)269        // The output is valid JSON for an independent parser too.270        let parsed = try JSONSerialization.jsonObject(with: Data(text.utf8)) as? [String: Any]271        #expect((parsed?["features"] as? [Any])?.count == 3)272    }273274    @Test func csvDetectionAndTyping() throws {275        let temp = TempDir()276        // Semicolons, BOM, quoted newline, postal codes with leading zeros, mixed numbers.277        let url = temp.write("s.csv", "\u{FEFF}Nom;LATITUDE;Longitude;Alt;code;n;mix;ok;jour;quand\n\"Trois\nRivières\";46.35;-72.55;61;00123;1;2;TRUE;2024-06-01;2024-06-01 10:00\nSorel;46.04;-73.11;;G3A;2.5;abc;false;;\n")278        let d = try read(url, .csv)279        let f = d.layers[0].features280        #expect(f[0].geometry == .point(Coordinate(-72.55, 46.35, z: 61)) && f[1].geometry == .point(Coordinate(-73.11, 46.04)))281        #expect(f[0].attributes == [Attribute("Nom", .string("Trois\nRivières")), Attribute("code", .string("00123")), Attribute("n", .real(1)), Attribute("mix", .string("2")),282                                    Attribute("ok", .bool(true)), Attribute("jour", .date("2024-06-01")), Attribute("quand", .dateTime("2024-06-01 10:00"))])283        #expect(f[1].attributes.map(\.value) == [.string("Sorel"), .string("G3A"), .real(2.5), .string("abc"), .bool(false), .null, .null])284        #expect(d.layers[0].crs.epsg == 4326 && d.layers[0].crs.assumed)285        #expect(d.warnings.contains { $0.code == "csv-crs-assumed" })286        // Explicit columns, case-insensitive, with projected numbers: no reference system is invented.287        var options = GeoReadOptions()288        options.csv.xColumn = "e"; options.csv.yColumn = "N"289        let projected = temp.write("p.csv", "id,E,N\n1,611928.7,5039683.6\n")290        let p = try read(projected, .csv, options)291        #expect(p.layers[0].crs.epsg == nil && p.layers[0].features[0].geometry == .point(Coordinate(611_928.7, 5_039_683.6)))292        #expect(p.layers[0].features[0].attributes == [Attribute("id", .integer(1))])293        options.csv.xColumn = "nope"294        #expect(throws: ConversionError.invalidOption("The X / longitude column \"nope\" does not exist. Columns: id, E, N.")) { _ = try read(projected, .csv, options) }295        options.csv.yColumn = ""296        options.csv.xColumn = "E"297        #expect(throws: ConversionError.self) { _ = try read(projected, .csv, options) }298        // No recognisable columns: the error lists them so the app can ask.299        let none = temp.write("n.csv", "a,b\n1,2\n")300        #expect(throws: ConversionError.self) { _ = try read(none, .csv) }301        // x / y names with projected values and nothing stated: the conversion refuses.302        let xy = temp.write("xy.csv", "x,y\n611928.7,5039683.6\n")303        #expect(try read(xy, .csv).layers[0].crs.epsg == nil)304        // Windows-1252 text is detected and reported.305        let latin = temp.write("l.csv", Data([0x6E, 0x61, 0x6D, 0x65] + Array(",lat,lon\n".utf8) + [0xC9, 0x74, 0xE9] + Array(",45,-73\n".utf8)))306        let l = try read(latin, .csv)307        #expect(l.layers[0].features[0]["name"] == .string("Été") && l.warnings.contains { $0.code == "csv-encoding-assumed" })308    }309310    @Test func csvWriterModes() async throws {311        let temp = TempDir()312        let source = temp.write("m.geojson", """313        {"type":"FeatureCollection","features":[314        {"type":"Feature","properties":{"latitude":"north","note":" padded "},"geometry":{"type":"Point","coordinates":[1,2]}},315        {"type":"Feature","properties":{"note":"a,b"},"geometry":{"type":"LineString","coordinates":[[0,0],[1,1]]}}316        ]}317        """)318        let auto = try await convert(source, .geojson, to: .csv, in: temp)319        #expect(try String(contentsOf: auto.output.files[0], encoding: .utf8) == "WKT,latitude,note\r\nPOINT (1 2),north,\" padded \"\r\n\"LINESTRING (0 0,1 1)\",,\"a,b\"\r\n")320        let xy = try await convert(source, .geojson, to: .csv, in: temp, options: ["csv.geometry": .string("xy")])321        #expect(xy.warnings.first("csv-wkt-fallback") != nil)322        // x / y columns of a projected CSV without a stated system: refused, then accepted once stated.323        let projected = temp.write("xy.csv", "x,y,label\n611928.7121664289,5039683.6301076,Montréal\n")324        await #expect(throws: ConversionError.self) { _ = try await convert(projected, .csv, to: .geojson, in: temp) }325        let stated = try await convert(projected, .csv, to: .geojson, in: temp, options: ["crs.source": .string("EPSG:26918")])326        let c = try #require(coordinates(stated.features[0].geometry).first)327        #expect(abs(c.x - -73.5673) < 1e-8 && abs(c.y - 45.5017) < 1e-8)328    }329330    @Test func precisionRoundingIsOptInAndReported() async throws {331        let temp = TempDir()332        let source = Cities.source(.geojson, in: temp)333        let request = ConversionRequest(source: source, sourceFormat: .geojson, destinationFormat: .kml, options: ConversionOptions(["precision.decimals": .int(2)]))334        #expect(GeoEngine().preflight(request, inspection: nil).contains { $0.code == "precision-rounded" && $0.severity == .dataLoss })335        #expect(GeoEngine().preflight(ConversionRequest(source: source, sourceFormat: .geojson, destinationFormat: .kml), inspection: nil).isEmpty)336        let r = try await convert(source, .geojson, to: .kml, in: temp, options: ["precision.decimals": .int(2)])337        #expect(r.features[0].geometry == .point(Coordinate(-73.57, 45.5, z: 36.5)))338        #expect(r.warnings.first("precision-rounded")?.severity == .dataLoss)339    }340}341342@Suite struct ShapefileLimitTests {343    @Test func dbfLimitsAreCountedExactly() async throws {344        let temp = TempDir()345        let long = String(repeating: "é", count: 200)   // 400 bytes in UTF-8346        let source = temp.write("limits.geojson", """347        {"type":"FeatureCollection","features":[348        {"type":"Feature","properties":{"population_total":10,"population_urban":4,"population_rural":6,"Élévation_moyenne":12.5,"note":"\(long)","seen":"2024-06-01T10:00:00Z","tags":["a"],"pad":"x  ","blank":"","mixed":1,"nan":1.5},"geometry":{"type":"Point","coordinates":[1,2]}},349        {"type":"Feature","properties":{"population_total":20,"population_urban":8,"population_rural":12,"Élévation_moyenne":3,"note":"short","seen":null,"tags":null,"pad":"y","blank":"z","mixed":"one","nan":2},"geometry":{"type":"Point","coordinates":[3,4]}}350        ]}351        """)352        let r = try await convert(source, .geojson, to: .shapefile, in: temp)353        let names = try #require(r.warnings.first("dbf-names-truncated"))354        #expect(names.severity == .dataLoss)355        #expect(names.message == "4 attribute name(s) were shortened or made unique for the dBASE table (10 characters at most): population_total → population; population_urban → populati_1; population_rural → populati_2; Élévation_moyenne → Élévatio.")356        #expect(r.warnings.first("dbf-text-truncated")?.message.hasPrefix("1 text value(s) longer than 254 bytes") == true)357        #expect(r.warnings.first("dbf-trailing-spaces")?.message.hasPrefix("1 text value(s)") == true)358        #expect(r.warnings.first("dbf-empty-text")?.message.hasPrefix("1 empty text value(s)") == true)359        // GeoJSON has no date type: "seen" is plain text there. A typed timestamp (KML TimeStamp) is counted.360        #expect(r.warnings.first("dbf-datetime-as-text") == nil)361        let kml = try await convert(temp.write("trip.kml", Samples.kml), .kml, to: .shapefile, in: temp, options: ["layer": .string("Sites")])362        #expect(kml.warnings.first("dbf-datetime-as-text")?.message.hasPrefix("1 date-and-time value(s)") == true)363        #expect(kml.features[1]["timestamp"] == .string("2024-06-01T10:00:00Z"))364        #expect(r.warnings.first("dbf-nested-as-text")?.message.hasPrefix("1 nested value(s)") == true)365        #expect(r.warnings.first("attributes-mixed-types")?.message.contains("1 attribute(s)") == true)366        let f = r.features367        #expect(f[0].attributes.map(\.name) == ["population", "populati_1", "populati_2", "Élévatio", "note", "seen", "tags", "pad", "blank", "mixed", "nan"])368        #expect(f[0]["population"] == .integer(10) && f[1]["populati_2"] == .integer(12))369        #expect(f[0]["Élévatio"] == .real(12.5) && f[1]["Élévatio"] == .real(3))370        #expect(f[0]["note"] == .string(String(repeating: "é", count: 127)), "254 bytes, cut on a character boundary")371        #expect(f[0]["seen"] == .string("2024-06-01T10:00:00Z") && f[0]["tags"] == .string("[\"a\"]") && f[0]["pad"] == .string("x") && f[0]["blank"] == .null)372        #expect(f[0]["mixed"] == .string("1") && f[1]["mixed"] == .string("one") && f[1]["nan"] == .real(2))373    }374375    @Test func mixedLayerIsSplitIntoSeveralShapefiles() async throws {376        let temp = TempDir()377        let source = temp.write("mix.geojson", """378        {"type":"FeatureCollection","features":[379        {"type":"Feature","properties":{"k":"p"},"geometry":{"type":"Point","coordinates":[1,2]}},380        {"type":"Feature","properties":{"k":"l"},"geometry":{"type":"LineString","coordinates":[[0,0],[1,1]]}},381        {"type":"Feature","properties":{"k":"a"},"geometry":{"type":"Polygon","coordinates":[[[0,0],[1,0],[1,1],[0,0]]]}},382        {"type":"Feature","properties":{"k":"m"},"geometry":{"type":"MultiPoint","coordinates":[[1,2],[3,4]]}},383        {"type":"Feature","properties":{"k":"l2"},"geometry":{"type":"MultiLineString","coordinates":[[[0,0],[1,1]],[[2,2],[3,3]]]}}384        ]}385        """)386        let r = try await convert(source, .geojson, to: .shapefile, in: temp)387        let files = try FileManager.default.contentsOfDirectory(atPath: r.output.files[0].path).filter { $0.hasSuffix(".shp") }.sorted()388        #expect(files == ["mix_line.shp", "mix_multipoint.shp", "mix_point.shp", "mix_polygon.shp"])389        #expect(r.warnings.first("shp-split-by-geometry")?.message == "The layer mix mixes geometry types (point, line, polygon, multiPoint): a shapefile holds one type, so 4 shapefiles were written.")390        #expect(r.output.facts["geo.shapefiles"] == "4" && r.output.facts["geo.features"] == "5")391        #expect(r.layers.map(\.features.count) == [2, 1, 1, 1])392    }393}394395/// KMZ as a source of its own (the core detects `.kmz` by content): every destination, through the KML reader.396@Suite struct KMZSourceTests {397    @Test func kmzIsASourceOnly() {398        let paths = GeoEngine().paths.filter { $0.source == .kmz || $0.destination == .kmz }399        #expect(Set(paths.map(\.destination)) == [.geojson, .kml, .gpx, .csv, .shapefile, .geopackage] && paths.allSatisfy { $0.source == .kmz })400        #expect(GeoEngine().inspectableFormats.contains(.kmz))401    }402403    @Test(arguments: [FormatID.geojson, .kml, .gpx, .csv, .shapefile, .geopackage]) func convertsKMZ(to destination: FormatID) async throws {404        let temp = TempDir()405        let kmz = temp.write("trip.kmz", ZipBytes.archive([("files/icon.png", Data([1, 2, 3]), false), ("doc.kml", Data(Samples.kml.utf8), true)]))406        #expect(FileTypeDetector.detect(kmz).format == .kmz)407        var options: [String: OptionValue] = ["geometry.explode-collections": .bool(true)]408        if destination == .gpx { options["gpx.polygons-as-tracks"] = .bool(true) }409        let result = try await convert(kmz, .kmz, to: destination, in: temp, options: options)410        #expect(result.features.count >= 6, "the six placemarks of the sample (collections split)")411        #expect(result.warnings.first("kmz-resources")?.severity == .dataLoss)412        let inspection = try await GeoEngine().inspect(kmz, format: .kmz)413        #expect(inspection.rowCount == 6)414    }415}416
Files main
Menu

Theme