gNATSGO

[1]:
import os
from pcxarray import pc_query, prepare_data
from pcxarray.utils import load_census_shapefile
from distributed import Client
import matplotlib.pyplot as plt
[2]:
states_gdf = load_census_shapefile(level='state', verify=False)
ms_gdf = states_gdf[states_gdf['NAME'] == 'Mississippi']
ms_gdf = ms_gdf.to_crs(epsg=3814)
geom = ms_gdf.geometry.union_all()
[3]:
items_gdf = pc_query(
    collections='gnatsgo-rasters',
    geometry=geom,
    crs=ms_gdf.crs,
)
print(f"Found {len(items_gdf)} items in the GNATSGO collection.")

fig, ax = plt.subplots(figsize=(5, 10))
ms_gdf.plot(ax=ax, color='lightgrey', edgecolor='black')
items_gdf.plot(ax=ax, alpha=0.2)
plt.show()
Found 10 items in the GNATSGO collection.
../_images/examples_gnatsgo_3_1.png
[4]:
client = Client(processes=True)
print(f"Dask client dashboard: {client.dashboard_link}")
Dask client dashboard: http://127.0.0.1:8787/status
[5]:
nccpi_da = prepare_data(
    items_gdf,
    geometry=geom,
    crs=ms_gdf.crs,
    bands=['nccpi3corn', 'nccpi3cot', 'nccpi3soy'],
    target_resolution=100, # 100 m resolution
    resampling_method='bilinear',
    chunks='auto',
    max_workers=-1,
    enable_progress_bar=True,
)
display(nccpi_da)
nccpi_da = nccpi_da.persist()
Constructing dask computation graph: 100%|██████████| 10/10 [00:00<00:00, 94.69tiles/s]
<xarray.DataArray 'reproject-1bd0dd4e259f40db9715e6cbf77d5b54' (band: 3,
                                                                y: 5393, x: 3325)> Size: 215MB
dask.array<where, shape=(3, 5393, 3325), dtype=float32, chunksize=(1, 5120, 1685), chunktype=numpy.ndarray>
Coordinates:
  * band         (band) <U10 120B 'nccpi3corn' 'nccpi3cot' 'nccpi3soy'
  * y            (y) float64 43kB 1.578e+06 1.578e+06 ... 1.039e+06 1.039e+06
  * x            (x) float64 27kB 3.186e+05 3.186e+05 ... 6.508e+05 6.51e+05
    spatial_ref  int32 4B 3814
Attributes:
    AREA_OR_POINT:  Area
    scale_factor:   1.0
    add_offset:     0.0
    long_name:      National Commodity Crop Productivity Index for Corn (weig...
    _FillValue:     nan
/home/dh2306/pcxarray/.venv/lib/python3.12/site-packages/distributed/client.py:3363: UserWarning: Sending large graph of size 17.22 MiB.
This may cause some slowdown.
Consider loading the data with Dask directly
 or using futures or delayed objects to embed the data into the graph without repetition.
See also https://docs.dask.org/en/stable/best-practices.html#load-data-with-dask for more information.
  warnings.warn(
[6]:
fig, ax = plt.subplots(1, 3, figsize=(15, 10), sharex=True, sharey=True)

nccpi_da.sel(band='nccpi3corn').plot.imshow(ax=ax[0], cmap='cividis', vmin=0, vmax=1, add_colorbar=False)
nccpi_da.sel(band='nccpi3cot').plot.imshow(ax=ax[1], cmap='cividis', vmin=0, vmax=1, add_colorbar=False)
nccpi_da.sel(band='nccpi3soy').plot.imshow(ax=ax[2], cmap='cividis', vmin=0, vmax=1, add_colorbar=False)

for a in ax:
    a.set_aspect('equal')
    a.set_xticks([])
    a.set_yticks([])
    a.set_xlabel('')
    a.set_ylabel('')

    # pad the axes to avoid overlap
    a.set_xlim(a.get_xlim()[0] - 5000, a.get_xlim()[1] + 5000)
    a.set_ylim(a.get_ylim()[0] - 5000, a.get_ylim()[1] + 5000)

ax[0].set_title('gNATSGO NCCPI Corn')

ax[1].set_title('gNATSGO NCCPI Cotton')

ax[2].set_title('gNATSGO NCCPI Soybean')

fig.colorbar(ax[0].images[0], ax=ax, orientation='horizontal', fraction=0.02, pad=0.02, label='National Commodity Crop Productivity Index')
plt.show()
../_images/examples_gnatsgo_6_0.png