AnsweredAssumed Answered

Mesuring Percentage of Feature Polygons that Overlap Each Other in Python

Question asked by dji43466 on Feb 13, 2015
Latest reply on May 4, 2016 by cadd-co

Hello,

 

I'm trying to gather preliminary census data within a 15 minute walkshed and a 15 min bikeshed boundary that we have drawn around a development site.  As expected, neither of the "shed" layers fit perfectly into the Census Block Groups (CBGs) we have.  Now I know this is far from scientific, but we were thinking for the CBGs that only partially overlap the shed layers, calculate the percentage of the overlap, place that percentage in a field, and multiply statistics by that percentage.  I've been working on this script for over a week now and I simply cannot get it to work right.  Basically what I want it to do is:

 

     -Create an Update Cursor in the CBGs layer

     -Select each CBG polygon as it scrolls through

     -Clip the "shed" layer to the selected polygon

     -Measure the shape area for the clipped layer

     -Divide the area of the clipped layer by the CBG

     -Set the result as the value in the percentage field in CBG

     -Delete the clipped polygon

     -Repeat

  

In its current form the script is completing without error, but with whole numbers that clearly are not the desired percentage.  Every time I try to create an AddMessage so that I can see what values are being divided I then get an error and am still unable to see where the values are coming.  This is the first time I've ever built a python script without step-by-step instructions so I'm very lost.  If anyone sees the errors in my code, any pointers would be greatly appreciated.  I have the script written below and have attached a zipped folder with the .py, the bikeshed, a few selected CBGs, and the toolbox I'm executing it with.

 

 

 

 

 

import arcpy

censuslayer = arcpy.GetParameter(0)#census unit layer
percentField = arcpy.GetParameter(1)#field where percentage will be input
studylayer = arcpy.GetParameter(2)#walk or bikeshed layer



#currently unused#arcpy.AddField_management (censuslayer, "perc_cov", "float", "3", "3") #adds blank percentage field to census units
censussize = arcpy.AddField_management (censuslayer, "shapearea", "FLOAT")#adds shape area to census
arcpy.CalculateField_management (censuslayer, "shapearea", "!shape.area@squarefeet!", "PYTHON") #calculates the geometry


#for polygon in studylayer:
arcpy.AddField_management (studylayer, "shapearea", "FLOAT") #adds a shape area to walk or bikeshed

  
#search = arcpy.SearchCursor(censuslayer)
update = arcpy.UpdateCursor(censuslayer)#creates update cursor to move through census layers

for row in update:
    ceblocksize=float(row.getValue("shapearea"))#places shape area value in variable
    currentCen=int(row.getValue("OBJECTID"))#places object ID field name in variable
    holder=("in_memory\holder")#output
    whereClause=str("'OBJECTID' = " + "'"+ str(currentCen)+"'")#formula to indicate record to select
    selectBlock=arcpy.Select_analysis(censuslayer, holder, whereClause)#select record where cursor should be
    
    temppolygon = arcpy.Clip_analysis (studylayer, selectBlock, "templayer") #clip shape from shed layer to selected feature
    
    studysize = arcpy.CalculateField_management (temppolygon, "shapearea", "!shape.area@squarefeet!", "PYTHON") #calculate geometry  of clipped feature
    studysearch = arcpy.SearchCursor(temppolygon)# new cursor in clipped feature
    for only in studysearch:
        shedsize = only.getValue("shapearea")#store the shape area in a variable
    
        
    #arcpy.AddMessage(shedsize"/"ceblocksize)
    percent = float(shedsize)/ceblocksize #get the value to place in new field
            
        
    row.setValue(percentField, percent)#sets the value in field
    update.updateRow(row)

    arcpy.Delete_management(temppolygon)#deletes the clipped polygon
    row = update.next


    

   

   

   

   

   Edit: Formatted Code Block

   

Attachments

Outcomes