Skip to content

Instantly share code, notes, and snippets.

@mejackreed
Last active September 14, 2018 15:37
Show Gist options
  • Save mejackreed/7a5a333979551a65937be23d71d69145 to your computer and use it in GitHub Desktop.
Save mejackreed/7a5a333979551a65937be23d71d69145 to your computer and use it in GitHub Desktop.
Stanford Spatial Data Infrastructure WFS and WMS requests
license - MIT

Stanford Spatial Data Infrastructure WFS and WMS requests

This example is adopted from the Mapbox GL JS examples and uses data sources from the Stanford Digital Repository.

Both WMS (Web Mapping Service) and WFS (Web Feature Service) requests are used in this example to create a map with labeled features.

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Stanford Spatial Data Infrastructure WFS and WMS requests</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.46.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.46.0/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<script>
function wfsURL(layerId) {
var url = new URL('https://geowebservices.stanford.edu/geoserver/ows');
var params = {
service : 'WFS',
version : '2.0',
request : 'GetFeature',
typeName : layerId,
outputFormat : 'application/json',
SrsName : 'EPSG:4326'
};
url.search = new URLSearchParams(params)
return url;
}
mapboxgl.accessToken = 'pk.eyJ1IjoibWVqYWNrcmVlZCIsImEiOiJLSUo2a0pZIn0.uo-4b21vBKQ5A24_AaHFUg';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 13,
center: [2.33, 48.85]
});
map.on('load', function() {
map.addLayer(
{
'id': 'basemap',
'type': 'raster',
'source': {
'type': 'raster',
'tiles': [
'https://geowebservices.stanford.edu/geoserver/druid/wms?service=WMS&version=1.3.0&request=GetMap&bbox={bbox-epsg-3857}&layers=druid:zz925nt9211&styles=&width=256&height=256&srs=EPSG:3857&format=image/png&tiled=true&transparent=true'
],
'tileSize': 256
},
'paint': {
'raster-fade-duration': 0,
'raster-opacity': 0.5
}
});
fetch(wfsURL('druid:mg627pw9195'))
.then(res => res.json())
.then(response => {
map.addLayer({
'id': 'Masonry Wall Remains',
'type': 'symbol',
'source': {
'type': 'geojson',
'data': response
},
"layout": {
"icon-image": "triangle-11",
"icon-optional": true,
// Only label it if the label value is present
"text-field": ["case", ['get', 'nom'] !== null, ['get', 'nom'], ''],
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}
});
fetch(wfsURL('druid:hd667bp4617'))
.then(res => res.json())
.then(response => {
map.addLayer({
'id': 'butchers',
'type': 'symbol',
'source': {
'type': 'geojson',
'data': response
},
"layout": {
"icon-image": "restaurant-11",
"icon-optional": true,
"text-field": "{nom_bouc}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}
});
});
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment