From 1e3f77e8f02c0cd1c01748a971387bceefef6686 Mon Sep 17 00:00:00 2001 From: Nicolas Dufrane Date: Fri, 21 Oct 2016 16:17:56 +0200 Subject: [PATCH] Add Customizable layer origin for ol3 wms Fix #1184 --- .../map/openlayers/__tests__/Layer-test.jsx | 24 +++++++++++++++++-- .../map/openlayers/plugins/WMSLayer.js | 16 ++++++++----- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/web/client/components/map/openlayers/__tests__/Layer-test.jsx b/web/client/components/map/openlayers/__tests__/Layer-test.jsx index 1fb06f4a59..a22c888075 100644 --- a/web/client/components/map/openlayers/__tests__/Layer-test.jsx +++ b/web/client/components/map/openlayers/__tests__/Layer-test.jsx @@ -163,7 +163,6 @@ describe('Openlayers layer', () => { , document.getElementById("container")); - expect(layer).toExist(); // count layers expect(map.getLayers().getLength()).toBe(1); @@ -184,13 +183,34 @@ describe('Openlayers layer', () => { , document.getElementById("container")); - expect(layer).toExist(); // count layers expect(map.getLayers().getLength()).toBe(1); expect(map.getLayers().item(0).getSource().urls.length).toBe(2); }); + it('creates a wms layer with custom origin', () => { + var options = { + "type": "wms", + "visibility": true, + "name": "nurc:Arc_Sample", + "group": "Meteo", + "format": "image/png", + "origin": [0, 0], + "url": ["http://demo.geo-solutions.it/geoserver/wms"] + }; + // create layers + var layer = ReactDOM.render( + , document.getElementById("container")); + + + expect(layer).toExist(); + // count layers + expect(map.getLayers().getLength()).toBe(1); + expect(map.getLayers().item(0).getSource().getTileGrid().getOrigin()).toEqual([0, 0]); + }); + it('creates a wms layer with proxy for openlayers map', () => { var options = { "type": "wms", diff --git a/web/client/components/map/openlayers/plugins/WMSLayer.js b/web/client/components/map/openlayers/plugins/WMSLayer.js index c354e74daa..2ac5fb8e47 100644 --- a/web/client/components/map/openlayers/plugins/WMSLayer.js +++ b/web/client/components/map/openlayers/plugins/WMSLayer.js @@ -13,7 +13,7 @@ const CoordinatesUtils = require('../../../../utils/CoordinatesUtils'); const ProxyUtils = require('../../../../utils/ProxyUtils'); const {isArray} = require('lodash'); const SecurityUtils = require('../../../../utils/SecurityUtils'); - +const mapUtils = require('../../../../utils/MapUtils'); function wmsToOpenlayersOptions(options) { // NOTE: can we use opacity to manage visibility? @@ -44,7 +44,7 @@ function proxyTileLoadFunction(imageTile, src) { } Layers.registerType('wms', { - create: (options) => { + create: (options, map) => { const urls = getWMSURLs(isArray(options.url) ? options.url : [options.url]); const queryParameters = wmsToOpenlayersOptions(options) || {}; urls.forEach(url => SecurityUtils.addAuthenticationParameter(url, queryParameters)); @@ -59,6 +59,8 @@ Layers.registerType('wms', { }) }); } + const mapSrs = map && map.getView() && map.getView().getProjection() && map.getView().getProjection() && map.getView().getProjection().getCode() || 'EPSG:3857'; + const extent = ol.proj.get(CoordinatesUtils.normalizeSRS(options.srs || mapSrs, options.allowedSRS)).getExtent(); return new ol.layer.Tile({ opacity: options.opacity !== undefined ? options.opacity : 1, visible: options.visibility !== false, @@ -66,10 +68,12 @@ Layers.registerType('wms', { source: new ol.source.TileWMS(objectAssign({ urls: urls, params: queryParameters, - tileGrid: options.tileSize ? ol.tilegrid.createXYZ({ - extent: ol.proj.get(CoordinatesUtils.normalizeSRS(options.srs || 'EPSG:3857', options.allowedSRS)).getExtent(), - tileSize: options.tileSize - }) : undefined + tileGrid: new ol.tilegrid.TileGrid({ + extent: extent, + resolutions: mapUtils.getResolutions(), + tileSize: options.tileSize ? options.tileSize : 256, + origin: options.origin ? options.origin : [extent[0], extent[1]] + }) }, (options.forceProxy) ? {tileLoadFunction: proxyTileLoadFunction} : {})) }); },