Select to view content in your preferred language

RemoveLayer not working

659
3
02-13-2020 11:18 AM
by Anonymous User
Not applicable

Hi,

I've tried a few different approach to remove a layer from the TOC, but none so far works.

The ultimate goal is to add the layer to a group, then remove it from the TOC

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]

addLayer =arcpy.mapping.Layer("C:\TEMP\ArcGIS\HighLake2004\Group.lyr")
arcpy.mapping.AddLayer(df,addLayer)

tgl=arcpy.mapping.ListLayers(mxd, "Group", df)[0]

lyr = arcpy.mapping.Layer(OutPutLayer)
arcpy.mapping.AddLayer(df, lyr)

arcpy.mapping.AddLayerToGroup(df,tgl,lyr)


arcpy.mapping.RemoveLayer(df,lyr)
arcpy.RefreshTOC()

Then I have another question, even if I remove them after in ArcGIS, they are still available in my script parameters...

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

where is this defined? OutPutLayer

Also, code formatting will provide line numbers to your code

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
by Anonymous User
Not applicable

InputGrid= arcpy.GetParameterAsText(0)
OutPutGrid = arcpy.GetParameterAsText(1)
Azimut = arcpy.GetParameterAsText(2)
OutPutLayer =OutPutGrid + "_grd"

0 Kudos
by Anonymous User
Not applicable

Hi Dan, Here is th full code :

# -*- coding: utf-8 -*-
# ---------------------------------------------------------------------------
# PYFromModel.py
# Created on: 2020-02-10 13:20:12.00000
#   (generated by ArcGIS/ModelBuilder)
# Usage: PYFromModel <co_27b_res56k_nad83_grd> <tiltd_27a> <res56k_27a_sh> <Azimuth>
# Description:
# ---------------------------------------------------------------------------

# Import arcpy module
import arcpy


# Script arguments

InputGrid= arcpy.GetParameterAsText(0)
OutPutGrid = arcpy.GetParameterAsText(1)
Azimut = arcpy.GetParameterAsText(2)
OutPutLayer =OutPutGrid + "_grd"

##if InputGrid == '#' or not co_27b_res56k_nad83_grd:
##    co_27b_res56k_nad83_grd = "co-27b_res56k-nad83.grd" # provide a default value if unspecified
print(InputGrid)
arcpy.AddMessage(InputGrid)
arcpy.env.workspace = "C:\TEMP\ArcGIS\HighLake2004"
sr = arcpy.SpatialReference("NAD 1983 UTM Zone 12N")


#copy raster
#tempEnvironment0 = arcpy.env.cartographicCoordinateSystem
arcpy.env.cartographicCoordinateSystem = "PROJCS['NAD_1983_UTM_Zone_12N',\
                                            GEOGCS['GCS_North_American_1983',\
                                            DATUM['D_North_American_1983',\
                                            SPHEROID['GRS_1980',6378137.0,298.257222101]],\
                                            PRIMEM['Greenwich',0.0],\
                                            UNIT['Degree',0.0174532925199433]],\
                                            PROJECTION['Transverse_Mercator'],\
                                            PARAMETER['False_Easting',500000.0],\
                                            PARAMETER['False_Northing',0.0],\
                                            PARAMETER['Central_Meridian',-111.0],\
                                            PARAMETER['Scale_Factor',0.9996],\
                                            PARAMETER['Latitude_Of_Origin',0.0],\
                                            UNIT['Meter',1.0]]"
arcpy.CopyRaster_management(InputGrid, OutPutGrid)
arcpy.DefineProjection_management(OutPutGrid,sr)

#arcpy.env.cartographicCoordinateSystem = tempEnvironment0

mxd = arcpy.mapping.MapDocument("CURRENT")
df  = arcpy.mapping.ListDataFrames(mxd)[0]

arcpy.MakeRasterLayer_management(OutPutGrid, OutPutLayer)

arcpy.gp.HillShade_sa(OutPutGrid, OutPutGrid + "_SH", 0, "45", "NO_SHADOWS", "1")

arcpy.MakeRasterLayer_management(OutPutGrid +"_sh",OutPutLayer + "_SH")
addLayer =arcpy.mapping.Layer("C:\TEMP\ArcGIS\HighLake2004\Group.lyr")
arcpy.mapping.AddLayer(df,addLayer)


tgl=arcpy.mapping.ListLayers(mxd, "Group", df)[0]

for lyrs in arcpy.mapping.ListLayers(mxd,"*",df):
    if lyrs.name == 'Group':
        lyrs.name=OutPutGrid


lyr = arcpy.mapping.Layer(OutPutLayer)
arcpy.mapping.AddLayer(df, lyr)
##arcpy.mapping.AddLayerToGroup(df,OutPutGrid,lyr)
arcpy.mapping.AddLayerToGroup(df,tgl,lyr)

#LayersToRemove =[ lyr]
arcpy.mapping.RemoveLayer(df,lyr)
arcpy.RefreshTOC()

lyr = arcpy.mapping.Layer(OutPutLayer + "_SH")
arcpy.mapping.AddLayer(df, lyr)
if lyr.supports("TRANSPARENCY"):
        lyr.transparency = 50
arcpy.mapping.AddLayerToGroup(df,tgl,lyr,"TOP")
arcpy.mapping.RemoveLayer(df,lyr)
arcpy.RefreshTOC()

#LayersToRemove.append( lyr)

#for LayerList in arcpy.mapping.ListLayers(mxd,"*",df):
#    if LayerList.name in (LayersToRemove):
#        arcpy.mapping.RemoveLayer(df,LayerList)
#        arcpy.RefreshTOC()

##'arcpy.mapping.AddLayerToGroup(df,OutPutGrid,lyr)

#arcpy.RefreshTOC()
#arcpy.RefreshActiveView()