Hello everybody,
I'm looking for a solution with which it is possible to add a point (x,y) into a SDE database.
I want to write a skript or add-in for it, so i prefer python (arcpy).
At first a need a way to connect to the database. Any ideas ?
I allready tried the insert cursor but this only works for a geodatabase not a SDE database.
I'm thankful for any help. Thanks.
Set up the connection file using the usual tools in ArcCatalog.
Then set a pointer to this in your python script.
like :
ConnectFile = "Path & name of my connection file.sde"
arcpy.env.workspace = ConnectFile
Then you are in....
Thanks for your fast answer.
I set up the databaseconnection using ArcCatalog and found the connection file.
So this works thanks.
But how can I access to different tables to add or read some data?
Once you are in and have the correct permissions you can do anything. So be careful!
I normally do this in the Python ILDE, try -
arcpy.ListTables() or arcpy.ListFeatureClasses() for example.
There is plenty of code snippets in the help or on this site to get going with.
What are you trying to do?
Ok I try a little bit.
I have a shape file which contains points and I have to copie all of these into a given table of the sde database.
At this point in time I'm able to get alle points out of the shape file and now I try to get these in the database.
If you already have spatial data (shapefile) why don't you just import it into your db. Or use the copy features tool.
If you want to use scripting to create and write geometries check out:
CreateFeatureClasses :
http://resources.arcgis.com/en/help/main/10.2/index.html#//00170000002p000000
da Cursors :
http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000001q000000
Writing geometries :
http://resources.arcgis.com/en/help/main/10.2/index.html#//002z0000001v000000
etc etc.
No thats no so easy as it sounds. First of all I use a customize version of ArcGis and the hole process is more complex than described, because not all points has to write into the same table.
Now I try to use the insert cursor but I have some problems with the path variables.
I want to access a feature class which is part of a diffrent dataset. So I tried this:
fc = "connectionFile.sde\database.sde\dataset\featureclass"
icur = arcpy.da.insertCursor(fc, "SHAPE@
")
But this doesn't work. I got a error message "RuntimeError: cannot open 'connectionFile.sde\database.sde\datasete\featureclass'".
Is the path correct ? In another thread I read that I have to use points link this :
fc = "connectionFile.sde\database.sde.dataset.featureclass"
But this dosen't work either and I get the same error message.
As I said before use the env.workspace to go inside your db.
Then use arcpy.ListFeatureClasses()
to see a list of the features available. This will give you the correct spelling of the feature you are trying to update including all the extra bits that are part of a sde feature (database name etc)
Then you know what fc should be called.
Once that be is established, try reading it first.
First of all verify the location and name of you sde (connection) file:
On Windows XP or Server 2003, navigate to Documents and Settings > <user_name> > Application Data > ESRI > Desktop<release#> > ArcCatalog.
On Windows Vista, 7, or Server 2008, navigate to Users > <user_name> > AppData > Roaming > ESRI > Desktop<release#> > ArcCatalog.
The file has an extension of .sde.
Example I have one here:
conn = r"C:\Users\xbakker\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\NameOfMyConnectionFile.sde"
To make use of the ListTables and ListFeatureClasses(), you will have to set your connection file to be the current workspace. You can do this like this:
arcpy.env.workspace = conn
Since featureclasses can reside in datasets you should list the available datasets first;
lst_fds = arcpy.ListDatasets()
lst_fds.append("")
The second line is to append an empty string to the list of feature datasets, since featureclasses may reside outside a dataset. Next go through each dataset in the list and create for each dataset a list of featureclasses:
for fds in lst_fds:
fcs = arcpy.ListFeatureClasses(feature_dataset=fds)
Using this list of featureclasses you can loop through each featureclass in that list and do something with it:
for fc in fcs:
print fc
So the script to, in this case, print the names of all the featureclasses in the enterprise geodatabase would be:
import arcpy
# name and location of your connection file
conn = r"C:\Users\xbakker\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\NameOfYourConnectionFile.sde"
# set the current workspace
arcpy.env.workspace = conn
# create a list of feature datasets and append an empty string to the list
lst_fds = arcpy.ListDatasets()
lst_fds.append("")
# loop through the list of feature datasets
for fds in lst_fds:
# list the featureclasses in the current dataset
fcs = arcpy.ListFeatureClasses(feature_dataset=fds)
# loop through each featureclass in the current dataset
for fc in fcs:
# do somethig with the featureclass
print fc
Kind regards, Xander