Select to view content in your preferred language

I need help, I want to label a layer using SQL Query i.e a Primary and Foreign Key

456
2
Jump to solution
08-31-2012 05:13 PM
OLANIYANOLAKUNLE
Frequent Contributor
I want to label a layer using SQL Query i.e a Primary and Foreign Key, the primary key field is the OBJECTID in the polygon layer while the ParcelID is the Foreign Key field in the Line_Clip layer. Below is my script but it is not working;

import arcpy mxd = arcpy.mapping.MapDocument("Current") lyr = "Lines_Clip" lyr1 = "Parcels_Clip" field1 = "OBJECTID" field2 = "ParcelID" for lyr in arcpy.mapping.ListLayers(mxd):     if lyr.supports("LABELCLASSES"):         if lyr.showLabels:   where "ParcelID" = "OBJECTID"


What im i doing wrong?
Thanks
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
OLANIYANOLAKUNLE
Frequent Contributor
Thanks i got a workaround for it; let me share my code

arcpy.SelectLayerByLocation_management("Parcels", "COMPLETELY_WITHIN", "ClipFeature", "", "NEW_SELECTION") fc = "Parcels" field = "OBJECTID" cursor = arcpy.SearchCursor(fc) for row in cursor:     val = row.getValue(field)     print (row.getValue(field))      mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd, "Lines_Clip")[0] for lblClass in lyr.labelClasses:     lblClass.SQLQuery = '"ParcelID"' + "=" + str(val)  arcpy.SelectLayerByAttribute_management("Parcels", "CLEAR_SELECTION") 

View solution in original post

0 Kudos
2 Replies
JeffBarrette
Esri Regular Contributor
First you need to join the two data sources.  Once joined, you can use a script that is simlar to the one in the help:

http://resources.arcgis.com/en/help/main/10.1/#/LabelClass/00s30000002t000000/

mxd = arcpy.mapping.MapDocument("current")
lyr = arcpy.mapping.ListLayers(mxd, "Lines_Clip")[0]
for lblClass in lyr.labelClasses:
    lblClass.expression = "[table.ParcelID]"   #NOTE - SQL syntax could be different depending on data source


Jeff
0 Kudos
OLANIYANOLAKUNLE
Frequent Contributor
Thanks i got a workaround for it; let me share my code

arcpy.SelectLayerByLocation_management("Parcels", "COMPLETELY_WITHIN", "ClipFeature", "", "NEW_SELECTION") fc = "Parcels" field = "OBJECTID" cursor = arcpy.SearchCursor(fc) for row in cursor:     val = row.getValue(field)     print (row.getValue(field))      mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd, "Lines_Clip")[0] for lblClass in lyr.labelClasses:     lblClass.SQLQuery = '"ParcelID"' + "=" + str(val)  arcpy.SelectLayerByAttribute_management("Parcels", "CLEAR_SELECTION") 
0 Kudos