jlangdon

Reclassify raster using variables from a SQL Server table (connecting with pyodbc)

Discussion created by jlangdon on Aug 22, 2012
Latest reply on Sep 13, 2012 by jlangdon
Hello all,

So I am trying to reclass a raster file (representing vegetation and land cover types) using values stored in a SQL Server Express table.  I have a set of ~400 species records in the table, with each row representing a different species, and a column for each vegetation type.  Each vegetation type is coded as either "1" if it is suitable habitat for that species, or "0" if it's unsuitable. The vegetation raster will then be reclassified to two values,  "1" or "0", based on the values associated with the species record (this will be slightly different for each species). 

In PythonWin, I'm using pyodbc to connect to the SQL Server Express database table, then executing a select query statement to collect the values for a species record (row) into the pyodbc cursor.  Then I want to assign each column value to the output raster value in the remap statement (see code attached).  Unfortunately, I keep getting the following error:

TypeError: list indices must be integers, not tuple

RasterCalculator seems to just want integers in the remap statement, not variables, but I don't know how to get around that.  Any ideas?

Thanks!

Jesse Langdon
Research Assistant
University of Washington


# Load Python libraries          
import pyodbc
import arcpy
from arcpy import env
from arcpy.sa import *
import os

arcpy.env.overwriteOutput = 1

# Check out the ArcGIS Spatial Analyst extension license
arcpy.CheckOutExtension("Spatial")

# set variables
modelList = arcpy.ListFiles() # build list of species model names for loop
biome_Cur = ("xxxxxxx/xxxx/xxxxxx/1_Input.gdb/biome_current") # the original raster which will be reclassed

# connect to SQL Server Express database
cnxn = pyodbc.connect('DRIVER={SQL Server Native Client 10.0};SERVER=DESKTOP\SQLEXPRESS;DATABASE=Species;UID=sa;PWD=XXXXXX')
cursor = cnxn.cursor()

# main processing loop
for model in modelList:

    # reclassifies biome raster based on suitability code from SQL Server Species database
    # sql SELECT query
    cursor.execute("""
                    SELECT [BIOME_1],[BIOME_6],[BIOME_7],[BIOME_8],[BIOME_9],[BIOME_10],[BIOME_14],[BIOME_19],[BIOME_20],
                    [BIOME_21],[BIOME_22],[BIOME_23],[BIOME_24],[BIOME_25],[BIOME_27],[BIOME_29],[BIOME_30],[BIOME_31],
                    [BIOME_32],[BIOME_35],[BIOME_36],[BIOME_37],[BIOME_38],[BIOME_39],[BIOME_40],[BIOME_41],[BIOME_42],
                    [BIOME_43],[BIOME_44],[BIOME_45],[BIOME_46],[BIOME_47],[BIOME_48],[BIOME_50],[BIOME_100],[BIOME_200]
                    FROM Species.dbo.BiomesPerSpp_Rehfeldt
                    WHERE ID = ?""", (model))

    # assign remap variable for reclassification
    remap_cur = RemapValue([7, row.BIOME_7][8, row.BIOME_8],[9, row.BIOME_9],[14, row.BIOME_14],[20, row.BIOME_20],
    [21, row.BIOME_21], [22, row.BIOME_22], [23, row.BIOME_23], [25, row.BIOME_25], [30, row.BIOME_30],       
    [31,row.BIOME_31],[32, row.BIOME_32], [36, row.BIOME_36], [38, row.BIOME_38], [41, row.BIOME_41], 
    [42, row.BIOME_42], [43, row.BIOME_43],[44, row.BIOME_44], [45, row.BIOME_45], [46, row.BIOME_46], 
    [47, row.BIOME_47], [50, row.BIOME_50], [100, row.BIOME_100],[200, row.BIOME_200])

    biomeReClass_cur = arcpy.sa.Reclassify(biome_Cur, "Value", remap_cur, "NODATA")  

Outcomes