Let's say I have a shapefile 'Mypolygons' with an ID field and a raster 'DEM' giving elevation values. I want to add a field for each row in the 'Mypolygons' attribute table to give the average elevation within each polygon.
My current approach is the following:
-
Add field "Avg_Elev" to 'Mypolygons'.
-
Use Zonal Statistics as Table with 'Mypolygons' as the zone data, 'ID' as the Zone field and 'DEM' as the in_value_raster, and 'Output' as the out_table to hold mean elevation values.
- Join 'Mypolygons' and 'Output' based on the 'ID' field.
- Use Calculate field to populate values of 'Mypolygons' 'Avg_Elev' field based on the 'MEAN' field in 'Output'.
- Remove join.
Is there a single tool or a more efficient way to perform this task? A python solution will work for me as well.
Best Answer
Your workflow is adequate and would easily be incorporated into a model but for a few tricky spots with joins...
Qualified field names are a pain in the join 'to' table, you can turn them off but they will suddenly fully qualify if the field name already exists in the from table (usually at a minute to knock-off on Friday). To avoid this inconsistency take charge of the temporary table name so you can predict the fully qualified field, in this case
'!RasterStatTable.{}!'.format(StatType)
- I have made the stat type a variable so as to make the code reusable for other statistic types but beware some fields in the table are not named the same as their statistic type.Or alternately not use a join at all as @FelixIP said in his comment, this is the way that I'd go - not any quicker but much less likely to fail randomly. Digest all the table into a dictionary object (you should have enough RAM to load the entire table) then update the polygons using the dictionary.
I've put both methods into this code, try it for yourself and see what you prefer: