birds-mpbx-gljs

Loading external GeoJSON file to your map

This section will guide you to adding custom data into your Mapbox map. We will add a point layer of threatened birds in the Philippines from an external GeoJSON and customize the symbols of each point based on their IUCN status.

About the data

The birds data is IUCN’s List of Threatened Birds in the Philippines from Birdlife’s Red Data Book (2005). The original list was converted to GeoJSON. Each point has the following properties:

An example of the GeoJSON is below:

{
      "type": "Feature",
      "properties": {
        "species": "Spizaetus philippensis",
        "en_name": "Philippine Hawk-eagle",
        "status": "VUL"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          121.15,
          14.8833
        ]
      }
}

Adding a GeoJSON to your map

      map.on('load', function() {
        var url = 'https://raw.githubusercontent.com/maning/birds-mpbx-gljs/master/data/bird.geojson';
        map.addSource('birds', {
          type: 'geojson',
          data: url
        });
        map.addLayer({
          id: 'birds',
          type: 'symbol',
          source: 'birds',
          layout: {
            'icon-image': 'rocket-15'
          }
        });
     });

Changing the style of your points using data-driven styling

We can improve the styling of the bird sighting by changing colors of each point according to the IUCN status. Using the data-driven styling available in Mapbox GL JS, we can style the data based on its properties.

    paint: {
        'circle-radius': {
          'base': 1.75,
          'stops': [[12, 3], [22, 180]]
        },
        'circle-color': [
          'match',
          ['get', 'status'],
          'CR', '#fbb03b',
          'DD', '#223b53',
          'EN', '#e55e5e',
          'Nt', '#3bb2d0',
          'VUL','#fb9a99',
          /* other */ '#000'
        ]
      }

The above code does the following:

Congratulations! You have just added and styled an external data into your Mapbox map! Before we move to the next section inspect your code and experiment with various circle size (circle-radius) and colors (circle-color).

Explore the Mapbox GL JS expressions tutorial for hints and examples.

See also