Skip to content

Instantly share code, notes, and snippets.

@ThomasG77
Last active May 5, 2018 16:01
Show Gist options
  • Save ThomasG77/6bfcb47cc946755653abecc83d7337e8 to your computer and use it in GitHub Desktop.
Save ThomasG77/6bfcb47cc946755653abecc83d7337e8 to your computer and use it in GitHub Desktop.
Demo to use OpenLayers with Finland projection and WMS Aeromagnetic anomalous layer from GTK
<!DOCTYPE html>
<html>
<head>
<title>Finland Aeromagnetic anomalous map with Proj4</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.4.4/proj4.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<script>
var newProjCode = 'EPSG:3067';
proj4.defs('EPSG:3067', '+proj=utm +zone=35 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
var newProj = ol.proj.get(newProjCode);
var fromLonLat = ol.proj.getTransform('EPSG:4326', newProj);
// very approximate calculation of projection extent
var extent = ol.extent.applyTransform(
[19.08, 58.84, 31.59, 70.09], fromLonLat);
newProj.setExtent(extent);
var view = new ol.View({
projection: newProj
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Tile({
source: new ol.source.TileWMS({
url: 'http://gtkdata.gtk.fi/arcgis/services/GTKWMS/GTKWMS/MapServer/WMSServer',
crossOrigin: 'anonymous',
attributions: '© <a href="http://en.gtk.fi/informationservices/interface_services/">Geological Survey of Finland</a>',
params: {
'LAYERS': 'aeromagneettinen_anomaliakartta',
'FORMAT': 'image/png'
}
})
})
],
target: 'map',
view: view
});
view.fit(extent);
map.getLayers().forEach(function(layer) {
if (layer instanceof ol.layer.Tile) {
var source = layer.getSource();
if (source instanceof ol.source.TileImage) {
source.setRenderReprojectionEdges(renderEdgesCheckbox.checked);
}
}
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment