1

I need to create a shapefile from a csv, apply symbology from a saved layer, and export the result into an PNG.

My code works up through the shapefile creation part, but it is not applying the symbology and not adding the layer to the active mxd so that I can export into an PNG.

Similar code works within the python window (see second code block) but does not as a standalone script in a custom toolbox.

Any thoughts on how to apply the symbology and add to current data frame?

Standalone Script that does not work:

import os
import arcpy
from arcpy import env

path   = 'O:\\xyz\\abc\\Development\\'
inDir  = path + 'data\\'

try:

#set variables
    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = path
    in_Table = "weekly.csv"
    x_coords = "X_LON"
    y_coords = "Y_LAT"
    out_Layer = "test_points"
    saved_Layer = "data_template.lyr"
    mxd = "template.mxd"
    spRef = "WGS_1984.prj"

#delete old layer so new layer can be called same
    arcpy.Delete_management(out_Layer + ".shp")

#create feature class from csv 
    arcpy.MakeXYEventLayer_management(in_Table, x_coords, y_coords, 
    out_Layer, spRef)

#convert feature class to shapefile
    arcpy.FeatureClassToShapefile_conversion(out_Layer, path)

#apply symbology to shapefile
    arcpy.ApplySymbologyFromLayer_management(out_Layer, saved_Layer)

#get the data frame
    df = mxd.activeDataFrame

#add the layer to the map at the top
    arcpy.mapping.AddLayer(df, out_Layer, "TOP")

except Exception:
    print(arcpy.GetMessages())

Python Window code that works as expected:

>>> import arcpy
... arcpy.env.overwriteOutput = True
... arcpy.env.workspace = "O:\xyz\abc\Development\data"
... arcpy.MakeXYEventLayer_management("weekly.csv", "X_LON", "Y_LAT",         
"test_points", "O:\xyz\abc\Development\WGS_1984.prj")
... arcpy.ApplySymbologyFromLayer_management("test_points",             
"O:\xyz\abc\Development\data\data_template.lyr")
... mxd = arcpy.mapping.MapDocument("CURRENT")  
... for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
...     if elm.text == "Old Map Title":
...         elm.text = "New Map Title"
... mxd = arcpy.mapping.MapDocument("CURRENT")
... out_png = (r"O:\xyz\abc\Development\exports\weekly.png")
... arcpy.mapping.ExportToPNG(mxd, out_png)
... del mxd

I was able to add the output of the xy event to the dataframe, but I still cant get any symbology.

I need to import symbology from another layer file using the value field. But I am getting an error

symbologyFields = ["VALUE_FIELD", "PCP_WKLY", "PCP_WKLY"] 
arcpy.ApplySymbologyFromLayer_management(temp_Shape, 
"O:\\XYZ\\ABC\\Development\\data_template.lyr", symbologyFields)
ApplySymbologyFromLayer() takes at most 2 arguments (3 given)
1
  • Try adding the layer first then apply the symbology Jan 11, 2018 at 17:26

3 Answers 3

2

As Keagan already pointed out, only layers in a dataframe can have a symbology. You need to add the layer first and apply the symbology afterwards.

I assume, in you second script the output of the xy event gets added directly to the dataframe. In a standalone version you have to add it explicitly.

0
2

Try to call arcpy.mapping.UpdateLayer (df, update_layer, source_layer) and then arcpy.RefrefshTOC () and arcpy.RefreshActiveView()

0

The error message you are getting:

ApplySymbologyFromLayer() takes at most 2 arguments (3 given)

is due to the fact the ApplySymbologyFromLayer() tool is different in ArcGIS Pro.

Earlier versions (ArcGIS 10.5 and below) only take two arguments: i.e. ApplySymbologyFromLayer_management (in_layer, in_symbology_layer)

You're trying to use the symbologyFields as your argument #3, which doesn't actually exist for the toll you're trying to use).

arcpy.ApplySymbologyFromLayer_management(temp_Shape, 
"O:\\XYZ\\ABC\\Development\\data_template.lyr", symbologyFields)

The symbologyFields argument is a good improvement to the tool, it just doesn't exist outside of ArcGIS Pro.

1
  • Also worth noting - you probably don't need the symbologyFields here anyways; your field names are matching, which is the default, so remove this argument from your tool and it will probably work fine. symbologyFields = ["VALUE_FIELD", "PCP_WKLY", "PCP_WKLY"] If you did need to make your field names match, you could use arcpy.AlterField_management
    – grego
    Mar 14, 2018 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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