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')
-
@Luke I'll try to explain. I'm trying to get the area of each raster. So before this script, I set all the raster values to 1. Then I use the first SearchCursor to create a dictionary with the raster name and the area. Then in the next bit, I create a table. In the laste cursor, here is the problem, im trying to write all the dict values into the table. For some reason I can't get the dict values into the table. It only writes if I create the table running first part of the script and then the second part of the script in two runs.birks– birks2017年10月27日 09:13:52 +00:00Commented Oct 27, 2017 at 9:13
-
@Luke I tried to change it, but it still didn't add the value to the table in the script. I really don't understand whats wrong.birks– birks2017年10月27日 09:37:46 +00:00Commented Oct 27, 2017 at 9:37
-
You are not telling us what you want to do. You want to add each pixel value of the raster as a cell value in excel? Or do you want to calculate something (like cell count per value) and export to excel?Bera– Bera2017年10月27日 11:10:30 +00:00Commented Oct 27, 2017 at 11:10
-
there are several potential issues with your code. if you create a cursor, you should close it after using it. this is done automatically when you use the ´with arcpy.da.InsertCursor() as cursor:´ statement. see this link (gis.stackexchange.com/questions/90372/…) for explanations. also why is there another insertCursor block after your else statment? make sure your indentation is correct.dru87– dru872017年10月27日 11:12:37 +00:00Commented Oct 27, 2017 at 11:12
-
@BERA Im summarizing the number of raster cells, so that I can count the area. Look at the first part of the code that reads the count of the raster. I want the script to do this for all the rasters in the catalog and export it to a table.birks– birks2017年10月27日 11:16:05 +00:00Commented Oct 27, 2017 at 11:16
1 Answer 1
By adding a if exists before the last insertCursor, it managed to insert all the rows into the newly created table.
if arcpy.Exists("niPunkt"):
cursor = arcpy.da.InsertCursor('niPunkt', ['flate','size'])
for key, value in dictArea.items():
cursor.insertRow((key,value))
del cursor