0.0.3 • Published 2 years ago

mapbox-gl-ogc-feature-collection v0.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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

OptionTypeDescription
sourceIdString requiredA string
mapObject requiredA mapbox-gl or maplibre-gl map instance.
collectionOptionsObject requiredA range of options which will be used to manage the collection. See below.
geojsonSourceOptionsObjectA object which will be passed to the creation of the mapbox-gl geojson source

Collection Options

OptionTypeDefaultDescription
urlString requiredThe base url of the OGC Feature API. https://demo.pygeoapi.io/covid-19.
collectionIdString requiredThe name of the collection. cases_country.
limitNumber5000Number of features to return in each tile.
propertiesStringList of properties to return for each feature.
datetimeDateDate or date range to filter items.
bboxStringApply bbox to items to only show in certain area. -180,-90,180,90
useStaticZoomLevelBooleanfalseWhether to only get tiles at a single zoom level. If true then set minZoom to the desired level.
minZoomNumberif useStaticZoom is true then 7, otherwise 2The zoom level to start requesting tiles.

Methods

MethodDescription
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()
}