Select to view content in your preferred language

return centroid x and y and concatenate for an attribute rule

1363
7
Jump to solution
11-11-2022 01:48 AM
Labels (2)
kazzie
by
Emerging Contributor

I'm quite new to arcade and need some help with returning centroid easting and northing from the new polygon and remove the leading digit and concatenate as text to be used in an attribute rule. 

Example: Centroid x=529099 Centroid y=172114

result= TQ2909972114

0 Kudos
1 Solution

Accepted Solutions
AndyFairbairn
Regular Contributor

Hi Kazzie, I've written a version in Arcade that will work for any grid square (provided your data layer is in BNG). You could make it shorter by removing any of the LU dictionary key value pairs you don't need.

 

 

var LU = {
    "4,12": "HP",
    "3,11": "HT",
    "4,11": "HU",
    "1,10": "HW",
    "2,10": "HX",
    "3,10": "HY",
    "4,10": "HZ",
    "0,9": "NA",
    "1,9": "NB",
    "2,9": "NC",
    "3,9": "ND",
    "0,8": "NF",
    "1,8": "NG",
    "2,8": "NH",
    "3,8": "NJ",
    "4,8": "NK",
    "0,7": "NL",
    "1,7": "NM",
    "2,7": "NN",
    "3,7": "NO",
    "1,6": "NR",
    "2,6": "NS",
    "3,6": "NT",
    "4,6": "NU",
    "1,5": "NW",
    "2,5": "NX",
    "3,5": "NY",
    "4,5": "NZ",
    "5,5": "OV",
    "2,4": "SC",
    "3,4": "SD",
    "4,4": "SE",
    "2,3": "SH",
    "3,3": "SJ",
    "4,3": "SK",
    "1,2": "SM",
    "2,2": "SN",
    "3,2": "SO",
    "4,2": "SP",
    "1,1": "SR",
    "2,1": "SS",
    "3,1": "ST",
    "4,1": "SU",
    "0,0": "SV",
    "1,0": "SW",
    "2,0": "SX",
    "3,0": "SY",
    "4,0": "SZ",
    "5,4": "TA",
    "5,3": "TF",
    "6,3": "TG",
    "5,2": "TL",
    "6,2": "TM",
    "5,1": "TQ",
    "6,1": "TR",
    "5,0": "TV",
 }

// Centroid x and y of feature
var x = centroid($feature).x
var y = centroid($feature).y

// Create a key for Look-Up (LU) dictionary
var xKey = floor(x/100000)
var yKey = floor(y/100000)
var key = xKey + "," + yKey

// key NOT found in LU dictionary
if(!HasKey(LU, key)){return "Invalid"}

return LU[key] + text(floor(x),'00000') + text(floor(y),'00000')

 

 

 

View solution in original post

0 Kudos
7 Replies
DanPatterson
MVP Esteemed Contributor

maybe you could use Calculate Geometry to add values from a field and then parse that for what you need

utm.png

 or maybe the mgrs?

utm2.png


... sort of retired...
0 Kudos
kazzie
by
Emerging Contributor

Im currently using Pro 2.8.3 in BNG Coordinate system and the property options don't seem to be available to me in the calculate Geometry attributes tool

0 Kudos
AndyFairbairn
Regular Contributor

Hi Kazzie

If you want to use BNG it's under Projected Coordinate System > National Grids > Europe > British National Grid, though you can just say same as the layer or map if they are already in BNG

AndyFairbairn_1-1668168197412.png

 

 

To put Grid Refs straight into an attribute table field using Python in Calculate Field, I use this (I'm only interested in SU, SP, TQ and TL squares, so needs modifying to handle other grid squares). Will only work if your layer is in BNG already.

# EXPRESSION - put in upper box 

tenFigGR(!shape!)

# CODE BLOCK  - lower large box 

def tenFigGR(myPoint):
		
    # round X and Y to nearest 1 meter
    # for 10 fig GRs
    X = str(round(myPoint.trueCENTROID.X,0))
    Y = str(round(myPoint.trueCENTROID.Y,0))

    GRStr = "Invalid"

    if (X[:1]=="4"):
        if (Y[:1] == "1"): 
            GRStr = "SU"
        elif (Y[:1]=="2"): 
            GRStr = "SP"
    elif(X[:1]=="5"):
        if(Y[:1]=="1"):
            GRStr = "TQ"
        elif(Y[:1]=="2"):
            GRStr ="TL"

    GRStr = GRStr + X[1:6] + Y[1:6]
    
    return GRStr

hope this helps

0 Kudos
kazzie
by
Emerging Contributor

Thanks great Andy, that's what I needed and works for calculated field but was hoping it would be possible for calculated attribute rule if possible.

0 Kudos
AndyFairbairn
Regular Contributor

Hi Kazzie, I've written a version in Arcade that will work for any grid square (provided your data layer is in BNG). You could make it shorter by removing any of the LU dictionary key value pairs you don't need.

 

 

var LU = {
    "4,12": "HP",
    "3,11": "HT",
    "4,11": "HU",
    "1,10": "HW",
    "2,10": "HX",
    "3,10": "HY",
    "4,10": "HZ",
    "0,9": "NA",
    "1,9": "NB",
    "2,9": "NC",
    "3,9": "ND",
    "0,8": "NF",
    "1,8": "NG",
    "2,8": "NH",
    "3,8": "NJ",
    "4,8": "NK",
    "0,7": "NL",
    "1,7": "NM",
    "2,7": "NN",
    "3,7": "NO",
    "1,6": "NR",
    "2,6": "NS",
    "3,6": "NT",
    "4,6": "NU",
    "1,5": "NW",
    "2,5": "NX",
    "3,5": "NY",
    "4,5": "NZ",
    "5,5": "OV",
    "2,4": "SC",
    "3,4": "SD",
    "4,4": "SE",
    "2,3": "SH",
    "3,3": "SJ",
    "4,3": "SK",
    "1,2": "SM",
    "2,2": "SN",
    "3,2": "SO",
    "4,2": "SP",
    "1,1": "SR",
    "2,1": "SS",
    "3,1": "ST",
    "4,1": "SU",
    "0,0": "SV",
    "1,0": "SW",
    "2,0": "SX",
    "3,0": "SY",
    "4,0": "SZ",
    "5,4": "TA",
    "5,3": "TF",
    "6,3": "TG",
    "5,2": "TL",
    "6,2": "TM",
    "5,1": "TQ",
    "6,1": "TR",
    "5,0": "TV",
 }

// Centroid x and y of feature
var x = centroid($feature).x
var y = centroid($feature).y

// Create a key for Look-Up (LU) dictionary
var xKey = floor(x/100000)
var yKey = floor(y/100000)
var key = xKey + "," + yKey

// key NOT found in LU dictionary
if(!HasKey(LU, key)){return "Invalid"}

return LU[key] + text(floor(x),'00000') + text(floor(y),'00000')

 

 

 

0 Kudos
kazzie
by
Emerging Contributor

Hi Andy

Thanks for all your help, exactly what I needed and worked a treat. 

have a great weekend

Kazzie

0 Kudos
AndyFairbairn
Regular Contributor

Just edited my above code to cope with coordinate values >999,999, and <10,000 (padding '0's using text(x, '00000').

I'm using floor to round down, so returned grid reference is the containing 1m square for the centroid point (not the 1m square with the closest x-min, y-min corner). 

0 Kudos