From 24295e78a2800e1c910c3c85a03a10bcea3eda87 Mon Sep 17 00:00:00 2001 From: Rufus Pollock Date: Sat, 21 Apr 2012 23:52:22 +0100 Subject: [PATCH] [view/map][s]: make map view much more robust regarding bad or missing data in location fields. --- src/view-map.js | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/view-map.js b/src/view-map.js index a6361d75..9553b9e3 100644 --- a/src/view-map.js +++ b/src/view-map.js @@ -256,9 +256,12 @@ my.Map = Backbone.View.extend({ if (!(docs instanceof Array)) docs = [docs]; + var count = 0; + var wrongSoFar = 0; _.every(docs,function(doc){ + count += 1; var feature = self._getGeometryFromDocument(doc); - if (typeof feature === 'undefined'){ + if (typeof feature === 'undefined' || feature === null){ // Empty field return true; } else if (feature instanceof Object){ @@ -275,16 +278,20 @@ my.Map = Backbone.View.extend({ feature.properties.cid = doc.cid; try { - self.features.addGeoJSON(feature); + self.features.addGeoJSON(feature); } catch (except) { - var msg = 'Wrong geometry value'; - if (except.message) msg += ' (' + except.message + ')'; + wrongSoFar += 1; + var msg = 'Wrong geometry value'; + if (except.message) msg += ' (' + except.message + ')'; + if (wrongSoFar <= 10) { my.notify(msg,{category:'error'}); - return false; + } } } else { - my.notify('Wrong geometry value',{category:'error'}); - return false; + wrongSoFar += 1 + if (wrongSoFar <= 10) { + my.notify('Wrong geometry value',{category:'error'}); + } } return true; }); @@ -317,13 +324,17 @@ my.Map = Backbone.View.extend({ return doc.attributes[this.state.get('geomField')]; } else if (this.state.get('lonField') && this.state.get('latField')){ // We'll create a GeoJSON like point object from the two lat/lon fields - return { - type: 'Point', - coordinates: [ - doc.attributes[this.state.get('lonField')], - doc.attributes[this.state.get('latField')] - ] - }; + var lon = doc.get(this.state.get('lonField')); + var lat = doc.get(this.state.get('latField')); + if (lon && lat) { + return { + type: 'Point', + coordinates: [ + doc.attributes[this.state.get('lonField')], + doc.attributes[this.state.get('latField')] + ] + }; + } } return null; }