Merge pull request #381 from Suz/380-add-DMS-map-tests

380 add dms map tests
This commit is contained in:
Rufus Pollock
2013-12-23 06:22:23 -08:00
2 changed files with 39 additions and 6 deletions

View File

@@ -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 // Private: Return a GeoJSON geomtry extracted from the record fields
// //
_getGeometryFromRecord: function(doc){ _getGeometryFromRecord: function(doc){
@@ -337,12 +363,12 @@ my.Map = Backbone.View.extend({
value = $.parseJSON(value); value = $.parseJSON(value);
} catch(e) {} } catch(e) {}
} }
if (typeof(value) === 'string') { if (typeof(value) === 'string') {
value = value.replace('(', '').replace(')', ''); value = value.replace('(', '').replace(')', '');
var parts = value.split(','); var parts = value.split(',');
var lat = parseFloat(parts[0]); var lat = this._parseCoordinateString(parts[0]);
var lon = parseFloat(parts[1]); var lon = this._parseCoordinateString(parts[1]);
if (!isNaN(lon) && !isNaN(parseFloat(lat))) { if (!isNaN(lon) && !isNaN(parseFloat(lat))) {
return { return {
"type": "Point", "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 // We'll create a GeoJSON like point object from the two lat/lon fields
var lon = doc.get(this.state.get('lonField')); var lon = doc.get(this.state.get('lonField'));
var lat = doc.get(this.state.get('latField')); var lat = doc.get(this.state.get('latField'));
lon = this._parseCoordinateString(lon);
lat = this._parseCoordinateString(lat);
if (!isNaN(parseFloat(lon)) && !isNaN(parseFloat(lat))) { if (!isNaN(parseFloat(lon)) && !isNaN(parseFloat(lat))) {
return { return {
type: 'Point', type: 'Point',

View File

@@ -110,10 +110,14 @@ test('GeoJSON geom field', function () {
test('_getGeometryFromRecord non-GeoJSON', function () { test('_getGeometryFromRecord non-GeoJSON', function () {
var test = [ var test = [
[{ lon: 47, lat: 53}, [47,53]], [{ lon: 47, lat: 53}, [47,53]],
[{ lon: -47, lat: 53}, [-47,53]],
["53.3,47.32", [47.32, 53.3]], ["53.3,47.32", [47.32, 53.3]],
["53.3, 47.32", [47.32, 53.3]], ["53.3, 47.32", [47.32, 53.3]],
["(53.3,47.32)", [47.32, 53.3]], ["(53.3,47.32)", [47.32, 53.3]],
[[53.3,47.32], [53.3, 47.32]] [[53.3,47.32], [53.3, 47.32]],
["53.3 N, 113.5 W", [-113.5, 53.3]],
["53° 18' N, 113° 30' W", [-113.5, 53.3 ]],
["22°4590″S, 43°1545″W", [-43.2625, -22.775]]
]; ];
var view = new recline.View.Map({ var view = new recline.View.Map({
model: new recline.Model.Dataset({ model: new recline.Model.Dataset({