From 5da1b800764697aaa36a6d538079f996e42ef004 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Mon, 28 Jul 2025 11:30:03 -0700 Subject: [PATCH] Update Meshtastic/Helpers/GeoJSONOverlayConfig.swift geo json validation Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Meshtastic/Helpers/GeoJSONOverlayConfig.swift | 49 ++++++++++--------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/Meshtastic/Helpers/GeoJSONOverlayConfig.swift b/Meshtastic/Helpers/GeoJSONOverlayConfig.swift index 8b301a66..b25b8509 100644 --- a/Meshtastic/Helpers/GeoJSONOverlayConfig.swift +++ b/Meshtastic/Helpers/GeoJSONOverlayConfig.swift @@ -177,33 +177,38 @@ struct GeoJSONStyledFeature: Identifiable { /// Create MKOverlay from this styled feature func createOverlay() -> MKOverlay? { + // Convert feature to standard GeoJSON format for MKGeoJSONDecoder + let featureDict: [String: Any] = [ + "type": feature.type, + "geometry": [ + "type": feature.geometry.type, + "coordinates": feature.geometry.coordinates.toAnyObject() + ], + "properties": feature.properties?.mapValues { $0.toAnyObject() } ?? [:] + ] + do { - // Convert feature to standard GeoJSON format for MKGeoJSONDecoder - let featureDict: [String: Any] = [ - "type": feature.type, - "geometry": [ - "type": feature.geometry.type, - "coordinates": feature.geometry.coordinates.toAnyObject() - ], - "properties": feature.properties?.mapValues { $0.toAnyObject() } ?? [:] - ] - - // Creating overlay for geometry - + // Serialize feature dictionary to JSON data let geojsonData = try JSONSerialization.data(withJSONObject: featureDict) - let mkFeatures = try MKGeoJSONDecoder().decode(geojsonData) - - // MKGeoJSONDecoder processing - - if let mkFeature = mkFeatures.first as? MKGeoJSONFeature { - // Processing geometry objects - if let geometry = mkFeature.geometry.first as? MKOverlay { - // Successfully created overlay - return geometry + do { + // Decode GeoJSON data into MKGeoJSONFeature objects + let mkFeatures = try MKGeoJSONDecoder().decode(geojsonData) + if let mkFeature = mkFeatures.first as? MKGeoJSONFeature { + // Extract geometry and create overlay + if let geometry = mkFeature.geometry.first as? MKOverlay { + // Successfully created overlay + return geometry + } else { + Logger.services.error("🗺️ GeoJSONStyledFeature: Failed to create overlay - Geometry is not an MKOverlay.") + } + } else { + Logger.services.error("🗺️ GeoJSONStyledFeature: Failed to decode GeoJSON - No valid MKGeoJSONFeature found.") } + } catch { + Logger.services.error("🗺️ GeoJSONStyledFeature: Failed to decode GeoJSON data: \(error.localizedDescription)") } } catch { - Logger.services.error("🗺️ GeoJSONStyledFeature: Failed to convert feature to overlay: \(error.localizedDescription)") + Logger.services.error("🗺️ GeoJSONStyledFeature: Failed to serialize feature dictionary to JSON: \(error.localizedDescription)") } return nil }