Giter Club home page Giter Club logo

geostyler-mapbox-parser's Introduction

geostyler-mapbox-parser

GeoStyler-Style-Parser implementation for Mapbox

Important Notes

Since mapbox works with spritesheets, geostyler-mapbox-parser is only capable of handling sprites/icons if the application that is using the parser implements following API:

GET /sprites/?name&baseurl

  • name - name of the sprite in the spritesheet corresponding json
  • baseurl - baseurl for retrieving spritesheet and sprite json

The endpoint MUST return a reference to a single image.


Mapbox Styles require the properties sources (root property) and source (layers property). geostyler-mapbox-parser only parses style related properties to keep a clear separation between style and data. Thus, sources and source have to be added to the created styleobject after parsing, manually. See code snippet below for an example implementation of a wrapper function.

/**
 * Example wrapper function that maps a source to the corresponding
 * layer based on layer id. Expects a mapper object with key value
 * pairs in the format of "layerId:'sourceName'".
**/
function wrapper(sources, mapper, style) {
  if (typeof style === 'undefined') {
    return;
  }
  if (typeof mapper === 'undefined') {
    return style;
  }
  if (typeof sources === 'undefined') {
    return style;
  }

  style.sources = sources;
  style.layers.forEach(l => {
    l.source = mapper[l.id];
  });
  return style;
}

// required mapper object where the key refers to the layer id
// and the value to the source name.
var mapper = {
  "water": "mapbox-streets"
};

// mapbox sources object
var sources = {
  "mapbox-streets": {
    "type": "vector",
    "url": "mapbox://mapbox.mapbox-streets-v6"
  }
};

// mapbox style object
var mbStyle = {
  version: 8,
  layers: [...]
};

var wrappedStyle = wrapper(sources, mapper, style);

How to use

ES6:

import MapboxParser from "geostyler-mapbox-parser";

const pointSimplePoint = {
  name: "My Style",
  rules: [
    {
      name: "My Rule",
      symbolizers: [
        {
          kind: "Mark",
          wellKnownName: "Circle",
          color: "#FF0000",
          radius: 6
        }
      ]
    }
  ]
};

const parser = new MapboxParser();

parser
  .writeStyle(pointSimplePoint)
  .then(mbStyle => console.log(mbStyle))
  .catch(error => console.log(error));

Browser:

const pointSimplePoint = {
  name: "My Style",
  rules: [
    {
      name: "My Rule",
      symbolizers: [
        {
          kind: "Mark",
          wellKnownName: "Circle",
          color: "#FF0000",
          radius: 6
        }
      ]
    }
  ]
};
var parser = new GeoStylerMapboxParser.MapboxStyleParser();
parser
  .writeStyle(pointSimplePoint)
  .then(function(mbStyle) {
    console.log(mbStyle);
  })
  catch(function(error) {
    console.log(error);
  });

geostyler-mapbox-parser's People

Contributors

jansule avatar hwbllmnn avatar kaivolland avatar dependabot-preview[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.