# Shapely flips lat/long coordinate

I have a pandas dataframe containing a Shapely geometry object. The shapely object (circle) was computed from the following SO https://stackoverflow.com/questions/27431528/find-the-intersection-between-two-geographical-data-points, where I have lat, long, and radius as input.

``````import pandas as pd
from shapely.geometry import Point
from shapely.ops import transform
import pyproj
from functools import partial

WGS84 = pyproj.Proj('epsg:4326')

def latlonbuffer(lat, lon, radius_m):
proj4str = '+proj=aeqd +lat_0=%s +lon_0=%s +x_0=0 +y_0=0' % (lat, lon)
AEQD = pyproj.Proj(proj4str) # azimuthal equidistant
project = partial(pyproj.transform, AEQD, WGS84)
return transform(project, Point(0, 0).buffer(radius_m))
``````

For example, using the above function, I created a table like below, with an additional centroid column

``````geometry, centroid
POLYGON((26.48306 50.09625, 26.47916 50.09604, ..)), ((26.48307336330026, 50.052005610561245))
``````

But when I convert this table to geopandas and plot it on mplleaflet

``````import geopandas as gpd
import mplleaflet
import matplotlib.pyplot as plt

df_gpd = gpd.GeoDataFrame(df)
ax = df_gpd.plot(figsize=(20,20), color='r')

mplleaflet.display(fig=ax.figure)
``````

it displays the polygon in a different location than expected.

Below is what's plotted And this is the location of the expected circle centroid I figured out that even though the Polygon shows the coordinates in (lat, long) format, when mplleaflet is plotting, it swaps the coordinates to (long, lat).

Is Polygon object holding the coordinates in (long, lat) format, and mplleaflet is flipping the coordinates to properly plot it on the map?

The result I'm expecting is a circle centered around the centroid (26.48307336330026, 50.052005610561245) which is in (lat, long) format.

tldr: I would like to know if mplleaflet flips the coordinates to (long, lat) when plotting Polygon object in Geopandas, or the Polygon object using the function `latlonbuffer()` results in (long, lat) format.

Update:

The problem was with pyproj flipping the coordinates while transforming. We can keep the Polygon object to the GIS standard (long, lat) by adding always_xy=True in the transform parameter.

``````import pandas as pd
from shapely.geometry import Point
from shapely.ops import transform
import pyproj
from functools import partial

WGS84 = pyproj.Proj('epsg:4326')

def latlonbuffer(lat, lon, radius_m):
proj4str = '+proj=aeqd +lat_0=%s +lon_0=%s +x_0=0 +y_0=0' % (lat, lon)
AEQD = pyproj.Proj(proj4str) # azimuthal equidistant
project = partial(pyproj.transform, AEQD, WGS84, always_xy=True)
return transform(project, Point(0, 0).buffer(radius_m))
``````

Using this yields

``````POLYGON((50.09625 26.48306, 50.09604 26.47916, ..))
``````

which can be directly plotted using mplleaflet without flipping the coordinates.

• There is a GDAL ticket github.com/OSGeo/gdal/issues/2979 with discussion about axis order in WKT. I would suggest to build your polygon WKT in traditional GIS style (long-lat or easting-northing). It may not be correct, but it is the more interoperable alternative. – user30184 Oct 16 '20 at 6:29
• you could add your "update" as an answer to your own question, because it solves it. – radouxju Mar 26 at 8:48

## 1 Answer

I believe that the issue is due to axis order changes in PROJ:

https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6

• I am not sure if anything has changed with interpretation of the axis order of WKT. Did you test that result with older Proj is what OP awaits? – user30184 Oct 16 '20 at 7:23
• Thanks, I looked through the Proj discussions and I think the correct way is to have always_xy=True when doing the transformation. This would keep the GIS convention of (long, lat) in the polygon object, and I can plot it without flipping the coordinates. – igeass Oct 16 '20 at 8:29
• Yes, I am pretty sure that would resolve your issue. Also see: pyproj4.github.io/pyproj/stable/… – snowman2 Oct 16 '20 at 12:36