Figure 1. 3D Terrain Modelling in Python

3D Terrain Modelling Technical Approach

Choosing Python Graphic Library

Figure 2. 3D terrain modelling with Matplotlib surface plot

Populate Interpolation Points

Figure 3. Measurement and interpolation points

Interpolation Method

The surface function from Plotly library is used to construct a 3D terrain model, . Defined an interval distance and populate interpolation points that cover the whole area. Estimate the height of unknown/interpolation points using IDW spatial interpolation method.

3D Terrain Modelling: Python Part

3D modelling is a nice way to view an object in order to get a more vivid visualization with more intense feeling as if we can touch it. Also in viewing topographic surface or terrain, 3D modelling gives more detail surface features in every angle of a region compare with 2D visualization. If we have a set of height point from topographic survey, we will need some processing steps to get a contour map and view the result in 3D. It can be done using some proprietary software like Surfer, ArcGIS ArcScene, Autodesk AutoCAD Map or even using Open Source Software like QGIS 3.x. In this post, I won't discuss any of those. Moreover I will show you how to create a 3D terrain model with a set of height point from topographic or field measurement in Python. So at the end of this tutorial we will get a 3D terrain surface like figure 1 below.Before writing this post, I had done some experiments with different technical approaches and results. It took some times till I got a good one and decided to share the result with you. So this post shows the final result. But before discussing how to do it step by step, let me explain briefly the story "behind the scene" until I come to this point.Matplotlib is the first graphic library I know when started learning Python. I use it frequently to plot any graph I need. So firstly I used Matplotlib to model a terrain surface in 3D. I found there are some functions to plot 3D model in Matplotlib such as wireframe, surface and trisurf. Figure 2 shows 3D terrain from Matplotlib's surface function.The first output from Matplotlib made me so happy, now I can make a terrain 3D model in Python. But later on I found the output with Matplotlib was getting slower as number of point increasing. Previously with a fewer points I can freely rotate it without any delay, but the terrain rendering became slower and needed longer time to compute when the surface is getting more complex with thousand points. It's hard to forget the first love, but really sorry I need to find something else.Finally after some adventures, I found Plotly library is a good one to model the terrain in 3D. It renders faster, smoother and it can produce a html output with some tools that enabled us to zoom in, zoom out, pan, rotate, export to static image and so forth.Might be you have a question. Why are there more points are required in creating 3D model and even getting thousands? In fact the point from field measurement many fewer that that? Actually when doing topographic measurement, what someone do is taking a number of sampling point appropriately in the surveying area. Then we populate more points that cover the whole region and estimate the height based on the measurement points, so we can model the 3D surface for the area that close to the actual condition.Let's take an example to make it more concrete and easy to understand. One day, we did a topographic survey of a region with area 100 x 100 m. From the survey, we took 50 measurement points (blue dots). Then we do interpolation every 5 m for both X and Y axis, so we have 20 points for X and 20 points for Y. In total, the number of interpolation points will be 400 points (red dots) as shown in figure 3. What about 1 m interval? how many points will it be? simply 100 x 100=10000 points.Next question could be, how we estimate the height of interpolation points? There are many spatial interpolation methods available such as linear interpolation, Inverse Distance Weight (IDW) and Kriging. I chose IDW because we can estimate an unknown point at any location and it's rather more simple to implement than Kriging method. On the other hand, the linear interpolation can only interpolate at any location between two known points along a straight line that connected the two known points. It can be used for creating contour line but not suitable for 3D topographic surface modelling. Please refer to my post about creating contour lines and IDW spatial interpolation for further explanation.From the explanation above, I'm sure you can grasp the main idea how to model a 3D terrain in Python. But let me write it down for clarity.