I am working on a mapbook and I require to label the features against different fields in different maps. How can I do this using arcpy?
Regards
Regarding labels, here's some python snippets that I've been playing around with that may be of interest:
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"LABELCLASSES"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"\n\nLayer name: "</SPAN> <SPAN class="operator token">+</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" Show Labels: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>lyr<SPAN class="punctuation token">.</SPAN>showLabels<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" Data Set: "</SPAN> <SPAN class="operator token">+</SPAN> lyr<SPAN class="punctuation token">.</SPAN>datasetName <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Data Source: "</SPAN> <SPAN class="operator token">+</SPAN> lyr<SPAN class="punctuation token">.</SPAN>dataSource <SPAN class="keyword token">for</SPAN> lblClass <SPAN class="keyword token">in</SPAN> lyr<SPAN class="punctuation token">.</SPAN>labelClasses<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"\n Class Name: "</SPAN> <SPAN class="operator token">+</SPAN> lblClass<SPAN class="punctuation token">.</SPAN>className <SPAN class="keyword token">print</SPAN> <SPAN class="string token">"Show Class Labels: "</SPAN> <SPAN class="operator token">+</SPAN> str<SPAN class="punctuation token">(</SPAN>lblClass<SPAN class="punctuation token">.</SPAN>showClassLabels<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" Expression: "</SPAN> <SPAN class="operator token">+</SPAN> lblClass<SPAN class="punctuation token">.</SPAN>expression <SPAN class="keyword token">print</SPAN> <SPAN class="string token">" SQL Query: "</SPAN> <SPAN class="operator token">+</SPAN> lblClass<SPAN class="punctuation token">.</SPAN>SQLQuery <SPAN class="string token">''' Sample output: Layer name: MyData Show Labels: True Data Set: FeatureTest Data Source: C:\...\MyFeature.gdb\FeatureTest Class Name: Default Show Class Labels: True Expression: def FindLabel ( [FeatureType] ): if [FeatureType] == "Type One": return "Type 1" else: return [FeatureType] SQL Query: FeatureType = '1234' '''</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"LABELCLASSES"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">for</SPAN> lblClass <SPAN class="keyword token">in</SPAN> lyr<SPAN class="punctuation token">.</SPAN>labelClasses<SPAN class="punctuation token">:</SPAN> lblClass<SPAN class="punctuation token">.</SPAN>SQLQuery <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RefreshActiveView<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># removes query that was found in first snippet</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"CURRENT"</SPAN><SPAN class="punctuation token">)</SPAN> LayerList <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">'Program'</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"LABELCLASSES"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name <SPAN class="keyword token">in</SPAN> LayerList<SPAN class="punctuation token">:</SPAN> lyr<SPAN class="punctuation token">.</SPAN>showLabels <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN> <SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> lyr<SPAN class="punctuation token">.</SPAN>showLabels <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>RefreshActiveView<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
I recommend checking the links that both bixb0012 and xander_bakker have mentioned.
Very true! good catch! +1 for that...
The LabelClass object does provide access to change the field through the expression property. When the label class is 'Default', the expression is simply the name of the field to label. If the user updates the expression to a different field and refresh the active view, the labels get updated.
I don't think the LabelClass—Help | ArcGIS Desktop provides access to set the field used for labeling a layer. If that is the case, you could create layerfiles with the appropriate label settings and apply these layerfiles for the different maps using UpdateLayer—Help | ArcGIS Desktop . Or duplicate the layer in your TOC with the different label settings and switch them on and off depending on the map using the visible property of the Layer—Help | ArcGIS Desktop .
Have you read LabelClass—Help | ArcGIS Desktop ? If so, are you having a problem with any specific aspect of working with labels through ArcPy?
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.