This question has been asked multiple times with proper answers:
- How to add feature class to MXD with ArcPy (Python)?
- Adding shapefile or feature class as layer in ArcGIS Desktop using Python/ArcPy?
- Adding feature class into ArcGIS 10.3 mxd using python script
but I still cannot get it right.
I am using ArcGIS 10.2.2.
I want to add via a Python standalone script a Featureclass from a file-gdb to the TOC. If I try it in the ArcGIS Python window it works fine. However in a standalone script nothing happens. There is no error message or anything. I already thought it might be a bug, but it seems to work for everyone else. I also tried it with a .lyr and it didn ́t work either. Someone got a hint for me?
Here is the code (I also tried the one from the ArcGIS help site, and from all the links above).
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\KUP\Check.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Ergebnisse.gdb\Potenziale_singlepart_final")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
I also tried :
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Potenziale_singlepart_final.lyr")
What am I doing wrong? I am trying for a week now. I`m getting crazy with this one. Seems so easy.
-
3You need to save the mxd afterward to make the changes permanent. Use mxd.save() or mxd.saveACopy (file_name, {version}) ... see resources.arcgis.com/en/help/main/10.1/index.html#//…Michael Stimson– Michael Stimson2015年09月09日 23:40:24 +00:00Commented Sep 9, 2015 at 23:40
-
@MichaelMiles-Stimson by "nothing happens" I think Sebastian is saying that the layer isn't added to the map at all (not that the change isn't permanent)?Stephen Lead– Stephen Lead2015年09月10日 00:03:34 +00:00Commented Sep 10, 2015 at 0:03
-
yes thats rightSebastian– Sebastian2015年09月10日 00:07:23 +00:00Commented Sep 10, 2015 at 0:07
-
2As you're creating a mapdocument object from a static (on disc) map and not "Current" the changes are not permanent, when you open the map document it appears that nothing has happened because the changes are discarded as soon as the mapdocument goes out of scope - at least that's my understanding of how that object works. Try saving it and see if the layer is added. The code shown is sound, it should work, so unless there's some code that isn't shown that is problematic the only logical conclusion is that the changes are made but not committed.Michael Stimson– Michael Stimson2015年09月10日 00:15:14 +00:00Commented Sep 10, 2015 at 0:15
-
1You cannot use "CURRENT" in a standalone script, only when you have a map open in ArcMap. If that works for you then I think you are doing something slightly different to what you have presented in your code snippet and question description.PolyGeo– PolyGeo ♦2015年09月10日 01:15:21 +00:00Commented Sep 10, 2015 at 1:15
1 Answer 1
On the code snippet and symptoms presented in the question:
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\GIS\KUP\Check.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]
addLayer = arcpy.mapping.Layer(r"C:\GIS\KUP\Ergebnisse.gdb\Potenziale_singlepart_final")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
it seems to me, like @MichaelMilesStimson has commented, that you are missing the line of code at the end which is necessary to persist any changes made to the MXD you open as a Map Document object:
mxd.save()
or, if you prefer:
mxd.saveACopy (file_name, {version})
Explore related questions
See similar questions with these tags.