fix issue 380 -- added function to parse DMS formatted map data (36d 56m 29s S)

This commit is contained in:
Suz 2013-12-20 17:27:10 -08:00
parent 27627c45d9
commit b744c88eb0

View File

@ -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',