Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding of GeoJSON output #6681

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions spec/suites/layer/GeoJSONSpec.js
Expand Up @@ -471,3 +471,44 @@ describe("L.LayerGroup#toGeoJSON", function () {
});
});
});

describe("Overriding #toGeoJSON formatting", function () {
it("is possible to overwrite output of #toGeoJSON coordinates", function () {
var marker = new L.Marker([-1.4837191022531273, 43.49222084042808]),
layerGroup = new L.LayerGroup([marker]);

expect(layerGroup.toGeoJSON()).to.eql({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [43.492221, -1.483719]
}
}]
});

L.GeoJSON.latLngToCoords = function (latlng, precision) {
if (typeof precision === 'number') {
return latlng.alt !== undefined ?
[L.Util.formatNum(latlng.lng, precision), L.Util.formatNum(latlng.lat, precision), L.Util.formatNum(latlng.alt, precision)] :
[L.Util.formatNum(latlng.lng, precision), L.Util.formatNum(latlng.lat, precision)];
} else {
return latlng.alt !== undefined ? [latlng.lng, latlng.lat, latlng.alt] : [latlng.lng, latlng.lat];
}
};

expect(layerGroup.toGeoJSON()).to.eql({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [43.49222084042808, -1.4837191022531273]
}
}]
});
Comment on lines +477 to +512
Copy link
Collaborator

@johnd0e johnd0e Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var marker = new L.Marker([-1.4837191022531273, 43.49222084042808]),
layerGroup = new L.LayerGroup([marker]);
expect(layerGroup.toGeoJSON()).to.eql({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [43.492221, -1.483719]
}
}]
});
L.GeoJSON.latLngToCoords = function (latlng, precision) {
if (typeof precision === 'number') {
return latlng.alt !== undefined ?
[L.Util.formatNum(latlng.lng, precision), L.Util.formatNum(latlng.lat, precision), L.Util.formatNum(latlng.alt, precision)] :
[L.Util.formatNum(latlng.lng, precision), L.Util.formatNum(latlng.lat, precision)];
} else {
return latlng.alt !== undefined ? [latlng.lng, latlng.lat, latlng.alt] : [latlng.lng, latlng.lat];
}
};
expect(layerGroup.toGeoJSON()).to.eql({
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: [43.49222084042808, -1.4837191022531273]
}
}]
});
var arrLatLng = [-1.4837191022531273, 43.49222084042808];
var arrLngLat = [arrLatLng[1], arrLatLng[0]];
var latLng = L.latLng(arrLatLng);
var latLngToCoords = L.GeoJSON.latLngToCoords; // keep ref to original func
// override with custom function, to prevent latlngs formatting when precision is not specified
// (to avoid round-off errors)
L.GeoJSON.latLngToCoords = function (latlng, precision) {
if (typeof precision === 'number') {
return latLngToCoords.apply(this, arguments);
}
return latlng.alt !== undefined ? [latlng.lng, latlng.lat, latlng.alt] : [latlng.lng, latlng.lat];
};
expect(L.GeoJSON.latLngToCoords(latLng, 6)).to.eql([43.492221, -1.483719]);
expect(L.GeoJSON.latLngToCoords(latLng)).to.eql(arrLngLat);
var marker = new L.Marker(latLng);
expect(marker.toGeoJSON()).to.eql({
type: 'Feature',
properties: {},
geometry: {
type: 'Point',
coordinates: arrLngLat
}
});
L.GeoJSON.latLngToCoords = latLngToCoords;

});
});
4 changes: 2 additions & 2 deletions src/layer/GeoJSON.js
Expand Up @@ -258,7 +258,7 @@ export function latLngsToCoords(latlngs, levelsDeep, closed, precision) {
for (var i = 0, len = latlngs.length; i < len; i++) {
coords.push(levelsDeep ?
latLngsToCoords(latlngs[i], levelsDeep - 1, closed, precision) :
latLngToCoords(latlngs[i], precision));
GeoJSON.latLngToCoords(latlngs[i], precision));
}

if (!levelsDeep && closed) {
Expand Down Expand Up @@ -292,7 +292,7 @@ var PointToGeoJSON = {
toGeoJSON: function (precision) {
return getFeature(this, {
type: 'Point',
coordinates: latLngToCoords(this.getLatLng(), precision)
coordinates: GeoJSON.latLngToCoords(this.getLatLng(), precision)
});
}
};
Expand Down