1

I have ndarray with shape of (12, 773, 1231) ((12 bands)). I want to save it as array in order to reproject it. I have image with the same shape and image but reprojected so I baiscally want my ndarray "to take" the coordinates from the original image.

I am trying to use the folliwing script from this post:

img=img=rasterio.open('TOA_22_10_2020_ (1).tif')

 ##doing  processes on the image as numpy ndarray....

#now let's save it as tiff with the original image coordinates

 with rasterio.open('ff_correction.tif', 
                    'w',
                    driver='GTiff',
                    height=ff_correction.shape[1],
                    width=ff_correction.shape[2],
                    count=12,
                    dtype=ff_correction.dtype,
                    crs=img.crs,
                    nodata=None, # change if data has nodata value
                    transform=img.transform) as dst:
        dst.write(ff_correction, 12)

But I recieve the next error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-74-786bb4d9518d> in <module>
      9                    nodata=None, # change if data has nodata value
     10                    transform=img.transform) as dst:
---> 11        dst.write(ff_correction, 1)

rasterio\_io.pyx in rasterio._io.DatasetWriterBase.write()

ValueError: Source shape (1, 12, 773, 1231) is inconsistent with given indexes 1

I don't know why the shape changes in the error. what is the mistake?

My end goal us to reproject the ndarray to have the same coordinates as the original image, but other solutions for how to give the ndarray the original image coordinates back would work as well.

Edit: Adding what I have done based on John answer, still getting error:

ff_correction.shape
>>>(12, 773, 1231)

np.squeeze(ff_correction).shape
>>>(12, 773, 1231)

>>> with rasterio.open('ff_correction.tif', 
                    'w',
                    driver='GTiff',
                    height=ff_correction.shape[1],
                    width=ff_correction.shape[2],
                    count=12,
                    dtype=ff_correction.dtype,
                    crs=img.crs,
                    nodata=None, # change if data has nodata value
                    transform=img.transform) as dst:
        dst.write(np.squeeze(ff_correction), 12)


>>>ValueError: Source shape (1, 12, 773, 1231) is inconsistent with given indexes 1

EDIT2: When I save only one band it works. but I need all the 12...

  • Does the shape change in the error? What happens if you try print(ff_correction.shape()) in your with-open block? I think if you use np.squeeze() on ff_correction, it will work. – Jon Nov 17 '20 at 15:33
  • in which part to print ff_correction.shape())? not sure I understood – Reut Nov 17 '20 at 15:47
  • After the "transform=img.transform) as dst:" line. What's likely happening is that rasterio is reading in 4 dimensions, one of which is a singleton. You can remove the singleton with squeeze(). – Jon Nov 17 '20 at 16:37
  • @Jon when I print it I get the shape (12, 773, 1231) then i'm not sure squuze will help. I have tried to put the squuze in the luine "dst.write('ff_correction.squeeze(),12) but recieved the same error – Reut Nov 18 '20 at 9:31
1

I found the solution: in dst.write I had to wrote each band seperatly. Need to find more esthetic way to write it but it worked.

 with rasterio.open('ff_correction.tif', 
                    'w',
                    driver='GTiff',
                    height=ff_correction.shape[1],
                    width=ff_correction.shape[2],
                    count=12,
                    dtype=ff_correction.dtype,
                    crs=img.crs,
                    nodata=None, # change if data has nodata value
                    transform=img.transform) as dst:
        print(ff_correction.shape)
        dst.write(ff_correction[0], 1)
        dst.write(ff_correction[1], 2)
        dst.write(ff_correction[2], 3)
        dst.write(ff_correction[3], 4)
        dst.write(ff_correction[4], 5)
        dst.write(ff_correction[5], 6)
        dst.write(ff_correction[6], 7)
        dst.write(ff_correction[7], 8)
        dst.write(ff_correction[8], 9)
        dst.write(ff_correction[9], 10)
        dst.write(ff_correction[10], 11)
        dst.write(ff_correction[11], 12)

  • 1
    Good job figuring it out. If you wanted to make it a little more compact, you could put all the dst.write() statements in a for loop. – Jon Nov 18 '20 at 13:42

Your Answer

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

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