Select to view content in your preferred language

Cannot Add FeatureClass from GeoDatabase

1463
7
Jump to solution
12-23-2013 01:23 PM
JonathanMulder
Occasional Contributor
Greetings,

I'm still fairly new to using the arcpy.mapping functions.  I want to add a FeatureClass to my map document which is open.

In looking at other forum threads and reading help, I tried the following code:

import arcpy import arcpy.mapping import os  GeoDatabaseName = "G:\Documents\GIS\HydstraData\HydstraMeasurementsDeep\HydstraMeasurementsDeep_GroundwaterElevation_Daily_Contours_20130625_20120731.gdb"  ##Reference the map document in memory. mxd = arcpy.mapping.MapDocument(r"G:\Documents\GIS\HydstraData\Contours_20131223.mxd") df = arcpy.mapping.ListDataFrames(mxd)[0] print df  FeatureClassToAdd = os.path.join(GeoDatabaseName,"AllPoints") print FeatureClassToAdd tempLayer = "layer" ## Make a layer from the feature class arcpy.MakeFeatureLayer_management(FeatureClassToAdd,tempLayer) addLayer = arcpy.Layer(tempLayer) arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE") arcpy.RefreshTOC()


This is the result I get back in my Python Shell windowing when I run the script in IDLE:

<geoprocessing Data Frame object object at 0x02C57368>
G:\Documents\GIS\HydstraData\HydstraMeasurementsDeep\HydstraMeasurementsDeep_GroundwaterElevation_Daily_Contours_20130625_20120731.gdb\AllPoints

Traceback (most recent call last):
  File "G:/Documents/GIS/HydstraData/UpdateMxd.py", line 24, in <module>
    addLayer = arcpy.Layer(tempLayer)
AttributeError: 'module' object has no attribute 'Layer'

Any suggestions?  Thanks for any help!

Jon Mulder
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JonathanMulder
Occasional Contributor
I FINALLY got this to work!  The syntax for adding a Featureclass was correct.  I believe my problem was that I wasn't referring to the "CURRENT" map document.  The code below works fine and the TOC is refreshed.

import arcpy import arcpy.mapping import os  arcpy.env.workspace = r"C:\Temp"  ##Reference the Current map document. mxd = arcpy.mapping.MapDocument("Current") df = arcpy.mapping.ListDataFrames(mxd)[0] print df  NewLayer = arcpy.mapping.Layer("AllContours3D.shp") print NewLayer arcpy.mapping.AddLayer(df, NewLayer, "AUTO_ARRANGE") arcpy.RefreshTOC()


So what have I learned?  My Python coding so far has involved only geoprocessing functions (NO mapping functions), and I didn't need a session of ArcMap open.  I could just run the script in IDLE, then open ArcCatalog to view the results.  Now that I am working with mapping functions, I NEED to have a map document open and run the Python script WITHIN the ArcMap session?

Does that make sense?  Thanks for your input!

Jon Mulder
California Department of Water Resources

View solution in original post

0 Kudos
7 Replies
WilliamCraft
MVP Alum
It looks like you're trying to add a Feature Layer directly to the table of contents in ArcMap; I'm not 100% sure you can do that.  Is there a specific reason why you're creating a feature layer from your feature class?  If you're open to doing so, please try the following?

arcpy.MakeFeatureLayer_management(FeatureClassToAdd,tempLayer)  -- remove this line
addLayer = arcpy.Layer(FeatureClassToAdd) -- modify "tempLayer" to FeatureClassToAdd
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
arcpy.RefreshTOC()


Let me know if this works for you or not.


EDIT: Ok, I did some research and I think your approach to using a feature layer might in fact be correct.  That being said, if my suggestion above doesn't work then you might try this as an alternative:

GeoDatabaseName = "G:\Documents\GIS\HydstraData\HydstraMeasurementsDeep\HydstraMeasurementsDeep_GroundwaterElevation_Daily_Contours_20130625_20120731.gdb"  -- change the \ slash to a / slash

