0

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')
artwork21
  • 35,114
  • 8
  • 66
  • 134
birks
  • 259
  • 1
  • 9
  • @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 Oct 27 '17 at 09: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 Oct 27 '17 at 09: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 Oct 27 '17 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 (https://gis.stackexchange.com/questions/90372/using-arcpy-da-insertcursor-to-insert-entire-row-that-is-fetched-from-search-cur) for explanations. also why is there another insertCursor block after your else statment? make sure your indentation is correct. – dru87 Oct 27 '17 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 Oct 27 '17 at 11:16
  • Add this to your question. We should not have to look at your code to understand the question. Do you know of raster attribute tables? Will only work on single band integer rasters though. (http://pro.arcgis.com/en/pro-app/tool-reference/data-management/build-raster-attribute-table.htm) – BERA Oct 27 '17 at 11:19
  • @BERA Thanks BERA, I edited the question and the code. Yeah, I looked into that, but I want every raster to be exported into one table with the raster name and the count(of the area). There are more than 1000 rasters, so I don't want 1000 tables. – birks Oct 27 '17 at 11:49
  • I managed to solve it. By adding a "if arcpy.Exists("niPunkt"):" before the last insertcursor, it actually writes to the table. – birks Oct 27 '17 at 11:58
  • Have you added some print statements to verify your logic? – artwork21 Oct 27 '17 at 11:58
  • @artwork21 Yes, it printed, but I managed to solve it now. It's still unclear to me why this was a problem though. – birks Oct 27 '17 at 12:02
  • Where are you creating the env variable used in this statement, arcpy.CreateTable_management(env, "niPunkt") – artwork21 Oct 27 '17 at 14:39

1 Answers1

0

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
birks
  • 259
  • 1
  • 9