Using a text file to set label expression in ArcPy

2491
7
12-01-2014 12:55 PM
WesleyAskew
New Contributor III

I am currently using the lyr.labelClasses[0].expression property to label features in ArcMap from a Python script.  I am wondering if it is possible to set this expression property from a string stored in a text document.  If so, what would be the best way of going about this?  Any help is greatly appreciated.  Thanks in advance.

0 Kudos
7 Replies
PaulCrickard1
Occasional Contributor II

ConfigParser

Here is a simple usage:

Create the file label.ini with contents:

[label1]

name: Paul

[label2]

color: Red

you can use it like this:

>>> import ConfigParser

>>> config=ConfigParser.ConfigParser()

>>> config.read("C:\\Users\\me\\Desktop\label.ini")

['C:\\Users\\me\\Desktop\\label.ini']

>>> config.sections()

['label1', 'label2']

>>> name = config.get("label1","name")

>>> name

'Paul'

>>> color=config.get("label2","color")

>>> color

'Red'

>>>

0 Kudos
WesleyAskew
New Contributor III

Thanks for your response.  Although this could potentially work, the issue is that I already have a .Net ArcObjects add-in which generates a fairly complex string based on numerous variables.  I need to use Python to drive the labeling and I want to pass this entire .Net generated string to the Python script.  Do you think that the method that you indicate will be suitable for this purpose?

0 Kudos
PaulCrickard1
Occasional Contributor II

Could work, but you have to write to text file first.

maybe you could just call the python code from .NET and pass the string as a command line argument.

Or use IronPython and combine .NET and Python in .NET or cimpile your .NET to DLL and call from python.

IronPython.net /

A short post I did on it

Python in C# Using IronPython | Architecture and Planning

WesleyAskew
New Contributor III

Thanks, this is very helpful.  I think that I am going to give the IronPython method as shot as it appears that it will do exactly what I am looking for.  I will let you know how it goes...

0 Kudos
WesleyAskew
New Contributor III

I took your advice and went with the IronPython method.  I was able to install and reference in Visual Studio with no problem.  However, when I run the add-in within ArcMap, I get an error message stating that the ArcPy module does not exist.  Will I need to provide a reference to ArcPy in Visual Studio as well?  I figured that since the add-in is executed in ArcMap it will use the ArcMap console to run the ArcPy script.

0 Kudos
PaulCrickard
New Contributor III

Do you have more than 1 version of python installed? I have python 2.7 and then another in arcgis subdirectory. My 2.7 does not have arcpy installed. I wonder if maybe it is referencing a python install that is not the ESRI install. if that is the case, you can copy C:\Python27\ArcGIS10.1\Lib\site-packages\Desktop10.1.pth to your other install site lib folder.

0 Kudos
WesleyAskew
New Contributor III

I checked and it appears that only one version of Python is installed on the machine.  What's weird is that when I run the script from the add-in, I get the message "No module named arcpy".  However, when running it through the ArcMap console, I get the message "No module named numpy".  Would this mean that there is a numpy module that is missing?  Below is my code, I don't even see a reason why it would use numpy.

import arcpy

import pythonaddins

mxd = arcpy.mapping.MapDocument("CURRENT")

lyr = pythonaddins.GetSelectedTOCLayerOrDataFrame()

if lyr.supports("LABELCLASSES"):

    lyr.showLabels = True

    lyr.labelClasses[0].expression = "[FID]"

arcpy.RefreshActiveView()

0 Kudos