Objectif Lire les coordonnées GPS d'un export legacy en décimal ou en degrés-minutes-secondes, échanger une paire inversée, et vider avec sa raison chaque point inutilisable.
Le dispatch reprend les sites de livraison d'un ERP legacy. Les points GPS y ont été saisis par plusieurs outils:
une virgule décimale ici, des degrés-minutes-secondes là;
une paire inversée, un 0;0 laissé par un géocodeur;
des mètres à la place des degrés.
Le manifeste en fait un point par site, ou dit pourquoi il n'y en a pas.
// Operator-provided: one delivery site's coordinates, as a legacy export wrote them, read as a GPS point.// Reads a decimal comma, a hemisphere letter (48.85 N, O 0.37) and degrees-minutes-seconds (48°50'12"N).// A pair it cannot trust is emptied, counted, and `coord_issue` says why. Nothing is guessed.@Suppress("UNCHECKED_CAST")val cells = bindings["cells"] as MutableMap<String, Any?>val context = bindings["context"] as Map<*, *>// The integrator's part. within: south, west, north, east — where the sites are. swapped: fix — exchange a swapped pair.val box = (context["within"] as List<*>?)?.map { (it as Number).toDouble() }require(box == null || (box.size == 4 && box[0] < box[2] && box[1] < box[3])) { "within: south, west, north, east" }require(context["swapped"] in setOf(null, "fix")) { "swapped: fix, or nothing" }val fixSwapped = context["swapped"] == "fix"// A cell as signed degrees, with the axis its hemisphere letter names (null: no letter).class Reading(val degrees: Double, val axis: String?)val letters = setOf("N", "S", "E", "W", "O") // O: Ouest, as a French export writes Westval signed = Regex("""([+-]?)(\d+(?:[.,]\d+)?)""")fun decimal(text: String, signAllowed: Boolean): Double? { val (sign, digits) = signed.matchEntire(text)?.destructured ?: return null if (sign.isNotEmpty() && !signAllowed) return null return digits.replace(',', '.').toDouble() * (if (sign == "-") -1 else 1)}fun whole(text: String) = text.takeIf { it.all(Char::isDigit) }?.toIntOrNull()// Degrees and minutes, seconds optional: 48°50'12", 43°42', 43 17 42.fun sexagesimal(parts: List<String>): Double? { val degrees = whole(parts[0]) ?: return null val minutes = whole(parts[1])?.takeIf { it < 60 } ?: return null val seconds = if (parts.size == 3) decimal(parts[2], false)?.takeIf { it < 60 } ?: return null else 0.0 return degrees + minutes / 60.0 + seconds / 3600.0}fun read(raw: String): Reading? { // Typographic marks and a no-break space, as a spreadsheet leaves them, read like the plain ones. val text = raw.uppercase().replace('’', '\'').replace('′', '\'').replace('”', '"').replace('″', '"') .replace(' ', ' ').trim() val before = text.take(1).takeIf { it in letters } val after = text.takeLast(1).takeIf { it in letters } if (before != null && after != null) return null val letter = before ?: after val body = text.removePrefix(before ?: "").removeSuffix(after ?: "") val parts = body.split(Regex("""[°'"\s]+""")).filter { it.isNotEmpty() } val magnitude = when { parts.size == 1 -> decimal(parts[0], signAllowed = letter == null) // a letter comes instead of a sign parts.size in 2..3 && letter != null -> sexagesimal(parts) else -> null } ?: return null val axis = when (letter) { null -> null; "N", "S" -> "lat"; else -> "lon" } return Reading(if (letter in setOf("S", "W", "O")) -magnitude else magnitude, axis)}fun inRange(lat: Double, lon: Double) = Math.abs(lat) <= 90 && Math.abs(lon) <= 180fun usable(lat: Double, lon: Double) = inRange(lat, lon) && (box == null || (lat in box[0]..box[2] && lon in box[1]..box[3]))fun rounded(degrees: Double) = java.math.BigDecimal.valueOf(degrees).setScale(6, java.math.RoundingMode.HALF_UP)fun put(lat: Double?, lon: Double?, issue: String?) { cells["lat"] = lat?.let(::rounded) cells["lon"] = lon?.let(::rounded) cells["coord_issue"] = issue}fun setAside(issue: String) { put(null, null, issue) (bindings["unreadable"] as Runnable).run() // one pair, one count}fun exchanged(lat: Double, lon: Double) = if (fixSwapped) put(lat, lon, "swapped: exchanged") else setAside("swapped")fun repair(rawLat: String, rawLon: String) { if (rawLat.isEmpty() && rawLon.isEmpty()) return put(null, null, null) // no point given: nothing to repair if (rawLat.isEmpty() || rawLon.isEmpty()) return setAside("half a point") val a = read(rawLat) val b = read(rawLon) if (a == null || b == null) return setAside("not a coordinate") // What the letters say: 2.35 E under lat, or 48.85 N under lon, is a swapped pair. val saysSwapped = a.axis == "lon" || b.axis == "lat" val saysAsWritten = a.axis == "lat" || b.axis == "lon" if (saysSwapped && saysAsWritten) return setAside("not a coordinate") // two letters of one axis val lettered = saysSwapped || saysAsWritten val (lat, lon) = if (saysSwapped) b.degrees to a.degrees else a.degrees to b.degrees when { usable(lat, lon) -> if (saysSwapped) exchanged(lat, lon) else put(lat, lon, null) // Without a letter, only the declared area can tell: the pair fits the other way round, and only that way. !lettered && usable(lon, lat) -> exchanged(lon, lat) inRange(lat, lon) || (!lettered && inRange(lon, lat)) -> setAside("outside the declared area") else -> setAside("not degrees") }}repair(cells["lat"]?.toString()?.trim().orEmpty(), cells["lon"]?.toString()?.trim().orEmpty())
apiVersion: connectors.lumnik.io/v1kind: Connectormetadata:name: scenario-sites-cconnector_type: csv-filescopes: [dispatch-c]spec:transport:kind: localpath: /tmp/lumnik-scenario/broken-coordinates/c/sites.csvparser:delimiter: ";"after_process: nonemapping:cell:-kotlin:script: hooks:coordinates-c.kts # the operator's reading of a coordinatecontext:within: [41.0, -5.5, 51.5, 10.0] # south, west, north, east: mainland Franceswapped: fix # a pair that only fits the other way round is exchanged
$ lm apply -f 10-sites.yamlApplied connector "scenario-sites-c" — 1 endpoint$ lm connector run scenario-sites-crun scheduled: 66d42a64-f55d-4c83-afe4-cca5c48e015e$ lm run get 66d42a64-f55d-4c83-afe4-cca5c48e015eID CONNECTOR ENDPOINT STATUS IN OUT SKIPPED DEAD-LETTERS STARTED ENDED EVALUATED
66d42a64 scenario-sites-c default Completed 21 21 0 0 2026-11-13 01:08 2026-11-13 01:08 no⚠ 7 cell(s) not delivered as declared (a hook, or a promoted value of the wrong type) — the rows were still written• script hooks:coordinates-c.kts sha256 cc87f4a8e8ccbb38f3306d4dce5a013dab984c36a8cd72afd72e8bd142504542
kind: Entityname: delivery_site_clabel: Site de livraisondescription: "A delivery site with its GPS point in decimal degrees; coord_issue says why a point was set aside or exchanged."match: [site_id]scopes: [dispatch-c]columns:site_id:label: "Code site"name:label: "Site"city:label: "Ville"lat:label: "Latitude"lon:label: "Longitude"coord_issue:label: "Coordonnées: remarque"sources:-table: t_scenario_sites_cmap:site_id: site_idname: namecity: citylat: latlon: loncoord_issue: coord_issue
$ lm entity apply -f 20-site.yamlApplied entity "delivery_site_c" → connector."v_delivery_site_c"$ lm entity get delivery_site_cSITE_ID NAME CITY LAT LON COORD_ISSUE _CONFLICTS
S01 Entrepôt Gerland Lyon 45.7302 4.8309 {}
S02 Dépôt Villeurbanne Villeurbanne 45.7719 4.8902 {}
S03 Agence Bercy Paris 48.836667 2.382778 {}
S04 Plateforme Nantes Nantes 47.2184 -1.5536 {}
S05 Agence Brest Brest 48.3904 -4.4861 {}
S06 Dépôt Marseille Marseille 43.2965 5.3698 swapped: exchanged {}
S07 Agence Lille Lille outside the declared area {}
S08 Relais Toulouse Toulouse not degrees {}
S09 Agence Bordeaux Bordeaux {}
S10 Dépôt Strasbourg Strasbourg half a point {}
S11 Relais Dijon Dijon not a coordinate {}
S12 Agence Fort-de-France Fort-de-France outside the declared area {}
S13 Agence Rennes Rennes 48.111667 -1.679444 {}
S14 Agence Tours Tours 47.3941 0.6848 swapped: exchanged {}
S15 Relais Reims Reims 49.2583 4.0317 swapped: exchanged {}
S16 Agence Caen Caen 49.1829 -0.3707 {}
S17 Dépôt Nice Nice 43.7 7.25 {}
S18 Relais Pau Pau 43.295 -0.37 {}
S19 Agence Metz Metz not a coordinate {}
S20 Dépôt Nancy Nancy outside the declared area {}
S21 Agence Colmar Colmar 48.079167 7.358333 {}
apiVersion: apps.lumnik.io/v1kind: Viewmetadata:name: scenario-sites-cspec:scope: dispatch-ctable: connector.v_delivery_site_ctitle: Sites de livraisoncolumns: [site_id, name, city, lat, lon, coord_issue]links:-column: name # tap a site: its point opens on a map, in a new taburl: "https://www.openstreetmap.org/?mlat={lat}&mlon={lon}#map=16/{lat}/{lon}"