{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# Extract random points\n", "\n", "This notebook allows to randomly select a certain number of samples (points) from a categorical raster." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "import random\n", "import numpy as np\n", "import pandas as pd\n", "import geopandas as gpd\n", "import rasterio\n", "from pathlib import Path" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "**Set directory**" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "computer_path = '/export/miro/ndeffense/LBRAT2104/'\n", "grp_nb = '99'\n", "\n", "data_path = f'{computer_path}data/' # Directory with data shared by the assistant\n", "work_path = f'{computer_path}STUDENTS/GROUP_{grp_nb}/TP/' # Directory for all work files\n", "\n", "\n", "# Input directory\n", "land_cover_path = f'{work_path}LAND_COVER/'\n", "\n", "# Output directory\n", "points_path = f'{work_path}SAMPLES_POINTS/'\n", "\n", "Path(points_path).mkdir(parents=True, exist_ok=True)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "**Set filenames**" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "categorical_tif = f'{land_cover_path}Corine_Land_Cover_32631_ROI.tif'\n", "\n", "randomly_selected_points_shp = f'{land_cover_path}Corine_Land_Cover_selected_points_ROI.shp'" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "**Set parameters**" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "no_data = 999\n", "\n", "epsg = '32631' # Projection of the categorical GeoTIFF\n", "\n", "# Select a different number of points per class\n", "classes_list = [1,2]\n", "nb_points_per_class_list = [50,75]\n", "\n", "# Select the same number of points for every classes\n", "classes_list = []\n", "np_points_for_all_classes = 10" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Build dataframe with the number of points to select for each class" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | Class | \n", "Nb points | \n", "
|---|---|---|
| 0 | \n", "1 | \n", "10 | \n", "
| 1 | \n", "2 | \n", "10 | \n", "
| 2 | \n", "3 | \n", "10 | \n", "
| 3 | \n", "4 | \n", "10 | \n", "
| 4 | \n", "7 | \n", "10 | \n", "
| 5 | \n", "10 | \n", "10 | \n", "
| 6 | \n", "11 | \n", "10 | \n", "
| 7 | \n", "12 | \n", "10 | \n", "
| 8 | \n", "16 | \n", "10 | \n", "
| 9 | \n", "20 | \n", "10 | \n", "
| 10 | \n", "21 | \n", "10 | \n", "
| 11 | \n", "23 | \n", "10 | \n", "
| 12 | \n", "25 | \n", "10 | \n", "
| 13 | \n", "40 | \n", "10 | \n", "