[GIS] Working with geographic coordinates in shapely

coordinate systempythonshapely

I have a very basic question about working with the shapely module in Python. I have some geographic coordinates received from a GPS. I would like to use shapely to calculate the great cicle distance in meters between two points. I started with:

>>> from shapely.geometry import Point
>>> p1 = Point(43.374880, -78.119956)
>>> p2 = Point(43.374868, -78.119666)

I believe that this gives me two points in the cartesian coordinate system, which isn't going to be very useful. These coordinates come from a GPS which presumably is using the WGS84 CRS. I have seen some example using pyproj and shapely.ops.transform to transform between coordinate systems, but these all seem to involve data points that are already in some valid cartographic coordinate system with an EPSG identifier. I'm not sure what do with naive points like the above that don't have an associated geographic CRS.

How do associate these points with the WGS84 CRS such that p1.distance(p2) will yield the distance in meters? Am I using the wrong tools?

Best Answer

The problem you're trying to solve is the Inverse Geodesic Problem. Happily the Python geographiclib can do this for you:

from geographiclib.geodesic import Geodesic

p1_lat, p1_lon = 43.374880, -78.119956
p2_lat, p2_lon = 43.374868, -78.119666
geod = Geodesic.WGS84

# note the Inverse method expects latitude then longitude
g = geod.Inverse(p1_lat, p1_lon, p2_lat, p2_lon)

print("Distance is {:.2f}m".format(g['s12']))

Giving the result:

Distance is 23.54m

That said, given your points are likely to be only 10s of metres apart, then working in projected units will likely be much easier.