1

I wrote the following code for GIS analysis, in which I am trying to find the sum of all pavement area values that meet the condition where Application == "Micro Surface" & CouncilDistrict == 1.

I have more than 85,000 records in the attribute table with any of six different values for the field named Application and values ranging from 0 to 10 for CouncilDistrict. I want to find the sum of values for the Pvment_Area field that meet the condition. I keep getting the following error:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

How do I extract values using NumPy?

import arcpy
import numpy
input = "C:\Temp\Street1.mdb\Scenario_78M"
arr = arcpy.da.FeatureClassToNumPyArray(input, ('Pvment_Area', 'Application', 'CouncilDistrict'))
x=(arr[arr['Application'] == "Micro Surface" AND ['CouncilDistrict'] == 1]]["Pvment_Area"].sum())
print'Pvment_Area is',x

1 Answer 1

1

The and condition needs to be expressed as &. In addition because of the precedence of & you will need parenthesis. like:

Code:

x=(arr[(arr['Application'] == "Micro Surface") & (['CouncilDistrict'] == 1])]["Pvment_Area"].sum()

Test Code:

import numpy as np

data = np.transpose([
    [200.0, 'Micro Surface', 1],
    [100.0, 'Micro Surface', 1],
    [100.0, 'Micro Surface', 0],
    [100.0, 'Not Micro Surface', 1],
    [100.0, 'Not Micro Surface', 0],
])
names = ('Pvment_Area', 'Application', 'CouncilDistrict')
arr = np.core.records.fromarrays(data, names=names, formats='f8, S20, i8')

records = ((arr['Application'] == b"Micro Surface") &
           (arr['CouncilDistrict'] == 1))

area = arr[records]["Pvment_Area"].sum()

print('Pvment_Area is', area)

Results:

Pvment_Area is 300.0
4
  • When I change from AND to & and specify the parenthesis, I get the following error "TypeError: 'numpy.ndarray' object is not callable". Plus, my data is not in Excel. Its in GIS attribute table. Thanks!
    – vanhorne
    Commented Jun 4, 2017 at 16:38
  • This means your parens are likely incorrect. Look for something that looks like a call eg: like MY_ARRAY() Commented Jun 4, 2017 at 16:40
  • I fixed the parenthesis but the code returns "false" values. [False False False ..., False False False]. I am not sure why.
    – vanhorne
    Commented Jun 4, 2017 at 17:23
  • Start with the working example in my post, and work up from there. GL Commented Jun 4, 2017 at 17:26

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