Coder Social home page Coder Social logo

leaflet-custom-projection's Introduction

Leafet custom projection

Being a lightweight library, LeafletJS does not support many projections out of the box. But thanks to the proj4leaflet plugin we can use whatever projection we want. In this short tutorial we will create a map using the swiss projection LV95 (EPSG:2056).

The dependencies

var L = require('leaflet')
var proj4 = require('proj4')
var proj4L = require('proj4leaflet')

Define the projection

var lv95 = {
	epsg: 'EPSG:2056',
	def: '+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs',
	resolutions: [ 4000, 3750, 3500, 3250, 3000, 2750, 2500, 2250, 2000, 1750, 1500, 1250, 1000, 750, 650, 500, 250, 100, 50, 20, 10, 5, 2.5, 2, 1.5, 1, 0.5],
	origin: [2420000, 1350000]
}

Define the CRS

var crs = new L.Proj.CRS(lv95.epsg, lv95.def, { 
	resolutions: lv95.resolutions, 
	origin: lv95.origin
})

Initialize the map

using the CRS and setting max-zoom to the number of available resolutions

var map = L.map('map', { 
	crs: crs,
	maxZoom: lv95.resolutions.length
})

map.setView([47.56,7.59], 22)

We are ready to add some layers.

Add a WMS layer

For this example I used the WMS of Kanton Basel-Stadt.

var wms = L.tileLayer.wms('https://wms.geo.bs.ch/wmsBS?', {
	layers: 'BS.HP.Historische_Stadtplaene.Stadtplan_1946',
	maxZoom: lv95.resolutions.length,
	attribution: '<a href="http://geo.bs.ch">Kanton Basel-Stadt</a>'
}).addTo(map)

Add a GeoJSON layer

For this layer I extracted the churches from the POIs of Kanton Basel.

var geojson = require('./data/churches.json')

To make sure that the data is projected correctly, add define the CRS on the GeoJSON.

geojson.crs = {
	type: 'name',
	properties: { name: 'urn:ogc:def:crs:EPSG::2056' }
}

Add the layer.

L.Proj.geoJson(geojson, {
	pointToLayer: function(feature, latlng) {
		var popup = '<b>' + feature.properties.NAME + '</b><br/>' 
			+ '<small>' + feature.properties.SUBKATEGOR + '</small>' // Define popup 
		return L.marker(latlng).bindPopup(popup) // Represent as marker
  }
}).addTo(map)

That is it.

If you need to convert between swiss coordinates (LV03 and LV95) in the browser or NodeJS, check out swiss-projection-light.

leaflet-custom-projection's People

Contributors

idris-maps avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.