Arcmap cannot label based on shape field

721
2
08-09-2019 03:23 AM
DuarteCarreira
Occasional Contributor II

I'm just looking for confirmation from fellow arcmap users.

Is this true? Can anyone have a simple X,Y label for points in arcmap?

Or a "XXX m2" label for polygons?

Just with a label expression?

For feature classes in enterprise geodatabases?

Without creating fields that need to be updated manually or in some convoluted automated way?

I am an old arcmap user, and I know this was bread&butter... did we lose this basic gis ability?

If not, please, please let me know how to do it.

Best,

Duarte

Tags (2)
0 Kudos
2 Replies
DuarteCarreira
Occasional Contributor II

Sooo... it works for lines and polygons, not points.

For lines, the field st_length(shape) appears in the fields list.

So this works (vbscript):

round([st_length(shape)],1) & "m"

For polygons, the field st_area(shape) appears in the field list.

So this works (vbscript):

round([st_area(shape)]/10000,1) & "ha"

For points, there's no access to the shape field nor any derivation.

There is a workaround, that's a bit complex, and may suffer low performance with many points.

But for a few points it works, it's just incredibly convoluted to do such a simple task (python):

def FindLabel ( [objectid] ):
 lr="layer name"
 with arcpy.da.SearchCursor(lr, 'Shape@',r'"objectid"='+str( [objectid] )) as cursor:
   for row in cursor:
     a=row[0]
     x = str(round(a[0].X,1))
     y = str(round(a[0].Y,1))
 return x + ";" + y
0 Kudos
DuarteCarreira
Occasional Contributor II

So more info... a few months later with arcmap 10.7.1 and using Python this time. See solution below.

I just noticed that shape_area is of type unicode...

str(type([st_area(shape)]))

shows <type 'unicode'>.

Also since my locale uses "," for decimal places it complicates even more.

For instance, multiplying st_area(shape) by 1 works fine, it returns "49698,074309".

Now let's divide by 1: 

[st_area(shape)]/1
TypeError: unsupported operand type(s) for /: 'unicode' and 'int'

Hmm, what's going on?

Let's try to convert to float:

float([st_area(shape)])
ValueError: invalid literal for float(): 49698,074309

What?

Ok, let's convert it to int:

int([st_area(shape)])
ValueError: invalid literal for int() with base 10: '49698,074309'

What if the issue is the decimal separator? Let's replace "," with ".":

int([st_area(shape)].replace(",","."))
ValueError: invalid literal for int() with base 10: '49698.074309'

That's no it either... strange enough it works for float which is good enough for me:

float([st_area(shape)].replace(",","."))
49698,074309

If you look carefully you'll see we've gone from "," to "." back to "," again...

So in short we have to replace decimal separator with "." and then convert to float so we can finally convert m2 to hectares:

str(float([st_area(shape)].replace(",","."))/10000) + " ha"
4.9698074309 ha

Notice how again we are showing "."?

Anyway, a bit convoluted but still better than using cursors!

0 Kudos