mbtmap

    Maps and WebGIS for MoonBit web apps: a typed API over the mbt-map npm package

    map
    gis
    webgis
    maplibre
    rabbita
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    9 hours ago
    Downloads
    9

    No dependencies

    #t-ujiie-g/mbtmap

    Maps and WebGIS for MoonBit web apps (js target): a typed MoonBit API over the mbt-map npm package, which does the drawing (its workers and WebAssembly come with it).

    moon add t-ujiie-g/mbtmap npm install mbt-map # bundled by your app's bundler (Vite, esbuild, …)

    let tokyo = @mbtmap.lnglat(139.7671, 35.6812)
    let map = @mbtmap.Map::new(
    "map",
    style=@style.Style::url("https://example.com/style.json"),
    center=tokyo,
    zoom=14.0,
    )
    map.on(Load, _ => {
    map.add_source(
    "places",
    @style.geojson_source(data={
    "type": "Feature",
    "properties": { "kind": "station" },
    "geometry": { "type": "Point", "coordinates": [tokyo.lng, tokyo.lat] },
    }),
    )
    map.add_layer(
    @style.circle(
    "places",
    source="places",
    circle_radius=@expr.num(8.0),
    circle_color=@expr.match_(
    @expr.get("kind"),
    [("station", @expr.color("#2166ac"))],
    default=@expr.color("#d6604d"),
    ),
    ),
    )
    })
    |> ignore
    map.on_layer(Click, ["places"], e => map.fly_to(center?=e.lnglat, zoom=16.0)) |> ignore

    Packages: t-ujiie-g/mbtmap (the map, camera, events, queries), t-ujiie-g/mbtmap/style (styles, sources and one function per layer type with its paint and layout properties as labelled arguments, generated from MapLibre's style specification; Layer::json, Source::json and Style::json take JSON as written) and t-ujiie-g/mbtmap/expr (expressions).

    Licensed under the Apache License 2.0.

    Bounds

    pub(all) struct Bounds {
    west : Double
    south : Double
    east : Double
    north : Double
    } derive(Eq,
    Debug
    )

    A box in degrees (west may exceed east across the antimeridian).

    Bounds::equal

    fn Bounds::equal(Bounds, Bounds) -> Bool

    Bounds::not_equal

    fn Bounds::not_equal(x : Bounds, y : Bounds) -> Bool

    Bounds::to_repr

    Event

    pub(all) struct Event {
    kind : EventType
    point : Point?
    lnglat : LngLat?
    features : Array[Feature]
    error : String?
    source : String?
    }

    What an event carries: mouse events their point (CSS pixels), its lnglat and the features of the layers listened to; Error its error message; SourceData the source id.

    EventType

    pub(all) enum EventType {
    MouseDown
    MouseUp
    MouseOver
    MouseOut
    MouseMove
    Click
    DblClick
    ContextMenu
    MouseEnter
    MouseLeave
    Load
    StyleData
    SourceData
    Render
    Idle
    Error
    MoveStart
    Move
    MoveEnd
    ZoomStart
    Zoom
    ZoomEnd
    RotateStart
    Rotate
    RotateEnd
    PitchStart
    Pitch
    PitchEnd
    }

    MapLibre's events: mouse events (on a layer's features with Map::on_layer, where alone MouseEnter and MouseLeave come), Load (first view drawn), Render (each frame), Idle (settled, also right after Load), Error, StyleData, SourceData, and the camera's moving, zooming, rotating, pitching.

    EventType::name

    fn EventType::name(self : EventType) -> String

    The name the npm package uses for self.

    Feature

    pub(all) struct Feature {
    id :
    Json
    ?
    layer : String
    source : String
    source_layer : String?
    properties :
    Json

    geometry :
    Json

    }

    A feature under the pointer or found by a query, as GeoJSON with where it was drawn: layer and source (and source_layer of a vector source), its id if it has one.

    JsValue

    #external
    pub type JsValue

    A JS value as mbt-map's npm package gives or takes it.

    JsValue::to_json

    The JSON value of v, as JSON.parse(JSON.stringify(v)) gives it (null for undefined).

    Listener

    pub struct Listener {
    // private fields
    }

    A listener Map::on added; off removes it.

    Listener::off

    fn Listener::off(self : Listener) -> Unit

    Stops the listener.

    LngLat

    pub(all) struct LngLat {
    lng : Double
    lat : Double
    } derive(Eq,
    Debug
    )

    A place in degrees: longitude east, latitude north (WGS 84).

    LngLat::equal

    fn LngLat::equal(LngLat, LngLat) -> Bool

    LngLat::not_equal

    fn LngLat::not_equal(x : LngLat, y : LngLat) -> Bool

    LngLat::to_repr

    Map

    #external
    pub type Map

    A map drawn into a page element (the npm package's Map).

    Map::add_layer

    fn Map::add_layer(self : Map, layer :
    Layer
    , before? : String) -> Unit

    Adds layer (@style.circle, … or @style.Layer::json) below layer before, or on top.

    Map::add_source

    fn Map::add_source(self : Map, id : String, source :
    Source
    ) -> Unit

    Adds source id (@style.geojson_source, … or @style.Source::json).

    Map::bearing

    fn Map::bearing(self : Map) -> Double

    Degrees clockwise from north the map is turned by.

    Map::center

    fn Map::center(self : Map) -> LngLat

    The point at the center of the map.

    Map::ease_to

    fn Map::ease_to(self : Map, center? : LngLat, zoom? : Double, bearing? : Double, pitch? : Double, duration? : Double) -> Unit

    Moves the camera to what is given over duration milliseconds (MapLibre's easeTo, default 500).

    Map::fit_bounds

    fn Map::fit_bounds(self : Map, bounds : Bounds, padding? : Double, max_zoom? : Double, duration? : Double) -> Unit

    Moves the camera to show bounds with padding pixels around them, zooming in no further than max_zoom; at once when duration is 0.

    Map::fly_to

    fn Map::fly_to(self : Map, center? : LngLat, zoom? : Double, bearing? : Double, pitch? : Double, duration? : Double, speed? : Double, curve? : Double) -> Unit

    Flies the camera to what is given, zooming out and back in (MapLibre's flyTo): speed (screenfuls a second, 1.2), curve (1.42), or a fixed duration in milliseconds.

    Map::jump_to

    fn Map::jump_to(self : Map, center? : LngLat, zoom? : Double, bearing? : Double, pitch? : Double) -> Unit

    Moves the camera at once to what is given.

    Map::layer_ids

    fn Map::layer_ids(self : Map) -> Array[String]

    The layer ids, bottom first.

    Map::move_layer

    fn Map::move_layer(self : Map, id : String, before? : String) -> Unit

    Moves layer id below layer before, or on top.

    Map::new

    fn Map::new(container : String, style~ :
    Style
    , center? : LngLat, zoom? : Double, bearing? : Double, pitch? : Double, min_zoom? : Double, max_zoom? : Double, min_pitch? : Double, max_pitch? : Double, interactive? : Bool, attribution? : Bool) -> Map

    A map in the element with id container drawing style (MapLibre's new Map): the camera at center, zoom, bearing, pitch (all 0 by default) within the min/max ranges; interactive false leaves the mouse and keyboard out, attribution false the sources' attributions.

    Map::on

    fn Map::on(self : Map, kind : EventType, f : (Event) -> Unit) -> Listener

    Calls f with each kind event from now on.

    Map::on_layer

    fn Map::on_layer(self : Map, kind : EventType, layers : Array[String], f : (Event) -> Unit) -> Listener

    Calls f with each kind mouse event on the features of layers (with those features).

    Map::pitch

    fn Map::pitch(self : Map) -> Double

    Degrees the map is tilted by, 0 looking straight down.

    Map::project

    fn Map::project(self : Map, p : LngLat) -> Point

    The screen point of p.

    Map::query_rendered_features

    fn Map::query_rendered_features(self : Map, point? : Point, layers? : Array[String], done : (Array[Feature]) -> Unit) -> Unit

    The features drawn at point (or in the whole view without it), topmost first, of layers if given, handed to done once the workers answered (MapLibre's queryRenderedFeatures); none when the query fails (an unknown layer), the error logged.

    Map::remove

    fn Map::remove(self : Map) -> Unit

    Removes the map from the page and frees what it holds.

    Map::remove_layer

    fn Map::remove_layer(self : Map, id : String) -> Unit

    Map::remove_source

    fn Map::remove_source(self : Map, id : String) -> Unit

    Map::set_data

    fn Map::set_data(self : Map, id : String, data :
    Json
    ) -> Unit

    Replaces the data of GeoJSON source id.

    Map::set_feature_state

    fn Map::set_feature_state(self : Map, source : String, id :
    Json
    , state :
    Json
    , source_layer? : String) -> Unit

    Merges state into the feature's state, which paint reading ["feature-state", key] draws with; source_layer names a vector source's layer.

    Map::set_filter

    fn Map::set_filter(self : Map, id : String, filter :
    Expr
    ?) -> Unit

    Sets the filter of layer id; None removes it.

    Map::set_layout

    fn Map::set_layout(self : Map, id : String, name : String, value :
    Expr
    ?) -> Unit

    Sets layout property name of layer id; None goes back to its default.

    Map::set_paint

    fn Map::set_paint(self : Map, id : String, name : String, value :
    Expr
    ?) -> Unit

    Sets paint property name of layer id; None goes back to its default.

    Map::set_style

    fn Map::set_style(self : Map, style :
    Style
    ) -> Unit

    Draws style from now on; sources and layers that stay alike keep their tiles.

    Map::style

    A copy of the style drawn, or None before it has loaded.

    Map::unproject

    fn Map::unproject(self : Map, p : Point) -> LngLat

    The place under screen point p.

    Map::zoom

    fn Map::zoom(self : Map) -> Double

    Point

    pub(all) struct Point {
    x : Double
    y : Double
    } derive(Eq,
    Debug
    )

    A screen point in CSS pixels from the map's top-left corner.

    Point::equal

    fn Point::equal(Point, Point) -> Bool

    Point::not_equal

    fn Point::not_equal(x : Point, y : Point) -> Bool

    Point::to_repr

    json_to_js

    j as a plain JS value (object keys as own properties, so __proto__ stays a key).

    lnglat

    fn lnglat(lng : Double, lat : Double) -> LngLat

    LngLat of lng and lat, degrees.