Sunday 21 October 2018

arcpy - Fastest way to numerate intersecting polygons in ArcGIS


I need to numerate a thousands of polygons in groups, so each group will get unique number. Every intersecting polygons is unique group (see picture).


So polygons have field "groups" with values:


polygons 1,2,3 is group 1;



polygons 4,5 is group 2;


polygons 6,7,8,9 is group 3 and so on...


enter image description here


My current approach is very slow it takes about 5 minutes to enumerate layer with about 10 000 polygons (it is 4 100 groups).


Is there any faster way to do this?


Here is what I do:



  1. Dissolve all polygons - that is my way to find groups (arcpy.Dissolve_management)

  2. Give unique numbers to every polygon in dissolved layer (arcpy.CalculateField_management)

  3. Iterate every feature in dissolved layer (arcpy.SelectLayerByAttribute_management)


  4. Select polygons that intersect with original (not dissolved) layer.

  5. Give them group number (arcpy.CalculateField_management)


Here is a part of the code:


#1
arcpy.Dissolve_management(fc, dissolve_shp, "", "", "SINGLE_PART")

#2
arcpy.CalculateField_management(dissolve_shp, "ID", '!FID!+1', "PYTHON")


#3
number_of_rows = arcpy.GetCount_management(dissolve_shp)
number_of_rows = int(number_of_rows.getOutput(0))
lyr_dissolve = r"in_memory\Temporary_layer_DISSOLVE_DEL"
lyr_merge = r"in_memory\Temporary_layer_MERGE_DEL"
arcpy.MakeFeatureLayer_management(dissolve_shp, lyr_dissolve)
arcpy.MakeFeatureLayer_management(fc, lyr_merge)

for i in range(number_of_rows):
i2=i+1

if float(i2)//100 == float(i2)/100:
print i2, "of", number_of_rows

arcpy.SelectLayerByAttribute_management (lyr_dissolve, "NEW_SELECTION", '"ID" = '+str(i2))

#4
arcpy.SelectLayerByLocation_management (lyr_merge, "INTERSECT", lyr_dissolve)

#5
arcpy.CalculateField_management(lyr_merge, "TERM_P_NUM", i2, "PYTHON")



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...