-1

I am trying to plot a raster image using Rasterio, Matplotlib, and Numpy. Although what I am doing is probably irrelevant and general to the actual python code and error.

Here is the code:

import rasterio
import matplotlib.pyplot as plt
import numpy as np

band5 = rasterio.open(r"C:\Users\new\Desktop\LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF")

band4 = rasterio.open(r"LC08_L1TP_015033_20170822_20170912_01_T1_B4.TIF")

#rasterio.windows.Window(col_off, row_off, width, height)
window = rasterio.windows.Window(1024, 1024, 1280, 2560)

with rasterio.open(band5) as src:
    subset = src.read(1, window=window)

plt.figure(figsize=(6,8.5))
plt.imshow(subset)
plt.colorbar(shrink=0.5)
plt.title(f'Band 5 Subset\n{window}')
plt.xlabel('Column #')
plt.ylabel('Row #')

Complete error message with traceback:

runfile('C:/Users/new/Desktop/RasterSubsetNDVI.py', wdir='C:/Users/new/Desktop')
Traceback (most recent call last):

  File "<ipython-input-131-5d04fa0ce75f>", line 1, in <module>
    runfile('C:/Users/new/Desktop/RasterSubsetNDVI.py', wdir='C:/Users/new/Desktop')

  File "C:\Users\new\Miniconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\new\Miniconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/new/Desktop/RasterSubsetNDVI.py", line 12, in <module>
    with rasterio.open(band5) as src:

  File "C:\Users\new\Miniconda3\lib\contextlib.py", line 81, in __enter__
    return next(self.gen)

  File "C:\Users\new\Miniconda3\lib\site-packages\rasterio\__init__.py", line 177, in fp_reader
    memfile = MemoryFile(fp.read())

  File "C:\Users\new\Miniconda3\lib\site-packages\rasterio\io.py", line 105, in __init__
    file_or_bytes=file_or_bytes, filename=filename, ext=ext)

  File "rasterio/_io.pyx", line 745, in rasterio._io.MemoryFileBase.__init__

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

I am using code found here and trying to reproduce it on my machine with my own TIF files. Where it says "Pull an image subset at full resolution". LINK: https://github.com/geohackweek/tutorial_contents/blob/ba5e9443137a9aca87cdcdcd70e9e6a237cc64ba/raster/notebooks/rasterio-landsat-aws.ipynb

I am running on the IDE Sypder and using Python 3.6

Please, any help is appreciated.

Thank you

yuen2
  • 79
  • 1
  • 2
  • 7
  • 1
    See [mcve] or [Short, Self Contained, Correct, Example](http://sscce.org). – ImportanceOfBeingErnest Nov 18 '18 at 19:59
  • @ImportanceOfBeingErnest Thank you, I should have produced a code that everyone could run on their own machines. It was kind of hard because I don't know how to give people access to my TIF files. I thought maybe it might be something in my computation. – yuen2 Nov 18 '18 at 20:05
  • @ImportanceOfBeingErnest I am trying to produce this code with my raster TIF files. Here is the link to the GitHub code where the title states "Pull an image subset at full resolution", LINK: https://github.com/geohackweek/tutorial_contents/blob/ba5e9443137a9aca87cdcdcd70e9e6a237cc64ba/raster/notebooks/rasterio-landsat-aws.ipynb – yuen2 Nov 18 '18 at 20:10
  • Whenever you report a Python error, include the *complete* error message (i.e. the complete traceback) in the question. There is useful information in there. – Warren Weckesser Nov 18 '18 at 20:14
  • @WarrenWeckesser Got it. I posted the complete traceback with the error message at the end. – yuen2 Nov 18 '18 at 20:17
  • Any ideas guys? – yuen2 Nov 18 '18 at 20:35

1 Answers1

0

Take a careful look at the following lines of your code (others omitted):

band5 = rasterio.open(r"C:\Users\new\Desktop\LC08_L1TP_015033_20170822_20170912_01_T1_B5.TIF")

# ...

with rasterio.open(band5) as src:
    # ...

From the Rasterio documentation:

Rasterio’s rasterio.open() takes a path and returns a dataset object.

In the second line above, you're passing a dataset to rasterio.open() instead of a path. Perhaps you wanted to pass the same path you used to create band5 from?

It would have been nice if rasterio.open() checked the type of the argument you gave it and gave you an exception that said 'file path is not a string' or suchlike, but sadly it appears not to do this.

Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • Thank you! @Luke Woodward yes that was it. I was calling rasterio.open twice. I am trying to create an 800x600 subset. Do you think this would be correct for that? Code: window = rasterio.windows.Window(1024, 1024, 800, 600)….Not exactly sure what the 1024, 1024, I know that the 800, 600 is essentially saying 800x600, but why would setting the col_off and col_row to 0 make everything purple? Also the original TIF image I had was just black and white but now when I plot it it's purple and blue. Why is this? – yuen2 Nov 18 '18 at 20:41
  • @yuen2: to be honest I don't know. I've never used `rasterio` before. It sounds to me like you have a separate question to ask, which isn't related to the error in your question. If so, please ask a separate question. – Luke Woodward Nov 18 '18 at 20:52
  • Luke Wooward Thank you. I am getting an IndexError: list index out of range. This is Geographic Information Systems problem. I posted the code for that error there. If you could take a look it would be much appreciated: https://gis.stackexchange.com/questions/303119/using-rasterio-matplotlib-and-numpy-trying-to-calculate-and-plot-ndvi-of-a-ras – yuen2 Nov 18 '18 at 21:03