Calculate Field with Python/ Arcade

2893
16
Jump to solution
11-25-2020 04:45 AM
NoahLeimgruber
New Contributor III

HI all!

I want to execute a Python/Arcade command in ArcGIS Calculate Field which gives me a result for the catchment area (table).
The command should select all IDs in the list that have an X-value greater than the ID to be calculated (Here ID 1) and a Y-value less than the ID to be calculated (Here ID 1).
From the selected IDs, the respective areas should then be summed up and saved to the catchment area field.

In this example, the X and Y values of IDs 2 and 3 fulfill the condition. Thus, the respective areas would be added together, which would result in 15. The result would be stored in the catchment area of ID 1.

Thank you very much for your help!

 

ID

X

Y

area

catchment area

1

1000

2000

23

? (15)

2

1034

1083

10

3

1599

1743

5

4

1403

2004

34

 

16 Replies
XanderBakker
Esri Esteemed Contributor

Hi @NoahLeimgruber ,

To include the min and max values in the range and also include the area of the polygon itself, you just need to change this line:

if oid != oidcurr and lst[0] > h1 and lst[1] < h2:

into this:

if lst[0] >= h1 and lst[1] <= h2:

 

This will assign a catchment area value to all polygons:

XanderBakker_0-1606418720715.png

 

0 Kudos
NoahLeimgruber
New Contributor III

hey @XanderBakker , the code works perfectly! thank you very much! you were a great help

0 Kudos
NoahLeimgruber
New Contributor III

Hey @XanderBakker 

Sorry for asking you again!

How do I need to change the code if the value to be summed up is not from the shape area ("SHAPE@AREA"), but from a field called "Q347"?

I already tried it with no luck. Maybe you have a short answer to this. If not, no worries.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @NoahLeimgruber ,

 

As far as I can see you should only have to change the line...

flds1 = ("OID@", "H1", "H2", "SHAPE@AREA")

 

into:

 

flds1 = ("OID@", "H1", "H2", "Q347")
0 Kudos
NoahLeimgruber
New Contributor III

Hi @XanderBakker 

Yes, that is exactly what I tried. But then I get the following error code

 

ERROR 000539: Traceback (most recent call last):
File "<string>", line 39, in <module>
File "<string>", line 19, in main
File "<string>", line 34, in SumAreaInRangeH
TypeError: unsupported operand type(s) for +=: 'float' and 'NoneType'
Failed to execute (CalculateField).

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @NoahLeimgruber ,

It looks like you have None (null) values in your Q347 field.

You can put an additional validation in the function:

def SumAreaInRangeH(oidcurr, dct):
    # function to determine catchment area
    sumarea = -1
    if oidcurr in dct:
        # set reference values
        h1 = dct[oidcurr][0]
        h2 = dct[oidcurr][1]
        sumarea = 0
        for oid, lst in dct.items():
            if oid != oidcurr and lst[0] > h1 and lst[1] < h2:
                if lst[2] is not None:
                    sumarea += lst[2]
    return sumarea

 

0 Kudos
NoahLeimgruber
New Contributor III

@XanderBakker 

Okay now it just worked. the problem were " NA " values in the column " Q347 ".
Thank you very much anyway

0 Kudos