<jupyter_start><jupyter_text>Given an input geotiff we create chips of the geotiff<jupyter_code># !pip install -r requirements.txt
import numpy as np
import rasterio
from rasterio.plot import show
def trainchips(img_filepath: str, stride: int, winsize: int):
    """
    Given a geotiff, chip into n chipped geotiffs of size winsize and stride
    """
    image = rasterio.open(img_filepath)
    imgarr = image.read()
    print(f"Shape of training data  is: {imgarr.shape}")

    written_files = []
    index = 0
    for i in np.arange(0, imgarr.shape[1], stride):
        for j in np.arange(0, imgarr.shape[2], stride):
            img = imgarr[:, i:i+winsize, j:j+winsize]
            x, y = (j*image.transform[0] + image.transform[2]), (image.transform[5] + i*image.transform[4])
            out_transform = [image.transform[0], 0, x, 0, image.transform[4], y]
            out_filename = f"chips/{str(index)}_img.tif"
            with rasterio.open(out_filename, "w", driver='GTiff', count=imgarr.shape[0], dtype=imgarr.dtype,
                         width=winsize, height=winsize, transform = out_transform, crs = image.crs) as raschip:
                         raschip.write(img)
            index += 1
            written_files.append(out_filename)      
    return written_files
stride = 32
winsize = 64
in_file = "example.tif"
imgchips = trainchips(in_file, stride, winsize)
rasterio.open(in_file).transform
show(rasterio.open(in_file))
show(rasterio.open(imgchips[0]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1240053050397878, 'fraction_numerical': 0.013262599469496022, 'mean_word_length': 3.191666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '229706', 'license': 'MIT', 'n_tokens_mistral': 528, 'n_tokens_neox': 514, 'n_words': 121, 'path': 'ImgChips.ipynb', 'repo_name': 'robmarkcole/GeoTagged_ImageChip', 'size': '1508'}