Select to view content in your preferred language

ZonalStatisticsAsTable misses 245 zones out of 3108! - ArcPy , ArcGIS Pro 2.8.2.

3455
12
Jump to solution
08-30-2021 12:05 PM
BabakJfard
Occasional Contributor

I have many .ncf files that I read as raster layers and then do zonal statistics to have mean and sd values over each of 3108 CONUS counties. But the final standalone table is missing 245 counties. This is how the raster layer and the counties look together. You see that the raster covers all the polygons

feature layer on top of the raster layerfeature layer on top of the raster layer

And the picture below shows missing counties  from the resulted zonal statistics in red color.Red counties are missed from the final tableRed counties are missed from the final table

This is my code that loops over different files with same rasters only different values in cells and the same shape file for zonal boundaries.

 

 

# Set the analysis environments
env.workspace = r"C:\ArcGIS_projects\PM25"

rasrtersFolder = r"C:\ArcGIS_projects\PM25\V4NA03\\"
zonalOutFolder = r"C:ArcGIS_projects\PM25\zonal_stats\\"

variable = "PM25"
XDimension = "LON"
YDimension = "LAT"
outRasterLayer = "PM25"
bandDimmension = ""
dimensionValues = ""
valueSelectionMethod = ""
cellRegistration = ""

# for zonal statistics
inZoneData = 'CONUS_counties_2016_WGS1984'
zoneField = 'GEOID'
inValueRaster = 'PM25'

for fileName in files:
    # Reading netcdf file into raster layer
    inNetCDFFile = rasrtersFolder + fileName

    # Execute MakeNetCDFRasterLayer
    arcpy.MakeNetCDFRasterLayer_md(inNetCDFFile, variable, XDimension, YDimension,
                               outRasterLayer, bandDimmension, dimensionValues, 
                               valueSelectionMethod, cellRegistration)

    # Now doing zonal statistics
    outName = fileName.split('.')[0]
    outTable = zonalOutFolder + outName+'.dbf'

    outZSaT = ZonalStatisticsAsTable(inZoneData, zoneField, inValueRaster,
                                  outTable, "DATA", "MEAN_STD")

    # Set local variables
    inTable = outName
    outLocation = r"C:\Users\babak.jfard\ArcGIS_projects\PM25\zonal_stats_csv"
    outTable = outName.split('-')[0] + ".csv"

    # Execute TableToTable
    arcpy.TableToTable_conversion(inTable, outLocation, outTable)
    
    arcpy.management.Delete('PM25')
    arcpy.management.Delete(outName)
    arcpy.management.Delete(outTable)

 

 

the files to reproduce can be found here: (All projection are set to WGS1984 to align with the ncd files)

Is it possible to be related to my updated ArcGIS pro (2.8.2)? two weeks ago I did the very same with an older version of ArcGIS pro and worked perfectly. Last night I updated my ArcGIS pro, and the very same raster and feature have missing zones in the results

0 Kudos
1 Solution

Accepted Solutions
BabakJfard
Occasional Contributor

Mystery solved!

The problem was that I had projected my shape file to the projection of my raster files (netcdf). But the raster files did not have any projected coordinate system (only contained Geographic Coordinate!). So, I solved it the other way. Now before doing the zonal statistics, I project each raster into my shape file projection that contains both GCS and PCS, using this command:

arcpy.management.ProjectRaster

 

View solution in original post

0 Kudos
12 Replies
MarkBoucher
Honored Contributor

I’ve found that zonal statistics can have problems if the polygons overlap. Check the topology. 

0 Kudos
BabakJfard
Occasional Contributor

I just did 'Find Overlaps' and nothing unusual found. Thank you for the suggestion.

0 Kudos
DanPatterson
MVP Esteemed Contributor

How the zonal statistics tools work—ArcGIS Pro | Documentation

can you confirm that the zone field is unique, integer and has no duplicates (as a check... it looks strange)

What is it about the zones that are "missing"


... sort of retired...
0 Kudos
BabakJfard
Occasional Contributor

I did it with FID as zone field (The automatic key field by ArcGIS) , too. The same problem. Then I changed GEOID into integer (and it is definitely unique, as US County FIPS code) and analyzed. Again, same problem!!

0 Kudos
BabakJfard
Occasional Contributor

Mystery solved!

The problem was that I had projected my shape file to the projection of my raster files (netcdf). But the raster files did not have any projected coordinate system (only contained Geographic Coordinate!). So, I solved it the other way. Now before doing the zonal statistics, I project each raster into my shape file projection that contains both GCS and PCS, using this command:

arcpy.management.ProjectRaster

 
0 Kudos
SarmisthaChatterjee
Esri Contributor

Hi BabakJfard,

 

Thank you for reporting this. I tried to download your data, but could not access it, can you please make it public, so that I can further investigate it. If you cannot make the data public, please let me know, so that I can reach out to you directly and get the data.

I have a few questions for you:

  1. Are the zones missing in the ZonalStatisticsAsTable dbf output, or in the TableToTable_conversion csv output?
  2. Do you also get the correct number of zone if you run ZonalStatistics?
  3. Do you get the correct number of zones in the output table if you use a raster zone? You can create a raster zone by converting your feature input zone to a raster zone using the PolygonToRaster tool using the same Cell Size, Snap Raster and Output Coordinate System as that of the value raster.

 

Thanks,

Sarmistha

0 Kudos
BabakJfard
Occasional Contributor

Hi Sarmistha,

Thank you very much for your response. I found the solution to the problem and posted it here two days before your answer.

Thank you very much again for reaching out.

0 Kudos
SarmisthaChatterjee
Esri Contributor
Hi BabakJfard,
 
Thank you for finding and posting the workaround. Even though projecting data from GCS to a PCS works for you, the underlying problem remains.
 
The Zonal Statistical as Table tool should work with Geographic Coordinate System, and you do not have to project them before the analysis.
 
If you can share the data with me, I can investigate it further.
 
Thanks,
Sarmistha
0 Kudos
BabakJfard
Occasional Contributor

Sure,

I updated the link to ncd file in my initial post. I think this is going to work this time for everyone who clicks the links.

Please let me know if you still have problem to download it.

 

yours,

Babak

0 Kudos