Add Feature Class to Map in ArcGIS Pro

2450
9
Jump to solution
01-29-2019 09:42 AM
JaredPilbeam2
MVP Regular Contributor

How do I add a feature class from an SDE to a Map in Pro? This is where most of our data is stored. 

Here's what I've tried:

import arcpy
p = arcpy.mp.ArcGISProject("CURRENT")
map = p.listMaps()[0]

#this returned a ValueError
map.addLayer(r"Database Connections\path\to\gisedit.DBO.Address_Points")

#this returned a RuntimeError
map.addDataFromPath(r"Database Connections\path\to\gisedit.DBO.Address_Points")

‍‍‍‍‍‍‍‍‍‍

All the methods for using classes seem to be referencing layer files, and nowhere do I see a reference to feature classes. 

Alphabetical list of arcpy.mp classes—ArcPy | ArcGIS Desktop 

EDIT:

The confusing thing is that when feature classes from the SDE are in the Map they're referred to as layers. The CURRENT project I'm testing on already has two feature classes from an SDE in it. You can list them with the following:

for lyrname in map.listLayers():
print(lyrname.name)
Reply
0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

In your original post, it looked like you are using the 'Database Connections' ArcGIS application alias.  Have you tried specifying the full Windows file system path to the SDE connection file and feature class?

View solution in original post

9 Replies
JoshuaBixby
MVP Esteemed Contributor

From the interactive Python window in ArcGIS Pro, the following code works for me, i.e., adds a feature class as a new layer in an open map window:

import arcpy

fc = # path to feature class

p = arcpy.mp.ArcGISProject("CURRENT")
map = p.listMaps()[0]
map.addDataFromPath(fc)
<arcpy._mp.Layer object at 0x000002549A35FF28>

Are you getting errors?  What version of Pro are you using?

Reply
0 Kudos
JaredPilbeam2
MVP Regular Contributor

I'm using Pro 2.0.

If you look at line 9 above that's what I did pretty much (also noted the errors above)

Is your feature class coming from a FGDB? Because I'm able to add a feature class from a FGDB. The thing is I'm trying to bring in a feature class from an SDE. 

Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Yes, FGDB feature class.  Regarding your error, it is helpful to provide the full error with traceback.

JaredPilbeam2
MVP Regular Contributor

Ok, Thanks. I'll have to get back to this on Thursday. Our building is being closed because of the cold weather.

Reply
0 Kudos
JaredPilbeam2
MVP Regular Contributor

Joshua,

I ran the same few lines of code that you have, but instead I'm directing the path to a Feature Class in an .SDE.

Here's the full error:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py", line 1248, in addDataFromPath
return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True)))
RuntimeError‍‍‍‍‍‍‍‍‍‍
Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

In your original post, it looked like you are using the 'Database Connections' ArcGIS application alias.  Have you tried specifying the full Windows file system path to the SDE connection file and feature class?

View solution in original post

JaredPilbeam2
MVP Regular Contributor

Yes, that's correct. I copied the path from ArcCatalog. This path is what I used in ArcGIS for Desktop Arcpy scripts and it always worked fine. I'm not sure how to specify the full Windows file system path? 

Reply
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Will it matter, possibly not it could.  Since the effort to specify the full path is trivial, it is good to test it to make sure the issue isn't related. 'Database Connections' is an application-level alias, and ArcGIS Pro doesn't have a similar construct.

Have you tried using a newer version of Pro?  Pro 2.0 is very old by Pro standards, and Esri has been addressing issues in 2.2.x and 2.3 with Oracle enterprise geodatabases.

by Anonymous User
Not applicable

I was working on the same problem with a SQL enterprise geodatabase. I had to use the fully qualified feature class name for it to be added to the map:  DatabaseName.SchemaName.FeatureClassName.