If Then field calculation using Python

270
1
2 weeks ago
Labels (1)
GG_CM
by
New Contributor

I am evaluating a set of tax lots (polygons) to calculate how much land of each has a slope greater than 25%. Here's what I've done so far:

1. DEM raster to Slope raster

2. 'Greater Than' to produce a boolean raster based on Slope > 25.

I now need to count how many '1' cells (areas over 25% slope) from the boolean raster are within each tax lot polygon.

I tried using Zonal Statistics as Table tool with the tax lot polygons as zones, but there doesn't seem to be a way to count '1' cells versus '0' cells. It returns a total count of all cells. My next thought was to add a new field and calculate it with an if/then statement related to the stats that were generated, but I am not experienced with Python and can't seem to get the formula right. I was able to write this formula quickly in Excel, but I don't know how to translate it to Python. Also wondering if there is a better way to accomplish my goal. 

The if/then statement I tried to write involves the following (in plain english), which would return the number of acres under 25% slope for each tax lot:

  • If MAJORITY equals MIN (i.e. 0, implying less than 25% slope), then calculate MAJORITY_PERCENT * mapacres.
  • If MAJORITY does not equal MIN (i.e. 1, implying over 25% slope), then calculate MINORITY_PERCENT * mapacres.
1 Reply
Luke_Pinner
MVP Regular Contributor

Zonal Statistics As Table. Statistic = SUM as sum of 1's == the count of 1's.   Alternatively, SetNull with Slope <= 25, 1 then you'll have 1's and NoData so Zonal Stats count will be just of the 1's.