Geolocation and Maps in Rust: Overview of Libraries and Tools
Rust is a systems programming language that is increasingly used for creating high-load and secure applications, including those working with geodata. In this article, we will review the main libraries of the Rust ecosystem for working with geolocation, coordinates, and maps, and provide practical code examples.
1. The geo Library — The Foundation of Geometric Operations
geo is a fundamental library for working with geometric types: points, lines, polygons. It provides algorithms for calculating distances, areas, intersections, and buffer zones. It is used as a basis for many other crates.
use geo::{Point, LineString, EuclideanDistance};
fn main() { let p1 = Point::new(37.6173, 55.7558); // Moscow let p2 = Point::new(30.3156, 59.9386); // Saint Petersburg
let distance = p1.euclidean_distance(&p2); println!("Euclidean distance: {:.2} degrees", distance);
// Creating a line (route) let line = LineString::from(vec![ (37.6173, 55.7558), (30.3156, 59.9386), ]);
// Line length in degrees let length: f64 = line.euclidean_length(); println!("Line length: {:.2} degrees", length);}2. georust — Ecosystem for Geospatial Data
The group of crates georust includes several complementary libraries:
geo-types— data types (Point, Polygon, MultiPolygon).geo-algorithms— algorithms (buffering, Delaunay triangulation, convex hull).proj— coordinate system transformation (e.g., from WGS84 to Mercator projection).gpx— reading/writing GPX files (GPS track exchange format).
Example of coordinate transformation using proj:
use proj::Proj;
fn main() { // Create a transformer from WGS84 (EPSG:4326) to Web Mercator (EPSG:3857) let from = "EPSG:4326"; let to = "EPSG:3857"; let transform = Proj::new_known_crs(from, to, None).unwrap();
// Moscow coordinates let (x, y) = transform.convert((37.6173, 55.7558)).unwrap(); println!("Web Mercator: x={:.2}, y={:.2}", x, y);}3. navi — Working with OpenStreetMap Maps
navi is a library for downloading and processing cartographic data from OpenStreetMap (OSM). It allows extracting roads, buildings, points of interest (POI), and building routes. It requires a map file in PBF format.
use navi::{Map, NodeId, WayId};
fn main() { // Load a map from a PBF file let map = Map::from_pbf("moscow.osm.pbf").unwrap();
// Get all nodes within a given radius let center = (37.6173, 55.7558); let nodes_in_radius: Vec<NodeId> = map.nodes_within_radius(center, 1000.0).unwrap(); println!("Nodes found: {}", nodes_in_radius.len());
// Extract highways let ways: Vec<WayId> = map.ways().filter(|w| w.is_highway()).collect(); println!("Number of roads: {}", ways.len());}4. Map Visualization: plotters + geo
For displaying maps in Rust, you can use the plotters library together with geo. It allows drawing polygons, lines, and points on a canvas and saving the result to PNG/SVG.
use plotters Also in library
Working with XML in Kotlin: Libraries and Tools for Parsing and Creation
Parsing HTML in C# (.NET): HtmlAgilityPack and AngleSharp Libraries
Data Validation in Swift (iOS/macOS): Tools and Practice
Geolocation and Maps in C# (.NET): Overview of Libraries and Tools
JavaScript (Node.js) QR Code Generator with qrcode and qr-image