geodesic

    Geodesics on an ellipsoid in MoonBit (distance, direct and inverse problems, areas), a port of geographiclib-geodesic 2.2.0

    geodesic
    geographiclib
    ellipsoid
    distance
    gis
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0 AND MIT
    Last updated
    5 hours ago
    Downloads
    3

    #geodesic

    Geodesics on an ellipsoid in MoonBit: a port of geographiclib-geodesic 2.2.0, Charles Karney's algorithms (accurate to round-off, for any pair of points). Distances and azimuths, points along a line, and the perimeter and area of polygons. Pure MoonBit on every backend (wasm, wasm-gc, js, native); it depends only on t-ujiie-g/trig.

    Part of mbt-map, a WebGIS library, published on its own.

    moon add t-ujiie-g/geodesic

    /// The distance in meters from Tokyo to Paris, and the point a quarter of the way.
    fn tokyo_paris() -> (Double, (Double, Double)) {
    let g = @geodesic.Geodesic::wgs84()
    let line = g.inverse_line(35.6812, 139.7671, 48.8566, 2.3522)
    let p = line.position(false, line.s13 / 4.0)
    (line.s13, (p.lat2, p.lon2))
    }

    ///|
    /// The area in square meters of a ring given as lng, lat pairs.
    fn ring_area(coords : Array[Double]) -> Double {
    @geodesic.Geodesic::wgs84().measure_path(coords[:], true)[1]
    }

    #What it gives

    • Geodesic::wgs84(), or Geodesic::new(a, f) for another ellipsoid (equatorial radius in meters, flattening; prolate ones too). None unless both semi-axes are finite and positive.
    • inverse(lat1, lon1, lat2, lon2): the distance s12 (meters), the azimuths azi1 and azi2 (degrees) and the arc a12; with area=true also S12, the area between the geodesic and the equator.
    • direct(lat1, lon1, azi1, s12): the point s12 meters away.
    • line(lat1, lon1, azi1) and inverse_line(lat1, lon1, lat2, lon2): a GeodesicLine, whose position(arcmode, s12_a12, unroll?) gives points by distance or by arc; unroll lets the longitude run past ±180° continuously.
    • polygon(polyline?): a PolygonArea; add_point(lat, lon), then compute(reverse?, sign?) gives the perimeter and the area.
    • Three helpers over arrays of lng, lat pairs (GeoJSON's order): measure_path(coords, closed) (length, a ring's area, the distance to each vertex), densify(coords, step, max_points) (points at most step meters apart along each edge) and circle(lng, lat, radius, n).
    • trig: wgs84 and new take a Trig (default Trig::moonbit()). Pass a wasm host's Math to get the JavaScript library's results bit for bit.

    The functions of the library take latitude first, as GeographicLib does; the three helpers take longitude first.

    Not ported: the library's output masks (every quantity is computed, except S12 on request), GenDirect's reduced length and geodesic scales in the results, and the DMS utilities.

    #Matching geographiclib-geodesic

    The repository compares the wasm-gc build, with the host's Math, against geographiclib-geodesic 2.2.0 on inverse and direct problems, polygons and densified paths (geodesic:golden): identical.

    #License

    Apache-2.0 for the port; geographiclib-geodesic is MIT (see NOTICE).

    Trig

    using @t-ujiie-g/trig { type Trig }

    The trigonometry geodesics use (@trig.Trig): a wasm build passes the host's Math to match the JavaScript library bit for bit.

    Geodesic

    pub struct Geodesic {
    a : Double
    f : Double
    // private fields
    }

    An ellipsoid of revolution with geodesic problems solved on it: a is the equatorial radius in meters and f the flattening.

    Geodesic::circle

    fn Geodesic::circle(self : Geodesic, lng : Double, lat : Double, radius : Double, n : Int) -> FixedArray[Double]

    A circle of radius meters around (lng, lat): n points at equal azimuths clockwise from north, then the first again, as lng, lat pairs.

    Geodesic::densify

    fn Geodesic::densify(self : Geodesic, coords : ArrayView[Double], step : Double, max_points : Int) -> Array[Double]?

    coords (lng, lat pairs) with points at most step meters apart along each geodesic edge, longitudes running on across ±180; None past max_points points or for a step that is not positive.

    Geodesic::direct

    fn Geodesic::direct(self : Geodesic, lat1 : Double, lon1 : Double, azi1 : Double, s12 : Double) -> Position

    Solves the direct problem: the point s12 meters from (lat1, lon1) at azimuth azi1 (degrees).

    Geodesic::inverse

    fn Geodesic::inverse(self : Geodesic, lat1 : Double, lon1 : Double, lat2 : Double, lon2 : Double, area? : Bool) -> Inverse

    Solves the inverse problem between two points (degrees); area asks for S12 too.

    Geodesic::inverse_line

    fn Geodesic::inverse_line(self : Geodesic, lat1 : Double, lon1 : Double, lat2 : Double, lon2 : Double) -> GeodesicLine

    The geodesic from point 1 to point 2 (degrees), which is point 3 of the line (s13, a13).

    Geodesic::line

    fn Geodesic::line(self : Geodesic, lat1 : Double, lon1 : Double, azi1 : Double) -> GeodesicLine

    The geodesic leaving (lat1, lon1) at azimuth azi1 (degrees).

    Geodesic::measure_path

    fn Geodesic::measure_path(self : Geodesic, coords : ArrayView[Double], closed : Bool) -> FixedArray[Double]

    The path through coords (lng, lat pairs): its length (a ring's includes the closing edge), the ring's area (counter-clockwise positive, 0 below three vertices, NaN unless closed), then the distance to each vertex; meters.

    Geodesic::new

    fn Geodesic::new(a : Double, f : Double, trig? :
    Trig
    ) -> Geodesic?

    The ellipsoid with equatorial radius a (meters) and flattening f; None unless both semi-axes are finite and positive.

    Geodesic::polygon

    fn Geodesic::polygon(self : Geodesic, polyline? : Bool) -> PolygonArea

    An empty polygon (or polyline) on self.

    Geodesic::wgs84

    The WGS 84 ellipsoid.

    GeodesicLine

    pub struct GeodesicLine {
    lat1 : Double
    lon1 : Double
    azi1 : Double
    s13 : Double
    a13 : Double
    // private fields
    }

    A geodesic from point 1 (lat1, lon1, azimuth azi1, degrees), whose points are found by distance or arc length; s13 and a13 are the distance and arc to point 3 when the line was made from two points.

    GeodesicLine::position

    fn GeodesicLine::position(self : GeodesicLine, arcmode : Bool, s12_a12 : Double, unroll? : Bool) -> Position

    GenPosition: the point s12_a12 meters (or degrees of arc, arcmode) along the line; unroll lets the longitude run past ±180 continuously.

    Inverse

    pub(all) struct Inverse {
    s12 : Double
    azi1 : Double
    azi2 : Double
    a12 : Double
    area : Double
    }

    An inverse problem's solution: s12 meters, azi1 and azi2 degrees at each end, a12 the arc in degrees, area (S12) square meters between the geodesic and the equator (NaN unless asked for).

    PolygonArea

    pub struct PolygonArea {
    polyline : Bool
    // private fields
    }

    The perimeter (meters) and area (square meters) of a geodesic polygon, or the length of a polyline, built a vertex at a time.

    PolygonArea::add_point

    fn PolygonArea::add_point(self : PolygonArea, lat : Double, lon : Double) -> Unit

    Adds the vertex (lat, lon) in degrees, and the edge to it from the previous one.

    PolygonArea::compute

    fn PolygonArea::compute(self : PolygonArea, reverse? : Bool, sign? : Bool) -> (Double, Double)

    The polygon's perimeter (closed) or the polyline's length, and the area (NaN for a polyline). Counter-clockwise is positive unless reverse; with sign the other way is negative instead of the rest of the earth's area.

    PolygonArea::count

    fn PolygonArea::count(self : PolygonArea) -> Int

    The number of vertices added.

    Position

    pub(all) struct Position {
    lat2 : Double
    lon2 : Double
    azi2 : Double
    s12 : Double
    a12 : Double
    }

    A point on a geodesic line: lat2, lon2, azi2 in degrees, s12 the distance from point 1 in meters and a12 the arc in degrees.

    WGS84_A

    let WGS84_A : Double

    WGS 84's equatorial radius in meters.

    WGS84_F

    let WGS84_F : Double

    WGS 84's flattening.