Giter Site home page Giter Site logo

mkeller3 / mapbox-gl-ogc-feature-collection Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 1.0 6.17 MB

A small package for requesting geojson from an OGC Feature API endpoint to serve tiles in MapBox/MapLibre.

Home Page: https://mkeller3.github.io/mapbox-gl-ogc-feature-collection/

License: MIT License

JavaScript 100.00%
mapbox mapbox-gl-js maplibre-gl-js ogc ogc-features ogc-services

mapbox-gl-ogc-feature-collection's Introduction

mapbox-gl-ogc-feature-collection

A small package for requesting geojson from an OGC Feature API endpoint to serve tiles in MapBox/MapLibre.

Built with inspiration from mapbox-gl-arcgis-featureserver.

Demo

Check out the demo at this link. This demo pulls in large lakes OGC Feature API collection at https://demo.pygeoapi.io/stable. In the demo, we are using the collection lakes.

demo image

Basic Usage

import OGCFeatureCollection from 'mapbox-gl-ogc-feature-collection'

map.on('load', () => {
    const sourceId = 'collection-src'

    new OGCFeatureCollection(sourceId, map, {
        url: 'https://demo.pygeoapi.io/stable',
        collectionId: 'lakes',
        limit: 10000
    })

    map.addLayer({
        'id': 'lyr',
        'source': sourceId,
        'type': 'fill',
        'paint': {
            'fill-color': '#B42222',
            'fill-opacity': 0.7
        }
    })
})

API

This library exposes a single OGCFeatureCollection class

Constructor Options

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

Collection Options

Option Type Default Description
url String required The base url of the OGC Feature API. https://demo.pygeoapi.io/covid-19.
collectionId String required The name of the collection. cases_country.
limit Number 5000 Number of features to return in each tile.
properties String List of properties to return for each feature.
datetime Date Date or date range to filter items.
useStaticZoomLevel Boolean false Whether to only get tiles at a single zoom level. If true then set minZoom to the desired level.
minZoom Number if useStaticZoom is true then 7, otherwise 2 The zoom level to start requesting tiles.

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 unfortunately I haven't found a way to make that happen because of how sources and layers are managed in mapbox-gl.

import OGCFeatureCollection from 'mapbox-gl-ogc-feature-collection'

map.on('load', () => {
    const sourceId = 'collection-src'
    const lyrId = 'lyr'

    const service = new OGCFeatureCollection(sourceId, map, {
        url: 'https://demo.pygeoapi.io/stable',
        collectionId: 'lakes',
        limit: 10000
    })

    map.addLayer({
        'id': 'lyr',
        'source': sourceId,
        'type': 'fill',
        'paint': {
            'fill-color': '#B42222',
            'fill-opacity': 0.7
        }
    })
})
    
    
function hideFsLayer () {
    map.setLayoutProperty(lyrId, 'visibility', 'none')
    service.disableRequests()
}
function showFsLayer () {
    map.setLayoutProperty(lyrId, 'visibility', 'visible')
    service.enableRequests()
}
function removeFsCompletelyFromMap () {
    map.removeLayer(lyrId)
    service.destroySource()
}

mapbox-gl-ogc-feature-collection's People

Contributors

mkeller3 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

bkvito

mapbox-gl-ogc-feature-collection's Issues

Howto specify url with query parameters?

Hi,
I need to add (CORS-specific) query parameters to my OGC server urls. How can this be done?
If I add them to the options url I get wrong resulting urls (/collection simply added to the end of string...).
Thomas

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.