Giter Site home page Giter Site logo

rowanwins / mapbox-gl-flatgeobuf Goto Github PK

View Code? Open in Web Editor NEW
18.0 3.0 3.0 3.88 MB

A little library for mapbox-gl that provides a tiled approach to consuming flatgeobuf files.

Home Page: https://rowanwins.github.io/mapbox-gl-flatgeobuf/

License: MIT License

JavaScript 100.00%
mapbox-gl-js maplibre-gl-js flatgeobuf

mapbox-gl-flatgeobuf's Introduction

mapbox-gl-flatgeobuf

A little library provides a tiled approach to consuming flatgeobuf files.

Note This library is compatible with both mapbox-gl and maplibre-gl.

Installation

npm install mapbox-gl-flatgeobuf

Basic Usage

  import FlatGeobuf from 'mapbox-gl-flatgeobuf'

  const fsSourceId = "fgb-src";

  new FlatGeobuf(fsSourceId, map, {
    url: "https://flatgeobuf.org/test/data/UScounties.fgb",
    minZoom: 8,
    idProperty: "FIPS",
  });

  map.addLayer({
    id: "fill-lyr",
    source: fsSourceId,
    type: "fill",
    paint: {
      "fill-opacity": 0.5,
      "fill-color": "#B42222",
    },
  });

API

This library exposes a single FlatGeobuf class

Constructor Options

Option Type Description
sourceId String required A string
map Object required A mapbox-gl or maplibre-gl map instance.
flatGeobufOptions Object required A range of options which will be used to access the flatgeobuf file. See below.
geojsonSourceOptions Object A object which will be passed to the creation of the mapbox-gl geojson source

FlatGeobuf Options

Option Type Default Description
url String required The url of the flatgeobuf file eg https://flatgeobuf.org/test/data/UScounties.fgb.
minZoom Number required 7 The zoom level to start requesting tiles.
idProperty String required An id field in the data to help minimise duplicate features being added to the map

Methods

Method Description
destroySource() Important The destroySource() method removes the source from the map and associated event listeners for retrieving data which request data as the map is panned, so it's important to call this method if removing the layer from the map completely.
disableRequests() Important The disableRequests() method temporarily disables the associated event listeners for retrieving data which request data as the map is panned, you may want to call this if you toggle your layer off.
enableRequests() Important The enableRequests() method enables the associated event listeners for retrieving data which request data as the map is panned. By default this is called by the constructor.

Example of disabling and enabling requests

It would be nice if disabling/enabling of requests happened automatically but unfortunantly I haven't found a way to make that happen because of how sources and layers are managed in mapbox-gl.

  const fsSourceId = "fgb-src";

  const fgb = new FlatGeobuf(fsSourceId, map, {
    url: "https://flatgeobuf.org/test/data/UScounties.fgb",
    minZoom: 8,
    idProperty: "FIPS",
  });
    
  const fgLyrId = 'fs-fill-lyr'
  map.addLayer({
    'id': fgLyrId,
    'source': fsSourceId,
    'type': 'fill',
    'paint': {
      'fill-opacity': 0.5,
      'fill-color': '#B42222'
    }
  })

  function hideFsLayer () {
      map.setLayoutProperty(fgLyrId, 'visibility', 'none')
      fgb.disableRequests()
  }

  function showFsLayer () {
      map.setLayoutProperty(fgLyrId, 'visibility', 'visible')
      fgb.enableRequests()
  }

  function removeFsCompletelyFromMap () {
    map.removeLayer(fgLyrId)
    fgb.destroySource()
  }

mapbox-gl-flatgeobuf's People

Contributors

rowanwins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.