0

I've used polygon neighbors to find all neighbors of a polygon layer, which I then use to look up all possible neighbor combinations and dissolve the polygons into all possible combinations of two polygons. I'd then like to run this again and build all possible three-polygon combinations.

However, when I run the tool again on the dissolved polygons layer it creates an empty table. I know the data is fine - is there another step i need to take, or is there another tool I could use to achieve this? Thanks in advance.

EDIT:

This is the script that i use after running the polygon neighbors tool to create the dissolves and then append them to 'Sites'. I then manually run polygon neighbors again on 'Sites', but get an empty table:

arcpy.env.workspace = "C:\Users\Ant\Documents\ArcGIS\ITN.gdb"
FC = "BLPUs"
TABLE = "BLPUs_PolygonNeighbors"
DISSOLVED = "Sites"
FIELDS = ["src_OBJECTID", "nbr_OBJECTID"]
with arcpy.da.SearchCursor(TABLE, FIELDS) as cursor:
    for row in cursor:
        OID1 = str(int(row[0]))
        OID2 = str(int(row[1]))
        arcpy.MakeFeatureLayer_management(DISSOLVED, "DISS_LYR")
        OUTPUT_NAME = "Dissolved_%s_%s"%(OID1, OID2)
        SQL = "OBJECTID = %s OR OBJECTID = %s"%(OID1, OID2)
        arcpy.SelectLayerByAttribute_management("DISS_LYR", "NEW_SELECTION", SQL)
        arcpy.Dissolve_management("DISS_LYR", "DISS_OUT")
        expression1 = arcpy.GetCount_management("DISS_LYR")
        fieldName1 = "BLPUCount"
        fieldName2 = "OID1"
        fieldName3 = "OID2"
        fieldName4 = "OID3"
        fieldName5 = "OID4"
        fieldName6 = "OID5"
        arcpy.AddField_management("DISS_OUT", fieldName1, "LONG")
        arcpy.AddField_management("DISS_OUT", fieldName2, "LONG")
        arcpy.AddField_management("DISS_OUT", fieldName3, "LONG")
        arcpy.AddField_management("DISS_OUT", fieldName4, "LONG")
        arcpy.AddField_management("DISS_OUT", fieldName5, "LONG")
        arcpy.AddField_management("DISS_OUT", fieldName6, "LONG")
        arcpy.CalculateField_management("DISS_OUT", "BLPUCount", expression1)
        arcpy.CalculateField_management("DISS_OUT", "OID1", OID1)
        arcpy.CalculateField_management("DISS_OUT", "OID2", OID2)
        arcpy.CopyFeatures_management("DISS_OUT", OUTPUT_NAME)
        arcpy.Delete_management("DISS_OUT", "DISS_LYR")

fcList = arcpy.ListFeatureClasses("Dissolved*")
for fc in fcList:
    arcpy.Append_management(fc, DISSOLVED, "", "", "")
  • Are you running a script that you can share? Also, I suggest manually processing a small portion of your data to be sure you are using the correct tools and correct fields. Sometimes, I mysteriously end up with an empty feature class when I stop an edit session after running tools on data in the edit workspace or folder. – klewis Aug 20 '13 at 15:19
  • @user2581350 - can you edit your question with the script you tried to add to the answer below (that I've moved). – Ian Turton Aug 20 '13 at 15:51
  • my bad. i'm currently only using 4 adjacent polygons to get it working... – user2581350 Aug 20 '13 at 16:00
  • Found the answer - I had to Report by Fields and select both OBJECTID and BLPUCount, then it worked fine – user2581350 Aug 20 '13 at 18:39
  • My suggestion was going to be possible you field names changed in the previous steps. @user2581350 please post your answer and check it off as correct. – Brad Nesom Aug 21 '13 at 4:53
2

A few notes....

  1. You shouldn't have to use both MakeFeatureLayer() and SelectLayerByAttribute(). You can pass a third, optional parameter to MakeFeatureLayer() that uses a SQL statement to select a subset. See the help.
  2. You should delete your layer after you are done using it so that way it won't exist for the next iteration of the loop.
  3. If you are looking to populate the field with GetCount(), you need to extract the number as it returns a result object and not the actual integer. See the help. This might be part of your problem.

    SQL = "OBJECTID = %s OR OBJECTID = %s"%(OID1, OID2)
    arcpy.MakeFeatureLayer_management(DISSOLVED, "DISS_LYR", SQL)
    arcpy.Dissolve_management("DISS_LYR", "DISS_OUT")
    expression1 = int(arcpy.GetCount_management("DISS_LYR").getOutput(0))
    if arcpy.Exists("DISS_LYR"):
        arcpy.Delete_management("DISS_LYR")
    
0

Adding the Report By fields as below worked:

arcpy.PolygonNeighbors_analysis(r"Sites", "Sites_Neighbors", [r"OBJECTID", r"BLPUCount"])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.