I'm working on an app where a user can upload a KML file and it renders the paths on screen using Leaflet, toGeoJSON & Angular-Leaflet-Directive.
I use toGeoJSON.kml()
to transform the KML text to a GeoJSON object and then pass it to Leaflet for rendering. This works great if the KML is valid. However, if the KML is malformed, it doesn't work so great. toGeoJSON
continues to render the data into an object, ignoring any errors that occur.
var pathData = toGeoJSON.kml(kmlDom);
var latlng = [];
for (var feature in pathData.features) {
var paths = pathData.features[feature].geometry.coordinates;
if (paths.length > 1) {
for (var path in paths) {
var prospectiveCoords = pathData.features[feature].geometry.coordinates[path];
var coordinate = L.GeoJSON.coordsToLatLng(prospectiveCoords);
latlng.push(coordinate);
}
}
}
try {
map.fitBounds(latlng); //Instance of L.getMap()
angular.extend($scope, {
geojson : {
data : pathData,
style : {
stroke : true,
weight : 5
}
}
});
} catch (e) {
$log.error(e.message);
$window.alert(e.message);
}
In my specific issue, if there is an error in the pathData
variable, it triggers the exception in LatLng line 7:
L.LatLng = function (lat, lng, alt) {
if (isNaN(lat) || isNaN(lng)) {
throw new Error('Invalid LatLng object: (' + lat + ', ' + lng + ')');
}
...
Unfortunately, this error does not get trapped by my catch block and Leaflet continues to render the map tiles without the path overlay, and the only notice the user gets is inside the console. My goal is to have a more visible user notice when an invalid file is uploaded.
Is it possible to intercept the error at the application level?