0

I have multiple rasters with 14 different categories. I want to use SearchCursor to loop through each raster's attribute table, obtain "Value" and use it in ExtractbyAttributes tool. This is the code I've so far, but it is giving me an invalid SQL statement error which I don't understand as I'm disregarding the optional SQL statement. I'm using 10.4.

inRas = "C:/ABCD_10000_P6_Mos"

rows = arcpy.SearchCursor(inRas, "", "", "", "")
for row in rows:
    luClass = row.getValue("Value")
    print (luClass)
    rasExtract = ExtractByAttributes(inRas, luClass)
    rasExtract.save (inRas + "_" + str(luClass) + "_P6_Split")

    ERROR:
    Runtime error  Traceback (most recent call last):   
    File "<string>", line 7, in <module>   
    File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\sa\Functions.py", line 1324, in ExtractByAttributes     where_clause)   
    File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\sa\Utils.py", line 53, in swapper     result = wrapper(*args, **kwargs)   
    File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\sa\Functions.py", line 1320, in Wrapper     out_raster)   
    File "c:\program files (x86)\arcgis\desktop10.4\arcpy\arcpy\geoprocessing\_base.py", line 506, in <lambda>     return lambda *args: val(*gp_fixargs(args, True)) 
    ExecuteError: ERROR 999999: Error executing function. 
    An invalid SQL statement was used. An invalid SQL statement was used. [VAT_ABCD_10000_P6_Mos] An invalid SQL statement was used. 
    [SELECT * FROM VAT_ABCD_10000_P6_Mos WHERE 1] Failed to execute (ExtractByAttributes).

Updated code:

inRas = "C:/ABCD_10000_P6_Mos"

rows = arcpy.SearchCursor(inRas, "", "", "", "")
for row in rows:
    luClass = row.getValue("Value")
    sqlQuery = "Value = luClass"
    print (luC)
    rasExtract = ExtractByAttributes(inRas, sqlQuery)
    rasExtract.save (inRas + "_" + str(luClass) + "_P6_Split")
3

Your WHERE statement is malformed:

SELECT * FROM VAT_ABCD_10000_P6_Mos WHERE 1

Looking at the documentation for ExtractByAttributes(in_raster, where_clause) , you should be passing "VALUE = 1" for where_clause rather than just row.getValue("Value") as your code is doing.

The documentation states:

attExtract = ExtractByAttributes("elevation", "VALUE > 1000")

Your updated code looks better but you wrote it wrong, are you getting a new error message now? It should be:

sqlQuery = "Value = " + luClass
  • Can you please comment on my sql query? I am slightly confused about proper syntax of the value field in this loop. – cptpython Sep 23 '16 at 16:09
  • @cptpython updated answer – kttii Sep 23 '16 at 16:45
  • It worked but I had to make add str() to luClass because concatenate works on strings only. Thanks! – cptpython Sep 23 '16 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.