Help calling a table attribute, and using it as a variable?

694
6
Jump to solution
06-19-2013 02:56 PM
TomKearns
Occasional Contributor II
I have a table with multiple min and a max numbers for multiple points.  I would like to use "Extract by Attribute" to pull the fitting areas from a DEM which corresponds to each area.

Reworded: I would like to pull all the raster cells which fall inside the ranges provided for each specific area by calling the table.

Object     Area1Min  Area1Max  Area2Min Area2Max
   A1          1000          1500        750          1200
   A2          900            1700       1200         1500

Using this example I would create four new rasters of A1 in Area1, A1 in Area2...

I am having trouble setting up the variable which would be used in the following code:

attExtract = ExtractByAttributes("elevation", "VALUE > 1000")
attExtract.save("c:/sapyexamples/output/attextract")

I know this code works:
attExtract = ExtractByAttributes("dem", "VALUE > 1000 AND VALUE < 1500")

I need to replace the 1000 and 1500 with a call to the places Area1Min and Area1Max for A1.

Thanks in advance!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
I have a table with multiple min and a max numbers for multiple points.  I would like to use "Extract by Attribute" to pull the fitting areas from a DEM which corresponds to each area.


The best way to get the variables from a table into Python is using cursors. For example:

rows = arcpy.SearchCursor("areatable.dbf") k = 1 for row in rows:     a1min = float(row.getValue("Area1Min"))     a1max = float(row.getValue("Area1Max"))     where = "VALUE > {0}  AND VALUE < {1}".format(a1min, a1max)     dem1 = ExtractByAttributes(Raster("dem"), where)     dem1.save("dema{}".format(k)) # save as grids dema1, dema2 del row, rows


Instead of ExtractByAttributes you could use the Con function like this. This is more of an Arcpy approach really:

dem = Raster("dem1") dem1 = Con(dem > a1min and dem < a1max, dem)

View solution in original post

0 Kudos
6 Replies
curtvprice
MVP Esteemed Contributor
I have a table with multiple min and a max numbers for multiple points.  I would like to use "Extract by Attribute" to pull the fitting areas from a DEM which corresponds to each area.


The best way to get the variables from a table into Python is using cursors. For example:

rows = arcpy.SearchCursor("areatable.dbf") k = 1 for row in rows:     a1min = float(row.getValue("Area1Min"))     a1max = float(row.getValue("Area1Max"))     where = "VALUE > {0}  AND VALUE < {1}".format(a1min, a1max)     dem1 = ExtractByAttributes(Raster("dem"), where)     dem1.save("dema{}".format(k)) # save as grids dema1, dema2 del row, rows


Instead of ExtractByAttributes you could use the Con function like this. This is more of an Arcpy approach really:

dem = Raster("dem1") dem1 = Con(dem > a1min and dem < a1max, dem)
0 Kudos
DarrenWiens2
MVP Honored Contributor
I believe you're looking for a SearchCursor to loop through your rows, then getValue(FIELDNAME) to read the individual value (see the first example in the link). You'll need four getValues (one for each field) and two ExtractByAttributes (one each for Area1 and Area2) in your search cursor loop.
0 Kudos
TomKearns
Occasional Contributor II
Thank you to both of you, the answers worked perfectly.  The Extract by attribute tool however created more questions than it answered.  I appreciate your inputs and am looking forward to investigating the Con function.
0 Kudos
curtvprice
MVP Esteemed Contributor
The Extract by attribute tool however created more questions than it answered. 


Did the string substitution to create the SQL expression throw you for a loop?

where = "VALUE > " + str(a1min) + " AND VALUE < " + str(a1max)
where = "VALUE > {0}  AND VALUE < {1}".format(a1min, a1max)


These expressions are equivalent, they both evaluate to something like:

VALUE > 5000 AND VALUE < 6000


I like to promote the use of string substitution because it makes the SQL expression setup much easier to read and debug.
0 Kudos
TomKearns
Occasional Contributor II
Thanks for asking but the string was perfect, I already had mine built in the same fashion so it was an easy substitution.

The tool turns out a raster with no "values".  While they can be displayed, they are incredibly large files and can not be converted to a shape file due to not having any data attached. 

The Con however, worked extremely well and the Raster To Polygon tool was easy to add on to the end of my script. It was an awesome suggestion.
0 Kudos
curtvprice
MVP Esteemed Contributor
The tool turns out a raster with no "values".  While they can be displayed, they are incredibly large files and can not be converted to a shape file due to not having any data attached. 


If polygons are what you want, you would need to do something like this to create an integer grid of all non-null cells, and then convert that to polygons:

dem1 = SetNull(IsNull(ExtractByAttributes(Raster("dem"), where)),1)
0 Kudos