geokit/latlng
Opaque LatLng (geographic coordinate) shared by every module
in geokit.
Latitudes are in degrees, in the range [-90, 90], with positive
values north of the equator. Longitudes are in degrees, in the
range [-180, 180], with positive values east of the prime
meridian. Out-of-range inputs to new return a typed
error; use wrap when your source data may be
denormalised (for example, sensor output that crosses the
antimeridian).
Types
Errors returned by new.
pub type LatLngError {
LatOutOfRange(lat: Float)
LngOutOfRange(lng: Float)
}
Constructors
-
LatOutOfRange(lat: Float)Latitude was outside
[-90, 90]. -
LngOutOfRange(lng: Float)Longitude was outside
[-180, 180].
Values
pub fn equal(a a: LatLng, b b: LatLng) -> Bool
Value equality. Two LatLng values are equal iff their lat and
lng components compare equal.
pub fn new(
lat lat: Float,
lng lng: Float,
) -> Result(LatLng, LatLngError)
Build a LatLng from latitude and longitude in degrees.
import geokit/latlng
let assert Ok(tokyo) = latlng.new(lat: 35.6812, lng: 139.7671)
pub fn new_or_panic(lat lat: Float, lng lng: Float) -> LatLng
Build a LatLng from literal coordinates, panicking on
out-of-range input. The companion to new for
compile-time-known coordinates (curated city lists, landmark
fixtures, test data, hand-coded routes) where wrapping every
call site in let assert Ok(...) adds noise without adding
safety. Out-of-range inputs are programmer errors at literal
sites — they cannot be data-driven — so a panic carries the
offending value back through the stack rather than forcing the
caller to thread Result through the constant.
import geokit/latlng
pub fn cities() -> List(#(String, latlng.LatLng)) {
[
#("Tokyo", latlng.new_or_panic(lat: 35.6812, lng: 139.7671)),
#("Osaka", latlng.new_or_panic(lat: 34.6937, lng: 135.5023)),
#("Kyoto", latlng.new_or_panic(lat: 35.0116, lng: 135.7681)),
]
}
For runtime input (user-typed coordinates, parsed-from-file,
network-supplied values) use new and handle the
LatLngError properly.
pub fn wrap(lat lat: Float, lng lng: Float) -> LatLng
Build a LatLng, normalising longitude into [-180, 180] and
clamping latitude into [-90, 90]. Use this when the source data
may be denormalised (sensor output crossing the antimeridian,
great-circle calculations producing 181°, …).
import geokit/latlng
let p = latlng.wrap(lat: 91.0, lng: 181.0)
// p has lat = 90.0, lng = -179.0