I am pulling my data from an AGOL feature service using the arcgis python api. I pull the data into a spatial dataframe then merge it in various ways in pandas, do various column calculations, move columns around, etc. I just can't figure out how to apply arcgis.geometry.distance or point.distance_to to whole columns in pandas to get a distance between the two columns in feet. The relevant columns of my data are in the following format:

SHAPE_1: {'x': -118.78663214499994, 'y': 46.70276817100006, 'spatialReference': {'wkt': 'GEOGCS["NAD83(NSRS2007)",DATUM["D_",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'}}

SHAPE_2: {'x': -118.78642560599997, 'y': 46.70243596700004, 'spatialReference': {'wkt': 'GEOGCS["NAD83(NSRS2007)",DATUM["D_",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'}}

Distance: ??


Very short code without handling any projection reference, it could start like that:

from shapely.geometry import Point

def distance(shape1, shape2):
    p1 = Point(shape1)
    p2 = Point(shape2)
    dist = p1.distance(p2)
    return dist * 111.132

shp1 = {'x': -118.78663214499994, 'y': 46.70276817100006, 'spatialReference': {'wkt': 'GEOGCS["NAD83(NSRS2007)",DATUM["D_",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'}}
shp2 = {'x': -118.78642560599997, 'y': 46.70243596700004, 'spatialReference': {'wkt': 'GEOGCS["NAD83(NSRS2007)",DATUM["D_",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]]'}}

coord1 = (shp1.get("x"), shp1.get("y"))
coord2 = (shp2.get("x"), shp2.get("y"))

print(distance(coord1,coord2), 'km')
print(distance(coord1,coord2)*1000, 'm')


(0.04347205662169258, 'km')
(43.47205662169258, 'm')

In Pandas, use the distance function with a .apply.

Not the answer you're looking for? Browse other questions tagged or ask your own question.