#geographic #coordinate #systems #convert #utm #different #mgrs

geoconvert

A lightweight library to convert between geographic coordinate systems

3 stable releases

1.0.2 Jan 23, 2024
1.0.0 Jan 7, 2024

#98 in Geospatial

Download history 20/week @ 2024-07-21 38/week @ 2024-07-28 139/week @ 2024-08-04 135/week @ 2024-08-11 202/week @ 2024-08-18 85/week @ 2024-08-25 141/week @ 2024-09-01 173/week @ 2024-09-08 83/week @ 2024-09-15 136/week @ 2024-09-22 81/week @ 2024-09-29 49/week @ 2024-10-06 61/week @ 2024-10-13 23/week @ 2024-10-20 53/week @ 2024-10-27 45/week @ 2024-11-03

186 downloads per month

MIT license

74KB
1.5K SLoC

geoconvert

Latest Version Docs

geoconvert is a lightweight library for converting between different geographic coordinate systems. Currently, there are three coordinate systems implemented:

  • LatLon
  • UtmUps
  • Mgrs

The implementation of this library is a translation of a subset of GeographicLib from C++ to Rust. Specifically, geoconvert implements some of the functionality of the GeoConvert command line tool.

Usage

You can create coordinates manually using a struct's create() function, then convert to other types using the to_*/from_* methods.

use geoconvert::{LatLon, Mgrs, UtmUps};

// This returns a result. When calling `create()`, the arguments are validated to ensure only a valid
// coordinate gets created.
let coord = LatLon::create(40.748333, -73.985278).unwrap();

// Convert to a UTM/UPS coordinate
let coord_utm = coord.to_utmups();
let coord_utm = UtmUps::from_latlon(&coord);

// Convert to an MGRS coordinate
// Note that for MGRS you must specify the precision
let coord_mgrs = coord.to_mgrs(6);
let coord_mgrs = Mgrs::from_latlon(&coord, 6);

Features

If you want serde compatibility with Serialize/Deserialize, activate the serde feature.

Testing Accuracy

To test the accuracy compared to GeographicLib yourself, you'll need a dataset of lat/lon and MGRS points. I have a gist that contains a sample dataset of ~100K points generated using Python and converted using GeoConvert. If you'd like to generate your own dataset to validate the accuracy, create files named mgrs.txt and latlon.txt, where mgrs.txt is a list of MGRS coordinates (one per line) and latlon.txt is a list of latitude longitude pairs, each pair internally delimited by a space (i.e. like <latitude> <longitude>). You can use GeoConvert to do the conversion, or use a different source for ground truth.

Once you have these files, place them into tests/ and run:

cargo test -- --include-ignored

The test will fail if the geoconvert conversion differs from the ground truth by a distance of 1mm or more (calculated using the haversine formula). It also prints the average distance error.

Dependencies

~0.7–1.3MB
~28K SLoC