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)