Not sure how to fix an Index Error

4696
11
09-30-2015 10:59 AM
RachelAlbritton
Occasional Contributor III

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>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>myArray[u-1]:
            constant2[u-1]=myArray[u-1]

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

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

        if myArray>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>myArray[u+1][v-1]:
            constant2 [u+1][v-1]=myArray[u+1][v-1]

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

        if myArray[v-1]>myArray[v-1]:
            constant2[v-1]=myArray[v-1]
0 Kudos
11 Replies
NeilAyres
MVP Alum

There seems to be some inconsistency in you code :

for u in range(len(myArray)-1): 

  • for v in range(len(myArray)-1): 
  • if myArray>myArray[u-1]
  •             constant2[u-1]=myArray[u-1] 
  • if myArray>myArray[u+1]
  •             constant2[u+1]=myArray[u+1] 
  • if myArray>myArray[u-1][v-1]: 
  •             constant2[u-1][v-1]=myArray[u-1][v-1
  • if myArray>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>myArray[u+1][v-1]: 
  •             constant2 [u+1][v-1]=myArray[u+1][v-1
  • if myArray>myArray[v+1]: 
  •             constant2[v+1]=myArray[v+1
  • if myArray[v-1]>myArray[v-1]: 
  •             constant2[v-1]=myArray[v-1

Gee that's looks terrible on my side.

All the "ifs" are comparing myArray(,v]) with the cells around it. Except at lines 11 & 17 (I think)

Why is that?

0 Kudos
NeilAyres
MVP Alum

Then, why not just run a raster filter for Min on the original raster with a 3X3 processing window?

0 Kudos
RachelAlbritton
Occasional Contributor III

The route makes a lot more sense. I ran the tool, but the output was exactly the same as the input. I find that odd, don't you?

0 Kudos
NeilAyres
MVP Alum

You ran this tool on your data...

FocalStatstics_Min.jpg

This is in SA Tools / Neighbourhood /

The result cannot be the same...

0 Kudos
RachelAlbritton
Occasional Contributor III

That's exactly what I ran and the results came out exactly like my DEM - somethings wrong and I'm not sure what. DEM being used as input (notes high and low values):

DEM.PNG

Focal Stats Window:

FocalStatsWindow.PNG

Results (Note image and high/low values are exactly the same as the DEM)

FocalResults.PNG

ResultsWindow.PNG

0 Kudos
NeilAyres
MVP Alum

Just because the min/max values in the TOC are the same, the internal values (ie the distribution) should / have to be a bit different.

But this is getting a little off the original topic.

0 Kudos
RachelAlbritton
Occasional Contributor III

I'm OK with getting a little off topic if the topic leads me to produce the results I need. Thank you

0 Kudos
DanPatterson_Retired
MVP Emeritus

I don't suppose you can tell me what the shape and number of dimensions the input and constant raster have?  And perhaps you can elaborate on what t=numpy.empty((49,1)) is supposed to be...is that the destination array? (which will only hold 49 rows and 1 column of data. What is constant? c? are there lines of code missing? Could you provide the un-updated script that you know worked?

0 Kudos
RachelAlbritton
Occasional Contributor III

Both the original input DEM & constant rasters are 3510 (columns) and 3645 (rows)

I'm still trying figure out what the Global variables are there for as well. I commented them out to see if it would make a difference and I still got the same error.

0 Kudos