3

This is the first python script I have tried to write and I can't find out why it is not working. All I want to do is apply symbology from one layer to another.

Working Code:

# Name: ApplySym.py
# Purpose: apply the symbology from one layer to another

# Import system modules
import arcpy
from arcpy import env
#import os

env.workspace = "C:\\Users\\jrutledge\\Desktop\\ArcGIS"

inputLayer = "TEST.lyr"

symbologyLayer = "LTSig_Template.lyr"

arcpy.ApplySymbologyFromLayer_management(inputLayer, symbologyLayer)

Error Message:

    Traceback (most recent call last):
  File "C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\Python\ImportSymbology.py", line 11, in <module>
    inputLayer = arcpy.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T00010_SOURCE_TEMPERATURE_C_C_ Events.lyr")
AttributeError: 'module' object has no attribute 'Layer'

Code:

# Purpose: apply the symbology from one layer to another

# Import system modules
import arcpy
from arcpy import env
import os

env.workspace = r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles"

inputLayer = arcpy.mapping.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T00010_SOURCE_TEMPERATURE_C_C_ Events.lyr")

symbologyLayer = arcpy.mapping.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T71850_NITRATE_LT_DIR.lyr")

arcpy.ApplySymbologyFromLayer_management(inputLayer, symbologyLayer)

Old Code(original attempt): RuntimeError: Object: Error in executing tool

# Name: ApplySym.py
# Purpose: apply the symbology from one layer to another

# Import system modules
import arcpy
from arcpy import env

# Set the current workspace
env.workspace = "C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles"

# Set layer to apply symbology to
inputLayerFile = arcpy.mapping.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T00010_SOURCE_TEMPERATURE_C_C_ Events.lyr")
inputLayer = arcpy.mapping.ListLayers(inputLayerFile, "T00010_SOURCE_TEMPERATURE_C_C_ Events")

# Set layer that output symbology will be based on
symbologyLayerFile = arcpy.mapping.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T71850_NITRATE_LT_DIR.lyr")
symbologyLayer = arcpy.mapping.ListLayers(symbologyLayerFile, "T71850_NITRATE_LT_DIR")

# Apply the symbology from the symbology layer to the input layer
arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)
  • You should always print your variables as your first debugging step. In this case, both inputLayer and symbologyLayer are lists that were returned by the ListLayers() function. You should add the index [0] to the end of the function to get the single layer, not a list containing a single layer. – mr.adam Jul 16 '15 at 21:57
  • I haven't tried this, but just looking at everyone's code, there seems to be a space after the arcpy.ApplySymbologyFromLayer_management and before (inputLayer, symbologyLayer). Could that be the reason? – Fezter Jul 16 '15 at 22:50
  • The space deletion made no difference. – Jeffrey Rutledge Jul 16 '15 at 23:02
  • 1
    Layer is part of the mapping module: call arcpy.mapping.Layer – GISGe Jul 17 '15 at 6:08
3

Thank you so much for everyone trying to solve my problem. I finally figured out what was causing my error. It was not a syntax problem.

The layer files which I was trying to use had source information in a table in a geodatabase. For some reason arcpy couldn't access it properly. I exported the source table to a shape file. Then remade the layer files so its source was now the new shape file. Now the program runs correctly.

2

Try this:

inputLayer = r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T00010_SOURCE_TEMPERATURE_C_C_ Events.lyr"
symbologyLayer = r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T71850_NITRATE_LT_DIR.lyr"
arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)

This tool simply requires an input layer and symbology layer.

  • 1
    This does not work because it thinks inputLayer is a string not a layer object and the method requires a layer object. How do I declare inputLayer as layer object and not a string? – Jeffrey Rutledge Jul 16 '15 at 22:00
  • No problems here. Read the documentation. It takes path strings as input. – Barbarossa Jul 16 '15 at 22:08
  • The code doesn't work if I copy paste it. Sorry if I made an incorrect assumption. – Jeffrey Rutledge Jul 16 '15 at 22:13
  • Layer = arcpy.Layer(LayerFilePath) or arcpy.MakeFeatureLayer(InFeatures,"Layer") will make a layer from a string... the first one from a path to a layer (.lyr) file on disc, the second a feature class (full path to feature class if not in the current workspace)... Note the first case is a layer object the second is a layer name; if you're getting the layers from ArcMap then they appear to be strings but are tokens for named objects (layers). – Michael Stimson Jul 16 '15 at 22:14
  • 1
    Your current code has not absorbed the above advice from @MichaelMiles-Stimson and Barbarossa about creating layer objects. – PolyGeo Jul 16 '15 at 23:09
