How to get label text for each feature in vb.net

3957
6
Jump to solution
07-06-2012 08:47 AM
FredHejazi
New Contributor III
I am trying to get text for the label that Arcview is going to place for each feature when the labeling is on, so that I can write it to a file. Since the labels may have complex operations involved, I don't want to go to the attributes. Any help would be appreciated.  I am working in vb.net
Thanks
0 Kudos
1 Solution

Accepted Solutions
DuncanHornby
MVP Notable Contributor
Fred,

OK this is how I did it in VBA. I added a layer and set a simple expression, see attached image.

Then the code below will will print out all the labels as you would see them on the map.


Duncan

Public Sub printLabels()     ' Get layer     Dim pMXD As IMxDocument     Set pMXD = ThisDocument     Dim pMap As IMap     Set pMap = pMXD.FocusMap     Dim pLayer As ILayer     Set pLayer = pMap.Layer(0)     Dim pGeoFeaturelayer As IGeoFeatureLayer     Set pGeoFeaturelayer = pLayer          ' Get annotation collection     Dim pAnnotateLayerPropertiesCollection As IAnnotateLayerPropertiesCollection     Set pAnnotateLayerPropertiesCollection = pGeoFeaturelayer.AnnotationProperties          ' Assumes 1 item in collection     Dim pAnnotateLayerProperties As IAnnotateLayerProperties     pAnnotateLayerPropertiesCollection.QueryItem 0, pAnnotateLayerProperties, Nothing, Nothing          ' Get label engine and then the expression     Dim pLabelEngineLayerProps As ILabelEngineLayerProperties     Set pLabelEngineLayerProps = pAnnotateLayerProperties     Dim simpleExpression As String     Let simpleExpression = pLabelEngineLayerProps.Expression          ' Get expression engine and create parser     Dim pAnnotationExpressionEngine As IAnnotationExpressionEngine     Set pAnnotationExpressionEngine = pLabelEngineLayerProps.ExpressionParser     Dim pAnnotationExpressionParser As IAnnotationExpressionParser     Set pAnnotationExpressionParser = pAnnotationExpressionEngine.SetExpression("", simpleExpression)          ' Loop through featurelayer printing to VBA immediate window the label as seen on map     Dim pFeatureCursor As IFeatureCursor     Set pFeatureCursor = pGeoFeaturelayer.Search(Nothing, True)     Dim pFeature As IFeature     Set pFeature = pFeatureCursor.NextFeature     Do While Not pFeature Is Nothing         Debug.Print pAnnotationExpressionParser.FindLabel(pFeature)         Set pFeature = pFeatureCursor.NextFeature     Loop End Sub

View solution in original post

0 Kudos
6 Replies
DuncanHornby
MVP Notable Contributor
Fred,

Have a look at the interfaces IGeoFeatureLayer and  ILabelEngineLayerProperties2 this will get you started.

Duncan
0 Kudos
FredHejazi
New Contributor III
Thanks - I did actually start with those interfaces.  But there is no way to cursor through the records.  They simply lead to the Symboltext which is fine for getting the properties and the value for a single (random) text, but I could not figure out a way to cursor through all the records and get the text values.  I know there is a way, because the the KML export tool dumps the text values out into the kml file, and I can only assume that it was written using Arcobjects.
0 Kudos
FredHejazi
New Contributor III
Thanks - I did actually start with those interfaces. But there is no way to cursor through the records. They simply lead to the Symboltext which is fine for getting the properties and the value for a single (random) text, but I could not figure out a way to cursor through all the records and get the text values. I know there is a way, because the the KML export tool dumps the text values out into the kml file, and I can only assume that it was written using Arcobjects.
0 Kudos
DuncanHornby
MVP Notable Contributor
Fred,

OK this is how I did it in VBA. I added a layer and set a simple expression, see attached image.

Then the code below will will print out all the labels as you would see them on the map.


Duncan

Public Sub printLabels()     ' Get layer     Dim pMXD As IMxDocument     Set pMXD = ThisDocument     Dim pMap As IMap     Set pMap = pMXD.FocusMap     Dim pLayer As ILayer     Set pLayer = pMap.Layer(0)     Dim pGeoFeaturelayer As IGeoFeatureLayer     Set pGeoFeaturelayer = pLayer          ' Get annotation collection     Dim pAnnotateLayerPropertiesCollection As IAnnotateLayerPropertiesCollection     Set pAnnotateLayerPropertiesCollection = pGeoFeaturelayer.AnnotationProperties          ' Assumes 1 item in collection     Dim pAnnotateLayerProperties As IAnnotateLayerProperties     pAnnotateLayerPropertiesCollection.QueryItem 0, pAnnotateLayerProperties, Nothing, Nothing          ' Get label engine and then the expression     Dim pLabelEngineLayerProps As ILabelEngineLayerProperties     Set pLabelEngineLayerProps = pAnnotateLayerProperties     Dim simpleExpression As String     Let simpleExpression = pLabelEngineLayerProps.Expression          ' Get expression engine and create parser     Dim pAnnotationExpressionEngine As IAnnotationExpressionEngine     Set pAnnotationExpressionEngine = pLabelEngineLayerProps.ExpressionParser     Dim pAnnotationExpressionParser As IAnnotationExpressionParser     Set pAnnotationExpressionParser = pAnnotationExpressionEngine.SetExpression("", simpleExpression)          ' Loop through featurelayer printing to VBA immediate window the label as seen on map     Dim pFeatureCursor As IFeatureCursor     Set pFeatureCursor = pGeoFeaturelayer.Search(Nothing, True)     Dim pFeature As IFeature     Set pFeature = pFeatureCursor.NextFeature     Do While Not pFeature Is Nothing         Debug.Print pAnnotationExpressionParser.FindLabel(pFeature)         Set pFeature = pFeatureCursor.NextFeature     Loop End Sub
0 Kudos
FredHejazi
New Contributor III
I converted your code to vb.net, which was essentially copy and paste.  And indeed that is the solution.  Thank you very much. 
I have been banging my head against this for 4 days and was in the process of writing code to convert the lables to a temporary annotation layer and reading that as a featureset.
0 Kudos
DuncanHornby
MVP Notable Contributor
Fred,

It was an interesting problem and something I have never done before so I was interested in finding out how to extract the information. All I've shown is how sad I am for knowing ArcObjects well enough to be able to answer the problem! 🙂 I am glad to have helped.

Duncan
0 Kudos