// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Positions on the Earth, and the questions a program asks about them. //! //! Is this place inside that area? How far apart are these two, and in //! which direction? What box contains all of these? That is the whole of //! it: no file format, no projection, no allocation and no dependencies. //! [zig-geojson](https://git.jcollie.dev/jeff/zig-geojson) reads and writes //! GeoJSON in terms of `Position`, and anything else that has coordinates can //! use these functions on its own storage, since they take slices. //! //! ```zig //! const geo = @import("geo"); //! //! const des_moines: geo.Position = .{ .longitude = -93.625, .latitude = 41.5868 }; //! const chicago: geo.Position = .{ .longitude = -87.6298, .latitude = 41.8781 }; //! const metres = geo.Ellipsoid.wgs84.distance(des_moines, chicago); // about 498 km //! //! const warned = [_]geo.Position{ ... }; // a closed ring //! if (geo.ring.contains(&warned, des_moines)) { ... } //! ``` //! //! Coordinates are decimal degrees of longitude and latitude, on WGS 84 unless //! the caller says otherwise: that is what GPS reports and what GeoJSON //! (RFC 7946 §4) requires. Three models of the Earth are used, each where it //! is the honest one: //! //! * `ring`, `polygon` and `BoundingBox` treat longitude and latitude as a //! plane. That is how GeoJSON itself defines a polygon's edges -- straight //! lines in coordinate space, RFC 7946 §3.1.1 -- so an area drawn in //! GeoJSON is exactly the area these test against. //! * `Ellipsoid` measures distances and bearings along the geodesics of a //! reference ellipsoid -- WGS 84, or any other the EPSG dataset has -- by //! Karney's algorithm in `Geodesic`, accurate to about 15 nanometres. //! * `sphere` measures them on a sphere, by default one of the Earth's mean //! radius, which is quicker and within 0.56 per cent of the ellipsoid //! everywhere. `Ellipsoid.sphere` makes one from another ellipsoid, or //! from another of its radii. const std = @import("std"); pub const BoundingBox = @import("BoundingBox.zig"); pub const Ellipsoid = @import("Ellipsoid.zig"); pub const Geodesic = @import("Geodesic.zig"); pub const ring = @import("ring.zig"); pub const polygon = @import("polygon.zig"); pub const sphere = @import("sphere.zig"); /// A place: decimal degrees, and optionally a height. /// /// The datum is the caller's: WGS 84 unless something says otherwise, and /// taken as given, since nothing here converts between datums. A sphere of /// the matching `Ellipsoid` measures in the same frame. /// /// The fields are named rather than being an array, because the two /// conventions for their order -- latitude first in speech and in most APIs, /// longitude first in GeoJSON and in mathematics, where it is x -- are each /// the cause of the other's bugs. pub const Position = struct { /// Degrees east of the prime meridian, -180 to 180. longitude: f64, /// Degrees north of the equator, -90 to 90. latitude: f64, /// Metres above the datum's ellipsoid, when known. Nothing here uses it: /// it is carried so that a position read from somewhere can be written /// back there unchanged. altitude: ?f64 = null, /// Whether two positions name the same place: the same longitude and /// latitude, exactly. The altitude is not compared. pub fn eql(a: Position, b: Position) bool { return a.longitude == b.longitude and a.latitude == b.latitude; } /// Whether the coordinates are finite and in range. pub fn isValid(p: Position) bool { return std.math.isFinite(p.longitude) and std.math.isFinite(p.latitude) and p.longitude >= -180 and p.longitude <= 180 and p.latitude >= -90 and p.latitude <= 90; } }; test { std.testing.refAllDecls(@This()); } test "Position.eql ignores altitude" { const a: Position = .{ .longitude = 1, .latitude = 2, .altitude = 3 }; const b: Position = .{ .longitude = 1, .latitude = 2 }; try std.testing.expect(a.eql(b)); try std.testing.expect(!a.eql(.{ .longitude = 2, .latitude = 1 })); } test "Position.isValid" { try std.testing.expect((Position{ .longitude = -180, .latitude = 90 }).isValid()); try std.testing.expect(!(Position{ .longitude = 180.1, .latitude = 0 }).isValid()); try std.testing.expect(!(Position{ .longitude = 0, .latitude = std.math.nan(f64) }).isValid()); }