{"id":2415,"date":"2016-11-23T01:06:55","date_gmt":"2016-11-23T01:06:55","guid":{"rendered":"https:\/\/grindgis.com\/?p=2415"},"modified":"2016-11-24T17:51:04","modified_gmt":"2016-11-24T17:51:04","slug":"python-arcgis-label-expression","status":"publish","type":"post","link":"https:\/\/grindgis.com\/?p=2415","title":{"rendered":"10 Python ArcGIS Label Expression Code"},"content":{"rendered":"<p style=\"text-align: justify;\">Python scripting has become popular with the ArcGIS software. ArcGIS adopted it after version 9.0. Python script can be used in many parts within ArcGIS; label expression, attribute calculator, model builder or geoprocessing tools. Label expression provides option for Python, VB Script and Jscript. On this tutorial I have written 10 python script that can be used in your daily GIS work. This scenario are something you might encounter that cannot be handled by Maplex extension.<\/p>\n<p><strong>To insert the code, Open the Layer Properties &gt; Labels &gt; Expressions<\/strong><\/p>\n<p><a href=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0352_10PythonArc1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2406 size-full\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0352_10PythonArc1.png\" alt=\"Label Expression window\" width=\"626\" height=\"493\" srcset=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0352_10PythonArc1.png 626w, https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0352_10PythonArc1-300x236.png 300w\" sizes=\"auto, (max-width: 626px) 100vw, 626px\" \/><\/a><\/p>\n<p><strong>Then select the python from the parser and check the Advanced box<\/strong><\/p>\n<p><a href=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0345_10PythonArc2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2400 size-full\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0345_10PythonArc2.png\" alt=\"Label Expression Python option\" width=\"459\" height=\"369\" srcset=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0345_10PythonArc2.png 459w, https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/112216_0345_10PythonArc2-300x241.png 300w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"font-size: 36pt;\">1.\u00a0<\/span>TEXT Format:<\/strong> This example demonstrate how to convert your text strings to different format.<\/p>\n<p><strong>A.\u00a0<\/strong>Convert text string to upper or lower case<\/p>\n<p><strong><span style=\"font-size: 12pt;\"><em>def FindLabel ( [Name] ):<\/em><\/span><\/strong><br \/>\n<strong><span style=\"font-size: 12pt;\"> <em>L = [Name]<\/em><\/span><\/strong><br \/>\n<strong><span style=\"font-size: 12pt;\"> <em>L = L. lower()<\/em><\/span><\/strong><br \/>\n<strong><span style=\"font-size: 12pt;\"> <em>return<\/em><\/span><\/strong><\/p>\n<p>Note: \u00a0\u00a0 For upper case use function <strong><em>upper()<\/em><\/strong><br \/>\n<strong>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<\/strong>For proper case use function<strong> <em>proper()<\/em><\/strong><\/p>\n<p style=\"text-align: justify;\"><strong>B.\u00a0<\/strong>Stacking of the fields, for example if you want three fields to be displayed in the stack. For this you do not need to check Advance box, so uncheck it.<\/p>\n<p><strong><span style=\"font-size: 12pt;\"><em>Example:\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 City Name: \u00a0Calgary<\/em><\/span><\/strong><br \/>\n<strong><span style=\"font-size: 12pt;\"> <em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 1000<\/em><\/span><\/strong><br \/>\n<strong><span style=\"font-size: 12pt;\"> <em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0 NE<\/em><\/span><\/strong><\/p>\n<p><strong><span style=\"font-size: 12pt;\"><em>\u201cCity Name: &#8221; + [NAME] + &#8216;\\n&#8217; + [Number] + &#8216;\\n&#8217; + [Direction]<\/em><\/span><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">2.\u00a0<\/span>Stack Field:<\/strong> Next example is to stack the text from the single field. Again check the Advance box if you have unchecked it. On the below example attribute values has the comma and stacking will be happing based on it.<\/p>\n<p>Name field: Calgary, 1000, NE will label as<\/p>\n<p><span style=\"color: #000000; font-size: 12pt;\"><em>City Name: \u00a0 \u00a0 \u00a0 \u00a0 Calgary<\/em><\/span><br \/>\n<span style=\"color: #000000; font-size: 12pt;\"> <em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1000<\/em><\/span><br \/>\n<span style=\"color: #000000; font-size: 12pt;\"> <em>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0NE<\/em><\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ( [NAME]\u00a0 ):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 L = [NAME]<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 L = L.replace(&#8216;, &#8216;, &#8216;\\n&#8217;)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 return L<\/em><\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">3<\/span>. \u00a0Color and Format Label:<\/strong> If you want to put some condition on the font; if there value meet some condition then change the color and size. Below example will change the font color to blue and size will be bold if there population is more than 5000 and others will be default value.<\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ( [NAME], [POPULATION] ):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 if long([POPULATION]) &gt;= 5000:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 return &#8220;&lt;CLR red=&#8217;255&#8242;&gt;&lt;FNT size = &#8217;14&#8217;&gt;&#8221; + [NAME] + &#8220;&lt;\/FNT&gt;&lt;\/CLR&gt;&#8221;<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 else:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 return [NAME]<\/em><\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"font-size: 36pt;\">4.\u00a0<\/span>Split Text:<\/strong> Let us say you have Name field that has the value \u201c Calgary,100\u201d and you want to plot value after \u201c,\u201c that is 100 , then use the python function called split ().<\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ( [Name] ):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0return [Name].split(&#8220;,&#8221;)[1]<\/em><\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">5.\u00a0<\/span>Exclude, Concatenate and Stack:<\/strong> You have Name field whose value is \u201c BP10200\u201d and next field Code that has value \u201c546\u201d. You want to plot both the fields but excluding number from first field and your result will be:<\/p>\n<p><span style=\"font-size: 12pt;\"><em>BP<\/em><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <em>546<\/em><\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ( [Name] , [Code]\u00a0\u00a0 ):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0 return [Name] [:2] + &#8216;\\n&#8217; + [Code]<\/em><\/strong><\/span><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">6.\u00a0<\/span>Replace command:<\/strong> If you have field name with long string and you want to replace with short form then use function called <strong>\u201creplace\u201d.<\/strong> For example your name field has \u201cCountyRoads2340\u201d and you want to label as \u201cCR2340\u201d.<\/p>\n<p><span style=\"font-size: 12pt;\"><em>Results: CR2340<\/em><\/span><\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ( [Name]):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 <\/em><\/strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <strong>\u00a0return [Name].replace(&#8220;CountyRoads&#8221;,&#8221;CR&#8221;)<\/strong><\/em><\/span><\/p>\n<p><span style=\"font-size: 10pt;\"><strong>\u00a0<\/strong><\/span><\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">7.\u00a0<\/span>Removing non-numeric characters:<\/strong> Sometime you want to plot only numbers from the alphanumeric. For example if we have text like \u201c 2 Highway, 22 minor road, 74 street\u201d and you want to plot only <strong>2 22 74<\/strong> .<\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ([Name]):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 import re<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 output = re.sub(&#8220;[^0-9 ]&#8221;, &#8220;&#8221;, [Name])<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 return output<\/em><\/strong><\/span><\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">8.\u00a0<\/span>Concatenation with Condition:<\/strong> Let us say you have two fields and you want to plot both, then you can concatenate it using simple expression: <strong>[Name] + &#8221; &#8221; + [Code].<\/strong> But scenario might be different, if one field is null and you still want to plot other field.<\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel([Name],[Code]):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 if str([Name]) == &#8220;None&#8221; and str([Code]) != &#8220;None&#8221;:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0return [Code]<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 elif str([Name]) != &#8220;None&#8221; and str([Code]) == &#8220;None&#8221;:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return [Name]<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 elif str([Name]) == &#8220;None&#8221; and str([Code]) == &#8220;None&#8221;:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return &#8220;&#8221;<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 else:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return str([Name]) + &#8221; &#8221; + str([Code])<\/em><\/strong><\/span><br \/>\n<strong>\u00a0<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">9.\u00a0<\/span>Count values:<\/strong> If values are repeating on the field and you want to count it; for example <strong>A(2) <\/strong>which means A is repeating 2 times. Let us say you have field name called \u201cname\u201d which have two \u201cA\u201d then labeling will display as A(2)<\/p>\n<p><strong><em>Note: <\/em><\/strong><em>on below code <strong>Name<\/strong> is the field name and <strong>Example<\/strong> is the layer name.<\/em><\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel ( [Name] ):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 mxd = arcpy.mapping.MapDocument(&#8220;CURRENT&#8221;)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 lyr = arcpy.mapping.ListLayers (mxd,&#8221;Example&#8221;)[0]<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 q='&#8221;NAME&#8221;=&#8217;+&#8221;&#8216;&#8221;+[NAME]+&#8221;&#8216;&#8221;<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 tbl=arcpy.da.TableToNumPyArray(lyr,&#8221;Name&#8221;,q)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 n=len(tbl)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0 return &#8216;%s(%s)&#8217; %([Name],str(n))<\/em><\/strong><\/span><\/p>\n<p>&nbsp;<\/p>\n<p style=\"text-align: justify;\"><strong><span style=\"font-size: 36pt;\">10.\u00a0<\/span>Rounding Values:<\/strong> To label numeric fields and want to round off based on the condition. For example if value is more than 1 and less 50 then 3 decimal, if between 50 to 100 \u00a0then 2 decimal and others 1 decimal.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/f898ce0f336acbac12d1bb47ff03f295.png\" \/><\/p>\n<p><span style=\"font-size: 12pt;\"><strong><em>def FindLabel([Code]):<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 if [Code] is not None:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 value = float([Code])<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if 1 &lt; value &lt; 50:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return round(value, 3)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 elif 50 &lt;= value &lt; 100:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return round(value, 2)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return round(value, 1)<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0 else:<\/em><\/strong><\/span><br \/>\n<span style=\"font-size: 12pt;\"> <strong><em>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return None<\/em><\/strong><\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/11\/97acf97d95216dd0281ab39cfbe948d8.png\" alt=\"Code output image\" width=\"290\" height=\"240\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Thank you for reading my tutorial, if you have any question please post on the comment box. If you want to take free course about the python, please visit <a href=\"https:\/\/grindgis.com\/blog\/free-python-course-for-gis-users-from-coursera-org\">Free Python Course.<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python scripting has become popular with the ArcGIS software. ArcGIS adopted it after version 9.0. Python script can be used in many parts within ArcGIS; label expression, attribute calculator, model builder or geoprocessing tools. Label expression provides option for Python, VB Script and Jscript. On this tutorial I have written 10 python script that can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2421,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,135,29],"tags":[31,420,63,136],"class_list":["post-2415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arcgis","category-python","category-software","tag-arcgis","tag-label-expression","tag-python","tag-python-code","entry","has-media"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/posts\/2415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2415"}],"version-history":[{"count":0,"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/posts\/2415\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/media\/2421"}],"wp:attachment":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}