0

I am trying python for the first time. I have a raster layer of Type Float32, and another raster output of Type Integer created from this raster layer outside the python code. I want to scan every column wise, and pick up the Float raster minimum value location within a 5 neighbourhood of Integer raster value of 1 in every column and assign the value 1 at this minimum value location and all other pixels as Null.

I am getting the Type Error : 'int' object does not support item assignment

I am giving the python code below:

new_array = [ [0 for i in range(band.XSize)] for j in range(band.YSize)]

for i, item in enumerate(myarray1):
    for j, element in enumerate(item):
        if (myarray2[i][j] == 1):
            if myarray1[i][j] < myarray1[i-2][j]:
                new_array[i-2][j] = -999
                if myarray1[i][j] < myarray1[i-1][j]:
                    new_array[i-1][j] = -999
                    if myarray1[i][j] < myarray1[i+1][j]:
                        if myarray1[i][j] < myarray1[i+2][j]:
                            new_array[i][j] = 1                
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
                    else:
                        if myarray1[i+1][j] < myarray1[i+2][j]:
                            new_array[i+1][j] = 1
                            new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
                else:
                    if myarray1[i-1][j] < myarray1[i+1][j]:
                        if myarray1[i-1][j] < myarray1[i+2][j]:
                            new_array[i-1][j] = 1
                            new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
                    else:
                        if myarray1[i+1][j] < myarray1[i+2][j]:
                            new_array[i+1][j] = 1
                            new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
            else:
                if myarray1[i-2][j] < myarray1[i-1][j]:
                    if myarray1[i-2][j] < myarray1[i+1][j]:
                        if myarray1[i-2][j] < myarray1[i+2][j]:
                            #new_array[i-2][j] = 1
                            new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
                    else:
                        if myarray1[i+1][j] < myarray1[i+2][j]:
                            new_array[i+1][j] = 1
                            new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
                else:
                    if myarray1[i-1][j] < myarray1[i+1][j]:
                        if myarray1[i-1][j] < myarray1[i+2][j]:
                            new_array[i-1] = 1
                            #new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
                    else:
                        if myarray1[i+1][j] < myarray1[i+2][j]:
                            new_array[i+1] = 1
                            new_array[i][j] = -999
                        else:
                            new_array[i+2][j] = 1
                            new_array[i][j] = -999
        else:
            new_array[i][j] = -999
  • 1
    At what line are you getting this error? – smiller Jun 25 at 11:28
  • You do not seem to be using QGIS so I removed the tag for that and added one for GDAL that you do appear to be using. Please try to provide a code snippet rather than your full code. – PolyGeo Jun 25 at 11:35
  • I am getting the error at the line 92 - corresponding to the line:new_array[i][j] = -999 – Slu Jun 25 at 11:37
  • 2
    This looks like a simple typo in the Python code, on the line new_array[i-1] = 1 (and any similar ones), where you accidentally change your array's structure, not at all related to GIS aspects of your code. – Dan Getz Jun 25 at 12:21
  • 1
    You are missing the whole point of using numpy by looping through each cell. Search for rolling window using numpy – Marcelo Villa Jun 25 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.