-
Notifications
You must be signed in to change notification settings - Fork 383
/
PrintUtils.js
437 lines (427 loc) · 17.2 KB
/
PrintUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright 2016, 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 CoordinatesUtils = require('./CoordinatesUtils');
const SecurityUtils = require('./SecurityUtils');
const MapUtils = require('./MapUtils');
const AnnotationsUtils = require("./AnnotationsUtils");
const {colorToHexStr} = require("./ColorUtils");
const {isArray} = require('lodash');
const url = require('url');
const defaultScales = MapUtils.getGoogleMercatorScales(0, 21);
const assign = require('object-assign');
// Non è detto che sia uniforme!!
const getGeomType = function(layer) {
return layer.features && layer.features[0] && layer.features[0].geometry ? layer.features[0].geometry.type :
layer.features && layer.features[0].features && layer.features[0].style && layer.features[0].style.type ? layer.features[0].style.type : undefined;
};
/**
* Utilities for Print
* @memberof utils
*/
const PrintUtils = {
/**
* Given a static resource, returns the resource's absolute
* URL. Supports file paths with or without origin/protocol.
* @param {string} uri the uri to transform
* @param {string} [origin=window.location.origin] the origin to use
*/
toAbsoluteURL: (uri, origin) => {
// Handle absolute URLs (with protocol-relative prefix)
// Example: //domain.com/file.png
if (uri.search(/^\/\//) !== -1) {
return window.location.protocol + uri;
}
// Handle absolute URLs (with explicit origin)
// Example: http://domain.com/file.png
if (uri.search(/:\/\//) !== -1) {
return uri;
}
// Handle absolute URLs (without explicit origin)
// Example: /file.png
if (uri.search(/^\//) !== -1) {
return (origin || window.location.origin) + uri;
}
},
/**
* Tranform the original URL configuration of the layer into a URL
* usable for the print service.
* @param {string|array} input Original URL
* @return {string} the URL modified as GeoServer requires
*/
normalizeUrl: (input) => {
let result = isArray(input) ? input[0] : input;
if (result.indexOf('?') !== -1) {
result = result.substring(0, result.indexOf('?'));
}
return PrintUtils.toAbsoluteURL(result);
},
/**
* Find the layout name for the given options.
* The convention is: `PAGE_FORMAT + ("_2_pages_legend"|"_2_pages_legend"|"") + ("_landscape"|"")``
* @param {object} spec the spec with the options
* @return {string} the layout name.
*/
getLayoutName: (spec) => {
let layoutName = [spec.sheet];
if (spec.includeLegend) {
if (spec.twoPages) {
layoutName.push('2_pages_legend');
}
} else {
layoutName.push('no_legend');
}
if (spec.landscape) {
layoutName.push('landscape');
}
return layoutName.join('_');
},
/**
* Gets the print scales allowed from the capabilities of the print service.
* @param {capabilities} capabilities the capabilities of the print service
* @return {array} the scales array
*/
getPrintScales: (capabilities) => {
return capabilities.scales.slice(0).reverse().map((scale) => parseFloat(scale.value)) || [];
},
/**
* Guess the nearest zoom level in the allowed scales
* @param {number} zoom the zoom level
* @param {array} scales the allowed scales
* @param {array} [mapScales=defaultScales] the map scales
* @return {number} the index that best approximates the current map scale
*/
getNearestZoom: (zoom, scales, mapScales = defaultScales) => {
const mapScale = mapScales[zoom];
return scales.reduce((previous, current, index) => {
return current < mapScale ? previous : index;
}, 0);
},
/**
* Guess the map zoom level from print scale
* @param {number} zoom the zoom level
* @param {array} scales the allowed scales
* @param {array} [mapScales=defaultScales] the map scales
* @return {number} the index that best approximates the current map scale
*/
getMapZoom: (scaleZoom, scales, mapScales = defaultScales) => {
const scale = scales[scaleZoom];
return mapScales.reduce((previous, current, index) => {
return current < scale ? previous : index;
}, 0) + 1;
},
/**
* Get the mapSize for print preview, parsing the layout and limiting the width.
* @param {object} layout the layout object
* @param {number} maxWidth the max width for the mapSize
* @return {object} width and height of a map limited by the maxWidth and with the same ratio of the layout
*/
getMapSize: (layout, maxWidth) => {
if (layout) {
const width = layout.rotation ? layout.map.height : layout.map.width;
const height = layout.rotation ? layout.map.width : layout.map.height;
return {
width: maxWidth,
height: height / width * maxWidth
};
}
return {
width: 100,
height: 100
};
},
/**
* Creates the mapfish print specification from the current configuration
* @param {object} spec the current configuration
* @return {object} the mapfish print configuration to send to the server
*/
getMapfishPrintSpecification: (spec) => {
const projectedCenter = CoordinatesUtils.reproject(spec.center, 'EPSG:4326', spec.projection);
return {
"units": CoordinatesUtils.getUnits(spec.projection),
"srs": CoordinatesUtils.normalizeSRS(spec.projection || 'EPSG:3857'),
"layout": PrintUtils.getLayoutName(spec),
"dpi": parseInt(spec.resolution, 10),
"outputFilename": "mapstore-print",
"geodetic": false,
"mapTitle": spec.name || '',
"comment": spec.description || '',
"layers": PrintUtils.getMapfishLayersSpecification(spec.layers, spec, 'map'),
"pages": [
{
"center": [
projectedCenter.x,
projectedCenter.y
],
"scale": spec.scale || defaultScales[spec.scaleZoom],
"rotation": 0
}
],
"legends": PrintUtils.getMapfishLayersSpecification(spec.layers, spec, 'legend')
};
},
/**
* Generate the layers (or legend) specification for print.
* @param {array} layers the layers configurations
* @param {spec} spec the print configurations
* @param {string} purpose allowed values: `map|legend`. Tells which spec to generate.
* @return {array} the configuration array for layers (or legend) to send to the print service.
*/
getMapfishLayersSpecification: (layers, spec, purpose) => {
return layers.filter((layer) => PrintUtils.specCreators[layer.type] && PrintUtils.specCreators[layer.type][purpose])
.map((layer) => PrintUtils.specCreators[layer.type][purpose](layer, spec));
},
specCreators: {
wms: {
map: (layer) => ({
"baseURL": PrintUtils.normalizeUrl(layer.url) + '?',
"opacity": layer.opacity || 1.0,
"singleTile": false,
"type": "WMS",
"layers": [
layer.name
],
"format": layer.format || "image/png",
"styles": [
layer.style || ''
],
"customParams": SecurityUtils.addAuthenticationParameter(PrintUtils.normalizeUrl(layer.url), assign({
"TRANSPARENT": true,
"TILED": true,
"EXCEPTIONS": "application/vnd.ogc.se_inimage",
"scaleMethod": "accurate"
}, layer.baseParams || {}, layer.params || {}))
}),
legend: (layer, spec) => ({
"name": layer.title || layer.name,
"classes": [
{
"name": "",
"icons": [
PrintUtils.normalizeUrl(layer.url) + url.format({
query: SecurityUtils.addAuthenticationParameter(PrintUtils.normalizeUrl(layer.url), {
TRANSPARENT: true,
EXCEPTIONS: "application/vnd.ogc.se_xml",
VERSION: "1.1.1",
SERVICE: "WMS",
REQUEST: "GetLegendGraphic",
LAYER: layer.name,
STYLE: layer.style || '',
SCALE: spec.scale,
height: spec.iconSize,
width: spec.iconSize,
minSymbolSize: spec.iconSize,
LEGEND_OPTIONS: "forceLabels:" + (spec.forceLabels ? "on" : "") + ";fontAntialiasing:" + spec.antiAliasing + ";dpi:" + spec.legendDpi + ";fontStyle:" + (spec.bold && "bold" || (spec.italic && "italic") || '') + ";fontName:" + spec.fontFamily + ";fontSize:" + spec.fontSize,
format: "image/png",
...assign({}, layer.params)
})
})
]
}
]
})
},
vector: {
map: (layer, spec) => ({
type: 'Vector',
name: layer.name,
"opacity": layer.opacity || 1.0,
styleProperty: "ms_style",
styles: {
1: PrintUtils.toOpenLayers2Style(layer, layer.style),
"Polygon": PrintUtils.toOpenLayers2Style(layer, layer.style, "Polygon"),
"LineString": PrintUtils.toOpenLayers2Style(layer, layer.style, "LineString"),
"Point": PrintUtils.toOpenLayers2Style(layer, layer.style, "Point"),
"FeatureCollection": PrintUtils.toOpenLayers2Style(layer, layer.style, "FeatureCollection")
},
geoJson: CoordinatesUtils.reprojectGeoJson({
type: "FeatureCollection",
features: layer.id === "annotations" && AnnotationsUtils.annotationsToPrint(layer.features) ||
layer.features.map( f => ({...f, properties: {...f.properties, ms_style: f && f.geometry && f.geometry.type && f.geometry.type.replace("Multi", "") || 1}}))
},
"EPSG:4326",
spec.projection)
}
)
},
osm: {
map: () => ({
"baseURL": "http://a.tile.openstreetmap.org/",
"opacity": 1,
"singleTile": false,
"type": "OSM",
"maxExtent": [
-20037508.3392,
-20037508.3392,
20037508.3392,
20037508.3392
],
"tileSize": [
256,
256
],
"extension": "png",
"resolutions": [
156543.03390625,
78271.516953125,
39135.7584765625,
19567.87923828125,
9783.939619140625,
4891.9698095703125,
2445.9849047851562,
1222.9924523925781,
611.4962261962891,
305.74811309814453,
152.87405654907226,
76.43702827453613,
38.218514137268066,
19.109257068634033,
9.554628534317017,
4.777314267158508,
2.388657133579254,
1.194328566789627,
0.5971642833948135
]
})
},
mapquest: {
map: () => ({
"baseURL": "http://otile1.mqcdn.com/tiles/1.0.0/map/",
"opacity": 1,
"singleTile": false,
"type": "OSM",
"maxExtent": [
-20037508.3392,
-20037508.3392,
20037508.3392,
20037508.3392
],
"tileSize": [
256,
256
],
"extension": "png",
"resolutions": [
156543.03390625,
78271.516953125,
39135.7584765625,
19567.87923828125,
9783.939619140625,
4891.9698095703125,
2445.9849047851562,
1222.9924523925781,
611.4962261962891,
305.74811309814453,
152.87405654907226,
76.43702827453613,
38.218514137268066,
19.109257068634033,
9.554628534317017,
4.777314267158508,
2.388657133579254,
1.194328566789627,
0.5971642833948135
]
})
}
},
rgbaTorgb: (rgba = "") => {
return rgba.indexOf("rgba") !== -1 ? `rgb${rgba.slice(rgba.indexOf("("), rgba.lastIndexOf(","))})` : rgba;
},
/**
* Useful for print (Or generic Openlayers 2 conversion style)
* http://dev.openlayers.org/docs/files/OpenLayers/Feature/Vector-js.html#OpenLayers.Feature.Vector.OpenLayers.Feature.Vector.style
*/
toOpenLayers2Style: function(layer, style, styleType) {
if (!style) {
return PrintUtils.getOlDefaultStyle(layer, styleType);
}
// commented the available options.
return {
"fillColor": colorToHexStr(style.fillColor),
"fillOpacity": style.fillOpacity,
// "rotation": "30",
"externalGraphic": style.iconUrl,
// "graphicName": "circle",
// "graphicOpacity": 0.4,
"pointRadius": style.radius,
"strokeColor": colorToHexStr(style.color),
"strokeOpacity": style.opacity,
"strokeWidth": style.weight
// "strokeLinecap": "round",
// "strokeDashstyle": "dot",
// "fontColor": "#000000",
// "fontFamily": "sans-serif",
// "fontSize": "12px",
// "fontStyle": "normal",
// "fontWeight": "bold",
// "haloColor": "#123456",
// "haloOpacity": "0.7",
// "haloRadius": "3.0",
// "label": "${name}",
// "labelAlign": "cm",
// "labelRotation": "45",
// "labelXOffset": "-25.0",
// "labelYOffset": "-35.0"
};
},
/**
* Provides the default style for
* each vector type.
*/
getOlDefaultStyle(layer, styleType) {
switch (styleType || getGeomType(layer) || "") {
case 'Polygon':
case 'MultiPolygon': {
return {
"fillColor": "#0000FF",
"fillOpacity": 0.1,
"strokeColor": "#0000FF",
"strokeOpacity": 1,
"strokeWidth": 3,
"strokeDashstyle": "dash",
"strokeLinecap": "round"
};
}
case 'MultiLineString':
case 'LineString':
return {
"strokeColor": "#0000FF",
"strokeOpacity": 1,
"strokeWidth": 3
};
case 'Point':
case 'MultiPoint': {
return layer.styleName === "marker" ? {
"externalGraphic": "http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/images/marker-icon.png",
"graphicWidth": 25,
"graphicHeight": 41,
"graphicXOffset": -12, // different offset
"graphicYOffset": -41
} : {
"fillColor": "#FF0000",
"fillOpacity": 0,
"strokeColor": "#FF0000",
"pointRadius": 5,
"strokeOpacity": 1,
"strokeWidth": 1
};
}
default: {
return {
"fillColor": "#0000FF",
"fillOpacity": 0.1,
"strokeColor": "#0000FF",
"pointRadius": 5,
"strokeOpacity": 1,
"strokeWidth": 1
};
}
}
}
};
module.exports = PrintUtils;