{ "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", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
directoryname_shortname_longextplot_id_refpix_e_ulpix_n_ulbuf_e_mbuf_n_mbuf_e_pixbuf_n_pixcrop_e_mcrop_n_mcrop_e_pixcrop_n_pixgdf_shft_e_mgdf_shft_n_mgdf_shft_e_pixgdf_shft_n_pix
0NoneNoneNone.bip10181130NaNNaNNaNNaNNaNNaN22976NaNNaNNaNNaN
1NoneNoneNone.bip9183420NaNNaNNaNNaNNaNNaN22976NaNNaNNaNNaN
2NoneNoneNone.bip8185710NaNNaNNaNNaNNaNNaN22976NaNNaNNaNNaN
3NoneNoneNone.bip7188000NaNNaNNaNNaNNaNNaN22976NaNNaNNaNNaN
4NoneNoneNone.bip61810290NaNNaNNaNNaNNaNNaN22976NaNNaNNaNNaN
\n", "
" ], "text/plain": [ " directory name_short name_long ext plot_id_ref pix_e_ul pix_n_ul buf_e_m \\\n", "0 None None None .bip 1018 113 0 NaN \n", "1 None None None .bip 918 342 0 NaN \n", "2 None None None .bip 818 571 0 NaN \n", "3 None None None .bip 718 800 0 NaN \n", "4 None None None .bip 618 1029 0 NaN \n", "\n", " buf_n_m buf_e_pix buf_n_pix crop_e_m crop_n_m crop_e_pix crop_n_pix \\\n", "0 NaN NaN NaN NaN NaN 229 76 \n", "1 NaN NaN NaN NaN NaN 229 76 \n", "2 NaN NaN NaN NaN NaN 229 76 \n", "3 NaN NaN NaN NaN NaN 229 76 \n", "4 NaN NaN NaN NaN NaN 229 76 \n", "\n", " gdf_shft_e_m gdf_shft_n_m gdf_shft_e_pix gdf_shft_n_pix \n", "0 NaN NaN NaN NaN \n", "1 NaN NaN NaN NaN \n", "2 NaN NaN NaN NaN \n", "3 NaN NaN NaN NaN \n", "4 NaN NaN NaN NaN " ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_plots = my_spatial_mod.crop_many_gdf(spyfile=io.spyfile, gdf=gdf)\n", "df_plots.head(5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Use the data from the first frow of `df_plots` to crop a single plot from the original image (uses `spatial_mod.crop_single`)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "pix_e_ul=113\n", "pix_n_ul=0\n", "crop_e_pix=229\n", "crop_n_pix=75\n", "plot_id_ref=1018\n", "array_crop, metadata = my_spatial_mod.crop_single(\n", " pix_e_ul=pix_e_ul, pix_n_ul=pix_n_ul, crop_e_pix=crop_e_pix, crop_n_pix=crop_n_pix,\n", " spyfile=io.spyfile, plot_id_ref=plot_id_ref)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save the cropped datacube and geotiff to a new directory" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n" ] } ], "source": [ "fname_out = os.path.join(dir_out, io.name_short + '_plot_' + str(1018) + name_append + '.' + io.defaults.envi_write.interleave)\n", "fname_out_tif = os.path.join(dir_out, io.name_short + '_plot_' + str(1018) + '.tif')\n", "\n", "io.write_cube(fname_out, array_crop, metadata=metadata, force=True)\n", "io.write_tif(fname_out_tif, spyfile=array_crop, metadata=metadata)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using a for loop, use `spatial_mod.crop_single` and `hsio.write_cube` to crop by plot and save cropped datacubes and geotiffs to file" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_cube()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_1010-crop-many-gdf.bip.hdr\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_tif()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_1010.tif\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_cube()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_910-crop-many-gdf.bip.hdr\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_tif()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_910.tif\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_cube()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_810-crop-many-gdf.bip.hdr\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_tif()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_810.tif\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_cube()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_710-crop-many-gdf.bip.hdr\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_tif()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_710.tif\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_cube()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_610-crop-many-gdf.bip.hdr\n", "\n", "The size of ``spyfile`` is zero; thus there is nothing to write to file and ``write_tif()`` is being aborted.\n", "Filename: Wells_rep2_20180628_16h56m_pika_gige_7_plot_610.tif\n", "\n" ] } ], "source": [ "for idx, row in df_plots.iterrows():\n", " io.read_cube(fname_in, name_long=io.name_long,\n", " name_plot=row['plot_id_ref'],\n", " name_short=io.name_short)\n", " my_spatial_mod.load_spyfile(io.spyfile)\n", " array_crop, metadata = my_spatial_mod.crop_single(\n", " pix_e_ul=row['pix_e_ul'], pix_n_ul=row['pix_n_ul'],\n", " crop_e_pix=row['crop_e_pix'], crop_n_pix=row['crop_n_pix'],\n", " buf_e_m=2.0, buf_n_m=0.75,\n", " plot_id_ref=row['plot_id_ref'])\n", "\n", " fname_out = os.path.join(dir_out, io.name_short + '_plot_' + str(row['plot_id_ref']) + name_append + '.bip.hdr')\n", " fname_out_tif = os.path.join(dir_out, io.name_short + '_plot_' + str(row['plot_id_ref']) + '.tif')\n", "\n", " io.write_cube(fname_out, array_crop, metadata=metadata, force=True) # force=True to overwrite the plot_1018 image\n", " io.write_tif(fname_out_tif, spyfile=array_crop, metadata=metadata)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open cropped geotiff images in *QGIS* to visualize the extent of the cropped images compared to the original datacube and the plot boundaries (the full extent image is darkened and displayed in the background):\n", "![crop_many_gdf](../.././docs/source/img/spatial_mod/crop_many_gdf_qgis.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "\n", "## `spatial_mod.crop_single`\n", "Crops a single plot from an image. If `plot_id_ref` and `gdf` are explicitly passed (i.e., they will not be loaded from `spatial_mod` class), the \"map info\" tag in the metadata will be adjusted to center the cropped area within the appropriate plot geometry. [[API]](api/hs_process.spatial_mod.html#hs_process.spatial_mod.crop_single)\n", "\n", "Load and initialize the `hsio` and `spatial_mod` modules" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "from hs_process import hsio\n", "from hs_process import spatial_mod\n", "\n", "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", "io = hsio(fname_in)\n", "my_spatial_mod = spatial_mod(io.spyfile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Crop an area with a width (easting) *200 pixels* and a height (northing) of *50 pixels*, with a northwest/upper left origin at the *342nd column* (easting) and *75th row* (northing)." ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "pix_e_ul = 342\n", "pix_n_ul = 75\n", "array_crop, metadata = my_spatial_mod.crop_single(\n", " pix_e_ul, pix_n_ul, crop_e_pix=200, crop_n_pix=50)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Save as a geotiff using ``io.write_tif``, then load into QGIS to visualize." ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Either `projection_out` is `None` or `geotransform_out` is `None` (or both are). Retrieving projection and geotransform information by loading `hsio.fname_in` via GDAL. Be sure this is appropriate for the data you are trying to write.\n", "\n" ] } ], "source": [ "fname_tif = os.path.join(data_dir, 'spatial_mod', 'crop_single', 'crop_single.tif')\n", "if not os.path.isdir(os.path.dirname(fname_tif)): # create a new folder named \"crop_single\" if it does not exist\n", " os.mkdir(os.path.dirname(fname_tif))\n", " \n", "io.write_tif(fname_tif, array_crop, metadata=metadata)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Open cropped geotiff image in QGIS to visualize the extent of the cropped image compared to the original datacube and the plot boundaries (the full extent image is darkened and displayed in the background):\n", "![crop_single](../.././docs/source/img/spatial_mod/crop_single_qgis.png)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "***\n", "\n", "## `spatial_mod.load_spyfile`\n", "Loads a ``SpyFile`` (Spectral Python object) for data access and/or manipulation by the ``hstools`` class. [[API]](api/hs_process.spatial_mod.html#hs_process.spatial_mod.load_spyfile)\n", "\n", "Load and initialize the `hsio` and `spatial_mod` modules" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "from hs_process import hsio\n", "from hs_process import spatial_mod\n", "\n", "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", "io = hsio(fname_in)\n", "my_spatial_mod = spatial_mod(io.spyfile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load datacube using `spatial_mod.load_spyfile`" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\tData Source: 'F:\\\\nigo0024\\Documents\\hs_process_demo\\Wells_rep2_20180628_16h56m_pika_gige_7-Radiance Conversion-Georectify Airborne Datacube-Convert Radiance Cube to Reflectance from Measured Reference Spectrum.bip'\n", "\t# Rows: 617\n", "\t# Samples: 1300\n", "\t# Bands: 240\n", "\tInterleave: BIP\n", "\tQuantization: 32 bits\n", "\tData format: float32" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_spatial_mod.load_spyfile(io.spyfile)\n", "my_spatial_mod.spyfile" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 4 }