Skip to content

Instantly share code, notes, and snippets.

@black-tea
Created December 30, 2015 22:42
Show Gist options
  • Save black-tea/be61f2eeb0932966d8f1 to your computer and use it in GitHub Desktop.
Save black-tea/be61f2eeb0932966d8f1 to your computer and use it in GitHub Desktop.
This script will take the centerline layer, and copy the assetIDs of all intersecting points to the attribute table of the centerline layer. It will also make sure that merged intersections are appropriately accounted for as well.
import arcpy
from arcpy import env
##### Input #####
env.workspace = "Z:/GIS/DataLibrary/Transportation/BOE_Centerline_Intersections150930/collisiontoInt_test.gdb"
env.overwriteOutput = True #so we can rerun this script and overwrite our output feature class
#input fc
col_fc = "Collisions_2009to2013"
input_centerline_fc = "Street_Centerline_Raw"
int_fc = "MergedIntersections"
boe_int_dup = "boe_int_duplicate"
#create layers for analysis
centerline_fc = "Street_Centerline"
boe_int_dup_lyr = "boe_int_dup_lyr" #new feature lyr for duplicate points
arcpy.FeatureClassToFeatureClass_conversion(input_centerline_fc, env.workspace, centerline_fc) #create edited fc
arcpy.MakeFeatureLayer_management(col_fc, "col_lyr")
arcpy.MakeFeatureLayer_management(centerline_fc, "centerline_lyr")
arcpy.MakeFeatureLayer_management(int_fc, "int_lyr")
arcpy.MakeFeatureLayer_management(boe_int_dup, boe_int_dup_lyr) #create a feature layer for duplicate points. export to fc later
##### Create Lists of Merged Intersections #####
AssetID_Replaced = []
CLNodeID_Dropped = []
maxValue = arcpy.SearchCursor(boe_int_dup_lyr, "", "", "", "FEAT_SEQ D").next().getValue("FEAT_SEQ") #Get max FEAT_SEQ
for i in range(1, maxValue + 1): #loop through each FEAT_SEQ, from 1 to the max
query = "\"FEAT_SEQ\" = " + str(i)
print query
arcpy.SelectLayerByAttribute_management(boe_int_dup_lyr, "NEW_SELECTION", query)
rank1 = arcpy.SearchCursor(boe_int_dup_lyr).next().getValue("ASSETID") #get the top ranked feature for each similar intersection
for row in arcpy.SearchCursor(boe_int_dup_lyr):
CLNodeID_Dropped.append(int(row.CL_NODE_ID))
AssetID_Replaced.append(int(rank1))
arcpy.SelectLayerByAttribute_management (boe_int_dup_lyr, "CLEAR_SELECTION") #clear selection so that the conversion includes all rows
##### Create and Populate Intersection Fields #####
arcpy.AddField_management("centerline_lyr", "int1", "LONG", field_length = 10) #add the fields that will store all intersecting assetIDs
arcpy.AddField_management("centerline_lyr", "int2", "LONG", field_length = 10) #especially around freeways, it is sometimes the case that
arcpy.AddField_management("centerline_lyr", "int3", "LONG", field_length = 10) #there are multiple interesction points along a line segment
arcpy.AddField_management("centerline_lyr", "int4", "LONG", field_length = 10)
arcpy.AddField_management("centerline_lyr", "int5", "LONG", field_length = 10)
arcpy.AddField_management("centerline_lyr", "int6", "LONG", field_length = 10)
arcpy.AddField_management("centerline_lyr", "int7", "LONG", field_length = 10)
arcpy.AddField_management("centerline_lyr", "int8", "LONG", field_length = 10)
arcpy.AddField_management("centerline_lyr", "int9", "LONG", field_length = 10)
arcpy.AddField_management("centerline_lyr", "int10", "LONG", field_length = 10)
centerline_cursor = arcpy.UpdateCursor("centerline_lyr")
for row in centerline_cursor:
obj = "OBJECTID=%s" % (row.OBJECTID)
print obj
#select the line segment
arcpy.SelectLayerByAttribute_management("centerline_lyr","NEW_SELECTION",obj)
#select all intersections that intersect with each line segment
arcpy.SelectLayerByLocation_management("int_lyr","INTERSECT","centerline_lyr",selection_type="NEW_SELECTION")
#create a new search cursor to loop through each of the selected intersections
#for each intersection, add a field to the selected centerline feature
int_cursor = arcpy.UpdateCursor("int_lyr")
ct=0
for row2 in int_cursor:
print int(row2.ASSETID)
ct += 1
name = "int%d" % (ct)
print name
row.setValue(name, int(row2.ASSETID))
centerline_cursor.updateRow(row)
print str(int(row2.ASSETID)) + " finished!"
#if the TO/FROM includes cl node id, +1, row.setValue
if int(row.INT_ID_FRO) in CLNodeID_Dropped:
index = CLNodeID_Dropped.index(int(row.INT_ID_FRO))
ct += 1
name = "int%d" % (ct)
row.setValue(name, AssetID_Replaced[index])
centerline_cursor.updateRow(row)
print("REPLACED AN ID!!!")
if int(row.INT_ID_TO) in CLNodeID_Dropped:
index = CLNodeID_Dropped.index(int(row.INT_ID_TO))
ct += 1
name = "int%d" % (ct)
row.setValue(name, AssetID_Replaced[index])
centerline_cursor.updateRow(row)
print("REPLACED AN ID!!!")
del centerline_cursor, int_cursor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment