1
votes

I have published a shapefile as a vector tile on GeoServer according to this. Now, how can I add this layer to Mapbox?

I would be very pleased if you can help me.

2

2 Answers

1
votes

I have been trying to get that to work and for a vector layer you must specify 'source-layer' attribute which is the name of a layer on your GeoServer (ie. 'states' if using topp workspace) here is a code example that eventually worked for me:

<script>
  mapboxgl.accessToken =
    '<YOUR MAPBOX TOKEN HERE>';
  var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/streets-v11',
    center: [-100, 40],
    zoom: 12,
  });

  map.on('load', function () {
    map.addSource('wms-test-source', {
      type: 'vector',
      // use the tiles option to specify a WMS tile source URL
      // https://docs.mapbox.com/mapbox-gl-js/style-spec/sources/
      tiles: [
        'http://<YOUR GEOSERVER ADDRESS>:8080/geoserver/gwc/service/wmts?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=topp:states&STYLE=&TILEMATRIX=EPSG:900913:{z}&TILEMATRIXSET=EPSG:900913&FORMAT=application/vnd.mapbox-vector-tile&TILECOL={x}&TILEROW={y}',
      ],
      'minZoom': 0,
      'maxZoom': 14,
    });
    map.addLayer(
      {
        'id': 'wms-test-layer',
        'type': 'fill',
        'source': 'wms-test-source',
        'source-layer': 'states',
        'paint': { 'fill-color': '#00ffff' },
      }
      //'aeroway-line'
    );
  });
</script>

For more info go to GeoServer documentation at GeoServer - Mapbox Style Specification

0
votes

The code provided at the link actually shows how to add the GeoServer layer to your Mapbox map. Assuming that you want to add the layer to your map with Mapbox GL JS (since your post is tagged with mapbox-gl-js, your code would look something like this:

mapboxgl.accessToken = /* YOUR MAPBOX ACCESS TOKEN HERE */;;
var map = new mapboxgl.Map({
  container: 'map', // container id
  style: 'mapbox://styles/mapbox/streets-v11', // stylesheet location
  center: [-74.5, 40], // starting position [lng, lat]
  zoom: 9 // starting zoom
});

// Add the GeoServer layers as a source to your map.
map.addSource(
  "<source-name>": {
    "type": "vector",
    "tiles": [
      "http://localhost:8080/geoserver/gwc/service/wmts?REQUEST=GetTile&SERVICE=WMTS
        &VERSION=1.0.0&LAYER=<workspace>:<layer>&STYLE=&TILEMATRIX=EPSG:900913:{z}
        &TILEMATRIXSET=EPSG:900913&FORMAT=application/vnd.mapbox-vector-tile
        &TILECOL={x}&TILEROW={y}"
    ],
    "minZoom": 0,
    "maxZoom": 14
  }
);

// Style the GeoServer source in order to display it visually on your map
map.addLayer({
  'id': 'geoserver-layer',
  'type': /* fill in based on options here: https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#type */,
  'source': '<source-name>'
  /* Add any additional properties, full details here: https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#type */
});

In short, the GeoServer documentation link provides the code for adding a source to your map, and then you should add a layer to style the source visually on your map.

I'd recommend taking a look at the many Mapbox GL JS examples to get a sense of how sources and layers can be added, styled, modified, etc.