<jupyter_start><jupyter_text>ex02-Visualize Elevation Contours from DEM dataA digital elevation model ([DEM](https://en.wikipedia.org/wiki/Digital_elevation_model)) is a 3D CG representation of a terrain's surface – commonly of a planet (e.g. Earth), moon, or asteroid – created from a terrain's elevation data. A "global DEM" refers to a discrete global grid.DEMs are used often in geographic information systems, and are the most common basis for digitally produced relief maps. While a digital surface model (DSM) may be useful for landscape modeling, city modeling and visualization applications, a digital terrain model (DTM) is often required for flood or drainage modeling, land-use studies, geological applications, and other applications, and in planetary science.There is no universal usage of the terms digital elevation model (DEM), digital terrain model (DTM) and digital surface model (DSM) in scientific literature. In most cases the term digital surface model represents the earth's surface and includes all objects on it. In contrast to a DSM, the digital terrain model (DTM) represents the bare ground surface without any objects like plants and buildings. DEM is often used as a generic term for DSMs and DTMs,only representing height information without any further definition about the surface. See more from https://en.wikipedia.org/wiki/Digital_elevation_model.A DEM stores data in the format of a 2D array where each cell has a corresponding elevation value. Therefore, it may sometimes prove useful to visualize these elevation models with mechanisms such as contour lines. Fortunately, there are quite a few tools written in Python available to deal with DEMs, such as GDAL, Rasterio, numpy, xarray, etc. As [xarray](http://xarray.pydata.org/en/stable/) is always my favorite tool, this notebook will still apply it. However, it is worth noting that xarray uses the [rasterio](https://rasterio.readthedocs.io/en/stable/)library as the backend engine to read DEM data. :) using xarray+rasterio will save quite a few lines of source code.<jupyter_code>%matplotlib inline

import numpy as np
import xarray as xr
import cmaps as nclcmaps
import matplotlib.pyplot as plt

import warnings
warnings.filterwarnings("ignore")<jupyter_output><empty_output><jupyter_text>Read dem dataData from USGS hydrosheds project: https://hydrosheds.cr.usgs.gov/datadownload.php<jupyter_code>infile = "data/n30w100_con"
da_dem = xr.open_rasterio(infile).drop('band')[0].rename({'x':'longitude', 'y':'latitude'})
da_dem = da_dem.where(da_dem>=0, np.nan)<jupyter_output><empty_output><jupyter_text>***Calucate mimimum and maximum values***Should be 0 and 745.<jupyter_code>print(da_dem.min(), da_dem.max())<jupyter_output><xarray.DataArray ()>
array(0.) <xarray.DataArray ()>
array(745.)<jupyter_text>Visualizexarray provides plotting functionality. However, it is a thin wrapper around the popular matplotlib library. Matplotlib syntax and function names were copied as much as possible, which makes for an easy transition between the two. Matplotlib must be installed before xarray can plot.<jupyter_code>fig, ax = plt.subplots(figsize=(15, 7))
da_dem.plot.contour(ax=ax,
                    levels = np.arange(0, 750, 100),
                    cmap=nclcmaps.MPL_viridis_r,
                    add_colorbar=True
                   )

_ = ax.set_title('Elevation Contours of USGS DEM at n30w100 (unit: m)', fontsize=16)<jupyter_output><empty_output><jupyter_text>From these coutours, we can clearly identify the ***river networks*** in the lower areas. In addition, it is quite easy to visualize the DEM with a full color map. Just need change a little bit of the above code as follows.<jupyter_code>fig, ax = plt.subplots(figsize=(15, 7))
da_dem.plot(ax=ax,
            levels = np.arange(0, 750, 100),
            cmap=nclcmaps.MPL_viridis_r,
            add_colorbar=True
           )

_ = ax.set_title('Elevation Contours of USGS DEM at n30w100  (unit: m)', fontsize=16)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08112830753869196, 'fraction_numerical': 0.013230154767848227, 'mean_word_length': 4.790462427745664, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '538486', 'license': 'MIT', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1127, 'n_words': 460, 'path': 'ex02-Visualize Elevation Contours from DEM.ipynb', 'repo_name': 'royalosyin/Work-with-DEM-data-using-Python-from-Simple-to-Complicated', 'size': '4006'}