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" Euse 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.
FormatKind | Format String | Positive | Negative |
|---|---|---|---|
Decimal | {} | 48.858222 | -48.858222 |
DmsSigned | {:#} | 48° 51′ 29.600000″ | -48° 51′ 29.600000″ |
DmsLabeled | N/A | 48° 51′ 29.600000″ N | 48° 51′ 29.600000″ S |
DmsBare | N/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 theallocandcorelibraries, orno_std.std(enabled by default) — The default configuration, it uses thestdlibrary and includes theserde,urn, andgeojsonfeatures.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
Coordinatetype,crate::coord!macro, and associated constants. - error
- This module provides the
Errortype. - fmt
- lat
- This module provides the
Latitudetype,crate::lat!macro, and associated constants. - long
- This module provides the
Longitudetype,crate::long!macro, and associated constants. - parse
- This module provides the
Parsedenum andparse_strfunction.