FeatureClassToAdd = os.path.join(GeoDatabaseName,"AllPoints")  -- it looks like you are appending "AllPoints" to the geodatabase full path; is "AllPoints" your feature class name?  I think you might need a slash between the ".gdb" and the "AllPoints" for this portion of the code.  Try modifying "AllPoints" to "/AllPoints" or "\AllPoints".
 
 
0 Kudos
JonathanMulder
Occasional Contributor
Thanks for the reply; much appreciated.  What I REALLY want to do is simply add the Featureclass that resides in my GeoDatabase.  In reading other threads (such as http://forums.arcgis.com/threads/42567-Insert-Feature-Class-using-arcpy),
it seemed fairly straight forward.  That thread said:

Use makefeaturelayer_management() and use the Layer object to add the layer object to the TOC.

#... other logic
tempLayer = "layer"
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("C:/data/mexico.gdb/cities",tempLayer)
addLayer = arcpy.Layer(tempLayer)
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
arcpy.RefreshTOC()


But for some reason, I can't quite make it work for me.  The ultimate goal is to add in a few feature classes, symbolize them on a previously created and saved ".lyr" file and then do some other processing.
0 Kudos
WilliamCraft
MVP Alum
As far as the part where you wish to add the feature class to your TOC, please refer back to my post and run through the various suggestions.  Let me know if any of those work.
0 Kudos
WilliamCraft
MVP Alum
The ultimate goal is to add in a few feature classes, symbolize them on a previously created and saved ".lyr" file and then do some other processing.


I will also say that, if your ultimate goal is to perform data processing, then you are not required to add your data to a map document.  You can use the GP tools via arcpy and output the resulting data directly to the desired format and skip the map document entirely.  However, if you require the map document for analysis and cartographic purposes, then disregard this specific post.
0 Kudos
JonathanMulder
Occasional Contributor
crafty762,

Thanks very much for your comments, but I'm still struggling with this.  I simplified the code by putting a map document and a stand-alone shapefile in my C:\Temp directory, so I wasn't dealing with the convoluted long directory path.  The code below runs with no errors, but I'm not seeing my TOC populated with the shapefile after it runs.

import arcpy
import arcpy.mapping
import os

arcpy.env.workspace = r"C:\Temp"

##Reference the map document in memory.
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Test.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
print df

NewLayer = arcpy.mapping.Layer("AllContours3D.shp")
print NewLayer
arcpy.mapping.AddLayer(df, NewLayer, "AUTO_ARRANGE")
arcpy.RefreshTOC()

During development, I am writing this code with IDLE, then hitting the "Run" menu and looking at my results in the Python Shell window.  I DO have my Test.mxd open while running it.

Next, I actually saved the Python code as a script, added it to my personal toolbox, and ran the script from WITHIN my Test.mxd.  In either case my TOC does not refresh.

I DO want to see the map updated because this script is ultimately meant to:

  • Add groundwater Contour Lines.

  • Symbolize Contour Lines from previously saved layer file.

  • Make the Contour Lines time-enabled.

  • Run the Time-Slider viewing the contours on a daily basis.


I've manually done the steps above in a map document and everything works fine.  Actually, it's pretty intense to see the groundwater contours wiggle all over the map over a 90 day period (very useful for groundwater extraction analysis).  I just want to fully automate the process so other staff can more easily run the model.
0 Kudos
JonathanMulder
Occasional Contributor
I FINALLY got this to work!  The syntax for adding a Featureclass was correct.  I believe my problem was that I wasn't referring to the "CURRENT" map document.  The code below works fine and the TOC is refreshed.

import arcpy import arcpy.mapping import os  arcpy.env.workspace = r"C:\Temp"  ##Reference the Current map document. mxd = arcpy.mapping.MapDocument("Current") df = arcpy.mapping.ListDataFrames(mxd)[0] print df  NewLayer = arcpy.mapping.Layer("AllContours3D.shp") print NewLayer arcpy.mapping.AddLayer(df, NewLayer, "AUTO_ARRANGE") arcpy.RefreshTOC()


So what have I learned?  My Python coding so far has involved only geoprocessing functions (NO mapping functions), and I didn't need a session of ArcMap open.  I could just run the script in IDLE, then open ArcCatalog to view the results.  Now that I am working with mapping functions, I NEED to have a map document open and run the Python script WITHIN the ArcMap session?

Does that make sense?  Thanks for your input!

Jon Mulder
California Department of Water Resources
0 Kudos
WilliamCraft
MVP Alum
Makes sense, yes.  That's kind of why I made my comment above about requiring the map document versus not.  I'm very glad that you got things working.  Good luck and happy holidays.
0 Kudos