{ "cells": [ { "cell_type": "code", "execution_count": 16, "id": "827e9aae-17df-4df6-9441-d9a6486d4b84", "metadata": { "tags": [] }, "outputs": [], "source": [ "import rastereasy" ] }, { "cell_type": "markdown", "id": "8903a54f-410d-487e-b9ab-bc455cc9259d", "metadata": {}, "source": [ "# Create Geoimage from single tif bands\n", "\n", "This is very useful for creating a stack from SENTINEL-2 images for example" ] }, { "cell_type": "code", "execution_count": 17, "id": "3bab1ebe-a52f-43f2-b787-6892186f974f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on function files2stack in module rastereasy.rastereasy:\n", "\n", "files2stack(imagefile_path, resolution=None, names='origin', dest_name=None, ext='jp2', history=False)\n", " Create a stacked Geoimage from multiple single-band images.\n", " \n", " This function creates a multi-band Geoimage by stacking individual images,\n", " either from a list of image paths or from all images in a directory.\n", " All input images should have 1 band each.\n", " \n", " Parameters\n", " ----------\n", " imagefile_path : str or list of str\n", " - If a list of strings: paths to image files to stack (e.g., ['image1.jp2', 'image2.jp2', ...])\n", " - If a string: path to a directory containing images with the specified extension\n", " resolution : float, optional\n", " Resolution to which all images will be resampled. If None, all images must\n", " have the same resolution already.\n", " Default is None.\n", " names : dict or str, optional\n", " How to name the spectral bands in the stack:\n", " - If a dict: Maps band names to indices (e.g., {'B': 1, 'G': 2, 'R': 3, ...})\n", " - If \"origin\" (default): Uses the original filenames as band names\n", " - If None: Assigns numeric names ('1', '2', '3', ...)\n", " Default is \"origin\".\n", " dest_name : str, optional\n", " Path to save the stacked image as a TIFF file.\n", " Default is None (no file saved).\n", " ext : str, optional\n", " File extension of images to load if imagefile_path is a directory.\n", " Default is 'jp2'.\n", " history : bool, optional\n", " Whether to enable history tracking for the output Geoimage.\n", " Default is False.\n", " \n", " Returns\n", " -------\n", " Geoimage\n", " A stacked Geoimage containing all the input images as bands.\n", " \n", " Examples\n", " --------\n", " >>> # Stack from a list of image files\n", " >>> list_images = ['band1.jp2', 'band2.jp2', 'band3.jp2']\n", " >>> stacked_image = files2stack(list_images)\n", " >>> stacked_image.save('stacked.tif')\n", " >>>\n", " >>> # Stack all jp2 files from a directory with resolution resampling\n", " >>> folder_path = './my_bands_folder'\n", " >>> stacked_image = files2stack(folder_path, resolution=10)\n", " >>> stacked_image.info()\n", " \n", " Notes\n", " -----\n", " This function is particularly useful for combining separate band files (common in\n", " satellite imagery) into a single multi-band image for analysis.\n", "\n" ] } ], "source": [ "help(rastereasy.files2stack)" ] }, { "cell_type": "markdown", "id": "ed1ea3b8-0564-4280-8c37-0b45a4570986", "metadata": {}, "source": [ "## 1) Organization of data\n", "\n", "Here, images are in `./data/demo/sentinel/copacabana_ipanema_synthesis/202406/`" ] }, { "cell_type": "code", "execution_count": 18, "id": "660e838b-666c-448e-8d53-83b4fc75c3a6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['B08.tif',\n", " 'B09.tif',\n", " 'B8A.tif',\n", " 'B02.tif',\n", " 'B03.tif',\n", " 'B01.tif',\n", " 'B04.tif',\n", " 'B11.tif',\n", " 'B05.tif',\n", " 'B07.tif',\n", " 'B06.tif',\n", " 'B12.tif']" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir_data = './data/demo/sentinel/copacabana_ipanema_synthesis/202406/'\n", "import os\n", "os.listdir(dir_data)" ] }, { "cell_type": "markdown", "id": "708aa2f3-da9e-4237-991d-6555c6ea3776", "metadata": {}, "source": [ "## 2) Creation of a stack with band names associated with file names\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "b488fb18-aed9-4ddd-a22d-a0795587b4b8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- Size of the image:\n", " - Rows (height): 369\n", " - Cols (width): 467\n", " - Bands: 12\n", "- Spatial resolution: 10.0 meters / degree (depending on projection system)\n", "- Central point latitude - longitude coordinates: (-22.97848940, -43.19246031)\n", "- Driver: GTiff\n", "- Data type: int16\n", "- Projection system: EPSG:32723\n", "- Nodata: -32768.0\n", "\n", "- Given names for spectral bands: \n", " {'B01': 1, 'B02': 2, 'B03': 3, 'B04': 4, 'B05': 5, 'B06': 6, 'B07': 7, 'B08': 8, 'B09': 9, 'B11': 10, 'B12': 11, 'B8A': 12}\n", "\n", "\n" ] } ], "source": [ "stack=rastereasy.files2stack(dir_data,ext='tif')\n", "stack.info()" ] }, { "cell_type": "markdown", "id": "61797809-5aef-45e7-b0df-6ef1a81c61ab", "metadata": {}, "source": [ "## 3) Creation of a stack with specified resolution\n" ] }, { "cell_type": "code", "execution_count": 21, "id": "62a3af07-8dc5-4ca4-a54e-56259c68ea85", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- Size of the image:\n", " - Rows (height): 123\n", " - Cols (width): 155\n", " - Bands: 12\n", "- Spatial resolution: 30.129032258064512 meters / degree (depending on projection system)\n", "- Central point latitude - longitude coordinates: (-22.97857857, -43.19236096)\n", "- Driver: GTiff\n", "- Data type: int16\n", "- Projection system: EPSG:32723\n", "- Nodata: -32768.0\n", "\n", "- Given names for spectral bands: \n", " {'B01': 1, 'B02': 2, 'B03': 3, 'B04': 4, 'B05': 5, 'B06': 6, 'B07': 7, 'B08': 8, 'B09': 9, 'B11': 10, 'B12': 11, 'B8A': 12}\n", "\n", "\n" ] } ], "source": [ "desired_resolution = 30\n", "stack=rastereasy.files2stack(dir_data,ext='tif',resolution=desired_resolution)\n", "stack.info()" ] }, { "cell_type": "markdown", "id": "a4e47afd-5b24-4bc2-870f-de7dcd2e8e38", "metadata": {}, "source": [ "## 4) Creation of a stack with reformated names\n" ] }, { "cell_type": "code", "execution_count": 22, "id": "7b25b576-c9ae-4e73-8809-d0ef47024e91", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "- Size of the image:\n", " - Rows (height): 369\n", " - Cols (width): 467\n", " - Bands: 12\n", "- Spatial resolution: 10.0 meters / degree (depending on projection system)\n", "- Central point latitude - longitude coordinates: (-22.97848940, -43.19246031)\n", "- Driver: GTiff\n", "- Data type: int16\n", "- Projection system: EPSG:32723\n", "- Nodata: -32768.0\n", "\n", "- Given names for spectral bands: \n", " {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}\n", "\n", "\n" ] } ], "source": [ "stack=rastereasy.files2stack(dir_data,ext='tif',names=None)\n", "stack.info()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.18" } }, "nbformat": 4, "nbformat_minor": 5 }