Leaflet: Switch to Longitude-Latitude for coordinates?

Created on 25 Feb 2013  ·  22Comments  ·  Source: Leaflet/Leaflet

In quite a few places I'm looking at APIs that are built for GeoJSON input and output and having to flip coords and reconstruct bounding boxes for Leaflet to read. Changing Leaflet's APIs to all do things the GeoJSON way would be (wonderful but) a big, breaking change. Having them all autodetect would be error-prone, and having an is_geojson argument to lots of functions would be lame.

Maybe GeoJSON everywhere is something to think about in 1.0.0, and a global flag, L.GEOJSON_API might be a better near-term goal for the [lat, lon] shortcut to be [lon, lat], for toJSON methods to return GeoJSON, and for bounds to be represented and usable as GeoJSON bounds.

feature

Most helpful comment

Just for reference, since this issue seems to become the main justification:
https://macwright.org/lonlat/
screenshot_20180429_225744

Which is right?
Neither. This is an opinion with no right answer.
Geographical tradition favors lat, lon. Math and software prefer lon, lat.

All 22 comments

That indeed is a problem. I spent some serious time trying to debug my map app just to realize that switching coordinates made it all work. Maybe it should be stated explicitly in the documentation about GeoJSON (tutorial and API), because I'm sure I was not the only one running into that issue ;-)

+1 this is a big pet peeve of mine

+1 this just cost me a solid 2 hours of debugging.

+1 2 hours of lost time here as well

+1 currently this is not very consistent

Added GeoServer to the list:

[longitude, latitude]-order: OpenLayers, MapboxGL, KML, GeoJSON, PostGIS, MongoDB, MySQL, GeoServer
[latitude, longitude]-order: Leaflet, Google Maps API, ArangoDB.

For reference, Redis new GEO commands are on first group too: http://redis.io/commands/geoadd

:+1:

+1 switching between PostGIS and Leaflet is always a lot of work

This issue is 3 years old and Leaflet is an incredibly established project. The inconsistency in the larger geo world is unfortunate, but unless @mourner and the maintainers feel like it's worthwhile to spend probably quite a bit of time implementing this change, it would do more harm than good.

Has anyone found a solution for this problem?

Switching log, lat in Geojson to lat, lon for import to Leaflet?
I built a Leaflet map of Wilmette, IL. I want to draw the town border. The geojson from https://mapzen.com/data/metro-extracts/ has the coords as log, lat.

Looking around at code samples and plugIns I couldn't find a log, lat flopper.

Any ideas are well appreciated!

@lukedohner use the builtin GeoJSON layer, it works out of the box with coordinates that follow the GeoJSON spec (lon/lat).

If you _really_ need to flip the axis, there are several ways to go about it, one is to use reproject's reverse function.

@perliedman thanks for the info =)

Just for reference, since this issue seems to become the main justification:
https://macwright.org/lonlat/
screenshot_20180429_225744

Which is right?
Neither. This is an opinion with no right answer.
Geographical tradition favors lat, lon. Math and software prefer lon, lat.

@ghybs Thanks for the overview.

In another life I was a carpenter, that is building with wood. ;)
In that field, measurements are usually given as height x width.
The standard size for an exterior door is 80 inches by 36 inches which is 6
ft, 8 inches by 3 ft. 96 inches or 8 ft.

In programming, it is usually width x height
and x cords by y cords.

Seems similar to the lat, log - log, lat input order.

On Sun, Apr 29, 2018 at 4:02 PM, Daniel Ruf notifications@github.com
wrote:

Thanks for the overview.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Leaflet/Leaflet/issues/1455#issuecomment-385277388,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFYxwx_jX0PU69VnBsdMuPEVsfFSjx9_ks5tthw_gaJpZM4AdMRL
.

--

Luke Dohner
Web Developer and Creative Technologist
NY, NY

Please see my loop for swapping geojson latitudes and latitudes:

https://gis.stackexchange.com/questions/133059/export-to-geojson-coordinates-in-the-reverse-order/286219#286219