2

I would start by replacing:

env.workspace = "C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles"

with:

env.workspace = r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles"
  • Unfortunately, I still get the same error. – Jeffrey Rutledge Jul 16 '15 at 22:52
  • Then please edit your question to keep us up to date with precisely what code snippet you are running, and precisely what error message including the line number is being thrown by it. – PolyGeo Jul 16 '15 at 22:57
  • I was just doing that :) – Jeffrey Rutledge Jul 16 '15 at 22:59
2

You need to add the layers to the dataframe before you can apply symbology to them - your current script and other solutions so far don't have any references to the MXD or dataframe. Depending on whether you're working in the currently open MXD or another saved MXD, you'll need to reference that map document. Check this ArcGIS help article for a detailed explanation.

The following code can be adapted to your data, and entered directly into the python window of a blank MXD for testing

import arcpy
#get reference to the MXD and dataframe
mxd = arcpy.mapping.MapDocument("CURRENT")
df = mxd.activeDataFrame

#get reference to the source of the symbology and add to MXD
symbologySource = arcpy.mapping.Layer(r"C:\yourLayerPath\yourSourceLayer.lyr")
arcpy.mapping.AddLayer(df,symbologySource,"AUTO_ARRANGE")

#get reference to the file to have symbology applied and add to MXD
symbologyTarget = arcpy.mapping.Layer(r"C:\yourLayerPath\yourTargetLayer.lyr")
arcpy.mapping.AddLayer(df,symbologyTarget,"AUTO_ARRANGE")

#apply symbology
arcpy.ApplySymbologyFromLayer_management(symbologyTarget,symbologySource)
  • Adding the layers to the map document is not necessary. The source is copied to the target, and the lyr file is modified permanently. – Barbarossa Jul 17 '15 at 16:13
  • I guess it depends on what works, and the other solutions aren't working in this case. – Adam Jul 19 '15 at 22:54
0

You need something in between what @barbarossa has and you have already:

inputLayer = arcpy.mapping.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T00010_SOURCE_TEMPERATURE_C_C_ Events.lyr")
symbologyLayer = arcpy.mapping.Layer(r"C:\Users\jrutledge\Desktop\Well DB Run 2_2015 (plus GAMA)\ArcMaps2015_2\MapOutput\ByWell\ShapeFiles\T71850_NITRATE_LT_DIR.lyr")

arcpy.ApplySymbologyFromLayer_management (inputLayer, symbologyLayer)

In my experience, ListLayers() is best used with a map document, or maybe a group layer, but it's unnecessary with a single layer.

  • This also doesn't work. I get the errors: ERROR 000967: The input layer type must be a feature layer, raster layer, or tin layer. ERROR 000840: The value is not a Layer. – Jeffrey Rutledge Jul 16 '15 at 22:07
  • Yet if I have an Arc Maps file open and I double click these same files so that they are imported to the file. I can then use the import symbology tool in arc maps and it works fine. – Jeffrey Rutledge Jul 16 '15 at 22:09
  • Ensure your layer files are the same data type (Raster vs. Vector) and that they are the same geometry type (Point, Line, Polygon), otherwise the tool will not work. – Barbarossa Jul 16 '15 at 22:13
  • They are both point files. I am not sure how to check raster vs vector, but the import works in the application itself shouldn't that confirm that they are the same type? – Jeffrey Rutledge Jul 16 '15 at 22:15
  • I try not to assume too much with ArcMap, but you are probably correct. – Barbarossa Jul 16 '15 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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