Add data into ArcMap from geodatabase using Python

13362
8
Jump to solution
11-13-2015 07:33 PM
MarcelAfif
New Contributor III

There must be a way to do Add Data from a .gdb using Python. I looked it up but could not find not one hit.

0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

To add to the above responses, below is how this would look in the code. The workflow will vary depending on if you're executing the code in-process or out-of-process.

# Option A : When  you're executing code in-process #
import arcpy

# Use this line if you're not sure if it's already true
arcpy.env.addOutputsToMap = True 

# Executing tool will automatically add layer to map
arcpy.management.MakeFeatureLayer(r"Path\To\GDB\FeatureClass", "NameForLayer") 

# Option B : When you're executing code out-of-process #
import arcpy
# Hook into the map document
mxd = arcpy.mapping.MapDocument(r"Path\To\MapDocument")

# Hook into the data frame where you want to add the layer
df  = arcpy.mapping.ListDataFrames(mxd)[0]

# Create a Layer object
lyr = arcpy.management.MakeFeatureLayer(r"Path\To\GDB\FeatureClass", "NameForLayer").getOutput(0)

# Add the layer object to the map
arcpy.mapping.AddLayer(df, lyr)

Add Layer

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/addlayer.htm

View solution in original post

8 Replies
DanPatterson_Retired
MVP Emeritus

The arcpy function and class tree

Makefeaturelayer​ when you are using python with arcmap open and you have  geoprocessing options set to add results to the display toggled on.

Now, that should be a start, but the process you are using will dictate other options.

RebeccaStrauch__GISP
MVP Emeritus

Make Feature Layer—Help | ArcGIS for Desktop  (Dan provided the Pro version)

Creates a feature layer from an input feature class or layer file. The layer that is created by the tool is temporary and will not persist after the session ends unless the layer is saved to disk or the map document is saved.

....

  • Layers created in ArcCatalog cannot be used in ArcMap unless they are saved to a layer file using the Save To Layer File tool.

The help page includes some sample code.

FreddieGibson
Occasional Contributor III

To add to the above responses, below is how this would look in the code. The workflow will vary depending on if you're executing the code in-process or out-of-process.

# Option A : When  you're executing code in-process #
import arcpy

# Use this line if you're not sure if it's already true
arcpy.env.addOutputsToMap = True 

# Executing tool will automatically add layer to map
arcpy.management.MakeFeatureLayer(r"Path\To\GDB\FeatureClass", "NameForLayer") 

# Option B : When you're executing code out-of-process #
import arcpy
# Hook into the map document
mxd = arcpy.mapping.MapDocument(r"Path\To\MapDocument")

# Hook into the data frame where you want to add the layer
df  = arcpy.mapping.ListDataFrames(mxd)[0]

# Create a Layer object
lyr = arcpy.management.MakeFeatureLayer(r"Path\To\GDB\FeatureClass", "NameForLayer").getOutput(0)

# Add the layer object to the map
arcpy.mapping.AddLayer(df, lyr)

Add Layer

http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/addlayer.htm

MarcelAfif
New Contributor III

MakeFeatureLayer is giving an error:

Input Features: Dataset ...\unified.gdb\feature21 does not exist or is not supported

0 Kudos
FreddieGibson
Occasional Contributor III

1. Are you able to navigate to add this feature class manually in ArcMap?

2. If yes, could you show me the full path to the data?

0 Kudos
MarcelAfif
New Contributor III

Yes, it's in C:\Users\Marcel\Documents\ArcGIS\scratch\unified.gdb\feature21

unified.gdb looks like a folder from the outside, but when I open it, it has all these files with weird extensions (.gdbtable, .gdbindexes, .gdbtblx, etc.). I know, must be one of these two, file database or personal database, just don't know which one.

0 Kudos
FreddieGibson
Occasional Contributor III

What do you get if you run the following command?

arcpy.Exists(r"C:\Users\Marcel\Documents\ArcGIS\scratch\unified.gdb\feature21")

MarcelAfif
New Contributor III

Apparently I wasn't using the "r", so it was giving me False at first. I was able to add the data, thanks.