Skip to main content

Crate lat_long

Crate lat_long 

Source
Expand description

Geographic latitude and longitude coordinate types.

This crate provides strongly-typed Latitude, Longitude, and Coordinate values that are validated on construction and carry their own display logic (decimal degrees or degrees–minutes–seconds). The goal is not to provide a single, large, and potentially unwieldy “geo” crate, but rather a collection of small, focused crates that can be used together or independently.

§Quick start

use lat_long::{Angle, Coordinate, Latitude, Longitude};

let lat = Latitude::new(48, 51, 29.6).expect("valid latitude");
let lon = Longitude::new(2, 21, 7.6).expect("valid longitude");
let paris = Coordinate::new(lat, lon);

// Decimal-degree display (default)
println!("{paris}");   // => 48.858222, 2.218778
// Degrees–minutes–seconds display (alternate flag)
println!("{paris:#}"); // => 48° 51' 29.6" N, 2° 21' 7.6" E
use lat_long::{parse::{self, Parsed}, Coordinate};

if let Ok(Parsed::Coordinate(london)) = parse::parse_str("51.522, -0.127") {
    println!("{london}"); // => 51.522, -0.127
}
// Convert to URL, requires `url` feature flag
let url = url::Url::from(paris);
println!("{url}"); // => geo:48.858222,2.218778
// Convert to JSON, requires `geojson` feature flag
let json = serde_json::Value::from(paris);
println!("{json}"); // => { "type": "Point", "coordinates": [48.858222,2.218778] }

§Formatting

The fmt module provides functionality for formatting and parsing coordinates.

FormatKindFormat StringPositiveNegative
Decimal{}48.858222-48.858222
DmsSigned{:#}48° 51′ 29.600000″-48° 51′ 29.600000″
DmsLabeledN/A48° 51′ 29.600000″ N48° 51′ 29.600000″ S
DmsBareN/A+048:51:29.600000-048:51:29.600000

Note that the DmsBare format is intended as a regular, easy-to-parse format for use in data files, rather than as a human-readable format. In it`s coordinate pair form, it is also the only format that does not allow whitespace around the comma separator.

§Parsing

The parse module provides functionality for parsing coordinates. The parser accepts all of the formats described above. The parser is also used by the implementation of FromStr for Latitude, Longitude, and Coordinate.

§Feature flags

The following features…

  • alloc (enabled by default) — This configuration only uses the alloc and core libraries, or no_std.
  • std (enabled by default) — The default configuration, it uses the std library and includes the serde, urn, and geojson features.
  • serde — Serde support provides decoding of lat/long (and Coord) values as strings or as decimal values.
  • geojson — GeoJSON support provides encoding of Coord values as GeoJSON coordinates.

§References

Re-exports§

pub use coord::Coordinate;
pub use error::Error;
pub use lat::Latitude;
pub use long::Longitude;

Modules§

coord
This module provides the Coordinate type, crate::coord! macro, and associated constants.
error
This module provides the Error type.
fmt
lat
This module provides the Latitude type, crate::lat! macro, and associated constants.
long
This module provides the Longitude type, crate::long! macro, and associated constants.
parse
This module provides the Parsed enum and parse_str function.

Macros§

coord
lat
long

Traits§

Angle