@versatiles/map-state
    Preparing search index...

    @versatiles/map-state

    Reads and writes the maps of the VersaTiles map editor: markers, lines, polygons and circles with their styles and popups, and the properties of a map like its background map and its legend.

    npm install @versatiles/map-state
    

    A map is a MapState: plain data, without classes.

    MapState
    ├─ elements[]   in drawing order, the first one at the back
    │  ├─ marker    point, label, style, popup
    │  ├─ line      points, smooth, style, popup
    │  ├─ polygon   points, smooth, style, outlineStyle, popup
    │  └─ circle    point, radius, style, outlineStyle, popup
    ├─ frame        what a shared map shows when it opens:
    │               bounds, bearing, pitch
    └─ meta         title, background, legend, viewer, labels,
                    colorScheme
    

    Only elements is required. Each part has its type: StateElement, StateFrame, StateMetadata.

    A link of a map, and the map of a link:

    import { encodeState, decodeState, type MapState } from '@versatiles/map-state';

    const state: MapState = {
    elements: [{ type: 'marker', point: [13.4, 52.5], label: 'Berlin', style: { color: '#0000ff' } }]
    };
    const link = 'https://versatiles.org/versatiles-map-editor/view/#' + encodeState(state);
    const again = decodeState(link.split('#')[1]);

    A shorter link: with a visible area, and positions only as fine as that area needs:

    import { encodeState, resolutionForArea, type Bounds } from '@versatiles/map-state';

    const bounds: Bounds = [13.3, 52.45, 13.5, 52.55]; // west, south, east, north
    const hash = encodeState({ ...state, frame: { bounds } }, { resolution: resolutionForArea(bounds) });

    A .mapjson file, which may come from anywhere:

    import { readFileSync } from 'node:fs';
    import {
    changedMapJSONValues,
    stateFromMapJSON,
    unknownMapJSONFields,
    MapJSONVersionError
    } from '@versatiles/map-state';

    const json = JSON.parse(readFileSync('walk.mapjson', 'utf8'));
    try {
    const map = stateFromMapJSON(json); // only its valid parts
    const unknown = unknownMapJSONFields(json); // e.g. ['elements[0].style.colour']
    if (unknown.length > 0) console.warn('Fields that were not read:', unknown);
    const changed = changedMapJSONValues(json); // e.g. ['elements[0].style.color'] for "red"
    if (changed.length > 0) console.warn('Values that were left out or corrected:', changed);
    } catch (error) {
    if (error instanceof MapJSONVersionError) console.error('The file is of a newer version of the format');
    else throw error;
    }

    A map for other tools:

    import { stateToGeoJSON, stateToKML, stateFromGeoJSON } from '@versatiles/map-state';

    const geojson = stateToGeoJSON(state); // a FeatureCollection, the styles as properties
    const kml = stateToKML(state); // for Google Earth and GIS tools
    const back = stateFromGeoJSON(geojson); // also reads the GeoJSON of other tools
    To … use
    make or read the link of a map encodeState, decodeState
    make a link shorter, with coarser positions encodeState(state, { resolution }), resolutionForArea
    see what makes a link long measureLink, and which texts: measureTexts
    read or write a .mapjson file stateFromMapJSON, stateToMapJSON
    exchange a map with other tools stateToGeoJSON, stateFromGeoJSON, stateToKML, stateFromKML
    check data that comes from elsewhere stateFromMapJSON keeps only what is valid; sanitizeState does the same for a state, and sanitizeStyle, sanitizeFrame, sanitizeViewer, sanitizeLegend, sanitizeBackground, sanitizeBounds for its parts
    know the values a field can have the tables of names, e.g. DASH_NAMES, BACKGROUND_THEMES, LEGEND_POSITIONS
    know the default of a field the defaults, e.g. LINE_DEFAULTS, MARKER_DEFAULTS, LEGEND_DEFAULTS
    • Defaults are left out. A field that is missing has its default value, so a state holds only what differs. The readers of this package return states without defaults.
    • Coordinates are [longitude, latitude] in degrees with 5 decimal places, about 1 m.
    • The formats are frozen. Every later version of the package reads the links and the .mapjson files of an earlier one (from 1.0.1 on), as the same map; an older version refuses a link of a newer format with a LinkVersionError, a file with a MapJSONVersionError. The README has the promise and its limits.
    • No dependencies. It is an ES module for browsers and for Node.js 18 or newer.

    The file format is explained field by field in MAPJSON.md, the formats of links, GeoJSON and KML in the README. Below, everything the package exports, by what it is for.

    A map as a compact string, e.g. in the address of a shared map: writing and reading it, how accurate its positions are, and what makes it long.

    coarsestResolutionForArea
    decodeState
    encodeState
    exponentForResolution
    measureState
    measureTexts
    resolutionForArea
    resolutionOfExponent
    LinkVersionError
    LinkMeasure
    LinkKind
    TextGroup
    CODEC_VERSION
    MAX_EXPONENT
    TEXT_GROUPS

    A map as a file: .mapjson, the format of the editor, and GeoJSON and KML for other tools.

    changedMapJSONValues
    stateFromGeoJSON
    stateFromKML
    stateFromMapJSON
    stateToGeoJSON
    stateToKML
    stateToMapJSON
    unknownMapJSONFields
    MapJSONVersionError
    GeoJSONDocument
    MapJSON
    MAPJSON_SCHEMA_URL
    MAPJSON_VERSION

    The map as a whole: its type, what a shared map shows of it when it opens, its positions and areas.

    boundsOf
    roundCoordinate
    sanitizeBounds
    sanitizeFrame
    sanitizeState
    MapState
    StateFrame
    StateLabels
    StateMetadata
    Bounds
    Position
    COORDINATE_DIGITS
    LIMITS
    MAX_PITCH
    MAX_ZOOM
    SCROLL_ZOOMS

    What is drawn on the map: markers, lines, polygons and circles, with their popups.

    StateElementCircle
    StateElementLine
    StateElementMarker
    StateElementPolygon
    StatePopup
    StateElement

    How an element looks: the types of its styles, the values that a field can have, and the defaults, which a style leaves out.

    hasArrow
    hasPattern
    sanitizeStyle
    AreaStyle
    LineStyle
    MarkerStyle
    OutlineStyle
    ArrowName
    DashName
    FillPatternName
    LabelPositionName
    StateStyle
    StyleRoleName
    AREA_DEFAULTS
    ARROW_NAMES
    DASH_NAMES
    FILL_PATTERN_NAMES
    LABEL_POSITION_NAMES
    LINE_DEFAULTS
    MARKER_DEFAULTS
    OUTLINE_DEFAULTS
    PATTERN_COVERAGE_RANGE
    PATTERN_SCALE_RANGE

    Reading and writing the colors of styles.

    formatHex
    parseColor
    RGBA
    HexColor

    The legend of a map: its entries, how it is laid out, and where a shared map shows it.

    sanitizeLegend
    StateLegend
    StateLegendArea
    StateLegendLine
    StateLegendMarker
    StateLegendEntry
    LEGEND_DEFAULTS
    LEGEND_ENTRY_TYPES
    LEGEND_FONTS
    LEGEND_LAYOUTS
    LEGEND_POSITIONS
    LEGEND_THEMES

    The map behind the elements: its theme, labels, colors, relief and buildings.

    sanitizeBackground
    StateBackground
    StateBackgroundColors
    BACKGROUND_BASES
    BACKGROUND_BUILDINGS
    BACKGROUND_COLOR_DEFAULTS
    BACKGROUND_DEFAULTS
    BACKGROUND_HALO_WIDTHS
    BACKGROUND_LABELS
    BACKGROUND_LANGUAGES
    BACKGROUND_THEMES

    What a shared map shows over the map, and where: the address search and the zoom buttons.

    sanitizeViewer
    StateViewer
    SCALE_POSITIONS
    SEARCH_POSITIONS
    VIEWER_DEFAULTS