<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need SDE connection? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99749#M7735</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When you are making selections you need to make your feature classes layers.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 18 Jul 2012 21:58:01 GMT</pubDate>
    <dc:creator>MathewCoyle</dc:creator>
    <dc:date>2012-07-18T21:58:01Z</dc:date>
    <item>
      <title>Need SDE connection?</title>
      <link>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99748#M7734</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help greatly appreciated. Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Error 1 - parcel = "CitySDE.GIS.Parcel"&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
Executing: CreateCaseFeature del1
Start Time: Wed Jul 18 10:52:30 2012
Running script CreateCaseFeature...
Case number: del1
Checking for previous layer
&amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: 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).
...
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Error 2 - parcel = "Parcel"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
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'
&amp;lt;type 'exceptions.UnboundLocalError'&amp;gt;: local variable 'layer' referenced before assignment
Failed to execute (CreateCaseFeature).
Failed at Wed Jul 18 11:03:02 2012 (Elapsed Time: 43.00 seconds)
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
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():
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''&amp;nbsp; Dissolve selected parcel features into one temporary lyr file.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Append it to the Cases feature class.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Delete the temporary lyr file.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Clear the parcel selection. '''


&amp;nbsp;&amp;nbsp;&amp;nbsp; clear = "CLEAR_SELECTION"
&amp;nbsp;&amp;nbsp;&amp;nbsp; target = "Cases"
&amp;nbsp;&amp;nbsp;&amp;nbsp; schemaType = "NO_TEST"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld = "CENTRACT"&amp;nbsp;&amp;nbsp;&amp;nbsp; # no data is stored in this field
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectType = "HAVE_THEIR_CENTER_IN"

&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Starting Dissolve...")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make temporary parcel lyr file from selected parcels, dissolving them
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # into one feature
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Dissolve_management(parcel, lyr, fld)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Dissolve complete...Appending to Cases")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # add case feature in temporary layer to Cases
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Append_management(lyr, target, schemaType, "", "")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # select new case feature
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(target, selectType, lyr)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # delete temporary layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(lyr)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Append complete...")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # clear selection on parcels
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(parcel, clear)

&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Error adding parcels to Cases in SelectCases(). \n" + e.message)

#-------------------------------------------------------------------------------
# Add case number to case_ field in Cases
#

def AddCaseNumber():
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = "Cases"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld = "Case_"

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Adding Case number...")

&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(fc, fld, '"%s"' % case_number)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Case number " + case_number + " added.")
&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Error setting Case_ field value to case number: " + case_number +&amp;nbsp; "\n" + e.message)

#-------------------------------------------------------------------------------
# Set the definition query to display only the new case
#

def SetDefinitionQuery():
&amp;nbsp;&amp;nbsp;&amp;nbsp; clear = "CLEAR_SELECTION"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fc = "Cases"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld = "Case_"
&amp;nbsp;&amp;nbsp;&amp;nbsp; query = "\"Case_\" = '%s'" % case_number
&amp;nbsp;&amp;nbsp;&amp;nbsp; # alternate formatting methods for query
&amp;nbsp;&amp;nbsp;&amp;nbsp; # query = "\"%s\" = '%s'" % (fld, case_number)
&amp;nbsp;&amp;nbsp;&amp;nbsp; # query = '"%s"' % fld + " = " + "'%s'" % case_number
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Setting definition query to: " + query)

&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer in arcpy.mapping.ListLayers(mxd, fc):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = query

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # clear selection on parcels
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(fc, clear)

&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Error setting definition query. \n" + layer.definitionQuery + "/n" + e.message)

&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd.save()
&amp;nbsp;&amp;nbsp;&amp;nbsp; del mxd

#-------------------------------------------------------------------------------
# Start 'er up!
#

def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Checking for previous layer")
&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.Exists(lyr):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(lyr)

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Check how many parcels are selected. Count returns all parcels if none are
&amp;nbsp;&amp;nbsp;&amp;nbsp; # selected, or only selected ones if there are any. Check returns a list of
&amp;nbsp;&amp;nbsp;&amp;nbsp; # selected parcels, or 0 if none are selected.
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = int(arcpy.GetCount_management(parcel).getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = arcpy.Describe(parcel)
&amp;nbsp;&amp;nbsp;&amp;nbsp; check = desc.FIDSet

&amp;nbsp;&amp;nbsp;&amp;nbsp; # make sure parcels are selected before running rest of script, otherwise exit
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(check) != 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("There are " + str(count) + " parcels selected \n" )
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for layer in arcpy.mapping.ListLayers(mxd, "Cases"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; layer.definitionQuery = ""
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MakeCaseFeature()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # create the case feature
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AddCaseNumber()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # add case number to the case field
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetDefinitionQuery()&amp;nbsp;&amp;nbsp;&amp;nbsp; # only display current case

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()

&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Quitting the Create Case tool \n")


if __name__ == '__main__':
&amp;nbsp;&amp;nbsp;&amp;nbsp; main()
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2012 15:49:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99748#M7734</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2012-07-18T15:49:14Z</dc:date>
    </item>
    <item>
      <title>Re: Need SDE connection?</title>
      <link>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99749#M7735</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;When you are making selections you need to make your feature classes layers.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 18 Jul 2012 21:58:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99749#M7735</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-07-18T21:58:01Z</dc:date>
    </item>
    <item>
      <title>Re: Need SDE connection?</title>
      <link>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99750#M7736</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;When you are making selections you need to make your feature classes layers.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks, appreciate it. Which feature classes? I create a temporary layer to hold the preliminary Case feature before adding it to the feature class. Also, both features - Case and Parcel - are feature classes in the TestCases_Geodatabase, where it runs fine, so I don't know why it wouldn't run in production, except that Parcel is in a sde database there. That's the only difference I can think of.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It would be inconvenient to make the sde Parcel feature class a lyr file. It contains tens of thousands of records that are updated fairly frequently. I'd have to recreate it every time the script is run. The purpose of this script is to automate a task for another department where the users don't have a lot of Arc experience.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Jul 2012 18:26:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99750#M7736</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2012-07-19T18:26:03Z</dc:date>
    </item>
    <item>
      <title>Re: Need SDE connection?</title>
      <link>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99751#M7737</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The "CitySDE.GIS.Parcel" I assume is the name of the feature class not the layer "Parcels" in your mxd correct? What you want to do is your second case where you have "Parcels" as the referenced layer the user is making the selection on.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The error you are receiving in your second case is that &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;target = "Cases"&lt;/PRE&gt;&lt;SPAN&gt; references a feature class in &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;env.workspace = r"\\ServerA\Planning\Cases_Geodatabase.gdb"&lt;/PRE&gt;&lt;SPAN&gt; not the layer in your mxd. So you can either references the layer in the mxd that is pointing to the "Cases" feature class, or you can create a feature layer using &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000"&gt;Make Feature Layer&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 19 Jul 2012 18:57:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/need-sde-connection/m-p/99751#M7737</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-07-19T18:57:33Z</dc:date>
    </item>
  </channel>
</rss>

