Building label expresssion with standalone Python script ?

852
4
10-28-2013 10:12 PM
PierreWeisse
New Contributor
Hello,

I work on a table of about 1200 objects. I wonder if it is possible to write a standalone Python script to replace one word with another without changing the values ??????of my table. As building label expressions.

Exemple:

I would like to replace Ec. for Ecole

other exemple

I would like to replace Egl. for Eglise

Thanks for your help
Tags (2)
0 Kudos
4 Replies
XanderBakker
Esri Esteemed Contributor
Hello,

I work on a table of about 1200 objects. I wonder if it is possible to write a standalone Python script to replace one word with another without changing the values �??�??of my table. As building label expressions.

Exemple:

I would like to replace Ec. for Ecole

other exemple

I would like to replace Egl. for Eglise

Thanks for your help


Hi Pierre,

I guess the expression that you're looking for is something along the lines of:
replace(replace([yourLabelFieldName],"Ec.","Ecole"),"Egl.","Eglise")


I don't know why you want a standalone Python script to change the expression, but it could be something like:

import arcpy

mxdPath = r"C:\Project\_LearnPython\Ohio2.mxd"
layerName = "OH30"
fldName = "yourLabelFieldName"

# in case of shapefile of personal geodatabase
labelExpression = 'replace(replace([{0}],"Ec.","Ecole"),"Egl.","Eglise")'.format(fldName)

# in case of file geodatabase
# labelExpression = 'replace(replace("{0}","Ec.","Ecole"),"Egl.","Eglise")'.format(fldName)

mxd = arcpy.mapping.MapDocument(mxdPath)
for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == layerName:
        if lyr.supports("LABELCLASSES"):
            lyr.showLabels == True
            for lblClass in lyr.labelClasses:
                lblClass.expression = labelExpression

mxd.save()
del mxd


Change the bold text to represent your situation. Activate the italic code in case your layer is in a file geodatabase.

The code will:

  • access your mxd

  • loop through the layer until it finds the layerName provided

  • change the label expression of the layer

  • saves the mxd


Kind regards,

Xander
0 Kudos
PierreWeisse
New Contributor
Thank you Xander

I started programming in Python. I try to understand how best to use this language.

You just give me the answer to the first part of my problem. I thank you.

The aim is for me now to write a script to compare my table with a table that will be updated a few times. I need to know what the new names in the table update. So as to complete the script for label expressions.

If new objects as Egl. appear I know this first script will do the job. But how can I do if for example a new object complements my table.

I hope to be clear enough

thank you
0 Kudos
PierreWeisse
New Contributor
Thank you Xander
As I started in Pyhon, I make mistakes. I want to make a standalone script to not update the data source. I don't know if it's the better solution.
In a second step I should compare my class annotation source with a class annotation update. And look at this update with the new data does not yet match.

I work from a class annotation. I don�??t want to change the data source, but simply change the label expression or display rather like my data is a class annotation.

The code returns me an error

>>> import arcpy
>>>
>>> mxdPath=r'M:\UTILISATEURS\Pierre\SERAIL\TEST.mxd'
>>> layerName=r'M:\UTILISATEURS\Pierre\SERAIL\MISE_A_JOUR.gdb\TEST'
>>> fldName="TEXTSTRING"
>>> labelExpression = 'replace(replace([{0}],"Ec.","Ecole"),"Egl.","Eglise")'.format(fldName)
>>> mxd = arcpy.mapping.MapDocument(mxdPath)
>>> for lyr in arcpy.mapping.ListLayers(mxd):
...     if lyr.name == layerName:
...         if lyr.supports("LABELCLASSES"):
...             lyr.showLabels == True
...             for lblClass in lyr.labelClasses:
...                 lblClass.expression = labelExpression
...                
>>> mxd.save()
Runtime error <type 'exceptions.IOError'>: UNKNOWN
>>> del mxd

Can you write the code with my data, please. I tried this morning, but I don�??t understand why it doesn�??t work.

My data

mxdPath= r'M:\UTILISATEURS\Pierre\SERAIL\TEST.mxd'
layerName = r'M:\UTILISATEURS\Pierre\SERAIL\MISE_A_JOUR.gdb\TEST'
fldName = "TEXTSTRING"

I hope to be clear enough

thank you
0 Kudos
XanderBakker
Esri Esteemed Contributor
The code returns me an error
Runtime error <type 'exceptions.IOError'>: UNKNOWN

layerName=r'M:\UTILISATEURS\Pierre\SERAIL\MISE_A_JOUR.gdb\TEST'


I work from a class annotation. I don�??t want to change the data source,  but simply change the label expression or display rather like my data is  a class annotation.


Hi Pierre,

I notice your layername. I used the name of the layer as it appears in the table of contents (TOC) of the mxd. You are using a reference to the data source itself and that's probably not how the layer is named in the TOC.

So change this:

layerName=r'M:\UTILISATEURS\Pierre\SERAIL\MISE_A_JOUR.gdb\TEST'


to this:

layerName='TEST'


... if your layer is called 'TEST' in the TOC.

You could also try with manually entering the label expression below in the layer properties:
replace(replace([TEXTSTRING],"Ec.","Ecole"),"Egl.","Eglise")


The error Runtime error <type 'exceptions.IOError'>: UNKNOWN you obtained refers to:
Raised when an I/O operation (such as a print statement, the built-in open() function or a method of a file object) fails for an I/O-related reason, e.g., "file not found" or "disk full".

Do you have write permissions to save the mxd?


But the biggest difference is that you are not labeling based on a field, but you have an (feature-linked?) annotation class. This works completely different. In stead of a label expression there is the "Display Expression". This display expression is not (yet) supported by arcpy.

In this case the best thing you can do is manually enter the properties of the annotation class and activate the Display TAB and press the Expression button:

[ATTACH=CONFIG]28710[/ATTACH]

In the Display Expression dialog enter the expression:
replace(replace([TEXTSTRING],"Ec.","Ecole"),"Egl.","Eglise")

[ATTACH=CONFIG]28711[/ATTACH]

When there are more parts of text you want to change in the feature, it may be better to use a multiline expression like this:

Function FindLabel ( [TextString] )
  Label = replace([TextString], "Ec.", "Ecole")
  Label = replace(Label, "Egl.", "Eglise")
  Label = replace(Label, "Text to search for", "Text to replace it with")
  'etc
  FindLabel = Label
End Function


I tried this with a dummy dataset and if I hit the "Verify" button it shows the replaced text correctly. If I look at the map however it doesn't display the changed text. Please note that I switched on the option "Show MapTips using the display expression"... See steps below.

[ATTACH=CONFIG]28714[/ATTACH]

What is even stranger, when I hover over the text in the map it shows a kind of tool tip with the corrected text...

This is odd.. maybe you're better of not using annotation...

Kind regards,

Xander
0 Kudos