0.0.9 • Published 12 months ago

@hansdo/mapbox-gl-arcgis-featureserver v0.0.9

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
12 months ago

mapbox-gl-arcgis-featureserver

A library for retrieving features from an ArcGIS FeatureServer or MapServer. This library makes tiled requests rather than simply requesting every feature.

By default it requests data using the arcgis pbf format to minimise payload size, however if that is not available it attempts to use the geojson format.

Check out the demo

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

ArcGIS Compatibility This library relies on ArcGIS Enterprise/Server v10.4+. In v10.4 (Feb 2016) the geojson format was added as a supported format. In v10.7 (Mar 2019) the pbf format was added. ArcGIS Online meets these requirements.

Basic Usage

  import FeatureService from 'mapbox-gl-arcgis-featureserver'

  map.on('load', () => {
    
    const fsSourceId = 'featureserver-src'

    const service = new FeatureService(fsSourceId, map, {
      url: 'https://portal.spatial.nsw.gov.au/server/rest/services/NSW_Administrative_Boundaries_Theme/FeatureServer/6'
    })
    
    map.addLayer({
      'id': 'fill-lyr',
      'source': fsSourceId,
      'type': 'fill',
      'paint': {
        'fill-opacity': 0.5,
        'fill-color': '#B42222'
      }
    })

  })

API

This library exposes a single FeatureService class

Constructor Options

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

ArcGIS Options

OptionTypeDefaultDescription
urlString requiredThe url of the feature service ending in a number eg https://services2.arcgis.com/CcI36Pduqd0OR4W9/ArcGIS/rest/services/County_Polygon/FeatureServer/0. Can also be a MapServer endpoint ending in a number.
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.
simplifyFactorNumber0.3An number between 0 & 1 indicating how much to simplify geometries by. 0 to doesn't simplify, 1 for maximum simplification.
precisionNumber6How many digits of precision to request from the server. Wikipedia has a helpfu; reference of digit precision.
setAttributionFromServiceBooleantrueRetrieves the copyrightText field from the esri service and adds it as an attribution to the map if something is available.
whereString1=1A where clause for restricting which features are returned. By default everything is returned.
outFieldsString*Which fields/attributes to return for each feature. By default everything is returned. If you have many attributes you may want to retrieve only a subset and then retrieve the rest for an individual feature using an on click event with the getFeatureByObjectIds() method.
fromDateA start date for a time-enabled layer.
toDateAn end date for a time-enabled layer.
useSeviceBoundsBooleantrueOnly retrieve data for those areas within the extent advertised by the service metadata.
projectionEndpointString* See note belowA url of an ArcGIS Server Geometry Server which is called if the extent of the services isn't in EPSG4326. The fallback uses the ArcGIS Online instance, however please ensure that you are using the service within the Esri licensing arrangements.
tokenString | nullnullThe token to use when making requests to Esri. null, the default, will not append any token paramter to the requests.

Note First tries to find a project endpoint on the Server instance based on the service URL, but falls back to using https://tasks.arcgisonline.com/arcgis/rest/services/Geometry/GeometryServer/project if it can't find one.

Properties

MethodDescription
serviceMetadataReturns the service endpoint metadata as JSON.

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.
getFeaturesByLonLat( latLon, <Number=20> searchRadiusInMeters, <Boolean=False>returnGeometry)Returns a promise which when resolved returns the features as an geojson object. By default the search radius is 20 metres, and the geometry is not returned.
getFeaturesByObjectIds(objectIds, <Boolean=False>returnGeometry)Takes a comma seperated list of strings, or an array of objectIds. Returns a promise which when resolved returns the features as an geojson object. By default the geometry is not returned.
setWhere(query)Redraws the layer with a new where query applied. Corresponds to the option above on the Esri Service Options.
clearWhere()Clears the where clause and redraws the layer with all features.
setDate( from, to)Redraws the layer with he passed time range.
setToken(token)Updates the token associated with requests to Esri. Note that if the service requires a token, the initial value should be passed as a parameter in ArcGIS options.

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 srcId = 'fs-src'
    const service = new FeatureService(srcId, map, {
      url: 'https://portal.spatial.nsw.gov.au/server/rest/services/NSW_Administrative_Boundaries_Theme/FeatureServer/6'
    })
    
    const fsLyrId = 'fs-fill-lyr'
    map.addLayer({
      'id': fsLyrId,
      'source': srcId,
      'type': 'fill',
      'paint': {
        'fill-opacity': 0.5,
        'fill-color': '#B42222'
      }
    })

    function hideFsLayer () {
       map.setLayoutProperty(fsLyrId, 'visibility', 'none')
       service.disableRequests()
    }

    function showFsLayer () {
       map.setLayoutProperty(fsLyrId, 'visibility', 'visible')
       service.enableRequests()
    }

    function removeFsCompletelyFromMap () {
      map.removeLayer(fsLyrId)
      service.destroySource()
    }