I'm trying to add some raster data to a File Geodatabase Table and export it to an excel file. The problem is that it wont print it to the file automatically, I have to create the file first by running the first part of the script, then add the area data from the dict by running the cursor in the last part of the script. What am I doing wrong?
Im summarizing the number of raster cells, so that I can count the area. The first part of the code reads the count of the raster, which are 1m by 1m so I can get an area. Then the script do this for all the rasters in the catalog and makes it to a dictionar. I then create a table and try to write it to a table, but this last part somehow fails. I verify that the insertCursor reads the dict, but it doesn't seem to be able to fill the newly genereated table.
arcpy.env.workspace = r"C:\Users\...\Output\Area.gdb"
rastList = arcpy.ListRasters() #list raster in catalog
dictArea = {}
for raster in rastList:
cursor = arcpy.SearchCursor(raster)
for row in cursor:
area = row.getValue("Count")
dictArea[raster[4:]] = area
del cursor
#check if table exists, if it does, delete and make a new one
if arcpy.Exists("niPunkt"):
print ("niPunkt.dbf exists, creates a new. ")
arcpy.Delete_management("niPunkt")
arcpy.CreateTable_management(env, "niPunkt")
arcpy.management.AddFields('niPunkt', [['flate', 'DOUBLE', 'Flate Nr', 255], ['size', 'DOUBLE', '', '']])
#if it doesnt exist, create a new table
else:
arcpy.CreateTable_management(env, "niPunkt")
arcpy.management.AddFields('niPunkt', [['flate', 'DOUBLE', 'Flate Nr', 255], ['size', 'DOUBLE', '', '']])
#create cursor that inserts to the new table with the fields "flate" and "size"
cursor = arcpy.da.InsertCursor('niPunkt', ['flate','size'])
#for all values in the dict, insert the key and the value
for key, value in dictArea.items():
cursor.insertRow((key,value))
#create excel file
arcpy.TableToExcel_conversion('niPunkt', '9PunktArea.xls')