2

We are migrating existing angular app from OL to Leaflet. In OL we were able to get the geoJSON in UTM with EPSG:3857 on draw finish However when we complete a draw in leaflet and try to get the geometry and geoJSON it gives the data in LatLong format.

This geometry and geoJSON is getting Saved in DB and considers LatLong as UTM due to string format.

this.map.on(L.Draw.Event.CREATED, function (event) 
{
  let layer = event.layer;   
  let drawnGeometry = event.layer.toGeoJSON().geometry
  console.log(drawnGeometry);
  let data = drawnGeometry.coordinates;
  self.Coordinates = data.join(); // coordinates are in LatLong format.
  self.cordiData = JSON.stringify(event.layer.toGeoJSON());
});

Please suggest if there is a way out in leaflet to get the Geometry and geoJSON in UTM format.

  • 1
    Strictly speaking GeoJSON is only one projection WGS 84 in long/lat – nmtoken Nov 4 at 14:45
  • 1
    GeoJSON can be in any CRS and has a "crs" member for exactly that purpose (see the docs). It is not restricted to WGS84 in decimal degrees. The output from Leaflet however, does default to WGS84 - but that's a Leaflet issue and nothing to do with GeoJSON format. – MappaGnosis Nov 4 at 15:01
  • 1
    The latest standard dropped support for projections and axis order – Ian Turton Nov 4 at 16:35
  • There has never been support for different axis order, it has always been longitude-latitude or easting-northing. Current specification does not tell how to support different coordinate systems, actually anything is OK if both sides agree: However, where all involved parties have a prior arrangement, alternative coordinate reference systems can be used without risk of data being misinterpreted. Standardized way for CRS support is probably coming back with an extension to OGC API Features github.com/opengeospatial/ogcapi-features/tree/master/…. – user30184 Nov 4 at 20:28
1

If you really want to have GeoJSON in projected coordinates, you can use Leaflet's L.CRS.EPSG3857.project method for that. Since result of drawing is not some complicated feature collection, you could do conversion simply by looping through coordinates.

It could look something like this (I'm using old fashioned JS):

var projGeoJSON = event.layer.toGeoJSON();
var point;
for (var i = 0; i < projGeoJSON.geometry.coordinates.length; i++) {
  for (var j = 0; j < projGeoJSON.geometry.coordinates[i].length; j++) {
    point = L.CRS.EPSG3857.project(L.latLng(projGeoJSON.geometry.coordinates[i][j]));
    projGeoJSON.geometry.coordinates[i][j] = [point.y, point.x];
  }
}
  • Thanks , This helped ! – user684805 Nov 8 at 13:01
0

That is the default behaviour in leaflet. While GeoJSON can be in any projection (contrary to the comment above), Leaflet expects it to be in EPSG:4326. While there is a Proj4Leaflet plugin, this mostly seems to allow GeoJSON in some other projection (identified by its "crs" member) to be displayed in Leaflet. I don't think it has any facility to change the export behaviour when going to GeoJSON.

There is, however a utility called "reproject" (see its Gitub repository) that you could use to reproject the GeoJSON (remember you CAN use npm client side). Alternatively, if you have GDAL installed server-side you could send the GeoJSON back to the server and re-export it in another projection (but that involves a round-trip of the data, which may be trivial depending on your use-case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.