Skip to content

Commit

Permalink
Fixes #77: pluggable leaflet layers support
Browse files Browse the repository at this point in the history
  • Loading branch information
mbarto committed Aug 28, 2015
1 parent 8b46d44 commit a89c40a
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 17 deletions.
19 changes: 3 additions & 16 deletions web/client/components/leaflet/Layer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* LICENSE file in the root directory of this source tree.
*/
var React = require('react');
var L = require('leaflet');
var WMSUtils = require('../../utils/leaflet/WMSUtils');
var Layers = require('../../utils/leaflet/Layers');

var LeafletLayer = React.createClass({
propTypes: {
Expand All @@ -33,20 +32,8 @@ var LeafletLayer = React.createClass({
},
createLayer(type, options) {
if (type) {
switch (type) {
case "osm":
this.layer = L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
});
break;
case "wms":
this.layer = L.tileLayer.wms(
WMSUtils.getWMSURL(options.url),
WMSUtils.wmsToLeafletOptions(options)
);
break;
default:
}
this.layer = Layers.createLayer(type, options);

if (this.layer) {
this.layer.addTo(this.props.map);
}
Expand Down
4 changes: 4 additions & 0 deletions web/client/components/leaflet/__tests__/Layer-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ var L = require('leaflet');
var LeafLetLayer = require('../Layer.jsx');
var expect = require('expect');

require('../../../utils/leaflet/Layers');
require('../plugins/OSMLayer');
require('../plugins/WMSLayer');

describe('Leaflet layer', () => {
document.body.innerHTML = '<div id="map"></div>';
let map = L.map('map');
Expand Down
4 changes: 3 additions & 1 deletion web/client/components/leaflet/__tests__/Map-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ var LeafletMap = require('../Map.jsx');
var LeafLetLayer = require('../Layer.jsx');
var expect = require('expect');

require('../../../utils/leaflet/Layers');
require('../plugins/OSMLayer');

describe('LeafletMap', () => {
afterEach((done) => {
React.unmountComponentAtNode(document.body);
Expand Down Expand Up @@ -71,7 +74,6 @@ describe('LeafletMap', () => {

it('check layers init', () => {
var options = {
"source": "osm",
"visibility": true
};
const map = React.render(<LeafletMap center={{lat: 43.9, lng: 10.3}} zoom={11}>
Expand Down
15 changes: 15 additions & 0 deletions web/client/components/leaflet/plugins/OSMLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Copyright 2015, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

var Layers = require('../../../utils/leaflet/Layers');
var L = require('leaflet');

Layers.registerType('osm', () => {
return L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'});
});
33 changes: 33 additions & 0 deletions web/client/components/leaflet/plugins/WMSLayer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2015, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

var Layers = require('../../../utils/leaflet/Layers');
var L = require('leaflet');
var objectAssign = require('object-assign');

function wmsToLeafletOptions(options) {
var opacity = options.opacity !== undefined ? options.opacity : 1;
// NOTE: can we use opacity to manage visibility?
return objectAssign({
layers: options.name,
styles: options.style || "",
format: options.format || 'image/png',
transparent: options.transparent !== undefined ? options.transparent : true,
opacity: opacity
}, options.params || {});
}

function getWMSURL( url ) {
return url.split("\?")[0];
}

Layers.registerType('wms', (options) => {
return L.tileLayer.wms(
getWMSURL(options.url),
wmsToLeafletOptions(options));
});
3 changes: 3 additions & 0 deletions web/client/examples/viewer/containers/Viewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var I18N = require('../../../components/I18N/I18N');
var Localized = require('../../../components/I18N/Localized');
var loadLocale = require('../../../actions/locale').loadLocale;

require('../../../components/leaflet/plugins/OSMLayer');
require('../../../components/leaflet/plugins/WMSLayer');

var Viewer = React.createClass({
propTypes: {
mapConfig: React.PropTypes.object,
Expand Down
26 changes: 26 additions & 0 deletions web/client/utils/leaflet/Layers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2015, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/

const layerTypes = {};

var Layers = {

registerType: function(type, impl) {
layerTypes[type] = impl;
},

createLayer: function(type, options) {
var layerCreator = layerTypes[type];
if (layerCreator) {
return layerCreator(options);
}
return null;
}
};

module.exports = Layers;

0 comments on commit a89c40a

Please sign in to comment.