It won't give you a complete geojson object but it will get you the coordinates.
This worked for me.

Just a quick comment because it gave me so many headaches. I was trying to add geojson into Leaflet and I thought I had to use a loop to swap the coordinates and put them into L.polygons. But the only real problem was that I was using L.geoJSON(geojson).addTo(map) instead of L.geoJson(geojson).addTo(map) to bring the shape into the map. This is because the syntax is different between the two versions.

@hjrobinson that's good to know! But ugh... what a terrible design pattern...

Also, if reprojecting to WGS 84 (EPSG 4326) doesn't work (I know no one asked but I'll mention it anyway), if you have a file with a native projection such as stateplane or user defined, in QGIS you may have to enter an edit session, select all the features in the attribute table, copy the features, and paste as a new vector file/layer. Then be sure to save the new layer as a shapefile, otherwise certain geometry processes such as making centroids won't work. Make sure project is set to the WGS 84 before doing any of this. You can test the result in geojson.io online for accuracy.

Hi @hjrobinson,

It looks like you are still working with Leaflet 0.7.x or lower, where the syntax was indeed L.geoJson.

Leaflet 1.0.0 was released on Sep 2016, and the syntax was changed to L.geoJSON for consistency, but backwards compatibility with L.geoJson was maintained. See https://github.com/Leaflet/Leaflet/issues/2444 for rationale.
Current stable version is 1.3.1.

Please make sure to use the proper documentation page given the version you are using.
If possible you should consider upgrading.

As for coordinates, as highlighted above and as you have noticed, the L.geoJSON / L.geoJson factory will happily parse GeoJSON compliant data, i.e. with [longitude, latitude] order, and automatically switch the coordinates internally when building Leaflet layers out of it.

Maybe the documentation could make this point even clearer.

I apologize for being unclear. I'm still working in an earlier version of leaflet because I have need of geojson-vt. The vector-grid plugin seems to have some performance issues by comparison. But I could be wrong about this too. Thanks for the feedback. I had data that belonged in the U.S. that
was ending up in the ocean off the coast of Africa or in China. I had gotten the data from the city where I live. When I got it fixed I wanted to help anyone who had the same problem. I've only been using Leaflet for few months. I'll be more thorough next time.

On Thu, Jun 28, 2018 at 6:12 PM, ghybs notifications@github.com wrote:

Hi @hjrobinson https://github.com/hjrobinson,

It looks like you are still working with Leaflet 0.7.x or lower, where the
syntax was indeed L.geoJson
https://leafletjs.com/reference-0.7.7.html#geojson.

Leaflet 1.0.0 https://github.com/Leaflet/Leaflet/releases/tag/v1.0.0
was released on Sep 2016, and the syntax was changed to L.geoJSON
https://leafletjs.com/reference-1.3.0.html#geojson for consistency, but
backwards compatibility with L.geoJson was maintained. See #2444
https://github.com/Leaflet/Leaflet/issues/2444 for rationale.
Current stable version is 1.3.1.

Please make sure to use the proper documentation page given the version
you are using.
If possible you should consider upgrading.

As for coordinates, as highlighted above
https://github.com/Leaflet/Leaflet/issues/1455#issuecomment-313685187
and as you have noticed, the L.geoJSON / L.geoJson factory will happily
parse GeoJSON compliant data
https://tools.ietf.org/html/rfc7946#section-3.1.1, i.e. with [longitude,
latitude] order, and automatically switch the coordinates internally when
building Leaflet layers out of it.

Maybe the documentation could make this point even clearer.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/Leaflet/Leaflet/issues/1455#issuecomment-401218281,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AcRaE1ffpG2zQWexqU9PN4E462dq6pcMks5uBX7vgaJpZM4AdMRL
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

piehei picture piehei  ·  3Comments

timwis picture timwis  ·  3Comments

JonnyBGod picture JonnyBGod  ·  4Comments

onethread picture onethread  ·  3Comments

brambow picture brambow  ·  3Comments