Select to view content in your preferred language

Put Raster Properties into table

743
9
10-05-2012 05:12 AM
JannWendt
Emerging Contributor
Hi,

maybe someone is able to help me with my problem (I'm new to python :)):

I try to get some raster data into a table with a python script but I get the following error:

Traceback (most recent call last):
File "C:/Users/Jann/Dropbox/EGEOS/Büro/2012/Projekte/Georeferenzierung in Attributtabelle/Online.py", line 21, in <module>
row.top = arcpy.GetRasterProperties_management(raster, "top")
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
return setattr(self._arc_object, attr, ao)
ValueError: Row: Invalid input value for setting

My script looks like this:

import arcpy
from arcpy import env
env.workspace = "E:/Data/X-005"

# Create table
if arcpy.Exists("E:/Data/X-005/Test.dbf"):
arcpy.Delete_management("E:/Data/X-005/Test.dbf")

table = arcpy.CreateTable_management("E:/Data/X-005", "Test.dbf")

# Add fields
arcpy.AddField_management(table, "name", "TEXT")
arcpy.AddField_management(table, "top", "TEXT")

# Insert values
rows = arcpy.InsertCursor("E:/Data/X-005/Test.dbf")
rasters = arcpy.ListRasters()

for raster in rasters:
row = rows.newRow()
row.top = arcpy.GetRasterProperties_management(raster, "top")
row.name = raster

rows.insertRow(row)

# Delete cursor and row objects ro remove locks on the data
#
del row
del rows


Maybe someone can give me some tipps.

Best regards,
Jann
Tags (2)
0 Kudos
9 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Jann,

Change the following line:

row.top = arcpy.GetRasterProperties_management(raster, "top")


to:

row.top = str(arcpy.GetRasterProperties_management(raster, "top"))
0 Kudos
curtvprice
MVP Esteemed Contributor
row.top = str(arcpy.GetRasterProperties_management(raster, "top"))


Thanks, Jake, I learned something new today. If you str() a result object, you'll get a string representation of its the result's value. If you know this is a single value and what type it is, this does make for a simple way to get the value:

>>> r = arcpy.GetRasterProperties_management("cdl20091.tif","top") # result object
>>> r
<Result '3173073.79641599'>
>>> r.getOutput(0)  # result object output 
u'3173073.79641599'
>>> str(r) # returns string rep of result output
'3173073.79641599'
>>> float(str(r)) # convert to float
3173073.79641599
0 Kudos
JannWendt
Emerging Contributor
Thank you both! JSkinn3 for the right way and curtvprice for the explanation 🙂
0 Kudos
JannWendt
Emerging Contributor
Okay now Ive an additional question which makes it a little bit more complicated... 😄

With the code above it is possible to get the information about the georeference of rasters in one folder into a .dbf file. My problem is that my folder structure is a little bit different. The rasters are distributed in subfolders. Does anybody has an idea how to get the information of all raster of the subfolders together in one .dbf instead of doing it for each folder individually?

Thanks for your help!
0 Kudos
RaphaelR
Deactivated User
you can get a list with the tif-rasters from different subfolders with os.walk:

import os

path = "e:\\test\\"
rasters = []

for path,dirs,files in os.walk(path):
    
    for file in files:
        if file.endswith(".tif") == True:
            rasters.append(str(path) + "\\" + str(file))
0 Kudos
JannWendt
Emerging Contributor
This works well but I don't know how to put the this together with the raster information script. As I've written before, I'm more or less completely knew to python...
0 Kudos
RaphaelR
Deactivated User
import os
import arcpy
from arcpy import env
env.workspace = "E:/Data/X-005"

# Create table
if arcpy.Exists("E:/Data/X-005/Test.dbf"):
    arcpy.Delete_management("E:/Data/X-005/Test.dbf")

    table = arcpy.CreateTable_management("E:/Data/X-005", "Test.dbf")

# Add fields
arcpy.AddField_management(table, "name", "TEXT")
arcpy.AddField_management(table, "top", "TEXT")

# Insert values
rows = arcpy.InsertCursor("E:/Data/X-005/Test.dbf")

# get rasters
path = "e:\\test"
rasters = []

for path,dirs,files in os.walk(path):
    
    for file in files:
        if file.endswith(".tif") == True:            
            rasters.append(str(path) + "\\" + str(file))


for raster in rasters:
    row = rows.newRow()
    row.top = arcpy.GetRasterProperties_management(raster, "top")
    row.name = raster
    
    rows.insertRow(row)

# Delete cursor and row objects ro remove locks on the data
#
del row
del rows
0 Kudos
JannWendt
Emerging Contributor
Hi rafaelr,

thanks for your support. Now I get the following problem when I run the script:

Traceback (most recent call last):
  File "W:\Organisation\10013 Aktualisierung der Luftbilddatenbank in Bezug auf Georeferenzierung\Daten\georeference_in_attribute_table_new.py", line 31, in <module>
    row.top = arcpy.GetRasterProperties_management(raster, "top")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
    return setattr(self._arc_object, attr, ao)
ValueError: Row: Invalid input value for setting


Here is the script I'm using:

import os
import arcpy
from arcpy import env
env.workspace = "C:/Data"

# Create table
if arcpy.Exists("C:/Data/Test.dbf"):
    arcpy.Delete_management("C:/Data/Test.dbf")

table = arcpy.CreateTable_management("C:/Data", "Test.dbf")

# Add fields
arcpy.AddField_management(table, "name", "TEXT")
arcpy.AddField_management(table, "top", "TEXT")

# Insert values
rows = arcpy.InsertCursor("C:/Data/Test.dbf")

# get rasters
path = "C:/data"
rasters = []

for path,dirs,files in os.walk(path):
    
    for file in files:
        if file.endswith(".ecw") == True:            
            rasters.append(str(path) + "/" + str(file))

for raster in rasters:
    row = rows.newRow()
    row.top = arcpy.GetRasterProperties_management(raster, "top")
    row.name = raster
    
    rows.insertRow(row)

# Delete cursor and row objects ro remove locks on the data
#
del row
del rows


Do you (or anybody else) have an idea what to do?

Thanks!
Jann
0 Kudos
curtvprice
MVP Esteemed Contributor
Now I get the following problem when I run the script:

Traceback (most recent call last):
  File "W:\Organisation\10013 Aktualisierung der Luftbilddatenbank in Bezug auf Georeferenzierung\Daten\georeference_in_attribute_table_new.py", line 31, in <module>
    row.top = arcpy.GetRasterProperties_management(raster, "top")
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\_base.py", line 35, in __setattr__
    return setattr(self._arc_object, attr, ao)
ValueError: Row: Invalid input value for setting


GetRasterProperties returns a result object. See the [post=238595]top of this thread[/post] for how to convert that into a value you can write to a field.
0 Kudos