Symbology and Dynamic values in Lyout disappear after running python code

247
0
06-23-2022 12:39 AM
Sahaja
by
New Contributor III

I wrote a script to automate the production of layout. All the elements in the layout are already added, the code is essentially turning on and off some of the elements based on a few conditions. I know my code is fine as it doesn't give any errors and also I tried executing it on a dummy (with minimal details). 

The problem arrives when I'm executing the code, the exported pdf doesn't have the symbology in the map extent (the layer displayed has only 1 polygon, the site. As this wasn't working I tried to see if it is working manually exporting the pdf, unfortunately, this time, all the dynamic values and the symbology are gone. In addition, the symbology in the map also disappears. 

As Bizzare as it would get, when I create a fresh folder and do the same thing manually again (without running the code, to begin with) everything works just fine. 

I checked the caches, and the minimum required for that project is at least 1.7 GB. 

Can someone please suggest any tips and tricks to resolve this?

The python script I created is this (sorry for the lengthy unorganised script)

 

import arcpy
import pandas as pd
import os
import time

abspath=os.path.abspath
abspath=os.path.abspath("__file__")
dname=os.path.dirname(abspath)
os.chdir(dname)
ProjectFolder=dname
print(dname)
arcpy.env.workspace=ProjectFolder

project=ProjectFolder +"\ContamCertificates.aprx"
aprx=arcpy.mp.ArcGISProject(project)

mapName= "Map"
layoutName="CTXXXX"
site= "siteboundary"
clr="NSW_EPA_CLR   [DQ: Status IN ('Current')]"
m=aprx.listMaps(mapName)[0]
sitelyr=m.listLayers(site)[0]
clrlyr=m.listLayers(clr)[0]
lyt=aprx.listLayouts(layoutName)[0]
mf = lyt.listElements("mapframe_element", "Map")[0]

mf.camera.setExtent(mf.getLayerExtent(sitelyr, False, True))
time.sleep(5)

arcpy.env.overwriteOutput = True

#Process: Select Layer By Location (Select Layer By Location) (management)
clrlyr_intersect, Output_Layer_Names, count_clrlyr_intersect = arcpy.management.SelectLayerByLocation(in_layer=[clrlyr], overlap_type="INTERSECT", select_features=sitelyr, search_distance="", selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT")

clrlyr_R_bt, Output_Layer_Names_2_, count_clrlyr_R_bt = arcpy.management.SelectLayerByLocation(in_layer=[clrlyr_intersect], overlap_type="BOUNDARY_TOUCHES", select_features=sitelyr, search_distance="", selection_type="REMOVE_FROM_SELECTION", invert_spatial_relationship="NOT_INVERT")

clrlyr_contains, Output_Layer_Names_3_, count_clrlyr_contains = arcpy.management.SelectLayerByLocation(in_layer=[clrlyr_R_bt], overlap_type="CONTAINS", select_features=sitelyr, search_distance="", selection_type="ADD_TO_SELECTION", invert_spatial_relationship="NOT_INVERT")

clrlyr_within_final, Output_Layer_Names_4_, Count_inside = arcpy.management.SelectLayerByLocation(in_layer=[clrlyr_contains], overlap_type="WITHIN", select_features=sitelyr, search_distance="", selection_type="ADD_TO_SELECTION", invert_spatial_relationship="NOT_INVERT")

clrlyr_intersect, Output_Layer_Names, Count_500 = arcpy.management.SelectLayerByLocation(in_layer=[clrlyr], overlap_type="INTERSECT", select_features=sitelyr, search_distance="500 Meters", selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT")

NSW_CLR_STATUS_adjacent, Output_Layer_Names, Count_adjacent = arcpy.management.SelectLayerByLocation(in_layer=[clrlyr], overlap_type="BOUNDARY TOUCHES", select_features=sitelyr, selection_type="NEW_SELECTION", invert_spatial_relationship="NOT_INVERT")

NSW_CLR_STATUS_adjacent_clear, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=NSW_CLR_STATUS_adjacent, selection_type="CLEAR_SELECTION",where_clause="",invert_where_clause="")

print (Count_500 )
print (Count_inside )
print(Count_adjacent)
print(Count)

if Count_inside !='0':
        print ("the site is at risk")
        table=lyt.listElements(wildcard="Contamination description table - NSW")[0]
        table.visible=True
        rslt_ident=lyt.listElements("TEXT_ELEMENT", "Records identified text")[0]
        rslt_ident.visible=True
        rslt_ident_gra=lyt.listElements(wildcard="Records identified background")[0]
        rslt_ident_gra.visible=True
else:
        if Count_adjacent !='0':
                rslt_adjacent=lyt.listElements("TEXT_ELEMENT", "Note: adjoining")[0]
                rslt_adjacent.visible=True
        else:
                if Count_500 !='0':
                        rslt_500=lyt.listElements("TEXT_ELEMENT", "Note: within 500m")[0]
                        rslt_500.visible=True


cur = arcpy.UpdateCursor(sitelyr)
for row in cur:
    ctid=row.getValue('OrderID')
    print (ctid)

fn = ProjectFolder+"\\"+ctid+".pdf"
print (fn)
lyt.exportToPDF(fn)

 

 

Here's how the export should be (notice the dynamic texts are underlined with maroon, and the site symbology with lemon yellow)

Sahaja_2-1655969889583.png

 

Here's how it exports with the script 

Sahaja_1-1655969835318.png

 

Here's how it exports after running the script but this time manually

Sahaja_0-1655969744040.png

 

0 Kudos
0 Replies