12

I'm using Leaflet to transform some drawn polygons to GeoJSON and send them to a backend. But the GeoJSON objects generated by layer.toGeoJSON are like this:

{
 "type":"Feature",
 "properties":{},
 "geometry":{
    "type":"Polygon",
    "coordinates":[[[18.69873046875,51.481382896100975],[18.69873046875,51.890053935216926],[20.456542968749996,51.890053935216926],[20.456542968749996,51.481382896100975],[18.69873046875,51.481382896100975]]]
  }
 }

When I receive that GeoJSON object in the backend of my application (Django based) and create a GEOS object from it, epsg:4326 is assumed. Even when epsg:3857 is used for the map.

I know GeoJSON spec supports CRS definition, but I don't know how to include it in the GeoJSON object generated by toGeoJSON.

Any suggestion?

3 Answers 3

11

You can just set the crs field:

object.crs =  {
  "type": "name",
  "properties": {
    "name": "epsg:3857"
    }
  }

Note however that the coordinates you give in your example really are epsg:4326 coordinates, which is also what leaflet would output by default. I don't think you need a change here.

3
  • Thanks! You're right. For any reason, I was thinking about epsg:3857 as a not projected CRS, what obviously isn't. Those coords are 4326, of course. Silly me! Commented Dec 17, 2013 at 18:43
  • 10
    As this reply just got an upvote, note 4 years later that setting a crs is no longer part of the geojson standard. You should really consider working only in epsg:4326 all the time.
    – johanvdw
    Commented Oct 4, 2017 at 19:00
  • Thanks! So, this isn't the valid response anymore. Commented Oct 5, 2018 at 11:06
9

This is a link to the comment made by @johanvdw saying the CRS for all GeoJSON is epsg:4326 (WGS84) https://www.rfc-editor.org/rfc/rfc7946#section-4

0

I am not an expert but I would like to share what I did to tackle it. Since GeoJSON is basically a string, I added a string specifying the CRS (3857 in this example) manually. Might be useful to someone.

json = geo_df.to_json()
attr = grid_out.split(',', 1)
json_with_crs = attr[0] + ', "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:EPSG::3857" } },' + attr[1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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