Polygon Neighbors Analysis script (ArcPy 10.1) producing erroneous results?

المشرف العام

Administrator
طاقم الإدارة
I'm struggling with a python script that is supposed to calculate a field for a polygon feature class. The calculation is of the average of a field value (P_TOT_DEV) for each of a given polygon's neighbors.

The script should:

  1. Make a table of each polygon and it's neighbors
  2. Use that table to get each polygon's neighbor's P_TOT_DEV value
  3. Calculate that to a table called P_TOT_DEV_SUM
  4. Use that table to calc to a table called P_TOT_DEV_AVG
  5. Determine each polygon's average of its neighbor's P_TOT_DEV score
  6. Write that back to the original polygon feature class.
This is not a script I wrote.

Here's the whole script:

# NeighborScore.py# Created on: 2013-01-07 13:26:35.00000# Description: Will create output tables based on the average and sum of neighboring polygon values# Updated 2014-03-10# Import arcpy moduleimport arcpy# Script arguments# This is the table that will be joined to the polygon to create the scoring# score_table = arcpy.GetParameterAsText(0)# if score_table == '#' or not score_table:# score_table = "C:\\Project Work\\Squaxin\\Data Recieved From Squaxin\\SquaxinCat.gdb\\NZ"# Join Field for the Score Table and the Polygon Featuressearchfield = arcpy.GetParameterAsText(1)if searchfield == '#' or not searchfield: searchfield = "HydroID"workspace = "\\\\Fs3\\fs\\Campus Share\\GIS\\Dept\\NR\\Current\\LandscapeAnalysis\\Working.gdb"# Polygon Features used in the Neigborhood scoringpolygon_features = arcpy.GetParameterAsText(2)if polygon_features == '#' or not polygon_features: polygon_features = workspace +"\\SSHIAP_Catchment_HARN_1220"# Key Field for the Polygon Table to Join to the score tableneighborhoodkeyfield = arcpy.GetParameterAsText(3)if neighborhoodkeyfield == '#' or not neighborhoodkeyfield: neighborhoodkeyfield = "HydroID"# List of fields to be scored (must be numeric)scorefields = arcpy.GetParameterAsText(4)if scorefields == '#' or not scorefields: scorefields = "P_TOT_DEV"# Swap the ; for a , in the delete fieldsscorefields = scorefields.split(';')# Get the name for the feature classinfeature_name = polygon_features[polygon_features.rindex("\\") + 1:]print infeature_name# inscore_name = score_table[score_table.rindex("\\") + 1:]# print inscore_name# Set Geoprocessing environmentsarcpy.AddMessage("Seting up the input tables")# polygon_features[:infeature_name.rindex("\\")]print workspacef_layer_out = "flayer"polygon_neighbors = workspace + "\\PolygonNeighbors"arcpy.AddMessage(workspace)arcpy.env.workspace = workspacearcpy.env.overwriteOutput = Truefield = "src_" + searchfieldneighborfield = "nbr_" + searchfieldsum_insert = "P_TOT_DEV_SUM"avg_insert = "P_TOT_DEV_AVG"# Process: Make Feature Layerarcpy.MakeFeatureLayer_management(polygon_features, f_layer_out, "", "", "")# Process: Add Join# arcpy.AddMessage("Joining score to neighborhood table")# arcpy.AddJoin_management(f_layer_out, neighborhoodkeyfield, score_table, searchfield, "KEEP_COMMON")# Process: Create Polygon Neighbors Tablearcpy.AddMessage("Creating Polygon Neighborhood Table")arcpy.PolygonNeighbors_analysis(f_layer_out, polygon_neighbors, searchfield, "NO_AREA_OVERLAP", "BOTH_SIDES", "", "FEET", "SQUARE_FEET")# Process: Copyarcpy.AddMessage("Copying score tables")arcpy.Copy_management(polygon_features, avg_insert, "Table")arcpy.Copy_management(polygon_features, sum_insert, "Table")# Process: Delete Rowsarcpy.AddMessage("Cleaning up the score tables")arcpy.DeleteRows_management(avg_insert)arcpy.DeleteRows_management(sum_insert)# List the Fields for processingfields = arcpy.ListFields(sum_insert)# Determine the Fields to Deletedeletefields = []for infield in fields: if not infield.name.upper() in scorefields and infield.name.upper() searchfield.upper(): print infield.name if infield.type == "OID" or infield.name == "Shape" or infield.name == "Shape_Length" or "Shape_Area": continue else: deletefields.append(infield.name)print deletefields# Clean up the extra fields we do not want here# arcpy.DeleteField_management(sum_insert, deletefields)# arcpy.DeleteField_management(avg_insert, deletefields)# Cursor through the neighborhood tablearcpy.AddMessage("Starting the Scoring System")cursor = arcpy.SearchCursor(polygon_neighbors)changeid = -99score = 0rowcount = 0average = 0for row in cursor: key_id = row.getValue(field) # shape = row.getValue("SHAPE") arcpy.AddMessage("Processing Row....") print key_id if changeid key_id: if changeid -99: try: # Insert the score values print score insert_cur = arcpy.InsertCursor(sum_insert) insert_avg_cur = arcpy.InsertCursor(avg_insert) insert_row = insert_cur.newRow() insert_avg_row = insert_avg_cur.newRow() insert_row.setValue(searchfield,key_id) # insert_row.setValue("SHAPE",shape) insert_avg_row.setValue(searchfield,key_id) # insert_avg_row.setValue("SHAPE",shape) for key, value in list.iteritems(): if key searchfield: insert_row.setValue(key.upper(),value) insert_avg_row.setValue(key.upper(),value / rowcount) insert_cur.insertRow(insert_row) insert_avg_cur.insertRow(insert_avg_row) del insert_cur, insert_avg_cur except Exception as err: print err.message # Track the neighborhood blocks changeid = key_id score = 0 rowcount = 0 list = {} for lookfield in scorefields: if(lookfield.upper() searchfield.upper()): list[lookfield.upper()] = 0 # Select the appropriate neighborhood fields print searchfield + "," + neighborfield where = "\"" + searchfield + "\"" + " = " + str(row.getValue(neighborfield)) print where score_table_search = arcpy.SearchCursor(polygon_features,where) for score_table_row in score_table_search: for lookfield in scorefields: if(lookfield.upper() searchfield.upper()): list[lookfield.upper()] = list[lookfield.upper()] + score_table_row.getValue(lookfield.upper()) arcpy.AddMessage(list) # Look through for all the doubles and calculate the scores rowcount = rowcount + 1 del score_table_search del cursor## Do not delete while testing/review# Perform some cleanup# if arcpy.Exists(polygon_neighbors):# Execute Delete# arcpy.Delete_management(polygon_neighbors, "")The script runs without crashing but, it appears;

  1. the first polygon of the PolygonNeighbors table is not being calculated
  2. fact the that first item/row in the table is not being calculated then throws off all the following calculations. It appears that the calculations (SUM & AVG) are being written to the wrong (+1) polygon. So, poly B is getting poly A's correct results. Poly C is getting B's. Poly A does not make it from the PolygonNeighbors table to the SUM & AVG tables.
The particulars that are confusing me are:

  1. line 44: infeature_name = polygon_features[polygon_features.rindex("\") + 1:]confused as to what infeature_name would be after this (thur each iteration of the loop)
  2. line 90: the delete_fields list appears to be emptyIs that right?

أكثر...
 
أعلى