Sunday 3 February 2019

arcmap - Accessing ArcSDE feature class with ArcPy?


I have a script that runs perfectly with shapefiles and GDB feature classes but I need to change it to run on feature classes stored on an SDE. However I cannot figure out how to reference them correctly via ArcPy and I haven't been able to find a good resource. Here is what I have so far:


C:/Users/cbunn/AppData/Roaming/ESRI/Desktop10.5/ArcCatalog/my.sde//FeatureDataset//FeatureClass


How should I change this path so it is correct and I can access it via ArcPy?



Answer



If you navigate to the feature class in question in the catalog window of ArcMap or in ArcCatalog, the "Location:" field should contain the valid full path for the feature class that you can copy and paste into your script. But in general for data stored in an SDE database, Feature Datasets and Feature Classes will have a full name of Database.Schema.Dataset


Example:



If you had a database called MyDatabase and it used the SDE schema, the path may look like


fc = r'C:\Folder\Subfolder\MyDatabase.sde\MyDatabase.OwnerSchema.FeatureDatasetName\MyDatabase.OwnerSchema.FeatureClassName'

or, since the database is a property of the connection,


fc = r'C:\Folder\Subfolder\MyDatabase.sde\OwnerSchema.FeatureDatasetName\OwnerSchema.FeatureClassName'

Feature classes outside a feature dataset would be one of:


fc1 = r'C:\Folder\Subfolder\MyDatabase.sde\MyDatabase.OwnerSchema.FeatureClassName'
fc2 = r'C:\Folder\Subfolder\MyDatabase.sde\OwnerSchema.FeatureClassName'
fc3 = r'C:\Folder\Subfolder\MyDatabase.sde\FeatureClassName'


(the last only valid if the connection user is the owning schema)


Similarly, if you used the DBO schema with SQL Server, it may look like:


fc1 = r'C:\Folder\Subfolder\MyDatabase.sde\MyDatabase.DBO.FeatureDatasetName\MyDatabase.DBO.FeatureClassName'
fc2 = r'C:\Folder\Subfolder\MyDatabase.sde\DBO.FeatureDatasetName\DBO.FeatureClassName'
fc3 = r'C:\Folder\Subfolder\MyDatabase.sde\MyDatabase.DBO.FeatureClassName'
fc4 = r'C:\Folder\Subfolder\MyDatabase.sde\DBO.FeatureClassName'

Also, when using a file path in Python, you have a few options for how you format the path.




  • You can flip the slash one for one (change \ to / ) as you appear to have done in your example. Except, after my.sde you doubled them up, and they should still be single forward slashes.

  • You can double up backslashes (ex: C:\\Folder\\Subfolder\\File.txt)

  • Or you can use the full path, exactly as is, and just denote it as a raw string by preceding it with r. Ex: r'C\Folder\Subfolder\File.txt'


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...