使用SQL Server表中的变量重新分类ESRI栅格(使用pyodbc连接)

时间:2012-08-23 04:46:14

标签: arcgis pyodbc esri arcpy

我正在尝试使用存储在SQL Server Express表中的值重新分类光栅文件(表示植被和土地覆盖类型)。表中有一组约400种记录,每行代表不同的种类,每种植被类型都有一列。如果它是适合该物种的栖息地,则每种植被类型编码为“1”,如果不适合,则编码为“0”。根据与物种记录相关的值,植被栅格将被重新分类为两个值“1”或“0”(对于每个物种,这将略有不同)。

在PythonWin中,我使用pyodbc连接到SQL Server Express数据库表,然后执行select查询语句以将物种记录(行)的值收集到pyodbc游标中。然后我想将每个列值分配给remap语句中的输出栅格值(请参阅附带的代码)。不幸的是,我一直收到以下错误:

TypeError: list indices must be integers, not tuple

光标具有基于select查询的SQL Server Express表中的所有值,我可以通过游标对象的row参数访问数据。因此row.BIOME_X变量肯定存储1或0值。或者我只是用Python语法做一些愚蠢的事情。有什么想法吗?

谢谢!

# 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
    # 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 = arcpy.sa.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")

1 个答案:

答案 0 :(得分:1)

您在RemapValue下列出的7到8项之间省略了逗号。 您还需要将整个列表列表括在括号内。

remap_cur = arcpy.sa.RemapValue([[7, row.BIOME_7],[8, row.BIOME_8]...[200, row.BIOME_200]])

有关详情,请参阅:http://resources.arcgis.com/en/help/main/10.1/index.html#//005m0000007q000000