1
## Import required tools...  
import arcpy
arcpy.env.overwriteOutput = True

## Set workspace environment
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
arcpy.env.workspace = "E:\Dropbox\GIS\MAS-GIS\GIS 601-602\gisclass\mgisdata\Labs\Python\PythonPractical.gdb"

## Define starting layers
layer1 = "Oregon_Boundries"
layer2 = "Parks"
layer3 = "Highways"
layer4 = "Park_Fires"

## Draw layers to ArcMap
dataSetLayers = [layer1, layer2, layer3, layer4]

for x in dataSetLayers:
    updateLyr = arcpy.mapping.Layer(x)
    arcpy.mapping.AddLayer(df, updateLyr, "TOP")

## Process: Intersect Parks and Fires
inFeatures = ["Parks", "Park_Fires"]
intersectOutput = "Park_Fires_Intersect"
arcpy.Intersect_analysis(inFeatures, intersectOutput)

layerList = arcpy.mapping.ListLayers(mxd)

arcpy.Delete_management(layer4)

When I paste into the python window in Arcmap, it adds the 4 layers to the map, does an Intersect on two of them, then removes a layer. Works great. When I use this same script from a toolbox where I "Added Script", it skips adding the 4th layer and doesn't do an Intersect at all. I'm not sure why the difference.

I'd like to mention I'm new to ArcMap, GIS, and Python. I just started school for GIS.

4 Answers 4

5

The python window and a standalone script are inherently different. The Python window uses built-in Arcmap behaviors that understand layers, because it natively exists in the Arcmap environment. Standalone scripts have no abilities to automate layer creation since they completely lack the Arcmap context. Standalone scripts are pure Python. In a standalone script you have to explicitly do every step that the Python window knows how to do implicitly. Therefore you have to use the Make Feature Layer tool to connect a data source to a map layer. You need to search the help for how to create and add layers in a script. You cannot rely on any automatic Arcmap behaviors in a standalone script, so assigning variable names in a script without using Make Feature Layer does no lookup of those names in the workspace and ends up trying to add a Python string variable to a map with no data connection.

1
  • Sounds good, let me look into that more. Thanks a lot! Sep 26, 2016 at 1:30
0

I think your question may be the same as Delete layer from ArcMap using Python

I am not at all sure that you want to Delete using the layer name "Park_Fires" because I think you will find that deletes the data source (probably a feature class or shapefile) of that layer.

7
  • It seems to only delete it from the map, not the actual file in the geodatabase. But when I run the script from the toolbox, layer4 = "Park_Fires" doesn't even get added. It seems so weird. Why? Then nothing afterwards is added, or Intersected. When the same code copied into the python window and run works fine. Sep 26, 2016 at 0:16
  • arcpy.Delete_management() shouldn't delete any actual data if you only specify a layer name - it is used in this way to remove layers created by arcpy.MakeFeatureLayer. While it may work to remove a layer from arcmap, I think arcpy.mapping.RemoveLayer() would be the better tool to use here.
    – Midavalo
    Sep 26, 2016 at 0:24
  • Can I use arcpy.mapping.RemoveLayer() without the for/if, etc? Sep 26, 2016 at 0:27
  • @PhilHanson I don't see why not
    – Midavalo
    Sep 26, 2016 at 0:31
  • I did try that but couldn't get it to work, which is why I went with arcpy.Delete_management lol. Issue is though the script never gets to that point for some reason, so the removal is kinda moot atm. Sep 26, 2016 at 0:49
0

Your script works as-is for me, from the python window and from a python script tool. It did require a refresh of my MXD to make things visible though. Through the python window it automatically added the intersected feature class, through the script-tool I had to add that step.

You can add the refresh into the script tool so it refreshes automatically, and need to expand on your arcpy.ListLayers() to be able to use arcpy.mapping.RemoveLayer() but it's not a lot of extra code to get these to work.

The first half of your script is fine, so here are just the bits I changed/added

## Process: Intersect Parks and Fires
inFeatures = ["Parks", "Park_Fires"]
intersectOutput = "Park_Fires_Intersect"
arcpy.Intersect_analysis(inFeatures, intersectOutput)
addIntersect = arcpy.mapping.Layer(intersectOutput) # Intersect Layer to add
arcpy.mapping.AddLayer(df, addIntersect, "TOP") # Add the Intersect Layer

# Remove layer4 (Park_Fires)
layerToRemove = arcpy.mapping.ListLayers(mxd, layer4)[0] # Find the layer4
arcpy.mapping.RemoveLayer(df, layerToRemove) # Remove layer4

# Refresh ArcMap
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
0

You need to use MakeFeatureLayer_management to add your layers. You should also use RemoveLayer instead of delete, unless you want to actually delete the feature class. Note that I've imported the os module:

## Import required tools...  
import arcpy
import os
arcpy.env.overwriteOutput = True

## Set workspace environment
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
gdb = r"E:\Dropbox\GIS\MAS-GIS\GIS 601-602\gisclass\mgisdata\Labs\Python\PythonPractical.gdb"
arcpy.env.workspace = gdb

## Define starting layers
layer1 = os.path.join(gdb,"Oregon_Boundries")
layer2 = os.path.join(gdb,"Parks")
layer3 = os.path.join(gdb,"Highways")
layer4 = os.path.join(gdb,"Park_Fires")


## Draw layers to ArcMap
dataSetLayers = [layer1, layer2, layer3, layer4]

for x in dataSetLayers:
    updateLyr = arcpy.mapping.Layer(x)
    arcpy.mapping.AddLayer(df, updateLyr, "TOP")

## OR do it this way:
layer1 = arcpy.MakeFeatureLayer_management(layer1,"Oregon_Boundries")
layer2 = arcpy.MakeFeatureLayer_management(layer2,"Parks")
layer3 = arcpy.MakeFeatureLayer_management(layer3,"Highways")
layer4 = arcpy.MakeFeatureLayer_management(layer4,"Park_Fires")

## Process: Intersect Parks and Fires
inFeatures = [layer2, layer3]
intersectOutput = os.path.join(gdb,"Park_Fires_Intersect")
arcpy.Intersect_analysis(inFeatures, intersectOutput)

layerList = arcpy.mapping.ListLayers(mxd)

arcpy.mapping.RemoveLayer(layer4)
arcpy.RefreshActiveView()
3
  • I believe the updateLyr = arcpy.mapping.Layer(x) removes the need for a MakeFeatureLayer()
    – Midavalo
    Sep 26, 2016 at 4:23
  • @Midavalo you're correct. I had removed that part of his code. I think all he needed was the full paths and the refresh view.
    – jbalk
    Sep 26, 2016 at 4:42
  • not even the full path as it all pulls via the arcpy.env.workspace anyway
    – Midavalo
    Sep 26, 2016 at 5:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.