Visible Some Layers by Python

934
6
09-17-2018 03:00 AM
ZakirmanIbros
New Contributor II

I am newbie. I want to learn step by step.

Please advice me.

In my Data Frame  (Current mxd) I have some layers : A, B, C, D and Group  Layer E, F, G, H

How to make python scrip to ArcGiS 10.4:

1. Make visible Layer A and B and invisible Layer B and C

2. Make visible Group Layer E and F and invisible Group Layer G adn H

Thanks

Zibros

0 Kudos
6 Replies
PanagiotisPapadopoulos
Esri Regular Contributor
This is a sample code to do it
 

#A list of layer names that you want to be turned off.
names = [x,y,z,etc] 
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(, mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)

for layer in layers:  
   if layer.name in names:    
      layer.visible = False 

arcpy.RefreshTOC()
arcpy.RefreshActiveView()


Reference
arcgis 10.1 - Turning off layers in ArcMap via ArcPy? - Geographic Information Systems Stack Exchang... 

ZakirmanIbros
New Contributor II

Thanks very much, it works.

I have a idea to run the script python which is triggered from Driven Pages, 

My driven page shapefile have a field I called "pyScript", I have some records with same extent, for each record (row) have different title of the map, of course the layer needed is also diffrent. For example, first record is landuse, second record is forest status, third record is geology and so on.  I want when I navigate to the driven page record the script on the attribute run automatically to turn off or turn on the certain layer or layer group in accordance with the title of the map. Do you think possible to make this idea? If yes how to make this.

Thanks

Zibros

0 Kudos
PanagiotisPapadopoulos
Esri Regular Contributor

Technically you can do something like this.

You have to try access the records from the Feature Class (http://pro.arcgis.com/en/pro-app/arcpy/get-started/data-access-using-cursors.htm) and set up the map according to the fields you mention using the previous python script.

0 Kudos
ZakirmanIbros
New Contributor II

I can't understand yet data-access-using-cursors, related with my idea, 

Let say we have "Driven_pages.shp" with fields "Title" and "pyScript" and have 2 rectangle polygons overlaping.

And we have two layers Landcover and Geology

Driven_Pages.shp

Row 1st

The attribute of Title = Landcover 

The attribute of pyScript = 

names = [Geology] 
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(, mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)
for layer in layers:  
   if layer.name in names:    
      layer.visible = False 
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

Row 2nd

The attribute of Title = Geology 

The attribute of pyScript = 

names = [Landcover] 
mxd = arcpy.mapping.MapDocument("current")
df = arcpy.mapping.ListDataFrames(, mxd, "Layers")[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)
for layer in layers:  
   if layer.name in names:    
      layer.visible = False 
arcpy.RefreshTOC()
arcpy.RefreshActiveView()

When we navigate to row 1st, we want the Landcover layer visible = True and Geology layer visible = False

and and vice versa when we navigate to row 2nd Landcover layer visible = False and Geology layer visible = True

Could you give me a simple way to do that.

Thanks

0 Kudos
PanagiotisPapadopoulos
Esri Regular Contributor

you have to access the records and read the data from the desired fields. This can be happened using arcpy and  searchCursor

see this sample from the help ArcGIS Desktop 

import arcpy  fc = "D:/st_johns/roads.shp"  
# Create a search cursor  
# 
rows = arcpy.SearchCursor(fc)   
# Create a list of string fields 
fields = arcpy.ListFields(fc, "", "String")  

for row in rows:     
   for field in fields:         
      if field.type != "Geometry":             
         print "%s: Value = %s" % (field.name, row.getValue(field.name))
ZakirmanIbros
New Contributor II

Ok,

I got the value (the python script I have written in the record of driven_page.shp)

So how to connect to the current data driven page extent

Let say

If data driven page  the Title = Landcover, and then print the row record of pyScript field, and how to run automatically the script

Sorry Papadopoulos, I am a newbie and and want to learn but confuse

0 Kudos