The world’s leading publication for data science, AI, and ML professionals.

3D Earth Visualisation with UV Texture Mapping in Python

A realistic plot of the Globe using texture mapping with NumPy and PyVista


Introduction

Many Data Science and engineering projects gain from engaging visualisations. Generating dynamic charts and diagrams enables better comprehension and data communication, among other benefits.

One such area where interactive visuals are a huge advantage is trajectory plotting, applicable for topics like the orbital mechanic’s Two-Body problem.

Photo by Juliana Kozoski on Unsplash
Photo by Juliana Kozoski on Unsplash

Application

It is possible to create a dynamic 3D plot using Matplotlib for the two-body problem after successfully applying numerical integration techniques to solve the equations of motion. However, a disadvantage to Matplotlib is that it uses a 2D renderer¹. Therefore, there are other options available to produce better 3D outputs.

The Two-Body Problem in Python

Figure 1 depicts the motion of two bodies due solely to the influence of their mutual gravitational attraction.

Instead of simply animating points masses using Matplotlib, having a planetary body texture would significantly enhance the graphics.

Figure 1 - Two-Body Problem Motion in Inertial Space (Image By Author)
Figure 1 – Two-Body Problem Motion in Inertial Space (Image By Author)

Texture Mapping

Texture mapping transforms realistic patterns or images onto computer models. Take Figure 2 as an example flat Earth map.

UV mapping projects this 2D image to a 3D model’s surface. The process assigns image pixels to surface areas on the model.

Figure 2 - Blue Marble Nasa Texture Map (Source: NASA)
Figure 2 – Blue Marble Nasa Texture Map (Source: NASA)

The Visualisation Toolkit (VTK) is software designed for 3D computer graphics, image processing and data visualisation. PyVista³ is a 3D plotting and mesh-analysis Python library which serves as a streamlined interface for VTK.

It is as trivial to create a sphere in PyVista using Gist 1. Executing the code below gives the result in Figure 3.

Carrying out this process is possible in Matplotlib. However, using PyVista gives an exceedingly more realistic image.

Figure 3 - PyVista Sphere (Image By Author)
Figure 3 – PyVista Sphere (Image By Author)

Relevant equations for spherical UV mapping are on Wikipedia², making the process straightforward to implement. Equations 1 and 2 determine the coordinates for correctly displaying a 2D pattern on a 3D image.

Equations 1 and 2 - UV Coordinates (Image By Author)
Equations 1 and 2 – UV Coordinates (Image By Author)

dₓ, dᵧ, and dᶻ are the cartesian positions from the centre of the 3D spherical model to a point on its surface.


Results

Figure 4 is a resulting 3D texture-mapped globe animation made with PyVista and the blue marble NASA jpeg.

The gif frame rate is relatively high, and the file size is compressed significantly to meet Medium’s 25 Megabyte image upload limit. Thus, the actual result is better on the desktop, which appears as a standard interactive Python window.

Figure 4 - Simulation Result for Texture Mapped Earth (Image By Author)
Figure 4 – Simulation Result for Texture Mapped Earth (Image By Author)

Conclusion

PyVista offers functions to update the mesh values and redraw the figure. Thus, following the 3D realism modelling results, the next steps are to incorporate this into the code for the orbital mechanics’ simulations and attempt to re-generate the Figure 1 gif.

Explore the PyVista documentation for other unique examples of what is possible with geographic data.


Check out my other articles if you are interested in Python, engineering, and data Science.

Join Medium with my referral link – Andrew Joseph Davies


References

[1] Python Map Image to 3D Sphere – StackOverflow [2] UV Mapping – Wikipedia [3] Why PyVista? – PyVista Documentation


Related Articles