Hello,
I have a feature class which is represented as lines (power lines). I have another feature class that are points (lights) that are on top of/intersecting with the line.
I need to do this in Arcpy. I believe I need to use either a Spatial Join or Transfer Attributes. Can anyone help me get started or advise what Arcpy tool I need to use? I really appreciate the help.
-JC from Boston
Solved! Go to Solution.
# load the power lines (geometry and year_installed)
power_lines = [row for row in arcpy.da.SearchCursor("path:/to/powerlines", ["SHAPE@", "year_installed"])]
# open an UpdateCursor on the lights with NULL values
with arcpy.da.UpdateCursor("path:/to/lights", ["SHAPE@", "year_installed"], "year_installed IS NULL") as cursor:
for shp, year in cursor:
# sort the power lines by distance to shp
power_lines.sort(key=lambda pl: pl[0].distanceTo(shp))
# use the closest power line's year value
cursor.updateRow([shp, power_lines[0][1]])
How do you want it to resolve ties if the point is snapped to the intersection of two different lines?
# load the power lines (geometry and year_installed)
power_lines = [row for row in arcpy.da.SearchCursor("path:/to/powerlines", ["SHAPE@", "year_installed"])]
# open an UpdateCursor on the lights with NULL values
with arcpy.da.UpdateCursor("path:/to/lights", ["SHAPE@", "year_installed"], "year_installed IS NULL") as cursor:
for shp, year in cursor:
# sort the power lines by distance to shp
power_lines.sort(key=lambda pl: pl[0].distanceTo(shp))
# use the closest power line's year value
cursor.updateRow([shp, power_lines[0][1]])
Thanks for this.. I think this will give me a good starting point!