In [1]:
%store -r

In In February 2019, massive forest fires erupted in several parts of Bandipur National Park, located in Chamarajanagar district of Karnataka, India..

This workflow focuses on analyzing vegetation cover changes in Chamarajanagar district between 2017 and 2021, with the aim of assessing the impact of the Bandipur forest fire.

In [38]:
# Import libraries
import json
import os
import pathlib
import shutil
from glob import glob

import earthpy.api.appeears as eaapp
import earthpy
import geopandas as gpd
import hvplot.pandas
import hvplot.xarray
import pandas as pd
import rioxarray as rxr
import xarray as xr
import holoviews as hv
In [2]:
data_dir = os.path.join(pathlib.Path.home(), 'bandipur')
# Make the data directory
os.makedirs(data_dir, exist_ok=True)
In [3]:
#To visualize the directory path
data_dir
Out[3]:
'/home/jovyan/bandipur'
In [5]:
from osmnx import features as osm # Search for locations by name
import geopandas as gpd
import hvplot.pandas
In [6]:
# Search for your site
Chamarajanagar_gdf = osm.features_from_address(
    'Bandipur National park, Karnataka',    
    {'name':'Chamarajanagar'}, 
      
    dist=1000)
Chamarajanagar_gdf
Out[6]:
geometry name name:kn name:ml name:ta wikidata wikipedia type admin_level boundary official_name
element id
relation 2019940 POLYGON ((76.40394 11.70685, 76.40352 11.7072,... Chamarajanagar ಚಾಮರಾಜನಗರ ಜಿಲ್ಲೆ ചാമരാജനഗർ ജില്ല சாமராசநகர் மாவட்டம் Q862912 en:Chamarajanagar district boundary 5 administrative Chamarajanagar District
In [46]:
Chamarajanagar_gdf.explore()
Out[46]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [8]:
# Plot the results with web tile images
Chamarajanagar_gdf.hvplot(geo=True,tiles='EsriImagery')
Out[8]:
In [9]:
# Initialize AppeearsDownloader for MODIS NDVI data
ndvi_downloader = eaapp.AppeearsDownloader(
    download_key='Chamarajanagar-ndvi',
    # ea_dir=project.project_dir,
    product='MOD13Q1.061',
    layer='_250m_16_days_NDVI',
    start_date="06-01",
    end_date="10-01",
    recurring=True,
    year_range=[2017,2021],
    polygon=Chamarajanagar_gdf
)
# Download the prepared download -- this can take some time!
ndvi_downloader.download_files(cache=True)
**Final Configuration Loaded:**
{}
Found 'data_home' in environment variables.
No stored credentials found for urs.earthdata.nasa.gov. Please log in.
/opt/conda/lib/python3.11/site-packages/earthpy/api/auth.py:192: UserWarning: Setting credentials not supported for 'netrc' backend.
  warnings.warn(
/opt/conda/lib/python3.11/site-packages/earthpy/api/auth.py:196: UserWarning: Failed to store credentials with 'keyring': No recommended backend was available. Install a recommended 3rd party backend package; or, install the keyrings.alt package if you want to use the non-recommended backends. See https://pypi.org/project/keyring for details.
  warnings.warn(
Credentials stored using 'env' backend.
In [12]:
# Get a sorted list of NDVI tif file paths
#ndvi_paths = sorted(list(project.project_dir.rglob('ndvi-pattern')))
ndvi_paths = sorted(list(ndvi_downloader.download_dir.rglob('*NDVI*.tif')))
len(ndvi_paths)
Out[12]:
45
In [20]:
scale_factor = 10000
d_start = -25
d_end = -18
In [30]:
doy_start = -25
doy_end = -18 
ndvi_Chamarajanagar = []
for ndvi_path in ndvi_paths:
    # Get date from file name
    print(ndvi_path)
    # doy = ndvi_path[d_start:d_end]
    
    date = pd.to_datetime(ndvi_path.name[doy_start:doy_end], format='%Y%j')

    # Open dataset
    da = rxr.open_rasterio(ndvi_path, mask_and_scale=True).squeeze()

    # Add date dimension and clean up metadata
    da = da.assign_coords({'date': date})
    da = da.expand_dims({'date': 1})
    da.name = 'NDVI'

    # Prepare for concatenation
    ndvi_Chamarajanagar.append(da)
    # Open dataset
    # da = rxr.open_rasterio(ndvi_path, masked=True).squeeze()

    # Add date dimension and clean up metadata
    # da = da.assign_coords({'date': date})
    # da = da.expand_dims({'date': 1})
    # da.name = 'NDVI'

    # Multiple by scale factor
    # da = da / scale_factor

    # Prepare for concatenation
    # ndvi_Chamarajanagar.append(da)

len(ndvi_Chamarajanagar)
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017145000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017161000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017177000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017193000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017209000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017225000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017241000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017257000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2017273000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018145000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018161000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018177000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018193000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018209000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018225000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018241000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018257000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2018273000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019145000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019161000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019177000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019193000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019209000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019225000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019241000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019257000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2019273000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020145000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020161000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020177000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020193000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020209000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020225000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020241000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020257000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2020273000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021145000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021161000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021177000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021193000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021209000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021225000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021241000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021257000000_aid0001.tif
/workspaces/data/earthpy-downloads/Chamarajanagar-ndvi/MOD13Q1.061_2017137_to_2021274/MOD13Q1.061__250m_16_days_NDVI_doy2021273000000_aid0001.tif
Out[30]:
45
In [31]:
# Combine NDVI images from all dates
annual_ndvi_da = (
    xr.combine_by_coords(ndvi_Chamarajanagar)
    .resample(dict(date='YS')).mean())
annual_ndvi_da
Out[31]:
<xarray.Dataset> Size: 5MB
Dimensions:      (date: 5, y: 346, x: 662)
Coordinates:
    band         int64 8B 1
  * x            (x) float64 5kB 76.4 76.41 76.41 76.41 ... 77.78 77.78 77.78
  * y            (y) float64 3kB 12.31 12.31 12.31 12.31 ... 11.6 11.6 11.59
    spatial_ref  int64 8B 0
  * date         (date) datetime64[ns] 40B 2017-01-01 2018-01-01 ... 2021-01-01
Data variables:
    NDVI         (date, y, x) float32 5MB 0.4124 0.4299 0.4426 ... 0.5837 0.592
xarray.Dataset
    • date: 5
    • y: 346
    • x: 662
    • band
      ()
      int64
      1
      array(1)
    • x
      (x)
      float64
      76.4 76.41 76.41 ... 77.78 77.78
      array([76.403125, 76.405208, 76.407292, ..., 77.776042, 77.778125, 77.780208],
            shape=(662,))
    • y
      (y)
      float64
      12.31 12.31 12.31 ... 11.6 11.59
      array([12.313542, 12.311458, 12.309375, ..., 11.598958, 11.596875, 11.594792],
            shape=(346,))
    • spatial_ref
      ()
      int64
      0
      crs_wkt :
      GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
      semi_major_axis :
      6378137.0
      semi_minor_axis :
      6356752.314245179
      inverse_flattening :
      298.257223563
      reference_ellipsoid_name :
      WGS 84
      longitude_of_prime_meridian :
      0.0
      prime_meridian_name :
      Greenwich
      geographic_crs_name :
      WGS 84
      horizontal_datum_name :
      World Geodetic System 1984
      grid_mapping_name :
      latitude_longitude
      spatial_ref :
      GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
      GeoTransform :
      76.40208332648884 0.0020833333331466974 0.0 12.314583332230129 0.0 -0.0020833333331466974
      array(0)
    • date
      (date)
      datetime64[ns]
      2017-01-01 ... 2021-01-01
      array(['2017-01-01T00:00:00.000000000', '2018-01-01T00:00:00.000000000',
             '2019-01-01T00:00:00.000000000', '2020-01-01T00:00:00.000000000',
             '2021-01-01T00:00:00.000000000'], dtype='datetime64[ns]')
    • NDVI
      (date, y, x)
      float32
      0.4124 0.4299 ... 0.5837 0.592
      units :
      NDVI
      AREA_OR_POINT :
      Area
      array([[[0.41235554, 0.42986667, 0.44264445, ..., 0.673     ,
               0.65761113, 0.66157776],
              [0.56686664, 0.52195555, 0.51862216, ..., 0.6597111 ,
               0.6400111 , 0.6358888 ],
              [0.57574445, 0.5201333 , 0.49308887, ..., 0.6001111 ,
               0.61099994, 0.64329994],
              ...,
              [0.6152222 , 0.6329778 , 0.6035333 , ..., 0.50308883,
               0.50013334, 0.49924448],
              [0.5681778 , 0.58728886, 0.61029994, ..., 0.4737    ,
               0.48171112, 0.49368885],
              [0.55268884, 0.5867111 , 0.57226664, ..., 0.4831222 ,
               0.49983326, 0.5161444 ]],
      
             [[0.47355556, 0.4699222 , 0.4671333 , ..., 0.5416333 ,
               0.63197774, 0.6269444 ],
              [0.4895    , 0.47540003, 0.49336666, ..., 0.5898334 ,
               0.6433    , 0.6621444 ],
              [0.46027777, 0.42846665, 0.53246665, ..., 0.6262222 ,
               0.6480889 , 0.6550667 ],
      ...
              [0.5070445 , 0.49457783, 0.4565    , ..., 0.60444444,
               0.6244555 , 0.63373333],
              [0.5763555 , 0.5498    , 0.49100003, ..., 0.5519999 ,
               0.58349997, 0.58603334],
              [0.54006666, 0.5656333 , 0.5594111 , ..., 0.57298887,
               0.5736111 , 0.5848222 ]],
      
             [[0.61712223, 0.484     , 0.47065556, ..., 0.63543326,
               0.6234222 , 0.61228883],
              [0.5057778 , 0.44865555, 0.5054444 , ..., 0.6729444 ,
               0.6271778 , 0.58914447],
              [0.5189334 , 0.46078885, 0.5100111 , ..., 0.7213111 ,
               0.62711114, 0.6184    ],
              ...,
              [0.52515554, 0.5312777 , 0.64327776, ..., 0.59146667,
               0.5945    , 0.5980889 ],
              [0.52317774, 0.51764446, 0.61187774, ..., 0.55147773,
               0.5633111 , 0.5584667 ],
              [0.5845444 , 0.5265777 , 0.5124889 , ..., 0.5855444 ,
               0.5837333 , 0.5919778 ]]], shape=(5, 346, 662), dtype=float32)
    • x
      PandasIndex
      PandasIndex(Index([76.40312499315542, 76.40520832648856, 76.40729165982171,
             76.40937499315486,   76.411458326488, 76.41354165982115,
              76.4156249931543, 76.41770832648744, 76.41979165982059,
             76.42187499315374,
             ...
             77.76145832636706, 77.76354165970021, 77.76562499303336,
              77.7677083263665, 77.76979165969965,  77.7718749930328,
             77.77395832636594, 77.77604165969909, 77.77812499303224,
             77.78020832636538],
            dtype='float64', name='x', length=662))
    • y
      PandasIndex
      PandasIndex(Index([12.313541665563555, 12.311458332230409, 12.309374998897262,
             12.307291665564115, 12.305208332230968, 12.303124998897822,
             12.301041665564675, 12.298958332231528, 12.296874998898382,
             12.294791665565235,
             ...
             11.613541665626265, 11.611458332293118, 11.609374998959971,
             11.607291665626825, 11.605208332293678, 11.603124998960531,
             11.601041665627385, 11.598958332294238, 11.596874998961091,
             11.594791665627945],
            dtype='float64', name='y', length=346))
    • date
      PandasIndex
      PandasIndex(DatetimeIndex(['2017-01-01', '2018-01-01', '2019-01-01', '2020-01-01',
                     '2021-01-01'],
                    dtype='datetime64[ns]', name='date', freq='YS-JAN'))
In [33]:
# Combine NDVI images from all dates
da = xr.combine_by_coords(ndvi_Chamarajanagar, coords=['date'])
da
Out[33]:
<xarray.Dataset> Size: 41MB
Dimensions:      (date: 45, y: 346, x: 662)
Coordinates:
    band         int64 8B 1
  * x            (x) float64 5kB 76.4 76.41 76.41 76.41 ... 77.78 77.78 77.78
  * y            (y) float64 3kB 12.31 12.31 12.31 12.31 ... 11.6 11.6 11.59
    spatial_ref  int64 8B 0
  * date         (date) datetime64[ns] 360B 2017-05-25 2017-06-10 ... 2021-09-30
Data variables:
    NDVI         (date, y, x) float32 41MB 0.6256 0.5689 0.5466 ... 0.661 0.661
xarray.Dataset
    • date: 45
    • y: 346
    • x: 662
    • band
      ()
      int64
      1
      array(1)
    • x
      (x)
      float64
      76.4 76.41 76.41 ... 77.78 77.78
      array([76.403125, 76.405208, 76.407292, ..., 77.776042, 77.778125, 77.780208],
            shape=(662,))
    • y
      (y)
      float64
      12.31 12.31 12.31 ... 11.6 11.59
      array([12.313542, 12.311458, 12.309375, ..., 11.598958, 11.596875, 11.594792],
            shape=(346,))
    • spatial_ref
      ()
      int64
      0
      crs_wkt :
      GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
      semi_major_axis :
      6378137.0
      semi_minor_axis :
      6356752.314245179
      inverse_flattening :
      298.257223563
      reference_ellipsoid_name :
      WGS 84
      longitude_of_prime_meridian :
      0.0
      prime_meridian_name :
      Greenwich
      geographic_crs_name :
      WGS 84
      horizontal_datum_name :
      World Geodetic System 1984
      grid_mapping_name :
      latitude_longitude
      spatial_ref :
      GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
      GeoTransform :
      76.40208332648884 0.0020833333331466974 0.0 12.314583332230129 0.0 -0.0020833333331466974
      array(0)
    • date
      (date)
      datetime64[ns]
      2017-05-25 ... 2021-09-30
      array(['2017-05-25T00:00:00.000000000', '2017-06-10T00:00:00.000000000',
             '2017-06-26T00:00:00.000000000', '2017-07-12T00:00:00.000000000',
             '2017-07-28T00:00:00.000000000', '2017-08-13T00:00:00.000000000',
             '2017-08-29T00:00:00.000000000', '2017-09-14T00:00:00.000000000',
             '2017-09-30T00:00:00.000000000', '2018-05-25T00:00:00.000000000',
             '2018-06-10T00:00:00.000000000', '2018-06-26T00:00:00.000000000',
             '2018-07-12T00:00:00.000000000', '2018-07-28T00:00:00.000000000',
             '2018-08-13T00:00:00.000000000', '2018-08-29T00:00:00.000000000',
             '2018-09-14T00:00:00.000000000', '2018-09-30T00:00:00.000000000',
             '2019-05-25T00:00:00.000000000', '2019-06-10T00:00:00.000000000',
             '2019-06-26T00:00:00.000000000', '2019-07-12T00:00:00.000000000',
             '2019-07-28T00:00:00.000000000', '2019-08-13T00:00:00.000000000',
             '2019-08-29T00:00:00.000000000', '2019-09-14T00:00:00.000000000',
             '2019-09-30T00:00:00.000000000', '2020-05-24T00:00:00.000000000',
             '2020-06-09T00:00:00.000000000', '2020-06-25T00:00:00.000000000',
             '2020-07-11T00:00:00.000000000', '2020-07-27T00:00:00.000000000',
             '2020-08-12T00:00:00.000000000', '2020-08-28T00:00:00.000000000',
             '2020-09-13T00:00:00.000000000', '2020-09-29T00:00:00.000000000',
             '2021-05-25T00:00:00.000000000', '2021-06-10T00:00:00.000000000',
             '2021-06-26T00:00:00.000000000', '2021-07-12T00:00:00.000000000',
             '2021-07-28T00:00:00.000000000', '2021-08-13T00:00:00.000000000',
             '2021-08-29T00:00:00.000000000', '2021-09-14T00:00:00.000000000',
             '2021-09-30T00:00:00.000000000'], dtype='datetime64[ns]')
    • NDVI
      (date, y, x)
      float32
      0.6256 0.5689 ... 0.661 0.661
      units :
      NDVI
      AREA_OR_POINT :
      Area
      array([[[0.6256    , 0.5689    , 0.5466    , ..., 0.76129997,
               0.7836    , 0.7815    ],
              [0.5897    , 0.5561    , 0.5561    , ..., 0.7425    ,
               0.7836    , 0.775     ],
              [0.5958    , 0.5958    , 0.5994    , ..., 0.766     ,
               0.7772    , 0.7772    ],
              ...,
              [0.5374    , 0.4502    , 0.4502    , ..., 0.5038    ,
               0.4886    , 0.4886    ],
              [0.60569996, 0.48479998, 0.48479998, ..., 0.4827    ,
               0.4886    , 0.4886    ],
              [0.60569996, 0.5309    , 0.48189998, ..., 0.4827    ,
               0.55979997, 0.55979997]],
      
             [[0.1566    , 0.1566    , 0.159     , ..., 0.8348    ,
               0.84889996, 0.84889996],
              [0.4878    , 0.4878    , 0.48889998, ..., 0.8188    ,
               0.8476    , 0.8476    ],
              [0.5412    , 0.5412    , 0.4833    , ..., 0.7337    ,
               0.7249    , 0.8628    ],
      ...
              [0.5036    , 0.46589997, 0.5749    , ..., 0.6127    ,
               0.6051    , 0.6428    ],
              [0.479     , 0.37939999, 0.567     , ..., 0.5812    ,
               0.56949997, 0.57919997],
              [0.567     , 0.567     , 0.57809997, ..., 0.57409996,
               0.5646    , 0.54679996]],
      
             [[0.62839997, 0.3988    , 0.5714    , ..., 0.174     ,
               0.36319998, 0.36319998],
              [0.3053    , 0.113     , 0.7705    , ..., 0.8598    ,
               0.3049    , 0.3049    ],
              [0.5458    , 0.3053    , 0.7463    , ..., 0.94369996,
               0.3075    , 0.3075    ],
              ...,
              [0.3399    , 0.14209999, 0.6667    , ..., 0.57629997,
               0.6113    , 0.6297    ],
              [0.2859    , 0.32      , 0.6423    , ..., 0.6235    ,
               0.6113    , 0.6113    ],
              [0.6106    , 0.5001    , 0.5001    , ..., 0.6235    ,
               0.66099995, 0.66099995]]], shape=(45, 346, 662), dtype=float32)
    • x
      PandasIndex
      PandasIndex(Index([76.40312499315542, 76.40520832648856, 76.40729165982171,
             76.40937499315486,   76.411458326488, 76.41354165982115,
              76.4156249931543, 76.41770832648744, 76.41979165982059,
             76.42187499315374,
             ...
             77.76145832636706, 77.76354165970021, 77.76562499303336,
              77.7677083263665, 77.76979165969965,  77.7718749930328,
             77.77395832636594, 77.77604165969909, 77.77812499303224,
             77.78020832636538],
            dtype='float64', name='x', length=662))
    • y
      PandasIndex
      PandasIndex(Index([12.313541665563555, 12.311458332230409, 12.309374998897262,
             12.307291665564115, 12.305208332230968, 12.303124998897822,
             12.301041665564675, 12.298958332231528, 12.296874998898382,
             12.294791665565235,
             ...
             11.613541665626265, 11.611458332293118, 11.609374998959971,
             11.607291665626825, 11.605208332293678, 11.603124998960531,
             11.601041665627385, 11.598958332294238, 11.596874998961091,
             11.594791665627945],
            dtype='float64', name='y', length=346))
    • date
      PandasIndex
      PandasIndex(DatetimeIndex(['2017-05-25', '2017-06-10', '2017-06-26', '2017-07-12',
                     '2017-07-28', '2017-08-13', '2017-08-29', '2017-09-14',
                     '2017-09-30', '2018-05-25', '2018-06-10', '2018-06-26',
                     '2018-07-12', '2018-07-28', '2018-08-13', '2018-08-29',
                     '2018-09-14', '2018-09-30', '2019-05-25', '2019-06-10',
                     '2019-06-26', '2019-07-12', '2019-07-28', '2019-08-13',
                     '2019-08-29', '2019-09-14', '2019-09-30', '2020-05-24',
                     '2020-06-09', '2020-06-25', '2020-07-11', '2020-07-27',
                     '2020-08-12', '2020-08-28', '2020-09-13', '2020-09-29',
                     '2021-05-25', '2021-06-10', '2021-06-26', '2021-07-12',
                     '2021-07-28', '2021-08-13', '2021-08-29', '2021-09-14',
                     '2021-09-30'],
                    dtype='datetime64[ns]', name='date', freq=None))
In [34]:
# plot before
pre_ndvi = (
    da
    .sel(date=slice('2017', '2019'))
    .mean(dim='date')
    .NDVI
)
pre_ndvi.plot()
Out[34]:
<matplotlib.collections.QuadMesh at 0x7d1711aeee10>
No description has been provided for this image
In [35]:
# plot after
post_ndvi = (
    da
    .sel(date=slice('2020', '2021'))
    .mean(dim='date')
    .NDVI
)
post_ndvi.plot()
Out[35]:
<matplotlib.collections.QuadMesh at 0x7d171162efd0>
No description has been provided for this image
In [39]:
# Compute the difference in NDVI before and after
ndvi_diff =  post_ndvi - pre_ndvi
# Plot the difference
ndvi_diff_plot = (
    ndvi_diff.hvplot(x='x', y='y', cmap='PiYG', geo=True,
        title='Difference in NDVI 2017-2019 vs. 2020-2021')
    *
    Chamarajanagar_gdf.hvplot(geo=True, fill_color=None, line_color='black')
)
hv.save(ndvi_diff_plot, 'NDVI_Difference_Chamarajanagar.html')
ndvi_diff_plot
WARNING:bokeh.core.validation.check:W-1005 (FIXED_SIZING_MODE): 'fixed' sizing mode requires width and height to be set: figure(id='p1187', ...)
Out[39]:
In [40]:
# Apply NDVI threshold
# Create a composite of NDVI values for each year
composite_da = (
    annual_ndvi_da
    .groupby('date.year')
    .mean(dim=['date'])
)
In [43]:
# Plot difference inside and outside the boundary
annual_veg_da = (
    composite_da
    # Identify vegetated pixels
    .where(composite_da> 0.3)
    # Count pixels in each annual image
    .count(dim=['x', 'y'])
)
annual_veg_da
Out[43]:
<xarray.Dataset> Size: 96B
Dimensions:      (year: 5)
Coordinates:
    band         int64 8B 1
    spatial_ref  int64 8B 0
  * year         (year) int64 40B 2017 2018 2019 2020 2021
Data variables:
    NDVI         (year) int64 40B 227973 227578 227842 228252 227878
xarray.Dataset
    • year: 5
    • band
      ()
      int64
      1
      array(1)
    • spatial_ref
      ()
      int64
      0
      crs_wkt :
      GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
      semi_major_axis :
      6378137.0
      semi_minor_axis :
      6356752.314245179
      inverse_flattening :
      298.257223563
      reference_ellipsoid_name :
      WGS 84
      longitude_of_prime_meridian :
      0.0
      prime_meridian_name :
      Greenwich
      geographic_crs_name :
      WGS 84
      horizontal_datum_name :
      World Geodetic System 1984
      grid_mapping_name :
      latitude_longitude
      spatial_ref :
      GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]
      GeoTransform :
      76.40208332648884 0.0020833333331466974 0.0 12.314583332230129 0.0 -0.0020833333331466974
      array(0)
    • year
      (year)
      int64
      2017 2018 2019 2020 2021
      array([2017, 2018, 2019, 2020, 2021])
    • NDVI
      (year)
      int64
      227973 227578 227842 228252 227878
      array([227973, 227578, 227842, 228252, 227878])
    • year
      PandasIndex
      PandasIndex(Index([2017, 2018, 2019, 2020, 2021], dtype='int64', name='year'))
In [44]:
# Plot number of vegetated pixels over time
plot = (
    annual_veg_da
    .hvplot(
        title=f'Vegetation over time in Chamarajanagar, Karnataka',
        ylabel='Vegetated pixels',
        xlabel='Year')
)
In [45]:
hv.save(plot, 'Vegetation_over_time_in_Chamarajanagar_Karnatak_India.html')
plot
Out[45]:
In [12]:
%store var1 var2
In [ ]:
%%capture
%%bash
#Coverting to HTML
jupyter nbconvert vegetation-92-download-ndvi.ipynb --to html

Finally, be sure to Restart and Run all to make sure your notebook works all the way through!