OpenLayers 3+: Adding layers

Antonis Christofides
GIS tips
Published in
2 min readJul 6, 2017

Unlike OpenLayers 2, OpenLayers 3+ does not distinguish between base layers and overlay layers; it just has layers, drawn one on top of the other. The following code results in a map with three layers: Open Street Map, Natura biotopes, and Corine biotopes:

var corine_url = 'http://filotis.itia.ntua.gr/mapserver?SERVICE=WFS&' +
'VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=biotopes_corine&' +
'SRSNAME=EPSG:4326&OUTPUTFORMAT=gml3';
var natura_url = 'http://filotis.itia.ntua.gr/mapserver?SERVICE=WFS&' +
'VERSION=1.1.0&REQUEST=GetFeature&TYPENAME=biotopes_natura&' +
'SRSNAME=EPSG:4326&OUTPUTFORMAT=gml3';
var map = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({ source: new ol.source.OSM() }),
new ol.layer.Vector({
title: 'Corine biotopes',
source: new ol.source.Vector({
format: new ol.format.WFS(),
url: corine_url
})
}),
new ol.layer.Vector({
title: 'Natura biotopes',
source: new ol.source.Vector({
format: new ol.format.WFS(),
url: natura_url
})
})],
view: new ol.View({
projection: 'EPSG:4326',
center: [24, 38], zoom: 6
})
});

There are some important things to keep in mind with respect to projections:

  1. You’d better avoid using reference systems that use latitude and longitude, such as WGS84 (EPSG:4326). It can be ambiguous which of the two numbers is the latitude and which is the longitude. For example, if in the urls you change VERSION=1.1.0 to VERSION=1.0.0, the biotopes will be drawn on the Red Sea instead of on Greece, because the co-ordinates will be transmitted as (longitude, latitude) whereas OpenLayers will interpret them as (latitude, longitude). Your best bet is to use EPSG:3857 everywhere. In this example I couldn’t use EPSG:3857 because the map server of filotis.itia.ntua.gr does not currently support EPSG:3857 correctly.
  2. WFS versions earlier than 1.1 probably do not support the SRSNAME parameter, so you will be stuck with the default reference system the geographical server uses for each layer.
  3. Normally OpenLayers will convert the reference system at which each layer is transmitted; so if different layers are transmitted at different reference systems, OpenLayers will, in theory, be able to draw all of them properly. However this depends on whether OpenLayers will be able to correctly determine the reference system in the transmitted layer. The way of specifying it is different in different versions of GeoServer/MapServer and possibly WMS/WFS and OpenLayers. For example, I’ve seen it specified as EPSG:3857 and also as http://www.opengis.net/gml/srs/epsg.xml#3857 and as urn:x-ogc:def:crs:EPSG:3857. In the above example, OpenLayers cannot correctly parse the reference system returned by filotis.itia.ntua.gr, and therefore assumes the default view’s reference system. This is why I’ve specified projection: 'EPSG:4326' in the view. Which brings us back to what I already said: if there’s no compelling reason, use EPSG:3857 everywhere.

--

--

Antonis Christofides
GIS tips

I help scientists and engineers bring their models to the web