Use of "Zonal statistic as table - 2" for overlaying polygons

4786
7
Jump to solution
02-16-2016 05:26 AM
JohannesDegoog
New Contributor II

Hello to all of you,

I am processing 11.000 polygons, which are partially overlaying, by the published "Zonal statistic as table - 2"  tool (New Spatial Analyst Supplemental tools, v1.3 | ArcGIS Blog ). Somehow, even though my input layers meet the criteria, it just creates an output table with 665 items, not 11.000. Is there any known reason for that? Since I am not so familiar with python: What is this whole colour thing about and could it be the reason for the limited items?

I attached the "Zonal statistic as table - 2" script file.

Thanks in advance

1 Solution

Accepted Solutions
JohannesDegoog
New Contributor II

Hello Dan, hello Peter,

thank you for your answer and help.

I did not work on that project for quite a while, so please excuse my delayed answer.

I finally found out why my "Zonal statistic as table - 2" - Tool behaved like the normal "Zonal statistics as table" tool. The solution turned out to be very simple...!

The script works with exception handling by using "TRY" and "EXCEPT", where "TRY" is dealing with overlapping polygons and "EXCEPT" is just the normal "Zonal statistics as table" tool (which provides unsatisfactory results).

When I tried to find out why an exception was caused, i saw that this part of the script:

if arcpy.env.overwriteOutput == "False":
     arcpy.env.overwriteOutput = "True"
     owFlag = 1
else:
     owFlag = 0

    

is not really working. I do not know why exactly it does not work, but by simply setting

arcpy.env.overwriteOutput = "True"

no exception was caused anymore and the script worked perfectly.

Simon

View solution in original post

0 Kudos
7 Replies
DanPatterson_Retired
MVP Emeritus

Did you read the response section in the link?  Problems have been identified because of

  • raster size
  • overlapping polygons completely overlapping others (eg...Note: all of the polygons that do not process using Zonal Statistics 2 are those that are overlapped by a single larger polygon. This may have something to do with why results are not generated for these polygons.)
  • the whole color thing? care to elaborate?
0 Kudos
JohannesDegoog
New Contributor II

Hey Dan,

thanks for your reply! Yes, I read the comment section. But it is not one of these problems.

What I meant by "the whole color thing" was this part of the code, where I did not really understand what it is good for (I am just a beginner):

            # Retrieves the colors of the neighboring nodes
            def get_colors(nodes, neighbors):
                colors = set()
                for neighbor in neighbors:
                    colors.add(nodes[neighbor][0])
                colors.difference([0])
                return colors
  
            # Create a new color
            def get_new_color(colors):
                return max(colors)+1 if len(colors) > 0 else 1
  
            # Chooses from existing colors randomly
            def choose_color(colors):
                return random.choice(list(colors))
0 Kudos
DanPatterson_Retired
MVP Emeritus

It appears to be an attempt to determine what the neighborhood values are and to ensure that adjacent cells with a different value don't end up being assigned the same color.  It is a visualization thing as well as  a means of discerning whether cells belong to a certain zone (which is probably assigned a color to represent its value)

PS

Don't forget that rasters are represented as zones, and distinctly separate 'shapes' of the same colour (read... class) will belong to the same zone.  This is the equivalent of a multipart polygon in vector world.

0 Kudos
PeterWilson
Occasional Contributor III

Hi Simon

I've been using the Spatial Analyst Supplemental Tools for quiet a while now for my hydrological model for determining average catchment slopes. When I determine watersheds for my rail or road alignments, they overlap of course. Graph Coloring is explained by William Huber: Exploding Overlapping Polygons into Non Overlapping Polygons.​ I suggest you look at repairing the geometry of your polygons or reload them into a new Feature Class. As I've not experienced any error message while using the following. I've attached portion of my python script below that I use daily for my hydrological modelling:

'''
Created on Feb 1, 2016


@author: PeterW
'''


# import site-packages and modules
import arcpy


# import custom python toolboxes
arcpy.ImportToolbox("E:\Python\Downloaded\general\SpatialAnalystSupplementalTools\Spatial Analyst Supplemental Tools.pyt")


# set input and output arguments
raster_folder = arcpy.GetParameterAsText(1)
fgdb = arcpy.GetParameterAsText(2)


# set variables
drain = "{}\\{}".format(fgdb, "Watersheds")
slp = "{}\\{}".format(raster_folder, "slope")
avgslp = "{}\\{}".format(fgdb, "avgslp")


# determine the average slope for each watershed
arcpy.AddMessage("Processing Average Catchment Slope")
arcpy.ZonalStatisticsAsTable02_sas(drain, "HydroID", slp, avgslp, "MEAN", "DATA") #@UndefinedVariable
0 Kudos
JohannesDegoog
New Contributor II

Hello Dan, hello Peter,

thank you for your answer and help.

I did not work on that project for quite a while, so please excuse my delayed answer.

I finally found out why my "Zonal statistic as table - 2" - Tool behaved like the normal "Zonal statistics as table" tool. The solution turned out to be very simple...!

The script works with exception handling by using "TRY" and "EXCEPT", where "TRY" is dealing with overlapping polygons and "EXCEPT" is just the normal "Zonal statistics as table" tool (which provides unsatisfactory results).

When I tried to find out why an exception was caused, i saw that this part of the script:

if arcpy.env.overwriteOutput == "False":
     arcpy.env.overwriteOutput = "True"
     owFlag = 1
else:
     owFlag = 0

    

is not really working. I do not know why exactly it does not work, but by simply setting

arcpy.env.overwriteOutput = "True"

no exception was caused anymore and the script worked perfectly.

Simon

0 Kudos
KaraUtter
Occasional Contributor III

Hello Johannes!

I am running into the same issue of zonal statistics 2 acting just like zonal statistics as table - I don't quite know how to edit the script in the way that you mention above. To simply set arcpy.env.overwriteOutput = "True", what does that mean for the other script around it? delete the if/then lines? I am not familiar enough with python to understand how to make the modification that fixed it for you.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Kara

it should be a boolean not a string for true  "True" is not the same as True

arcpy.env.overwriteOutput = True