AnsweredAssumed Answered

Not sure how to fix an Index Error

Question asked by rralbritton on Sep 30, 2015
Latest reply on Oct 1, 2015 by rralbritton

I am working on updating a python script that was created in python 2.6 for ArcGIS 10.0.

We are now working with python 2.7.8 in ArcGIS 10.3

 

I did not write the script and am still a little fuzzy on exactly what its doing (which is probably why I need help figuring out this error).

 

The script is performing a Moore Neighborhood analysis on an array where it's walking through each cell in the DEM raster and comparing that cell value to the 8 surrounding cells, it extracts a minimum value from those 8 cells and then inserts that value into a corresponding constant array.

 

The script runs through the first 3 if statements ok, but then gets to the 4th one and throws the error:

 

Traceback (most recent call last):

  File "S:\Rachel\Projects\Storm_Sewer\Python\Surface_Flow.py", line 51, in <module>

    if myArray[u][v]>myArray[u-1][v+1]:

IndexError: index 3510 is out of bounds for axis 0 with size 3510

 

Presumably, the script is calculating something outside a a predefined range but I'm not sure what that is and/or how to fix it so that the script still returns the correct values.

 

Both the original DEM and constant raster's were floating values before turning them into arrays. Not sure if that's relevant.

 

SCRIPT:

 

 

#Global Variables:
t=numpy.empty((49,1))
c=0

#Turn DEM into an array matrix.
myArray = arcpy.RasterToNumPyArray(DEM)
constant2 = arcpy.RasterToNumPyArray(constant)

#Moore Neighborhood Analysis
#Neighboorhood Analysis  will walk through each cell comparing all surrounding
#cells, extract minimum values, and insert them into the constant raster location

for u in range(len(myArray)-1):
    for v in range(len(myArray)-1):
        
        if myArray[u][v]>myArray[u-1][v]:
            constant2[u-1][v]=myArray[u-1][v]

        if myArray[u][v]>myArray[u+1][v]:
            constant2[u+1][v]=myArray[u+1][v]

        if myArray[u][v]>myArray[u-1][v-1]:
            constant2[u-1][v-1]=myArray[u-1][v-1]

        if myArray[u][v]>myArray[u-1][v+1]:
            constant2[u-1][v+1]=myArray[u-1][v+1]

        if myArray[u+1][v+1]>myArray[u+1][v+1]:
            constant2 [u+1][v+1]=myArray[u+1][v+1]

        if myArray[u][v]>myArray[u+1][v-1]:
            constant2 [u+1][v-1]=myArray[u+1][v-1]

        if myArray[u][v]>myArray[u][v+1]:
            constant2[u][v+1]=myArray[u][v+1]

        if myArray[u][v-1]>myArray[u][v-1]:
            constant2[u][v-1]=myArray[u][v-1]

Outcomes