Skip to content

Instantly share code, notes, and snippets.

@jopfre
Created December 17, 2018 02:24
Show Gist options
  • Save jopfre/08beb3c369069172066fafcbf4a54ffc to your computer and use it in GitHub Desktop.
Save jopfre/08beb3c369069172066fafcbf4a54ffc to your computer and use it in GitHub Desktop.
os-leaflet with 250px tiles
/**
* os-leaflet ; A [Leafletjs](http://leafletjs.com/) TileLayer to display Ordnance Survey
* data in your Leaflet map via the OS OpenSpace web map service.
*
* https://github.com/rob-murray/os-leaflet
*/
(function (root, factory) {
// UMD for Node, AMD or browser globals
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['leaflet', 'proj4leaflet'], factory);
} else if (typeof exports === 'object') {
// Node & CommonJS-like environments.
var L = require('leaflet'); // eslint-disable-line vars-on-top
require('proj4leaflet');
module.exports = factory(L);
} else {
// Browser globals
if (typeof window.L === 'undefined') {
throw new Error('Leaflet missing');
}
root.returnExports = factory(root.L);
}
}(this, function (L) {
/* This is our namespace for OSOpenSpace on Leaflet js */
L.OSOpenSpace = L.OSOpenSpace || {};
L.OSOpenSpace.VERSION = '1.0.0';
L.OSOpenSpace.CRS = L.extend(
new L.Proj.CRS(
'EPSG:27700',
'+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +datum=OSGB36 +units=m +no_defs', {
// Choose mapping layers
// https://www.ordnancesurvey.co.uk/business-and-government/help-and-support/web-services/os-ondemand/configuring-wmts.html
// resolutions: [2500, 1000, 500, 200, 100, 50, 25, 10, 5, 2.5] //200px tiles
resolutions: [4, 2, 1] //250px tiles
}
), {
distance: function (a, b) {
return L.CRS.Earth.distance(a, b);
}
}
);
L.OSOpenSpace.LogoControl = L.Control.extend({
options: {
position: 'bottomleft'
},
onAdd: function () {
var container = L.DomUtil.create('div', 'os-logo-control');
var logoImage = L.DomUtil.create('img', '', container);
logoImage.title = 'Powered by OS OpenSpace';
logoImage.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEU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om Layer for Ordnance Survey OpenSpace service.
* Note: An API key is needed, see OS website for details
*
*/
L.OSOpenSpace.TileLayer = L.TileLayer.WMS.extend({
initialize: function (apiKey, apiUrl, options) { // (String, String, Object)
if (!apiKey) {
throw new Error('OSOpenSpace layer requires an API Key parameter to function.');
}
if (!apiUrl) {
apiUrl = 'file:///';
}
options = L.extend({
crs: L.OSOpenSpace.CRS,
// Set tile size and max zoom based on chosen resolutions
// tileSize: 200
// maxZoom: 14,
tileSize: 250
maxZoom: 2,
minZoom: 0,
}, options);
L.TileLayer.WMS.prototype.initialize.call(this,
'https://openspace.ordnancesurvey.co.uk/osmapapi/ts',
options);
this.wmsParams = {
KEY: apiKey,
FORMAT: 'image/png',
URL: apiUrl,
REQUEST: 'GetMap',
WIDTH: this.options.tileSize,
HEIGHT: this.options.tileSize
};
},
getAttribution: function () {
return '© Crown copyright and database rights ' +
new Date().getFullYear() +
' Ordnance Survey. ' +
'<a target="_blank" ' +
'href="https://www.ordnancesurvey.co.uk/web-services/os-openspace/developer-agreement.html" ' +
'title="OS OpenSpace Terms of Use">' +
'Terms of Use</a>.';
},
onAdd: function (map) {
if (map.options.attributionControl) {
this.logoControl = new L.OSOpenSpace.LogoControl();
map.addControl(this.logoControl);
}
L.TileLayer.prototype.onAdd.call(this, map);
},
onRemove: function (map) {
if (this.logoControl) {
map.removeControl(this.logoControl);
}
L.TileLayer.prototype.onRemove.call(this, map);
},
/**
* Return a url for this tile.
* Calculate the bbox for the tilePoint and format the wms request
*/
getTileUrl: function (tilePoint) { // (Point, Number) -> String
var resolutionMpp = this.options.crs.options.resolutions[tilePoint.z],
tileSizeMetres = this.options.tileSize * resolutionMpp,
tileBboxX0 = tileSizeMetres * tilePoint.x,
tileBboxY0 = tileSizeMetres * (-1 - tilePoint.y); // TODO: Is there a missing transformation ? tilePoint appears to be topLeft in this config
// service is a tile based wms format and only requires x0, y0 - ignore other points
this.wmsParams.BBOX = [tileBboxX0, tileBboxY0, 0, 0].join(',');
this.wmsParams.LAYERS = resolutionMpp;
return this._url + L.Util.getParamString(this.wmsParams); // eslint-disable-line no-underscore-dangle
}
});
/*
* Factory method to create a new OSOpenSpace tilelayer.
*
* @public
* @param {string} apiKey Your API key for OSOpenSpace.
* @param {string} apiUrl The URL of your site as provided to OSOpenSpace.
* @param {object} options Any options to pass to the tilelayer.
* @return {L.TileLayer} TileLayer for Ordnance Survey OpenSpace service.
*/
L.OSOpenSpace.tilelayer = function (apiKey, apiUrl, options) {
return new L.OSOpenSpace.TileLayer(apiKey, apiUrl, options);
};
return L.OSOpenSpace;
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment