4

Is it possible to label a feature in ArcMap using geometry values?

In this instance I want to label features (polylines) with the Live Z value of the last vertex obtained from the Shape.

However when I try something simple like the XMin of the extent of the shape:

enter image description here

It returns an error:

enter image description here

Is it possible to use the features' geometry to formulate a label? I do not want to calculate values from the geometry into a field and then label that field unless I absolutely have to:

enter image description here

Ideally the label should be live, changing as the feature is modified.

Based on the answer by Felix:

def FindLabel ( [OBJECTID]  ):
  mxd = arcpy.mapping.MapDocument("CURRENT")
  layers=arcpy.mapping.ListLayers(mxd, "WatrcrsL")
  lr=layers[0]
  with arcpy.da.SearchCursor(lr, 'Shape@','OBJECTID = '+str( [OBJECTID] )) as cursor:
      for row in cursor:
         a=row[0].lastPoint.Z
  return a

Will label the features with the last points' Z value live!

  • Shoud be !Shape! in expession – FelixIP Dec 4 '14 at 4:21
  • You'd think @FelixIP.. just like the field calculator expression but I tried that and got a different error: Invalid syntax (<string>, line 1). You would also note that Shape doesn't appear in the list of fields to select (it's directly after OBJECTID). – Michael Stimson Dec 4 '14 at 4:26
  • 1
    I think being able to access the SHAPE@ tokens for writing label expressions would make a great ArcGIS Idea. – PolyGeo Dec 4 '14 at 6:09
  • Me 2 @PolyGeo. You can declare cursors in a label expression same as in a field calculator expression so why not SHAPE@? – Michael Stimson Dec 4 '14 at 6:14
  • Yeah - my superficial understanding is that both label expressions and the field calculator run cursors in the background and in the case of the former it would already seem to be accessing its geometry to decide where to place the label. – PolyGeo Dec 4 '14 at 6:22
9

Example for point layer

def FindLabel ( [FID] ):
  mxd = arcpy.mapping.MapDocument("CURRENT")
  layers=arcpy.mapping.ListLayers(mxd, "ac_pipes_aspoints")
  lr=layers[0]
  with arcpy.da.SearchCursor(lr, 'Shape@XY',r'"FID"='+str( [FID] )) as cursor:
      for row in cursor:
         a=row[0]
  return a[0]
  • 1
    Absolutely correct FelixIP! Way to think outside-the-box. See edit at the end of my question (it was always about the Z value of the 'to' end).. it's surprisingly fast thanks to the where clause in the cursor. Nice one! – Michael Stimson Dec 4 '14 at 5:23
  • 3
    +1 That is a very useful technique that I had not thought of trying. – PolyGeo Dec 4 '14 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.