I am trying to calculate the distance (in meters) between 2 lon/lng points of type shapely.geometry.point.Point using Python and Shapely. These 2 points are returned from PostGIS by SQLAlchemy/Geoalchemy2 as geoalchemy2.elements.WKBElement, which is then converted to shapely's shapely.geometry.point.Point using geoalchemy2.
However, when I try to use the distance function to calculate the distance in meters, the result is expected to be around 1490 m, but the code calculated it to be 0.013402616901217364.
What went wrong with our code?
Note: Distance calculations will not exceed 30 km
from geoalchemy2.shape import to_shape
users = db_session().query(User).limit(2).all()
foo, bar = users[0], users[1]
print(type(foo.coordinates)) # <class 'geoalchemy2.elements.WKBElement'>
print(type(to_shape(foo.coordinates))) # <class 'shapely.geometry.point.Point'>
distance = to_shape(foo.coordinates).distance(to_shape(bar.coordinates))
print(distance) # 0.013402616901217364
WKBElementoriginally returned by Geoalchemy? – Nyxynyx Jan 23 '21 at 16:50geopandasorpyproj(which is what geopandas uses). For examplepyproj.Geod.invwill solve your problem, I think. – Jeremiah England Feb 03 '21 at 22:56