{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tutorial: `spatial_mod`\n", "\n", "## Sample data\n", "Sample imagery captured from a [Resonon](https://resonon.com/) Pika II VIS-NIR line scanning imager and ancillary sample files can be downloaded from this [link](https://drive.google.com/drive/folders/1KpOBB4-qghedVFd8ukQngXNwUit8PFy_?usp=sharing).\n", "\n", "Before trying this tutorial on your own machine, please download the [sample files](https://drive.google.com/drive/folders/1KpOBB4-qghedVFd8ukQngXNwUit8PFy_?usp=sharing) and place into a local directory of your choosing (and do not change the file names). Indicate the location of your sample files by modifying `data_dir`:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "data_dir = r'F:\\\\nigo0024\\Documents\\hs_process_demo'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "\n", "## Confirm your environment\n", "\n", "Before trying the tutorials, be sure `hs_process` and its dependencies are [properly installed](installation.html#). If you installed in a *virtual environment*, first check we are indeed using the Python instance that was installed with the virtual environment:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Python install location: C:\\Users\\nigo0024\\Anaconda3\\envs\\hs_process\\python.exe\n", "Version: 0.0.4\n" ] } ], "source": [ "import sys\n", "import hs_process\n", "print('Python install location: {0}'.format(sys.executable))\n", "print('Version: {0}'.format(hs_process.__version__))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The *spec* folder that contains `python.exe` tells me that the activated Python instance is indeed in the `spec` environment, just as I intend. If you created a virtual environment, but your `python.exe` is not in the `envs\\spec` directory, you either did not properly create your virtual environment or you are not pointing to the correct Python installation in your IDE (e.g., Spyder, Jupyter notebook, etc.). \n", "\n", "***\n", "\n", "## `spatial_mod.crop_many_gdf`\n", "Crops many plots from a single image by comparing the image to a polygon file (geopandas.GoeDataFrame) that contains plot information and geometry of plot boundaries. [[API]](api/hs_process.spatial_mod.html#hs_process.spatial_mod.crop_many_gdf)\n", "\n", "Load the `geopandas`, `hsio` and `spatial_mod` modules" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import geopandas as gpd\n", "import os\n", "from hs_process import hsio\n", "from hs_process import spatial_mod" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read datacube and spatial plot boundaries (refer to the [API](api/hs_process.spatial_mod.html#hs_process.spatial_mod.crop_many_gdf) for more information about the parameter meanings and options)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "fname_in = os.path.join(data_dir, 'Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip.hdr')\n", "fname_gdf = os.path.join(data_dir, 'plot_bounds.geojson')\n", "\n", "gdf = gpd.read_file(fname_gdf)\n", "io = hsio(fname_in)\n", "my_spatial_mod = spatial_mod(io.spyfile)\n", "dir_out = os.path.join(io.base_dir, 'spatial_mod', 'crop_many_gdf')\n", "if not os.path.isdir(os.path.join(io.base_dir, 'spatial_mod')): # create a new folder named \"spatial_mod\" if it does not exist\n", " os.mkdir(os.path.join(io.base_dir, 'spatial_mod'))\n", "if not os.path.isdir(dir_out): # create a new folder named \"crop_many_gdf\" if it does not exist\n", " os.mkdir(dir_out)\n", " \n", "name_append = '-crop-many-gdf'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get instructions on how plots should be cropped via `spatial_mod.crop_many_gdf`; note that a `pandas.DataFrame` is returned with information describing how each plot should be cropped." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | directory | \n", "name_short | \n", "name_long | \n", "ext | \n", "plot_id_ref | \n", "pix_e_ul | \n", "pix_n_ul | \n", "buf_e_m | \n", "buf_n_m | \n", "buf_e_pix | \n", "buf_n_pix | \n", "crop_e_m | \n", "crop_n_m | \n", "crop_e_pix | \n", "crop_n_pix | \n", "gdf_shft_e_m | \n", "gdf_shft_n_m | \n", "gdf_shft_e_pix | \n", "gdf_shft_n_pix | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "None | \n", "None | \n", "None | \n", ".bip | \n", "1018 | \n", "113 | \n", "0 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "229 | \n", "76 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
1 | \n", "None | \n", "None | \n", "None | \n", ".bip | \n", "918 | \n", "342 | \n", "0 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "229 | \n", "76 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
2 | \n", "None | \n", "None | \n", "None | \n", ".bip | \n", "818 | \n", "571 | \n", "0 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "229 | \n", "76 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
3 | \n", "None | \n", "None | \n", "None | \n", ".bip | \n", "718 | \n", "800 | \n", "0 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "229 | \n", "76 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
4 | \n", "None | \n", "None | \n", "None | \n", ".bip | \n", "618 | \n", "1029 | \n", "0 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "229 | \n", "76 | \n", "NaN | \n", "NaN | \n", "NaN | \n", "NaN | \n", "