Update Meshtastic/Helpers/MapDataManager.swift

Add geojson validation

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Garth Vander Houwen 2025-07-28 11:29:12 -07:00 committed by GitHub
parent ad439dbd0a
commit 310b371616
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -143,13 +143,29 @@ class MapDataManager: ObservableObject {
}
}
// TODO: Add proper GeoJSON schema validation here
// - Validate required properties (type, features)
// - Validate geometry types and coordinates
// - Validate feature structure
// - Consider using JSONSchema validation
// - Ensure coordinates are within valid ranges (lat: -90 to 90, lon: -180 to 180)
// - Validate that feature properties follow expected patterns
// Validate GeoJSON schema
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
guard let geoJSON = jsonObject as? [String: Any] else {
throw NSError(domain: "MapDataManager", code: 1, userInfo: [NSLocalizedDescriptionKey: "Invalid GeoJSON format"])
}
// Check required properties
guard let type = geoJSON["type"] as? String, type == "FeatureCollection",
let features = geoJSON["features"] as? [[String: Any]] else {
throw NSError(domain: "MapDataManager", code: 2, userInfo: [NSLocalizedDescriptionKey: "GeoJSON must be a FeatureCollection with features"])
}
// Validate each feature
for feature in features {
guard let geometry = feature["geometry"] as? [String: Any],
let coordinates = geometry["coordinates"] as? [Any],
let geometryType = geometry["type"] as? String else {
throw NSError(domain: "MapDataManager", code: 3, userInfo: [NSLocalizedDescriptionKey: "Invalid feature structure in GeoJSON"])
}
// Validate coordinates based on geometry type
try validateCoordinates(coordinates, for: geometryType)
}
// If this is the first file uploaded, make it active by default
let isFirstFile = uploadedFiles.isEmpty