Error when using iteration in Zonal table as Statistics

2853
6
Jump to solution
06-14-2015 08:21 PM
bilalcelik1
New Contributor

I am pretty new in ArcGIS, however I was able to create my script accept the last step.

I am using night lights to get my data. I light data for several years, i want to get my data for each year. I was able to generate iterations and it is working accept last step.

Here is my code:

# Import arcpy module
import arcpy
from arcpy import env
from arcpy.sa import *

arcpy.env.workspace = r"D:\Research-NightLights\yeni"
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")

# Local variables:
Tur_pro_09 = "D:\\Turkey09\\admin\\Turpro09.shp"
Tur_lyr = "D:\\Turkey09\\admin\\Turkey"

Tur_dist_09 = "D:\\Turkey2009\\admin\\Turdist09.shp"

arcpy.MakeFeatureLayer_management(Tur_pro_09, Turkey_lyr)
print "makefeature"

# Process: Select Layer By Attribute
arcpy.SelectLayerByAttribute_management(Tur_lyr, "NEW_SELECTION", "\"ID\" = '01'")
print "attribute"

rasterList = arcpy.ListRasters("f*")
for raster in rasterList:
    print "list"

# Process: Clip (1)
    arcpy.Clip_management(raster, "25.665181 35.812875 44.826408 42.104629", "y1"+str(raster[1:]), Tur_lyr, "255", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
print "clip1"

rasterList1 = arcpy.ListRasters("y1*")
for raster in rasterList1:
    print "list (2)"
   
# Process: Raster to Polygon
    arcpy.RasterToPolygon_conversion(raster,"y2"+str(raster[1:]), "NO_SIMPLIFY", "Value")
print "polygon"

fcs = arcpy.ListFeatureClasses("y2*", "polygon")
for raster in fcs:
    print "poly_clip"
   
# Process: Clip (2)
arcpy.Clip_analysis(Tur_dist_09, raster, "y3"+str(raster[1:]), "")
print "clip2"

rasterList3 = arcpy.ListRasters("y1*")
for raster in rasterList3:
    print "list (3)"

# Process: Zonal Statistics as Table
    arcpy.gp.ZonalStatisticsAsTable_sa(Turdist_09, "Name", raster,"j"+str(raster[5:])+".dbf", "DATA", "MEAN")
    arcpy.Delete_management(raster)
    print "Got zonal stats for "

Here is the Error I get: (it works perfect until Zonal Statistics as Table step)

Traceback (most recent call last):
  File "C:/Users/Bilalce/Desktop/fully working12.py", line 64, in <module>
    arcpy.gp.ZonalStatisticsAsTable_sa(Turkey_districts_2009, "Name", raster,"j"+str(raster[5:])+".dbf", "DATA", "MEAN")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 498, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
ExecuteError: ERROR 999999: Error executing function.
Create output table failed
Failed to execute (ZonalStatisticsAsTable).

(Curtis Price added formatting)

0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Looks to me like the expression:

"j"+str(raster[5:])+".dbf"

may be creating an invalid pathname (a common cause of 999999 errors).

Also, why aren't you simply using this syntax?

arcpy.sa.ZonalStatisticsAsTable(args)

View solution in original post

6 Replies
curtvprice
MVP Esteemed Contributor

Looks to me like the expression:

"j"+str(raster[5:])+".dbf"

may be creating an invalid pathname (a common cause of 999999 errors).

Also, why aren't you simply using this syntax?

arcpy.sa.ZonalStatisticsAsTable(args)

bilalcelik1
New Contributor

I also think it is because of expression

"j"+str(raster[5:])+".dbf"

Like I said I am pretty new. I created my script using model builder. model builder is using

arcpy.gp.ZonalStatisticsAsTable_sa() syntax

I also used help from the script in http://economics.mit.edu/files/8955

He is also using  arcpy.gp.ZonalStatisticsAsTable_sa() syntax.

and I got expression of "j"+str(raster[5:])+".dbf" from this the webpage above.

I don't know how to correct the output expression so I can get output for each year.

Any help is appreciated.

Thanks in advance.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Moved the thread to the Python space...

0 Kudos
NeilAyres
MVP Alum

So, what does the value of your variable "raster" look like in each of the loops.

Perhaps examine this more carefully.

0 Kudos
bilalcelik1
New Contributor

Thank you so much for your requests. I don't think there are any problem value of the raster in each loop. Here if I don't make any loop in the last step, script works perfectly. Here is the changes that I made to get rid off loop in the last step.

I added two local variable:

y1121998="D:\\Research-NightLights\\yeni\\y1121998.tif"

zonalst_shp23="C:\\Users\\Bilalce\\Documents\\ArcGIS\\Default.gdb\\ZonalSt_shp23"

Then changed last Zonal Statistics as table step as:

# Process: Zonal Statistics as Table, and got rid of the loop before zonal statistics as table step.

arcpy.gp.ZonalStatisticsAsTable_sa(Turkey_districts_2009, "Name", y1121998,zonalst_shp23, "DATA", "ALL")

arcpy.Delete_management(raster)

print "Got zonal stats for "

It works perfectly. Thus i am thinking, problem is related to expression of "j"+str(raster[5:])+".dbf".

But I don't know how to change this expression so I can get output for every year.

0 Kudos
bilalcelik1
New Contributor

Okay, I solved problem. I changed the code in last part as Curtis advice. I used

arcpy.sa.ZonalStatisticsAsTable(Tur_dist_09, "Name", raster,

                                  "j"+str(raster[5:]), "DATA", "ALL")

Now it is all good.

Thank you so much

0 Kudos