Select to view content in your preferred language

Different results when running arcpy outside ArcMap?

4830
14
10-09-2014 04:38 AM
JamesRamm
Deactivated User

I am trying to run ZonalStatisticsAsTable using python.

I have a feature class in a FGDB and a raster within an SDE database (SQL Server).

If I load these into layers within arcmap, open the python console (within arcmap) and execute:

arcpy.CheckOutExtension("Spatial")

arcpy.sa.ZonalStatisticsAsTable(r"Site Boundary\Site", "Site", "sde_jbacfm.JBAGISEDITOR.River09Q1000D", "TempTable")

I get a table of results.

If I run this code, from a python console outside of arcmap:

import arcpy

arcpy.CheckOutExtension("Spatial")

# In these filepaths I have purposefully replaced the full path with '...' for clarity. This is just for this example

arcpy.env.workspace = r"C:\...\Output.gdb" # This is an empty geodatabase initiated with a WGS84 projection

input_feat = r"C:\...\SitePolygon.gdb\Site Boundary\Site"

raster = r"C:\...\connection.sde\sde_jbacfm.JBAGISEDITOR.River09Q1000D"

output = "TempTable"

arcpy.sa.ZonalStatisticsAsTable(input_feat, "Site", raster, output)

This works but I get an entirely different result for the statistics.

I am trying to work out what is going on? The only thing I can think of is that the projection of each input (and the output database) is different, but I had understood that arcpy would handle different projections automatically?

In any case, I would still expect to get the same result using python through arcmap as I did not reproject anything there either.

0 Kudos
14 Replies
curtvprice
MVP Alum

This sounds like a different issue. Please start a new thread with more detail (i.e. which spatial references, data types, even some test data).

0 Kudos
giuseppemolinario
Deactivated User

In any case, I would still expect to get the same result using python through arcmap as I did not reproject anything there either.

I published a peer reviewed paper in 2015, based on tabulated results that were obtained using a python script that used the arcpy method zonal histogram. I had no environments set there, other than snap to raster. The area shapefile and the raster, were in the same projection. Recently,having to check those results again, I used zonal histogram in the ArcMap GUI and, using the same exact input layers, and also not setting any environments other than snap to raster, I got drastically different results.

How is this not a bug? The answer to the OP should be simple and clear: sorry - we'll fix this.

arcpy/ArcMap should not have opaque settings that produce different results, when the same tool is used and all the input layers and settings are equal. Period. 

It took me several hours of debugging to figure out the issue - the default (and OPAQUE) environment settings in zonal histogram when used from the arcmap GUI analyze the data at a coarser resolution than the inputs, to save computational time. Setting the environment setting to match the pixel size of the raster being analyzed, makes the tool perform as expected, and as it should be as default, obviously, and therefore matches the output results i get when using zonal histogram on the same exact layers in an arcpy script. 

gdal next time? 

0 Kudos
curtvprice
MVP Alum

It's only fair to note in this thread that the help for this tool Zonal Histogram—Help | ArcGIS Desktop  and Zonal Statistics as Table—Help | ArcGIS Desktop  both have a bullet item warning about this behavior. (No doubt in response to user input in the form of support tickets!)

It is recommended to only use rasters as the zone input, as it offers you greater control over the vector-to-raster conversion. This will help ensure you consistently get the expected results.

I am the author of a tool that does Zonal stats and I have it avoid this problem by defaulting the cell size (and snap) to the raster dataset resolution as you suggest. Unless the environment cell size has been set, in which case it honors the env cell size, resampling the value raster, but warning the user. 

More on environments: Setting geoprocessing environments—Help | ArcGIS Desktop 

0 Kudos
DanPatterson_Retired
MVP Emeritus

At least in PRO they have put the Environments tab at the top, front and center instead of being buried in the bottom right corner of the dialog 

Small steps... I would love to have a non-removable popup when settings haven't been explicitly set OR checked.  Would make the classroom go a lot smoother 

curtvprice
MVP Alum

I totally agree with giuseppe molinario that the environments are quite a bit below the hood, and that sometimes it can be absolutely maddening  when you get results you didn't expect because of environment settings set at the application level inside a dialog that is not even included as a button on the ArcMap toolbar! (Personally I use Customize mode to add an environments button next to the Python button.)

I also am missing a button you can push in the environments dialog to "reset all environments" to defaults. Except, the question is asked: which defaults, as the current settings are saved with the map document. Something that is supposed to be a convenience can become a curse in a hurry. 

I hope the links I offered were in a spirit of helpfulness to readers of this thread, and definitely not in disrespect to anyone frustrated with the platform, which is very powerful like a knife with many blades that can cut you. Been there many times in the last 30 years, I get it. Trust but verify.