recurvata

Need SDE connection?

Discussion created by recurvata on Jul 18, 2012
Latest reply on Jul 19, 2012 by mzcoyle
The script below runs fine when all the feature classes are in the same geodatabase (TestCases_Geodatabase.gdb). But for production, one of the features, Parcel, is in a SDE geodatabase. This fails, however. Not sure if problem is in the workspace setting, feature class name, if I need a connection string to the SDE, or what. The actual feature class name is in a feature dataset in the db and is named CitySDE.GIS.Parcel. If I use the name CitySDE.GIS.Parcel in the line parcel =, I get error 1; if I change the line to parcel = "Parcel" (the name in the TOC), I get error 2.

Any help greatly appreciated. Thanks.

Error 1 - parcel = "CitySDE.GIS.Parcel"
Executing: CreateCaseFeature del1
Start Time: Wed Jul 18 10:52:30 2012
Running script CreateCaseFeature...
Case number: del1
Checking for previous layer
<class 'arcgisscripting.ExecuteError'>: Failed to execute. Parameters are not valid.
ERROR 000732: Input Rows: Dataset TempleSDE.GIS.Parcel does not exist or is not supported
Failed to execute (GetCount).
...


Error 2 - parcel = "Parcel"

Dissolve complete...Appending to Cases
Error adding parcels to Cases in SelectCases(). 
Failed to execute. Parameters are not valid.
The value cannot be a feature class
ERROR 000840: The value is not a Raster Catalog Layer.
ERROR 000840: The value is not a Mosaic Layer.
Failed to execute (SelectLayerByLocation).

Adding Case number...
Case number del1 added.
Setting definition query to: "Case_" = 'del1'
<type 'exceptions.UnboundLocalError'>: local variable 'layer' referenced before assignment
Failed to execute (CreateCaseFeature).
Failed at Wed Jul 18 11:03:02 2012 (Elapsed Time: 43.00 seconds)



import arcpy, sys
from arcpy import env

env.workspace = r"\\ServerA\Planning\Cases_Geodatabase.gdb"
#env.workspace = r"\\ServerA\Planning\TestCases_Geodatabase.gdb"
env.overwriteOutput = True
case_number = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Case number: " + case_number)
lyr = r"\\ServerA\Planning\Cases_Geodatabase.gdb\temp_parcel_lyr"
#lyr = r"\\ServerA\Planning\TestCases_Geodatabase.gdb\temp_parcel_lyr"
parcel = "CitySDE.GIS.Parcel"
mxd = arcpy.mapping.MapDocument("CURRENT")

#-------------------------------------------------------------------------------

def MakeCaseFeature():
    '''  Dissolve selected parcel features into one temporary lyr file.
         Append it to the Cases feature class.
         Delete the temporary lyr file.
         Clear the parcel selection. '''


    clear = "CLEAR_SELECTION"
    target = "Cases"
    schemaType = "NO_TEST"
    fld = "CENTRACT"    # no data is stored in this field
    selectType = "HAVE_THEIR_CENTER_IN"

    try:
        arcpy.AddMessage("Starting Dissolve...")

        # make temporary parcel lyr file from selected parcels, dissolving them
        # into one feature
        arcpy.Dissolve_management(parcel, lyr, fld)

        arcpy.AddMessage("Dissolve complete...Appending to Cases")

        # add case feature in temporary layer to Cases
        arcpy.Append_management(lyr, target, schemaType, "", "")

        # select new case feature
        arcpy.SelectLayerByLocation_management(target, selectType, lyr)

        # delete temporary layer
        arcpy.Delete_management(lyr)

        arcpy.AddMessage("Append complete...")

        # clear selection on parcels
        arcpy.SelectLayerByAttribute_management(parcel, clear)

    except Exception as e:
        arcpy.AddError("Error adding parcels to Cases in SelectCases(). \n" + e.message)

#-------------------------------------------------------------------------------
# Add case number to case_ field in Cases
#

def AddCaseNumber():
    fc = "Cases"
    fld = "Case_"

    arcpy.AddMessage("Adding Case number...")

    try:
        arcpy.CalculateField_management(fc, fld, '"%s"' % case_number)
        arcpy.AddMessage("Case number " + case_number + " added.")
    except Exception as e:
        arcpy.AddError("Error setting Case_ field value to case number: " + case_number +  "\n" + e.message)

#-------------------------------------------------------------------------------
# Set the definition query to display only the new case
#

def SetDefinitionQuery():
    clear = "CLEAR_SELECTION"
    fc = "Cases"
    fld = "Case_"
    query = "\"Case_\" = '%s'" % case_number
    # alternate formatting methods for query
    # query = "\"%s\" = '%s'" % (fld, case_number)
    # query = '"%s"' % fld + " = " + "'%s'" % case_number
    mxd = arcpy.mapping.MapDocument("CURRENT")

    arcpy.AddMessage("Setting definition query to: " + query)

    try:
        arcpy.RefreshActiveView()
        for layer in arcpy.mapping.ListLayers(mxd, fc):
            layer.definitionQuery = query

        # clear selection on parcels
        arcpy.SelectLayerByAttribute_management(fc, clear)

    except Exception as e:
        arcpy.AddError("Error setting definition query. \n" + layer.definitionQuery + "/n" + e.message)

    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    mxd.save()
    del mxd

#-------------------------------------------------------------------------------
# Start 'er up!
#

def main():
    arcpy.AddMessage("Checking for previous layer")
    if arcpy.Exists(lyr):
        arcpy.Delete_management(lyr)

    # Check how many parcels are selected. Count returns all parcels if none are
    # selected, or only selected ones if there are any. Check returns a list of
    # selected parcels, or 0 if none are selected.
    count = int(arcpy.GetCount_management(parcel).getOutput(0))
    desc = arcpy.Describe(parcel)
    check = desc.FIDSet

    # make sure parcels are selected before running rest of script, otherwise exit
    if len(check) != 0:
        arcpy.AddMessage("There are " + str(count) + " parcels selected \n" )
        for layer in arcpy.mapping.ListLayers(mxd, "Cases"):
            layer.definitionQuery = ""
        MakeCaseFeature()       # create the case feature
        AddCaseNumber()         # add case number to the case field
        SetDefinitionQuery()    # only display current case

        arcpy.RefreshActiveView()
        arcpy.RefreshTOC()

    else:
        arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n")
        arcpy.AddError("Quitting the Create Case tool \n")


if __name__ == '__main__':
    main()

Outcomes