Hello everyone,
I am trying to update a map based on a pre-built template. However, after everything ran successfully (python code below), the shapefile that was updated did not show up on the map. Its symbology was also changed from "Quantities: Graduated colors" to "Features: Single symbol". I'm not sure what I did wrong? Any pointer is appreciated. Thanks!
Steps that I did:
I opened the new mxd file. The data source is correctly updated to the new file. Values in the attribute table were also correct.
import os
import ntpath
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True
### set project directory
project_dir = os.path.expanduser('~/Projects/')
mxd_dir = os.path.join(project_dir, 'GIS/mxd/')
gis_dir = os.path.join(project_dir, 'GIS/shapefile')
env.workspace = os.path.join(project_dir, gis_dir)
year = '2019'
dframe = 'Baseline'
csv_file = 'sample.csv'
### open mxd template file
mxd_file = "my_template.mxd"
full_path = os.path.join(mxd_dir + mxd_file)
mxd = arcpy.mapping.MapDocument(full_path)
### list data frame in the mxd file
df = arcpy.mapping.ListDataFrames(mxd, dframe)[0]
### List layers
layers = arcpy.mapping.ListLayers(df)
### This is the shapefile in the template with preset symbology
inFeatures = os.path.join(gis_dir, "Q_" + dframe + ".shp")
print(inFeatures)
### make a copy
outFeatures = os.path.join(gis_dir, "output", "Q_" + dframe + "_" + year + ".shp")
arcpy.CopyFeatures_management(in_features = inFeatures, out_feature_class = outFeatures)
### join then do field calculator on the new feature
arcpy.MakeFeatureLayer_management(in_features = outFeatures, out_layer = 'new_lyr')
arcpy.MakeTableView_management(in_table = csv_file, out_view = 'csv_ec')
arcpy.AddJoin_management(in_layer_or_view = 'new_lyr', in_field = 'ID', join_table = 'csv_ec', join_field = 'ID')
arcpy.CalculateField_management('new_lyr',
"Q_" + dframe + "_" + year + ".Q", "!" + "Q_" + dframe + ".csv" + ".Q_new!",
"PYTHON")
### update data source
for lyr in layers:
if dframe in lyr.name:
lyr.replaceDataSource(os.path.join(gis_dir, "output", dframe), "SHAPEFILE_WORKSPACE",
os.path.splitext(ntpath.basename(outFeatures))[0])
### Update Layout View
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
### save to new mxd file
new_mxd = "new_Q_" + year.upper() + ".mxd"
new_mxd_path = os.path.join(mxd_dir + new_mxd)
mxd.saveACopy(new_mxd)
### Clean up the MapDocument object by deleting it
del mxd
Update: I thought the reason was because I did not remove join after performing field calculator. I tried this (after line #43) but the result was still the same
# remove all joins
arcpy.RemoveJoin_management('new_lyr')
My current workaround is to duplicate the original folder then do all the calculation on the original shapefile which won't cause any problem