How do you: Reference SDE Data within Spatial Database Connection???

1482
7
03-17-2011 11:09 AM
AaronPaul
New Contributor II
I can set my env.workspace to my SDE but have not figured out how to do anything with features that are in it.
So, if I can list my Feature Classes within the SDE and see what Versions are available.

How do I use or reference them?

I want to Add them to an existing mxd based on a query like OBJECTID = 2

I know how to do this if the Features are already in the mxd.
I want to access them directly from the SDE Spatial Database Connection.

It seems that I'm almost there, might just need a push in the right direction.

Thanks, AP
Tags (2)
0 Kudos
7 Replies
RussellBrennan
Esri Contributor
Working with SDE data should be the same as working with any other data.

The path to the data can be slightly different but the behavior of the data should be the same. Take a look at this topic:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002t0000000v000000.htm
0 Kudos
MAWashington
New Contributor
All-

From this link I see some differences in working with SDE vs geodatabases or shapefiles.

Variables are assigned to create ArcSDE Connection
These variables include password, version, and more
Then they use CreateArcSDEConnectionFile_management(Using these variables)

At the end a MakeFeatureLayer_management() is used.

I bet you can add the feature layer to a map after all of this is complete.

Am I seeing this right?
Are most of these steps necessary to simply add an SDE Feature Class to an mxd?

If this was just a shapefile we could just use arcpy.mapping.AddLayer

I/we could use further insight if anyone has some. Thanks All
0 Kudos
RussellBrennan
Esri Contributor
You do not need to create the connection file in Python. If you already have a connection file you can reference the existing file.

Here is an example of adding a layer.
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd)[0]
addlayer = arcpy.mapping.Layer(r'Database Connections\Connection to database.sde\pythonDBO.GDB.houses')
arcpy.mapping.AddLayer(df, addlayer,"AUTO_ARRANGE")
0 Kudos
MAWashington
New Contributor
Thank you once again Russell. I hope your solution works, will test and post back yeah or neah.

Maybe a stupid question but here it is.
What do you mean by already having a connection file?

I can make a connection in ArcCatalog, consume the data in ArcMap, and see the Feature Classes in python window using print fcList

Does this mean I already have a connection?

Your code below really simplifies the process, especially after trying to work through link seen below:
http://help.arcgis.com/en/arcgisdesk...000v000000.htm

So all I need to do is reference the Feature Class, then Add to mxd??

Why do you think I can't do this?
What are some typical causes of not being able to reference the data or add it in this case?

Thank you very much.
Your assistnace is greatly appreciated
0 Kudos
RussellBrennan
Esri Contributor
Yes, you already have a connection.

The connection files that you use to access your enterprise geodatabase through ArcMap/ArcCatalog/ArcGlobe etc. are the same files that you use to access your data through Python. These are the connections you create through ArcGIS Desktop when you go to 'Database Connections' > 'Add Database Connection'.

When you are working in ArcGIS Desktop you can think of these connection files as being the same as a file or personal geodatabase. You double click to view the contents and drag and drop contents from these connections into tools or into your map. The only difference is that the SDE Geodatabase has added functionality for managing permissions and editing transactions (more menu options). In Python this is the same story, you reference an existing SDE connection file the same way you reference a file or personal geodatabase 'connection'.

You can think of the 'createArcSDEConnectionFile' tool as something on par with the 'create file geodatabase' or 'create personal geodatabase' tools. You only need to use these tools if your 'connection' (aka file or personal geodatabase or sde connection file) does not already. However if you have database connections or existing file or personal geodatabases you do not need to create them in every script that you write.

I do not know why this is not working. I will need some more information to assist in answering that.
- What is the Python code you are using?
- What DBMS are you using (Oracle, SQL Server, PostGres, DB2, Informix)
- What error messages are you getting?
0 Kudos
MAWashington
New Contributor
This is excellent information and hopefully exactly what I need.
What you're saying is that I already have the connection which is why I'm seeing versions and all of the Feature Classes within my SDE.

My problem is that I've only been able to view them.
I haven't figured out how to reference them.

I've become fairly familiar with arcpy using file geodatabases and shapefiles.

So, I thought I'd be able to do something with the SDE Feature Classes but am unsuccesful at this point.

I'll post the items you requested this evening after testing.

If I have a connection, I should be able to easily add a layer to an mxd but can't.
For now, that's all I want to do.
Once I get a solid hook, I can perform queries and graduate to next levels.

Your help is greatly appreciated Russell.
0 Kudos
MAWashington
New Contributor
Good Afternoon ALL-

The solution was here:
addlayer = arcpy.mapping.Layer(r'Database Connections\Connection to database.sde\pythonDBO.GDB.houses')
arcpy.mapping.AddLayer(df, addlayer,"AUTO_ARRANGE")

My bad, as I was not assigning the layer, then adding to map.

Thank you very much Russell for further explaining.

Your last posts really brought clarity to my oversight.

Enjoy
0 Kudos