Arcpy update NULL values in point table by copying value from nearest line table

640
3
Jump to solution
05-03-2022 02:48 PM
JohnConnor01
New Contributor II

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. 

  • The light table has NULL values for year_installed
  • For the light that the power line is on top of/intersecting with, I want to copy the nearest power lines year_installed value into the lights field where the lights year_installed value is NULL.

 

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

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor
# 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]])

Have a great day!
Johannes

View solution in original post

3 Replies
BlakeTerhune
MVP Regular Contributor

How do you want it to resolve ties if the point is snapped to the intersection of two different lines?

JohannesLindner
MVP Frequent Contributor
# 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]])

Have a great day!
Johannes
JohnConnor01
New Contributor II

Thanks for this.. I think this will give me a good starting point!

0 Kudos