Quantcast
Channel: Question and Answer » arcpy
Viewing all articles
Browse latest Browse all 767

Identify polygons greater than n-th percentile and perform further analyses using arcpy

$
0
0

My goal is

  1. to identify tracts that are above the 99 percentile of a variable (xvar),
  2. to dissolve contiguous tracts by summing a variable (yvar), and
  3. to get the coordinates of the centroids (longitude and latitude) of the merged polygons.

At the very first stage for picking the above-99th-percentile tracts, I have got some errors. The following is my current work:

import arcpy
import numpy as np

input = r'D:7362.shp'
myarray = arcpy.da.FeatureClassToNumPyArray(input, ('xvar'))

print myarray
# Printed output:
# [(5.204770523,) (8.6471839055,) (2.1095837756,) ..., (1.4788840302,
# (6.24183499,) (3.6710648163,)]

p99 = np.percentile(myarray, 99)

# use cursor to create the indicator field
# Do I have to create the abovep99 field in the first place???
with arcpy.da.UpdateCursor(input, ['xvar','abovep99']) as cursor:
    for row in cursor:
        if row[0] > p99:
            row[1] = 1
        else:
            row[1] = 0
        cursor.updateRow(row)

The error messages in the percentile calculation p99 = np.percentile(myarray, 99) are following:

Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
p99 = np.percentile(myarray, 99)
File "C:Python27ArcGIS10.3libsite-packagesnumpylibfunction_base.py", line 3096, in percentile
return _compute_qth_percentile(sorted, q, axis, out)
File "C:Python27ArcGIS10.3libsite-packagesnumpylibfunction_base.py", line 3132, in _compute_qth_percentile
return add.reduce(sorted[indexer]*weights, axis=axis, out=out)/sumval
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.ndarray'

How do I fix this problem for the percentile calculation?


Viewing all articles
Browse latest Browse all 767

Trending Articles