Obtain Index Layer in Python

402
3
05-30-2011 08:48 AM
StefanoBogo
New Contributor
Hi!
I'm trying to write my custum script to automatize my map series...
I've got a field for the "variable" part of the name and I would like to obtain it by the index layer in a filter field. If I insert the layer in a parameter all works fine, but I would like to obtain the index layer automatically...
is it possibile?

ex
index_layer = self.params[15].value
self.params[4].filter.list = [str(field.name) for field in arcpy.ListFields(index_layer)]

At the moment I'm using a parameter (params[15]) where I put the field name but I would like to automate it...

thanx!
0 Kudos
3 Replies
JeffBarrette
Esri Regular Contributor
At 10.0 you need to know the name of your index layer in the map document.

(e.g., lyr = arcpy.mapping.ListLayers(mxd, "Index Layer")[0]

At 10.1 we'll be introducing a new DDP Class property called indexLayer.

(e.g., lyr = ddp.indexLayer) 

I hope this helps,
Jeff
0 Kudos
StefanoBogo
New Contributor
Thanx
I know your proposal, so I have to specify the name of the index layer and waiting to 10.1 😉
0 Kudos
StefanoBogo
New Contributor
Just for fun, I found a not very performing solution...
For each layer I get field list and I try to use the name field to the data driven, otherwise I break the cicle; if all the fields name work in data driven I check the number of field with the cicle progressive and I get the layer name... (and in my script tool I get the filter in my parameter to compose the file name to export with data driven...)

import arcpy, os
mxd = arcpy.mapping.MapDocument("CURRENT")
trovato = 0
for lyr in arcpy.mapping.ListLayers(mxd):
        try:
          trova_layer = arcpy.mapping.ListLayers(mxd, lyr)[0]
          elenco_campi = arcpy.ListFields(trova_layer)
          numero_campi_trova = len(elenco_campi)
          for field in elenco_campi:
            campo_trova = field.name
            try:
              campo_ddp = eval("mxd.dataDrivenPages.pageRow." + campo_trova)
              trovato = trovato + 1
            except:
              trovato = 0 
              break
          if trovato == len(elenco_campi):
              break   
        except:
          trovato = 0
index_layer = trova_layer
0 Kudos