Hi guys,
I want to calculate distance between polygons and points which are in 2 SHP using "near" function.
in my 2 SHP, each point is related to a specific polygon, there is a link thanks to the attributes.
"Near" function seem to calculate the distance between the closest objects (in my case : point to the closest boundary of the polygon) whereas I just want to calculate for each point distance between this point and the closest boundary of THE polygon with THE SAME ID (where point.polygon_id = polygon.id).
Do you see a simple way to do that in python ?
Solved! Go to Solution.
It's probably doable with some tool (eg GenerateNearTable and then joining the output to the inputs somehow), but you can also rig up a quick script like this:
in_features = "Points" # distance field will be added to this fc
near_features = "Polygons"
in_key = "polygon_id"
near_key = "id"
distance_field = "DISTANCE"
# read near_features as dictionary {key: geometry}
near_geometries = {key: shp for key, shp in arcpy.da.SearchCursor(near_features, [near_key, "SHAPE@"])}
# add field to in_features
arcpy.management.AddField(in_features, distance_field, "DOUBLE")
# calculate
with arcpy.da.UpdateCursor(in_features, [in_key, distance_field, "SHAPE@"]) as cursor:
for key, dist, shp in cursor:
try:
dist = shp.distanceTo(near_geometries[key])
except KeyError:
dist = None
print(f"key {key} could not be found in {near_features}.")
cursor.updateRow([key, dist, shp])
It's probably doable with some tool (eg GenerateNearTable and then joining the output to the inputs somehow), but you can also rig up a quick script like this:
in_features = "Points" # distance field will be added to this fc
near_features = "Polygons"
in_key = "polygon_id"
near_key = "id"
distance_field = "DISTANCE"
# read near_features as dictionary {key: geometry}
near_geometries = {key: shp for key, shp in arcpy.da.SearchCursor(near_features, [near_key, "SHAPE@"])}
# add field to in_features
arcpy.management.AddField(in_features, distance_field, "DOUBLE")
# calculate
with arcpy.da.UpdateCursor(in_features, [in_key, distance_field, "SHAPE@"]) as cursor:
for key, dist, shp in cursor:
try:
dist = shp.distanceTo(near_geometries[key])
except KeyError:
dist = None
print(f"key {key} could not be found in {near_features}.")
cursor.updateRow([key, dist, shp])
Thanks a lot Johannes, I am going to look at your code and try to modify it to my files.
I was also on the way to (try to) use "Distance to" but you have been much quicker 🙂
Hmm, there was a post here this morning, but when I refreshed right now, it disappeared, maybe you deleted it?
Anyway, I remember that there was a problem with the key fields you assigned in your code.
So, just to be clear (which I wasn't in my original answer):
near_features is the polygon fc. It has a field that acts as primary key: it uniquely identifies each polygon (like OBJECTID). Assign this field's name to near_key. In my example script, this field would be Polygons.id
in_features is the point fc. It has a field that links to the primary key of the polygon fc. Assign this field's name to in_key. In my example script, this field would be Points.polygon_id
I think I read "point_id" somewhere in your code, that seemed wrong. Sorry that I wasn't very clear on that point. Feel free to ask questions, of course.
Hi Johannes,
You're right, I deleted the post I wrote too quickly 😉 , sorry...
Since that, I took time to modify & test your arcpy code on my data (and took some vacation also 🏔) and everything works fine now : it does exactly what I want, thanks a lot !!!
Have a great day too !
Maxence