I'm having some issues that I'm seeking help with. I have a 13 band TIFF and a trained random forest classifier with 13 features that are in the correct order to match the 13 bands. I haven't made it to the prediction part yet. The TIFF's dimensions are (13,3426,3010). I'm trying to reshape my TIFF so it can be classified by the RFC. The very last line is the line that raises an error. In the very last line of the code sample below a ValueError "cannot reshape array of size 578994 into shape (10312260,13)" is returned. I have tried different ways of reshaping so the last two lines of code below are the most recent try.
import geopandas as gpd
from sklearn.ensemble import RandomForestClassifier
import pandas as pd
import numpy as np
import geopandas as gpd
from sklearn.metrics import accuracy_score, classification_report
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import glob
import os
import rasterio
### There is other code here in the middle that isn't relevant ###
tifs = glob.glob(os.path.join(datafolder,'*season1.tif'))
src = rasterio.open(tifs[0])
ht,wd,crs,tform = src.height,src.width,src.crs,src.transform
src.close()
timesatStack = rasterio.open(
os.path.join(datafolder,'timesatStack1.tif'), mode = 'w',
driver = 'GTiff',
height = ht,
width = wd,
count = 13,
dtype = 'float32',
crs = crs,
transform = tform,
compress='lzw'
)
for ctr, image in enumerate(tifs):
src = rasterio.open(image)
timesatStack.write(src.read(1), ctr +1)
src.close()
timesatStack.close()
stacked_img = rasterio.open(os.path.join(datafolder, 'timesatStack1.tif'), mode = 'r')
stacked_image = stacked_img.read()
new_shape = (stacked_img.shape[1] * stacked_img.shape[0], 13)
img_as_array = stacked_image[:, :, :13].reshape(new_shape)
stacked_image[:, :, :13]
probably returns all bands but only a 3426x13 image for each band. The behaviour matches the docs: rasterio.readthedocs.io/en/stable/topics/… where a call toread()
with no arguments returns an image where the first axis is the bands. The thing I don't understand is hownew_shape = (stacked_img.shape[1] * stacked_img.shape[0], 13)
is equal to(10312260, 12)
?