
Introduction: Exploring Digital Elevation Models (DEMs) in 3D with Python
Digital Elevation Models (DEMs) are crucial in geospatial analysis, providing a representation of the Earth’s surface topography. These gridded datasets encode elevation information, making them valuable for various applications, including terrain analysis, hydrology, and environmental modeling. In this tutorial, we will delve into the fascinating realm of DEMs, leveraging the power of Python and 3D visualization to gain insights into geographical landscapes.
Understanding DEMs: A DEM consists of a grid of elevation values, where each cell in the grid corresponds to a specific geographical location, and the cell value represents the elevation at that point. These models play a pivotal role in Geographic Information Systems (GIS) and are used for tasks such as assessing slope, aspect, and watershed delineation.
3D Visualization and GIS: Python, with its versatile libraries, serves as an excellent tool for working with geospatial data. The integration of PyVista, a library for 3D visualization, and rioxarray, a geospatial raster data handler, allows us to seamlessly explore and visualize DEMs in three dimensions. This not only enhances our understanding of the terrain but also aids in effective communication of spatial information.
Tutorial Overview: In this tutorial, we’ll guide you through the process of reading DEM data, creating a mesh grid, assigning elevation values, and visualizing the terrain in 3D. The structured grid representation and the warp_by_scalar method provided by PyVista will be employed to create an engaging and informative 3D plot.
Prerequisites: Before proceeding, ensure you have Python installed along with the required libraries – pyvista, rioxarray, and numpy. Additionally, download a DEM file (in GeoTIFF format, for example) to follow along with the tutorial.
|
1 2 3 4 |
(base) geoknight@pop-os:~$conda create -n spatial-dev.guru python=3.12 (base) geoknight@pop-os:~$conda activate spatial-dev.guru (spatial-dev.guru) geoknight@pop-os:~$conda install -c conda-forge rioxarray (spatial-dev.guru) geoknight@pop-os:~$conda install -c conda-forge pyvista |
To get full source code for this tutorial, click here.
Now, let’s embark on this journey to transform DEM data into a captivating 3D visualization using the power of Python and GIS.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# Import necessary libraries import pyvista as pv import rioxarray as riox import numpy as np # Read the data from a DEM file data = riox.open_rasterio("data/input/dem.tif") data = data[0] # Save the raster data as an array values = np.asarray(data) # Create a mesh grid x, y = np.meshgrid(data['x'], data['y']) # Set the z values and create a StructuredGrid z = np.zeros_like(x) mesh = pv.StructuredGrid(x, y, z) # Assign Elevation Values mesh["Elevation"] = values.ravel(order='F') # Warp the mesh by scalar topo = mesh.warp_by_scalar(scalars="Elevation", factor=0.000015) # Plot the elevation map p = pv.Plotter() p.add_mesh(mesh=topo, scalars=topo["Elevation"], cmap='terrain') p.show_grid(color='black') p.set_background(color='white') p.show(cpos="xy") |
Explanation:
- Import necessary libraries:
- Read the data from a DEM file:
- Save the raster data as an array:
- Create a mesh grid:
- Set the z values and create a StructuredGrid:
- Assign Elevation Values:
- Warp the mesh by scalar:
- Plot the elevation map:
This code essentially reads DEM data, creates a structured grid, assigns elevation values, warps the mesh based on elevation, and visualizes the elevation map in 3D using PyVista.
Now, let’s delve into the concept of a mesh grid. A mesh grid is a technique used in various fields such as mathematics, physics, and computer graphics to represent a set of points in a structured way. It consists of a two-dimensional grid of coordinates where each point in the grid represents a specific position in the data.
In our code, we use the numpy.meshgrid function to create a mesh grid with the x and y coordinates. This function takes two 1D arrays as input and returns two 2D arrays representing the x and y coordinates of the points in the grid. The numpy.meshgrid function is often used in conjunction with other functions to perform tasks such as interpolation, plotting, and data analysis.
In this particular code, we use the mesh grid to represent the elevation data in a structured way. The mesh grid is then used to create a StructuredGrid, which is a type of data structure used by PyVista to represent and manipulate 3D data. Finally, we use the mesh grid to plot the elevation map and visualize the data.
So, in a nutshell, the mesh grid is a powerful tool for representing and analyzing data in a structured way, and it is used in our code to create a 3D visualization of a digital elevation model.
I hope this tutorial will create a good foundation for you. If you want tutorials on another GIS topic or you have any queries, please send an email at contact@spatial-dev.guru.
