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
commit b800297dc1
2 changed files with 39 additions and 6 deletions

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

View File

@ -110,10 +110,14 @@ test('GeoJSON geom field', function () {
test('_getGeometryFromRecord non-GeoJSON', function () {
var test = [
[{ 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], [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({
model: new recline.Model.Dataset({
@ -219,7 +223,7 @@ test('geoJsonLayerOptions', function () {
model: dataset
});
$('.fixtures').append(view.el);
view.geoJsonLayerOptions.point
view.geoJsonLayerOptions.point
view.geoJsonLayerOptions.pointToLayer = function(feature, latlng) {
var marker = new L.CircleMarker(latlng, { radius: 8 } );
marker.bindPopup(feature.properties.popupContent);