bobm007

Label missing after joining layer to table

Discussion created by bobm007 on Apr 17, 2013
Latest reply on Apr 21, 2013 by bobm007
Hi

The code below uses an expression to display 'PARCEL_SPI' labels.
It works without the JOIN but I lose the label when the JOIN is applied.

Any ideas?

import arcpy, os, sys
# Import environment module
from arcpy import env

# Get first two Arguments
datetime = sys.argv[1]
factor = sys.argv[2]
labelsTF = sys.argv[3]
outletCount = int(sys.argv[4])
parcels = sys.argv[5 + 3 * outletCount]

# Create in_memory table
table = arcpy.CreateTable_management("in_memory","vt")
arcpy.AddField_management(table, "parcel_id", "TEXT", field_length=30)
arcpy.AddField_management(table, "owner", "TEXT", field_length=100)
arcpy.AddField_management(table, "outlet", "TEXT", field_length=20)

# Load table with First Parcel ID's, Owners & Outlets
rows = arcpy.InsertCursor(table)
for num in range(5, outletCount + 4):
    row = rows.newRow()
    row.parcel_id = sys.argv[num]
    row.owner = sys.argv[num + outletCount]
    row.outlet = sys.argv[num + 2 * outletCount]

print parcels
print outletCount
print labelsTF

# Display date/time
arcpy.AddMessage(datetime)

# Add rest of parcels to parcels string using remaining Arguments
lenArg = len(sys.argv)
for num in range(6 + 3 * outletCount,lenArg):
    parcels = parcels + " OR " + sys.argv[num]

# Display parcels string
arcpy.AddMessage(" Parcels to display: " + parcels)

# Set workplace data folder
env.workspace = "S:/MID_Owners_Database_Workspace/GIS MID Owner Property Data"
env.qualifiedFieldNames = False

# Local variables:
MIDPropertyNew_shp = "MIDPropertyNew.shp"
OwnerProperty_shp = "TempOwnerPropertyMap_" + datetime + ".shp"

# Set up PDF file names
pdfMIDFileName = "Temp_MID_" + datetime
pdfFileName = "Temp_Extent_" + datetime
pdfFileExt = ".pdf"

# Process: Select
arcpy.Select_analysis(MIDPropertyNew_shp, OwnerProperty_shp,parcels)

# Get the Layer
newLayer = arcpy.mapping.Layer(OwnerProperty_shp)

# JOIN new Layer to in-memory table
joinField1 = "PARCEL_SPI"
joinField2 = "parcel_id"
arcpy.AddJoin_management(newLayer, joinField1, table, joinField2)


# Set layer that MID map symbology will be based on
symbologyLayer = "MIDPropertyMIDSymbology.lyr"

# Apply the symbology from the symbology layer to the input layer
arcpy.ApplySymbologyFromLayer_management(newLayer, symbologyLayer)

# Get map document
base_property_mxd = arcpy.mapping.MapDocument(r"S:/MID_Owners_Database_Workspace/GIS MID Owner Property Data/base_property.mxd")

# Get the dataframe
df = arcpy.mapping.ListDataFrames(base_property_mxd, "*")[0]

# Add the new Layer
arcpy.mapping.AddLayer(df, newLayer)


# Export MID map to PDF
arcpy.AddMessage(" Exporting MID map to PDF")
arcpy.mapping.ExportToPDF(base_property_mxd, pdfMIDFileName + pdfFileExt)

# Set layer that Extent map symbology will be based on
symbologyLayer = "MIDPropertyExtentSymbology.lyr"

# Apply the symbology from the symbology layer to the input layer & show and format labels if shown
arcpy.ApplySymbologyFromLayer_management(newLayer, symbologyLayer)
if labelsTF == "-1":
    newLayer.showLabels = True
    if newLayer.supports("LABELCLASSES"):
        print "in labels"
        for lblclass in newLayer.labelClasses:
            lblclass.expression = '"%s" + [PARCEL_SPI] + "%s"' % ("<CLR red='255'><FNT size = '10'>", "</FNT></CLR>")

# Get Layer Extent
lyrExtent = newLayer.getSelectedExtent()

# Change map Extent to Layer Extent
df.extent = lyrExtent
df.scale *= float(factor)

# Export Selected Extent map to PDF
arcpy.AddMessage(" Exporting Selected Extent map to PDF")                 
arcpy.mapping.ExportToPDF(base_property_mxd, pdfFileName + pdfFileExt)

# Display PDF's
arcpy.AddMessage(" Displaying PDFs")
os.startfile(pdfMIDFileName + pdfFileExt)
os.startfile(pdfFileName + pdfFileExt)

arcpy.Delete_management(table)

Outcomes