Select to view content in your preferred language

How can I use a layer name with spaces in python?

1914
5
Jump to solution
09-14-2018 12:29 AM
LindsayRaabe_FPCWA
Honored Contributor

Hi All. I'm trying to run the below python code with no luck. The 2 layers listed on line 20 are referenced again on line 37 when I attempt to cycle through their extents. The code should pull the extents and apply them to the data frame. The issue is that the list at line 20 won't work unless there are no spaces in the layer names. If enclose the names in "..." then the code is "syntax error free" but fails at line 37 when trying to extract the extent of a string object instead of a layer. How can I work around this (would prefer to not have to rename my layers in the mxd!). 

import arcpy
from arcpy import env, mapping
from bisect import bisect

#input parameters;
Plantation = arcpy.GetParameter(0)
ForestID = arcpy.GetParameter(1)
CommonName = arcpy.GetParameter(2)
PYear = arcpy.GetParameter(3)
OpCode = arcpy.GetParameter(4)

#variables;
title1 = Plantation + " PLANTATION"
title2 = Plantation + "\r\nPLANTATION"
title3 = Plantation + " (" + CommonName + ")"
title4 = Plantation + " P" + PYear + " AREA STATEMENT"
mxd = mapping.MapDocument("CURRENT")
elements = mapping.ListLayoutElements(mxd)
layers = mapping.ListLayers(mxd)
HTL_layer_list = ["Entered In GeoMaster", "Entered In GeoMaster Archive"]
dflist = arcpy.mapping.ListDataFrames(mxd, "")
opcode_exp1 = "EnteredInGeoMaster IS NOT NULL AND Ops_Code = '" + OpCode + "'"
opcode_exp2 = "Ops_Code = '" + OpCode + "'"

    if lyr.name == "Entered In GeoMaster":
        lyr.definitionQuery = opcode_exp1
        arcpy.AddMessage(lyr.name + " definition query updated")
        
    elif lyr.name == "Entered In GeoMaster Archive":
        lyr.definitionQuery = opcode_exp2
        arcpy.AddMessage(lyr.name + " definition query updated")
        # get current map extent
        xmin, xmax = dflist[0].extent.XMin, dflist[0].extent.XMax
        ymin, ymax = dflist[0].extent.YMin, dflist[0].extent.YMax
        # loop through def query layer extents and create one extent to fit them all
        for HTL_lyr in HTL_layer_list:
            HTL_ext = HTL_lyr.getExtent()
            if HTL_ext.XMin < xmin:
                xmin = ext.XMin
            if HTL_ext.YMin < ymin:
                ymin = ext.YMin
            if HTL_ext.XMax > xmax:
                xmax = ext.XMax
            if HTL_ext.YMax > ymax:
                ymax = ext.YMax
        # set df extent to new extent
        dflist[0].extent = arcpy.Extent(xmin, ymin, xmax, ymax)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
1 Solution

Accepted Solutions
RichardFairhurst
MVP Honored Contributor

The issue is that line 20 does not contain layers, it contains a list of strings that represent layer names and you have no code that matches those layer names to actual layers.  Also, as far as I can see your code should throw an error at line 25, because the variable lyr has never been initialized in your code.  It seems like the code you posted is missing a for loop at line 24 that assigns each layer in the layers variable you created in line 19 to the lyr variable you are referencing in line 25.

for lyr in layers:‍‍‍‍‍‍‍

Line 36 should do a similar for loop and then be followed by an if statement that determines if the layer name is in the HTL_layer_list of names.  Also, your code defines the HTL_ext variable, but does not define an ext variable, so I think you meant to use the HTL_ext variable throughout this section of code.  This code will work:

for HTL_lyr in layers:
  if HTL_lyr.name in HTL_layer_list:
    HTL_ext = HTL_lyr.getExtent()
    if HTL_ext.XMin < xmin:
      xmin = HTL_ext.XMin
    if HTL_ext.YMin < ymin:
      ymin = HTL_ext.YMin
    if HTL_ext.XMax > xmax:
      xmax = HTL_ext.XMax
    if HTL_ext.YMax > ymax:
      ymax = HTL_ext.YMax‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

5 Replies
PeteCrosier
Frequent Contributor

This should do it..

layer = arcpy.mapping.Layer("This is a layer in my open MXD")

layer.getExtent().XMin

LindsayRaabe_FPCWA
Honored Contributor

Thanks. I'm going to try both solutions and I think both will likely work form the logic I'm reading. 

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The issue is that the list at line 20 won't work unless there are no spaces in the layer names.

It is helpful to provide the text of the error along with the Traceback.  If no error is generated, describing what you expect to happen and what is happening is helpful.

If this is being run from within ArcMap, you will likely need to call RefreshActiveView—Help | ArcGIS Desktop for the changes to the definition query to take effect.

RichardFairhurst
MVP Honored Contributor

The issue is that line 20 does not contain layers, it contains a list of strings that represent layer names and you have no code that matches those layer names to actual layers.  Also, as far as I can see your code should throw an error at line 25, because the variable lyr has never been initialized in your code.  It seems like the code you posted is missing a for loop at line 24 that assigns each layer in the layers variable you created in line 19 to the lyr variable you are referencing in line 25.

for lyr in layers:‍‍‍‍‍‍‍

Line 36 should do a similar for loop and then be followed by an if statement that determines if the layer name is in the HTL_layer_list of names.  Also, your code defines the HTL_ext variable, but does not define an ext variable, so I think you meant to use the HTL_ext variable throughout this section of code.  This code will work:

for HTL_lyr in layers:
  if HTL_lyr.name in HTL_layer_list:
    HTL_ext = HTL_lyr.getExtent()
    if HTL_ext.XMin < xmin:
      xmin = HTL_ext.XMin
    if HTL_ext.YMin < ymin:
      ymin = HTL_ext.YMin
    if HTL_ext.XMax > xmax:
      xmax = HTL_ext.XMax
    if HTL_ext.YMax > ymax:
      ymax = HTL_ext.YMax‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
LindsayRaabe_FPCWA
Honored Contributor

Good pickup on the missing "for lyr in layers" statement. The code was a cut down of a larger script and I missed copying the first line of that section so that's all good. I'll give the "if HTL_lyr.name in HTL_layerl_list:" section a try and see what happens. Thanks!

Lindsay Raabe
GIS Officer
Forest Products Commission WA
0 Kudos