Example of displaying features from an MapServer OGC Features API
This example loads features from a MapServer OGC API Server using the OGC API - Features specification.
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import TileLayer from 'ol/layer/Tile.js';
import VectorLayer from 'ol/layer/Vector.js';
import {bbox as bboxStrategy} from 'ol/loadingstrategy.js';
import OSM from 'ol/source/OSM.js';
import VectorSource from 'ol/source/Vector.js';
const mapServerUrl = `https://demo.mapserver.org/cgi-bin/mapserv/localdemo/ogcapi/collections/lakes/items`;
const params = `f=json&limit=1000`;
const lakeStyle = {
'fill-color': 'rgba(70, 130, 180, 0.6)',
'stroke-color': 'rgba(25, 25, 112, 1)',
'stroke-width': 2,
};
const layer = new VectorLayer({
style: lakeStyle,
source: new VectorSource({
url: function (extent) {
const extentString = extent.join(',');
const url = `${mapServerUrl}?${params}&bbox=${extentString}`;
return url;
},
strategy: bboxStrategy,
format: new GeoJSON(),
}),
});
const map = new Map({
layers: [
new TileLayer({
className: 'bw',
source: new OSM(),
}),
layer,
],
target: 'map',
view: new View({
projection: 'EPSG:3857',
center: [0, 0],
zoom: 4,
}),
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>MapServer OGC Features</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
.bw {
filter: grayscale(100%);
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script type="module" src="main.js"></script>
</body>
</html>
{
"name": "mapserver-ogc-features",
"dependencies": {
"ol": "10.9.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}