Exploring 3D Terrain Visualization with Python: A DEM and PyVista Tutorial

EmailTwitterLinkedInFacebookWhatsAppShare

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.

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.

Explanation:

  1. Import necessary libraries:
    • pyvista: A library for 3D plotting and mesh analysis.
    • rioxarray: A library for geospatial raster data handling.
    • numpy: A fundamental package for scientific computing with Python.
  2. Read the data from a DEM file:
    • The riox.open_rasterio function is used to open and read a raster file (Digital Elevation Model – DEM in this case).
    • The [0] indexing extracts the first layer of the raster data.
  3. Save the raster data as an array:
    • Convert the raster data to a NumPy array using np.asarray.
  4. Create a mesh grid:
    • Use np.meshgrid to create a 2D grid of x and y coordinates based on the DEM data.
  5. Set the z values and create a StructuredGrid:
    • Initialize z values as zeros to create a flat surface.
    • Use pv.StructuredGrid to create a structured grid using x, y, and z coordinates.
  6. Assign Elevation Values:
    • Assign the elevation values from the DEM data to the mesh as scalar data.
  7. Warp the mesh by scalar:
    • Use the warp_by_scalar method to displace the mesh points along their normals based on the scalar values (Elevation) multiplied by a factor.
  8. Plot the elevation map:
    • Create a PyVista Plotter object.
    • Add the mesh to the plot, using the warped mesh and the Elevation scalar values for coloring.
    • Display the grid and set the background color.
    • Finally, visualize the plot with the specified camera position (cpos="xy").

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.

We also offer freelancing services. Please email us at contact@spatial-dev.guru for any query.

Leave a ReplyCancel reply

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Spatial Dev Guru

Subscribe now to keep reading and get access to the full archive.

Continue reading