Skip to content

Commit

Permalink
L.Proj.GeoJSON supports addData method.
Browse files Browse the repository at this point in the history
  • Loading branch information
perliedman committed Jan 15, 2014
1 parent 4ba2db9 commit f391d06
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 34 deletions.
34 changes: 22 additions & 12 deletions src/proj4leaflet.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,31 @@

L.Proj.GeoJSON = L.GeoJSON.extend({
initialize: function(geojson, options) {
L.GeoJSON.prototype.initialize.call(this, null, options);
this.addData(geojson);
},

addData: function(geojson) {
var crs;
if (geojson.crs && geojson.crs.type === 'name') {
crs = new L.Proj.CRS(geojson.crs.properties.name);
} else if (geojson.crs && geojson.crs.type) {
crs = new L.Proj.CRS(geojson.crs.type + ':' + geojson.crs.properties.code);
}

if (crs !== undefined) {
options = options || {};
options.coordsToLatLng = function(coords) {
var point = L.point(coords[0], coords[1]);
return crs.projection.unproject(point);
};
if (geojson) {
if (geojson.crs && geojson.crs.type === 'name') {
crs = new L.Proj.CRS(geojson.crs.properties.name);
} else if (geojson.crs && geojson.crs.type) {
crs = new L.Proj.CRS(geojson.crs.type + ':' + geojson.crs.properties.code);
}

if (crs !== undefined) {
this.options.coordsToLatLng = function(coords) {
var point = L.point(coords[0], coords[1]);
return crs.projection.unproject(point);
};
} else {
delete this.options.coordsToLatLng;
}
}
L.GeoJSON.prototype.initialize.call(this, geojson, options);

L.GeoJSON.prototype.addData.call(this, geojson);
}
});

Expand Down
85 changes: 63 additions & 22 deletions test/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,16 +257,20 @@ describe('L.Proj.GeoJSON', function() {
'name': 'EPSG:3006'
}
}
}
},
options = {
onEachFeature: function(f, l) {
var ll = l.getLatLng();

L.Proj.geoJson(geojson, {
onEachFeature: function(f, l) {
var ll = l.getLatLng();
expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
};

expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
});
spyOn(options, 'onEachFeature');

L.Proj.geoJson(geojson, options);
expect(options.onEachFeature).toHaveBeenCalled();
});

it('handles legacy CRS', function() {
Expand All @@ -279,32 +283,40 @@ describe('L.Proj.GeoJSON', function() {
'code': 3006
}
}
},
options = {
onEachFeature: function(f, l) {
var ll = l.getLatLng();

expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
};

L.Proj.geoJson(geojson, {
onEachFeature: function(f, l) {
var ll = l.getLatLng();
spyOn(options, 'onEachFeature');

expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
});
L.Proj.geoJson(geojson, options);
expect(options.onEachFeature).toHaveBeenCalled();
});

it('handles missing CRS', function() {
var geojson = {
'type': 'Point',
'coordinates': [11.96526, 57.70451]
},
options = {
onEachFeature: function(f, l) {
var ll = l.getLatLng();

expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
};

L.Proj.geoJson(geojson, {
onEachFeature: function(f, l) {
var ll = l.getLatLng();
spyOn(options, 'onEachFeature');

expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
});
L.Proj.geoJson(geojson, options);
expect(options.onEachFeature).toHaveBeenCalled();
});

it('throws on undefined CRS', function() {
Expand All @@ -321,6 +333,35 @@ describe('L.Proj.GeoJSON', function() {

expect(function() { L.Proj.geoJson(geojson); }).toThrow();
});

it('handles data added with addData', function() {
var geojson = {
'type': 'Point',
'coordinates': [319180, 6399862],
'crs': {
'type': 'name',
'properties': {
'name': 'EPSG:3006'
}
}
},
options = {
onEachFeature: function(f, l) {
var ll = l.getLatLng();

expect(ll.lat).toBeCloseTo(57.70451, 5);
expect(ll.lng).toBeCloseTo(11.96526, 5);
}
},
l;

spyOn(options, 'onEachFeature');

l = L.Proj.geoJson(geojson, options);
l.addData(geojson);

expect(options.onEachFeature).toHaveBeenCalled();
});
});

describe('legacy API', function() {
Expand Down

0 comments on commit f391d06

Please sign in to comment.