proj

    Map projections and datum shifts in MoonBit, a port of proj4js 2.22.0

    proj
    projection
    crs
    epsg
    gis
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0 AND MIT
    Last updated
    5 hours ago
    Downloads
    3

    #proj

    Map projections and datum shifts in MoonBit: a port of proj4js 2.22.0's transform pipeline. 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/proj

    /// Tokyo Station in Japan's plane rectangular zone IX (EPSG:6677): about (-5995.19, -35367.23).
    fn station_in_zone_9() -> (Double, Double)? {
    guard @proj.Crs::from_epsg(4326) is Some(wgs84) else { return None }
    guard @proj.Crs::from_epsg(6677) is Some(plane) else { return None }
    @proj.transform(wgs84, plane, 139.7671, 35.6812)
    }

    ///|
    /// Lambert 93 (`+proj=lcc +lat_0=46.5 +lon_0=3 +lat_1=49 +lat_2=44 +x_0=700000 +y_0=6600000
    /// +ellps=GRS80 +towgs84=0,0,0`), built by hand: angles in radians.
    fn lambert93() -> @proj.Crs? {
    let rad = 0.017453292519943295
    let d = @proj.Definition::new(Lcc, 6378137.0, 6356752.314140356)
    d.lat0 = 46.5 * rad
    d.long0 = 3.0 * rad
    d.lat1 = 49.0 * rad
    d.lat2 = 44.0 * rad
    d.x0 = 700000.0
    d.y0 = 6600000.0
    d.towgs84 = [0.0, 0.0, 0.0]
    @proj.Crs::new(d)
    }

    #What it gives

    • transform(from, to, x, y): the point in to's coordinates, shifting datums through WGS 84 where both are known; None where a projection cannot reach the point or the input is not finite. Geographic coordinates are longitude, latitude in degrees; projected ones easting, northing.
    • Crs::from_epsg(code): the codes the module lists itself: WGS 84 (4326) and Web Mercator (3857), WGS 84 UTM zones, and Japan's datums (Tokyo, JGD2000, JGD2011) with their plane rectangular and UTM zones. None for any other code.
    • Crs::new(definition): any other CRS, from a Definition: what a PROJ string says, already parsed. Definition::new(method, a, b) starts one, and the fields are the PROJ parameters (lat0, long0, lat1, lat2, lat_ts, k0, x0, y0, towgs84 with 3 or 7 parameters, to_meter, zone, south, …), angles in radians, NaN for a parameter not given. None if the parameters are outside the projection's domain.
    • Method, the projections: LongLat, Merc, Tmerc (Poder/Engsager's extended series), Utm, Lcc, Aea, Aeqd, Stere, Sterea, Cass, Laea, Eqc, Eqdc, Sinu, Eck6, Moll, Robin, Cea, Mill, Poly, Omerc, Somerc, Krovak, Ortho, Gnom, Eqearth, Bonne.
    • Crs::is_geographic().
    • trig: from_epsg and new take a Trig (default Trig::moonbit()). Pass a wasm host's Math to compute exactly as proj4js does.

    Not here: a PROJ string or WKT parser (mbt-map parses them in TypeScript and hands over 34 numbers, which Definition::decode reads: "CRS definition" in the repository's docs/dev/FORMATS.md), grid shifts (+nadgrids), vertical datums and axis orders other than easting, northing.

    #Matching proj4js

    The repository compares the module with proj4js 2.22.0 on every EPSG code it knows and on PROJ strings for each projection, both ways (proj:golden): within 1 mm and 1e-8°.

    #License

    Apache-2.0 for the port; proj4js is MIT, and its Equal Earth projection Apache-2.0 (see NOTICE).

    Trig

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

    The elementary functions the projections use (@trig.Trig): a wasm build passes the host's Math, as proj4js computes.

    Crs

    pub struct Crs {
    // private fields
    }

    A coordinate reference system: from an EPSG code (from_epsg) or a parsed PROJ string (new).

    Crs::from_epsg

    fn Crs::from_epsg(code : Int, trig? :
    Trig
    ) -> Crs?

    The CRS with EPSG code if listed below (Japan's datums, plane rectangular zones and UTM, WGS 84 UTM, Web Mercator); other CRSs come from PROJ strings (Crs::new). Projected axes are easting, then northing.

    Crs::is_geographic

    fn Crs::is_geographic(self : Crs) -> Bool

    Whether the CRS's coordinates are longitude and latitude in degrees.

    Crs::new

    fn Crs::new(d : Definition, trig? :
    Trig
    ) -> Crs?

    The CRS d defines, computing with trig; None if its parameters are out of the projection's domain (an oblique Mercator without a line, a transverse Mercator on a sphere, a UTM zone outside 1-60, ...).

    Definition

    pub(all) struct Definition {
    proj : Method
    a : Double
    b : Double
    r_a : Bool
    towgs84 : Array[Double]
    datum_wgs84 : Bool
    to_meter : Double
    from_greenwich : Double
    long_wrap : Double
    lat0 : Double
    lat1 : Double
    lat2 : Double
    lat_ts : Double
    long0 : Double
    long1 : Double
    long2 : Double
    alpha : Double
    gamma : Double
    longc : Double
    x0 : Double
    y0 : Double
    k0 : Double
    zone : Int
    south : Bool
    no_uoff : Bool
    no_rot : Bool
    czech : Bool
    }

    A parsed PROJ string: a, b in meters (r_a: the sphere of equal area), towgs84 3 or 7 Helmert parameters in meters, arc seconds and ppm (empty for an unknown datum: no shifts), to_meter NaN for meters, then the projection's parameters. Angles are in radians; NaN marks a parameter not given.

    Definition::decode

    fn Definition::decode(s : ArrayView[Double]) -> Definition?

    The definition encoded in s as mbt-map's FORMATS.md "CRS definition" lays it out; None if the slots are too few or the projection index or towgs84 count is not one it allows.

    Definition::new

    fn Definition::new(proj : Method, a : Double, b : Double) -> Definition

    A definition of method on the ellipsoid with semi-axes a and b, with no datum shift and no other parameter given.

    Method

    pub(all) enum Method {
    LongLat
    Merc
    Tmerc
    Utm
    Lcc
    Aea
    Aeqd
    Stere
    Sterea
    Cass
    Laea
    Eqc
    Eqdc
    Sinu
    Eck6
    Moll
    Robin
    Cea
    Mill
    Poly
    Omerc
    Somerc
    Krovak
    Ortho
    Gnom
    Eqearth
    Bonne
    }

    The map projections a Definition can name, as PROJ calls them (+proj=...).

    DEFINITION_SLOTS

    let DEFINITION_SLOTS : Int

    The number of f64 slots of an encoded definition (mbt-map's FORMATS.md "CRS definition").

    transform

    fn transform(from : Crs, to : Crs, x : Double, y : Double) -> (Double, Double)?

    (x, y) in from as coordinates of to, shifting datums through WGS 84 where both are known; None where a projection cannot reach the point or the input is not finite.