Friday, 18 May 2018

Leaflet GeoJSON with projected coordinates (3857)


I am trying to get a GeoJSON with projected coordinates to show in Leaflet.


I have no problems loading GeoJSON with lat/lng (EPSG:4326). Here is a fiddle where I load the GeoJSON with unprojected lat/lng.


I am using the Leaflet 1.0.0-beta.2 (954e83d) in the fiddle but I am happy to use another version.


I am aware of the coordsToLatLng method of L.geoJson but no idea how to make this work.



This is the GeoJSON with the projected (EPSG:3857) coordinates.


{
"type": "FeatureCollection",
"crs": {
"type": "name",
"properties": {
"name": "urn:ogc:def:crs:EPSG::3857"
}
},
"features": [{

"type": "Feature",
"properties": {

},
"geometry": {
"type": "Polygon",
"coordinates": [[
[-625570.6, 6465993.0],
[-305006.9, 6696510.8],
[-546330.2, 6768547.6],

[-445478.6, 7053093.0],
[-279794.0, 7056694.8],
[-359034.5, 7337638.4],
[-211359.0, 7485313.8],
[62380.8, 6955843.3],
[220861.7, 6909019.4],
[91195.5, 6700112.6],
[213658.1, 6682103.4],
[-625570.6, 6465993.0]]]
}

}]
}

enter image description here


Notes: I am new to Leaflet. I come from a GIS background. I have a very good understanding of projections and transformations. I have a very good understanding of OpenLayers3. I use OL3 to do things such as this on a daily basis. The data in this post is obviously just an example. I am trying to learn Leaflet in order to compare the performance with OL3. I know how to use QGIS and can unproject the data manually but this is not the point of this post.


This is not a repost. I have checked every bit of information on coordsToLatLng but didn't find any good documentation and nothing like a working example. Certainly no fiddle.


Just saying this as I have read a lot of Leaflet posts on gis.stackexchange with half-a-code. And a lot of answers which suggest to change the data. I know that Leaflet is not a GIS system and the makers are not from a GIS background. In the last hour I have just read a lot of rubbish about Leaflet using EPSG:3857 as default SRS and WGS84 being an SRS.



Answer



One option is to use Proj4Leaflet to read the projected GeoJSON. If you have a variable called sampleProjected referencing your GeoJSON (and assuming you are using a projection that is defined in Proj4, as EPSG:3857 is), you could just add it like so:


L.Proj.geoJson(sampleProjected).addTo(map);


Here is an example comparing your projected data (in blue) with the unprojected data (dashed, in white):


http://jsfiddle.net/nathansnider/ru7oyquq/


There is a small amount of offset, but on inspection, it is unclear whether this is due to the rounding of your unprojected sample or a problem with the proj4 implementation. The magnitudes and directions of the offsets are not uniform, so it may just be rounding. Projections can also be added or modified using the proj4.defs method.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...