Sometimes it is necessary to display large amounts of data on an interactive map while keeping it usable and responsive. Online interactive maps are implemented in HTML and adding many visual elements to the map display generally degrades performance and usability. A possible alternative is to draw all elements offline and display them on the map as a transparent layer using tiles. Each square tile overlaps the map tiles perfectly, and the interactive map control handles far fewer visual elements.
I tackled this problem a few years ago by writing a custom map tile generator using Python and data from Vehicle energy data set. This project illustrated how to display massive amounts of information on an interactive online map by using custom tile layers over the map. The process involves using a web application that generates, caches, and serves the tiles.
As you know, Python is not fast, so performance suffers significantly while the web application generates each tile. When a tile is cached, the publishing process is fast and is not noticeable when interacting with the map.
Still, I was not satisfied with the performance, so I wanted to solve the problem again by drastically improving the speed of code execution. At first, I thought of converting the code base to I quotebut then my attention was diverted to another candidate.
The Rust programming language has been on my radar for quite some time. With experience in C, C++, and C#, I was intrigued by the language's promise of memory safety and C-like performance. I finally decided to give it a try, and this problem seemed like a perfect starting point to learn and exercise the language.
After reviewing many YouTube videos and various written material, I started using Rust to address this problem. I had three main questions: How difficult is it to create a web application, access SQLite data and programmatically create a transparent PNG image? Fortunately, the answers to these questions were simpler than anticipated.
Rocket
To answer the question about the web application, I turned to Rocket. He Starting Rocket's online documentation page shows how easy it is to set up a basic web application. We'll probably need more complexity to build our tile server, but the boilerplate looks minimal and simple. And it turned out that Rocket is very easy to use and adapt. It's a keeper for me.
sqlx
After a few minutes online, I quickly realized that the most popular answer for accessing SQLite databases was through the sqlx package. It presents a different paradigm than the one I used in Python but much closer to the one I used in my previous life when developing in C#. Instead of generic data structures or pandas Data frames, here you should use strongly typed data structures. Although they are a little more work to work with, they will bring an extra layer of sanity to your life.
Figure 1 Below is the first complete code example that I used to retrieve the data from the level range table.
PNG
Create, draw and save PNG files using the image The checkout is easy. The code to create a transparent tile is quite simple:
I also used the color graduation package to handle color gradient of tiles.
Before analyzing the code in detail, let's review the principle behind drawing traffic density tiles.
Map tiles typically consist of 256×256 square bitmaps. We can approach each mosaic by combining x and and coordinates, a “zoom” level or a quadruple code. Each zoom level corresponds to a square mosaic of tiles of different dimensions. The entire Earth is represented in a single mosaic on the top level. When you zoom, the original tile is divided into four tiles. The next Figures 2 and 3 illustrate the approach process.
If we continue zooming in, and after eight iterations, each resulting tile corresponds to one pixel of the first tile. This observation is the information that allows us to calculate and display traffic density information on the tiles.
As described in the previous article, the mosaic information is prepared and stored in a database. See that article for instructions on how to generate the density database from the Vehicle energy data set.
Now we can analyze the Rust server code for generating, caching and serving tiles. The present solution closely follows the previous tile server design. Figure 5 Below is the main entry point that decides whether to provide a painted tile or a default transparent tile after parsing and accepting the query parameters.
As you can see, the server responds only to zoom levels ranging from one to eighteen. This limitation was included in the data generation process for the density database.
The web application draws each tile using the function listed in Figure 6 below.
As you can see from the list above, the tile painting process has three steps. First, on line 12, we collect the per-pixel density information of the mosaic. Next, we retrieve the mosaic level range, that is, the minimum and maximum density levels for the “zoom” level of the mosaic. Finally, on line 14, we paint the mosaic bitmap. The function ends by saving the mosaic bitmap to the file cache.
After correctly setting the database file path, start the tile server by opening a terminal window, changing to the Rust project directory, and running the following command:
cargo run --release
You can then open the map client and set the URI for the density tile layer. Figure 8 Below is the Jupyter Notebook code cell to load the interactive map:
And that is! Figure 9 Below is the result.
My first foray into Rust wasn't as difficult as I expected. I started by diving into the available literature and YouTube videos before giving it a try. Next, I made sure to have the help of an excellent IDE from JetBrains: RustRover. Although it is still in preview mode, I found this IDE useful and instructive when using Rust. Still, you'll also be perfectly fine if you prefer. visual studio code. Just make sure you get authorized plugins.
used to grammatically revised the writing and accepted several of their suggestions for rewriting.
ai/” rel=”noopener ugc nofollow” target=”_blank”>JetBrains ai The wizard wrote some of the code and I also used it to learn Rust. It has become a staple of my daily work with both Rust and Python.
The Extended Vehicle Energy Dataset is licensed under the Apache 2.0 license, as is its creator, the Vehicle energy data set.
Vehicle energy data set (GitHub)