Repository Public BSD-3-Clause
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project : ORIX Maps9// File : Packages/ORIXGeoCore/Tests/ORIXGeoCoreTests/GeoCoreTests.swift10// Module : ORIXGeoCoreTests11// Repository : https://git.spboucher.ai/orix-maps12// Created : 2026-11-2513// Last Modified : 2026-11-2514//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author : Simon-Pierre Boucher20// Position : Professor of Finance21// Institution : Université du Québec en Outaouais (UQO)22// Department : Département des sciences administratives23// Organization : SP Boucher24//25// Website : https://spboucher.ai26// Git : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General : info@spboucher.ai33// Professional : contact@spboucher.ai34// Direct : simon@spboucher.ai35// Research : research@spboucher.ai36// Development : dev@spboucher.ai37// Support : support@spboucher.ai38// Security/Admin : admin@spboucher.ai39// Notifications : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification : Public66// Distribution : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License : Proprietary, all rights reserved (see license file)80// License File : LICENSE81// Maintainer : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import Foundation89import Testing90@testable import ORIXGeoCore9192private let montreal = Coordinate(latitude: 45.5088, longitude: -73.5878)93private let quebec = Coordinate(latitude: 46.8139, longitude: -71.2080)9495@Suite struct CoordinateTests {96 @Test func wrapAndValidity() {97 #expect(Coordinate.wrapLongitude(190) == -170)98 #expect(Coordinate.wrapLongitude(-190) == 170)99 #expect(Coordinate.wrapLongitude(180) == -180)100 #expect(Coordinate.wrapLongitude(540) == -180)101 #expect(!Coordinate(latitude: 91, longitude: 0).isValid)102 #expect(!Coordinate(latitude: .nan, longitude: 0).isValid)103 #expect(Coordinate(latitude: 120, longitude: 200).normalized == Coordinate(latitude: 90, longitude: -160))104 }105106 @Test func e7RoundTrip() {107 let e = montreal.e7108 let back = Coordinate(latE7: e.lat, lonE7: e.lon)109 #expect(abs(back.latitude - montreal.latitude) < 1e-7 && abs(back.longitude - montreal.longitude) < 1e-7)110 }111112 @Test func boundingBoxAntimeridian() {113 let fiji = BoundingBox(minLat: -20, minLon: 177, maxLat: -15, maxLon: -178)114 #expect(fiji.crossesAntimeridian)115 #expect(fiji.contains(Coordinate(latitude: -17, longitude: 179.5)))116 #expect(fiji.contains(Coordinate(latitude: -17, longitude: -179.5)))117 #expect(!fiji.contains(Coordinate(latitude: -17, longitude: 0)))118 #expect(abs(fiji.lonSpan - 5) < 1e-9)119 #expect(abs(fiji.center.longitude - 179.5) < 1e-9)120 #expect(fiji.intersects(BoundingBox(minLat: -18, minLon: -180, maxLat: -16, maxLon: -179)))121 #expect(!fiji.intersects(BoundingBox(minLat: -18, minLon: 0, maxLat: -16, maxLon: 10)))122 }123124 @Test func boundingBoxEnclosingAndExpand() throws {125 let box = try #require(BoundingBox(enclosing: [montreal, quebec]))126 #expect(box.contains(Coordinate(latitude: 46, longitude: -72)))127 let grown = box.expanded(byMeters: 10_000)128 #expect(grown.minLat < box.minLat && grown.maxLon > box.maxLon)129 #expect(abs((box.minLat - grown.minLat) * Geodesy.metersPerDegreeLatitude - 10_000) < 1)130 #expect(BoundingBox(enclosing: [Coordinate]()) == nil)131 }132}133134@Suite struct GeodesyTests {135 @Test func distanceMontrealQuebec() {136 // Reference great-circle distance ≈ 233 km.137 let d = Geodesy.distance(montreal, quebec)138 #expect(abs(d - 233_400) < 1_500)139 #expect(Geodesy.distance(montreal, montreal) == 0)140 let near = Coordinate(latitude: 45.5188, longitude: -73.5778)141 #expect(abs(Geodesy.fastDistance(montreal, near) - Geodesy.distance(montreal, near)) < 0.5)142 }143144 @Test func distanceAcrossAntimeridian() {145 let a = Coordinate(latitude: 0, longitude: 179.9), b = Coordinate(latitude: 0, longitude: -179.9)146 #expect(abs(Geodesy.distance(a, b) - 22_239) < 50)147 #expect(abs(Geodesy.fastDistance(a, b) - 22_239) < 50)148 #expect(abs(abs(Geodesy.interpolate(a, b, t: 0.5).longitude) - 180) < 1e-9)149 }150151 @Test func bearingAndDestination() {152 #expect(abs(Geodesy.bearing(from: Coordinate(latitude: 0, longitude: 0), to: Coordinate(latitude: 1, longitude: 0))) < 1e-9)153 #expect(abs(Geodesy.bearing(from: Coordinate(latitude: 0, longitude: 0), to: Coordinate(latitude: 0, longitude: 1)) - 90) < 1e-9)154 let dest = Geodesy.destination(from: montreal, bearing: 57, distance: 5_000)155 #expect(abs(Geodesy.distance(montreal, dest) - 5_000) < 0.01)156 #expect(abs(Geodesy.bearing(from: montreal, to: dest) - 57) < 0.01)157 #expect(Geodesy.bearingDelta(350, 10) == 20)158 #expect(Geodesy.bearingDelta(10, 350) == -20)159 #expect(Geodesy.bearingDelta(0, 180) == 180)160 }161162 @Test func areaOfOneDegreeSquareAtEquator() {163 let ring = [Coordinate(latitude: 0, longitude: 0), Coordinate(latitude: 0, longitude: 1), Coordinate(latitude: 1, longitude: 1), Coordinate(latitude: 1, longitude: 0)]164 // ≈ 12 364 km² on the mean sphere.165 #expect(abs(Geodesy.area(of: ring) / 1e6 - 12_364) < 30)166 #expect(Geodesy.area(of: ring.reversed()) == Geodesy.area(of: ring))167 #expect(Geodesy.area(of: Array(ring.prefix(2))) == 0)168 }169170 @Test func projectionOnPolyline() throws {171 let line = [Coordinate(latitude: 45, longitude: -73), Coordinate(latitude: 45, longitude: -72.9), Coordinate(latitude: 45.1, longitude: -72.9)]172 let p = Coordinate(latitude: 45.001, longitude: -72.95)173 let r = try #require(Geodesy.project(p, onto: line))174 #expect(r.segmentIndex == 0)175 #expect(abs(r.fraction - 0.5) < 0.001)176 #expect(abs(r.distance - 111.2) < 1)177 #expect(abs(r.distanceAlong - Geodesy.distance(line[0], line[1]) / 2) < 2)178 let beyond = try #require(Geodesy.project(Coordinate(latitude: 45.2, longitude: -72.9), onto: line))179 #expect(beyond.segmentIndex == 1 && beyond.fraction == 1)180 #expect(Geodesy.project(p, onto: []) == nil)181 let at = try #require(Geodesy.point(along: line, at: Geodesy.length(of: line) + 500))182 #expect(Geodesy.distance(at.point, line[2]) < 0.01)183 #expect(abs(at.bearing) < 0.01)184 }185186 @Test func simplifyKeepsShape() {187 var line: [Coordinate] = []188 for i in 0...100 { line.append(Coordinate(latitude: 45 + Double(i) * 0.0001, longitude: -73 + (i == 50 ? 0.01 : 0))) }189 let s = Geodesy.simplify(line, toleranceMeters: 5)190 #expect(s.count == 5)191 #expect(s.first == line.first && s.last == line.last)192 #expect(s.contains(line[50]))193 #expect(Geodesy.simplify(line, toleranceMeters: 0) == line)194 }195196 @Test func pointInPolygon() {197 let ring = [Coordinate(latitude: 0, longitude: 0), Coordinate(latitude: 0, longitude: 2), Coordinate(latitude: 2, longitude: 2), Coordinate(latitude: 2, longitude: 0)]198 #expect(Geodesy.contains(ring: ring, point: Coordinate(latitude: 1, longitude: 1)))199 #expect(!Geodesy.contains(ring: ring, point: Coordinate(latitude: 3, longitude: 1)))200 }201}202203@Suite struct WebMercatorTests {204 @Test func projectRoundTrip() {205 for c in [montreal, quebec, Coordinate(latitude: -33.86, longitude: 151.21), Coordinate(latitude: 0, longitude: 0), Coordinate(latitude: 84.9, longitude: -179.9)] {206 let p = WebMercator.project(c)207 let back = WebMercator.unproject(x: p.x, y: p.y)208 #expect(abs(back.latitude - c.latitude) < 1e-9 && abs(back.longitude - c.longitude) < 1e-9)209 }210 #expect(WebMercator.project(Coordinate(latitude: 0, longitude: 0)).x == 0.5)211 #expect(abs(WebMercator.project(Coordinate(latitude: 0, longitude: 0)).y - 0.5) < 1e-12)212 }213214 @Test func projectionLimits() {215 #expect(WebMercator.project(Coordinate(latitude: 90, longitude: 0)).y == 0 || WebMercator.project(Coordinate(latitude: 90, longitude: 0)).y < 1e-9)216 #expect(abs(WebMercator.project(Coordinate(latitude: -90, longitude: 0)).y - 1) < 1e-9)217 #expect(abs(WebMercator.unproject(x: 0.5, y: 0).latitude - WebMercator.maxLatitude) < 1e-9)218 #expect(abs(WebMercator.unproject(x: 1.25, y: 0.5).longitude - (-90)) < 1e-9)219 #expect(abs(WebMercator.unproject(x: -0.25, y: 0.5).longitude - 90) < 1e-9)220 }221222 @Test func knownTiles() {223 // Montréal city hall area at z10 is tile 10/302/366 in the XYZ scheme.224 #expect(WebMercator.tile(for: montreal, zoom: 10) == TileID(z: 10, x: 302, y: 366))225 #expect(WebMercator.tile(for: Coordinate(latitude: 0, longitude: 0), zoom: 1) == TileID(z: 1, x: 1, y: 1))226 #expect(WebMercator.tile(for: Coordinate(latitude: 89, longitude: 180), zoom: 2) == TileID(z: 2, x: 0, y: 0))227 #expect(WebMercator.tile(for: Coordinate(latitude: -89, longitude: 179.9999), zoom: 2) == TileID(z: 2, x: 3, y: 3))228 #expect(WebMercator.tile(for: montreal, zoom: 0) == TileID(z: 0, x: 0, y: 0))229 }230231 @Test func tileHierarchyAndBounds() throws {232 let t = WebMercator.tile(for: montreal, zoom: 14)233 #expect(t.bounds.contains(montreal))234 #expect(t.isValid)235 #expect(!TileID(z: 2, x: 4, y: 0).isValid)236 let parent = try #require(t.parent)237 #expect(parent.children.contains(t))238 #expect(t.ancestor(atZoom: 10) == TileID(z: 10, x: 302, y: 366))239 #expect(t.ancestor(atZoom: 15) == nil)240 #expect(TileID(z: 0, x: 0, y: 0).parent == nil)241 let world = TileID(z: 0, x: 0, y: 0).bounds242 #expect(world.minLon == -180 && world.maxLon == 180)243 #expect(abs(world.maxLat - WebMercator.maxLatitude) < 1e-9)244 #expect(TileID(z: 3, x: 7, y: 2).bounds.maxLon == 180)245 }246247 @Test func tileCover() throws {248 let box = BoundingBox(minLat: 45.4, minLon: -73.98, maxLat: 45.71, maxLon: -73.47)249 let tiles = try #require(WebMercator.tiles(covering: box, zoom: 10))250 #expect(tiles.contains(TileID(z: 10, x: 302, y: 366)))251 #expect(UInt64(tiles.count) == WebMercator.tileCount(covering: box, zoom: 10))252 #expect(WebMercator.tiles(covering: box, zoom: 18, limit: 100) == nil)253 let fiji = BoundingBox(minLat: -20, minLon: 177, maxLat: -15, maxLon: -178)254 let ranges = WebMercator.tileRanges(covering: fiji, zoom: 6)255 #expect(ranges.count == 2)256 #expect(ranges[0].x.upperBound == 63 && ranges[1].x.lowerBound == 0)257 let whole = WebMercator.tileRanges(covering: BoundingBox(minLat: -90, minLon: -180, maxLat: 90, maxLon: 180), zoom: 3)258 #expect(whole.count == 1 && whole[0].x == 0...7 && whole[0].y == 0...7)259 }260261 @Test func resolution() {262 #expect(abs(WebMercator.metersPerPoint(latitude: 0, zoom: 0) - 78_271.5) < 1)263 #expect(abs(WebMercator.metersPerPoint(latitude: 60, zoom: 10) - 78_271.5 / 1024 / 2) < 0.01)264 }265}266267@Suite struct CoordinateTextTests {268 @Test func parseDecimal() {269 #expect(CoordinateText.parse("45.5088, -73.5878") == montreal)270 #expect(CoordinateText.parse("45.5088 -73.5878") == montreal)271 #expect(CoordinateText.parse(" 45.5088;-73.5878 ") == montreal)272 #expect(CoordinateText.parse("45,5088 -73,5878") == montreal)273 #expect(CoordinateText.parse("geo:45.5088,-73.5878?z=15") == montreal)274 #expect(CoordinateText.parse("−45.5, 170") == Coordinate(latitude: -45.5, longitude: 170))275 }276277 @Test func parseHemispheres() throws {278 #expect(CoordinateText.parse("45.5088 N 73.5878 W") == montreal)279 #expect(CoordinateText.parse("N45.5088 W73.5878") == montreal)280 #expect(CoordinateText.parse("73.5878W, 45.5088N") == montreal)281 #expect(CoordinateText.parse("45.5088 N 73.5878 O") == montreal)282 let dms = try #require(CoordinateText.parse("45°30'31.7\"N 73°35'16.1\"W"))283 #expect(abs(dms.latitude - 45.508_806) < 1e-5 && abs(dms.longitude - -73.587_806) < 1e-5)284 let ddm = try #require(CoordinateText.parse("45 30.528 N, 73 35.268 W"))285 #expect(abs(ddm.latitude - 45.5088) < 1e-6 && abs(ddm.longitude - -73.5878) < 1e-6)286 }287288 @Test func rejectNonCoordinates() {289 for text in ["", "Montréal", "1234 rue Sherbrooke", "45.5", "95, 10", "45, 200", "45 N 46 N", "1, 2, 3", "45.5088, -73.5878 extra", "12 Main St W", "-45 S 73 W", "45 61 N 73 W"] {290 #expect(CoordinateText.parse(text) == nil, "\(text)")291 }292 }293294 @Test func formatStyles() throws {295 #expect(CoordinateText.format(montreal) == "45.50880, -73.58780")296 #expect(CoordinateText.format(montreal, style: .degreesMinutesSeconds) == "45°30′31.7″N 73°35′16.1″W")297 #expect(CoordinateText.format(montreal, style: .degreesDecimalMinutes) == "45°30.528′N 73°35.268′W")298 #expect(CoordinateText.format(Coordinate(latitude: -0.999_999_9, longitude: 59.999_999_99), style: .degreesMinutesSeconds) == "1°00′00.0″S 60°00′00.0″E")299 for style in CoordinateText.Style.allCases where style != .utm {300 let back = try #require(CoordinateText.parse(CoordinateText.format(quebec, style: style)))301 #expect(Geodesy.distance(back, quebec) < 5)302 }303 }304305 @Test func utm() throws {306 // Montréal: zone 18T. Reference values from the standard Krüger series (±1 m).307 let u = try #require(UTM(montreal))308 #expect(u.zone == 18 && u.band == "T")309 #expect(abs(u.easting - 610_310) < 30 && abs(u.northing - 5_040_440) < 30)310 #expect(CoordinateText.format(montreal, style: .utm).hasPrefix("18T 61"))311 #expect(UTM(Coordinate(latitude: 89, longitude: 0)) == nil)312 let bergen = try #require(UTM(Coordinate(latitude: 60.39, longitude: 5.32)))313 #expect(bergen.zone == 32)314 let south = try #require(UTM(Coordinate(latitude: -33.86, longitude: 151.21)))315 #expect(south.zone == 56 && south.band == "H" && south.northing > 6_000_000)316 }317318 @Test func encodedPolyline() throws {319 // Reference vector from the published algorithm description.320 let line = [Coordinate(latitude: 38.5, longitude: -120.2), Coordinate(latitude: 40.7, longitude: -120.95), Coordinate(latitude: 43.252, longitude: -126.453)]321 #expect(EncodedPolyline.encode(line) == "_p~iF~ps|U_ulLnnqC_mqNvxq`@")322 #expect(EncodedPolyline.decode("_p~iF~ps|U_ulLnnqC_mqNvxq`@") == line)323 #expect(EncodedPolyline.decode("_p~iF~ps|U_ul") == nil)324 #expect(EncodedPolyline.decode("\u{1}") == nil)325 let precise = try #require(EncodedPolyline.decode(EncodedPolyline.encode([montreal, quebec], precision: 6), precision: 6))326 #expect(Geodesy.distance(precise[0], montreal) < 0.2)327 }328}329