From b744c88eb04700d2f29014b0093a1c9fe108c291 Mon Sep 17 00:00:00 2001 From: Suz Date: Fri, 20 Dec 2013 17:27:10 -0800 Subject: [PATCH] fix issue 380 -- added function to parse DMS formatted map data (36d 56m 29s S) --- src/view.map.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/view.map.js b/src/view.map.js index b4014123..a7af1fcf 100644 --- a/src/view.map.js +++ b/src/view.map.js @@ -10,7 +10,7 @@ this.recline.View = this.recline.View || {}; // This view allows to plot gereferenced records on a map. The location // information can be provided in 2 ways: // -// 1. Via a single field. This field must be either a geo_point or +// 1. Via a single field. This field must be either a geo_point or // [GeoJSON](http://geojson.org) object // 2. Via two fields with latitude and longitude coordinates. // @@ -326,6 +326,32 @@ my.Map = Backbone.View.extend({ }, + // Private: convert DMS coordinates to decimal + // + // north and east are positive, south and west are negative + // + _parseCoordinateString: function(coord){ + if (typeof(coord) != 'string') { + return(parseFloat(coord)); + } + var dms = coord.split(/[^\.\d\w]+/); + console.log(dms); + var deg = 0; var m = 0; + var toDeg = [1, 60, 3600]; // conversion factors for Deg, min, sec + var i; + for (i = 0; i < dms.length; ++i) { + if (isNaN(parseFloat(dms[i]))) { + continue; + } + deg += parseFloat(dms[i]) / toDeg[m]; + m += 1; + } + if (coord.match(/[SW]/)) { + deg = -1*deg; + } + return(deg); + }, + // Private: Return a GeoJSON geomtry extracted from the record fields // _getGeometryFromRecord: function(doc){ @@ -337,12 +363,12 @@ my.Map = Backbone.View.extend({ value = $.parseJSON(value); } catch(e) {} } - if (typeof(value) === 'string') { value = value.replace('(', '').replace(')', ''); var parts = value.split(','); - var lat = parseFloat(parts[0]); - var lon = parseFloat(parts[1]); + var lat = this._parseCoordinateString(parts[0]); + var lon = this._parseCoordinateString(parts[1]); + if (!isNaN(lon) && !isNaN(parseFloat(lat))) { return { "type": "Point", @@ -370,6 +396,9 @@ my.Map = Backbone.View.extend({ // We'll create a GeoJSON like point object from the two lat/lon fields var lon = doc.get(this.state.get('lonField')); var lat = doc.get(this.state.get('latField')); + lon = this._parseCoordinateString(lon); + lat = this._parseCoordinateString(lat); + if (!isNaN(parseFloat(lon)) && !isNaN(parseFloat(lat))) { return { type: 'Point',