Wednesday 12 April 2017

arcgis 10.2 - Why is ArcPy script to apply symbology not working?


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
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)

Answer



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.



No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...