Skip to content

rob-murray/os-leaflet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

83 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

os-leaflet

npm version

A Leafletjs TileLayer to display Ordnance Survey map data in your Leaflet map via the OS OpenSpace web map service.

This project is no way affiliated, nor supported or endorsed by Ordnance Survey. Please review Ordnance Survey OpenSpace service terms and conditions

Description

The os-leaflet project is a new L.TileLayer that can be used to easily get Ordnance Survey products into a Leaflet map, the Leaflet API can be used as normal.

Note: This layer uses an Ordnance Survey service that requires an API KEY - attempt to get one here.

ScreenShot

Versions

Version 1.x supports Leaflet 1.0, we depend on other Leaflet 1.0 compatible libraries too.

If you are using Leaflet version < 1.0 then please use v0.2.1 of this library, or the 0.2-stable branch.

Contents

This repository contains the following sections:

  1. OSOpenSpace.js - This contains the source code to the L.OSOpenSpace tilelayer.
  2. Demo - A simple demo to show off the functionality.

Getting started

You can get hold of the code using npm and it should work fine with browserify. Or you can just manually import the layer.

Dependency management

With npm:

$ npm install os-leaflet --save

Bundle for the browser with whichever tool you're using, eg webpack, browserify.

Then just require the libary along with Leaflet in your app.

require('os-leaflet');
layer = L.OSOpenSpace.tilelayer(apiKey, apiUrl, ...options);

Manually

Download these dependencies and import into your project so that this OS OpenSpace layer can work.

Note: The order of import should be as above; Leaflet, Proj4js, Proj4Leaflet and then OS OpenSpace layer - see the demo for an example.

Register for an API key

Ordnance Survey require an API key for use with their tile service, head over to OS OpenSpace to register before using on own website.

This layer uses the OS Openspace Free service and with the mapstack or products configured for the best experience available with the Free service.

Note: This will work locally an key; both localhost and file:/// hostname and protocol override the hostname check.

Displaying a map

The os-leaflet project extends Leaflet's L.TileLayer.WMS class and integrates easily with Leaflet.

To use the Layer in your map just get the EPSG:27700 L.Proj.CRS (Coordinate Reference System - how the earth is represented on Ordnance Survey maps) - via a factory method as below.

var osgbCrs = L.OSOpenSpace.CRS;

Create a L.Map as normal but specify the L.Proj.CRS from this library, see example below. The zoom levels available are essentially the layers provided by this OSOpenSpace layer so set these as below.

var map = new L.Map('map', {
  zoom: 6,
  crs: L.OSOpenSpace.CRS,
});

Finally, create a new L.TileLayer via the factory method L.OSOpenSpace.tilelayer and add to the map instance as normal. L.OSOpenSpace.tilelayer accepts the parameters below.

Argument Required? Description
apiKey Yes The Ordnance Survey OpenSpace API key for the website domain name to be used.
apiUrl No The URL of your site associated with the API key, as provided to OSOpenSpace
options No An object of layer options to pass to the tilelayer
var options = {};
var openspaceLayer = L.OSOpenSpace.tilelayer("<API Key>", "<API URL>", options);
map.addLayer(openspaceLayer);

Don't forget to set the map centre to somewhere in Great Britain too 😉

Example

Check out the demo for an example of how to use the layer.

Map products

This layer is currently hard-coded to work with only Ordnance Survey products that have 200x200 pixel tiles at the moment, the resolutions available are below. Other products have varying tile sizes which don't play well with Leaflet, it prefers a power-of-2 stack.

// OV0, OV1, OV2, MSR, MS, 250KR, 250K, 50KR, 50K, VMD
[2500, 1000, 500, 200, 100, 50, 25, 10, 5, 2.5]

For the full spec, see OS website.

Coordinates

Points can be added to the map in WGS84 reference system - if you have British National Grid coords then these should be converted.

L.polygon([
  [50.978633, -1.5740458],
  [51.068553, -1.5732215],
  [51.067945, -1.4305097],
  [50.978027, -1.4316098]
]).addTo(map).bindPopup('I am the SU32 10K square');

OS Logo

The Ordnance Survey logo in the bottom left can be removed, along with other attribution, by supplying the option { attributionControl: false } to the map constructor. It it enabled by default.

map = new L.Map('map', {
  zoom: 6,
  crs: L.OSOpenSpace.CRS,
  attributionControl: false
});

Issues

Please open an issue for any problems.

Contributions

Please use the GitHub pull-request mechanism to submit contributions.

License

This project is available for use under the MIT software license. See LICENSE