@@ -16,6 +16,31 @@ const PRINTING_DEFAULT_POLL_INTERVAL = 2000 // interval between each polling of
16
16
const PRINTING_DEFAULT_POLL_TIMEOUT = 600000 // ms (10 minutes)
17
17
18
18
const SERVICE_PRINT_URL = `${ API_SERVICES_BASE_URL } print3/print/default`
19
+
20
+ class GeoAdminCustomizer extends BaseCustomizer {
21
+ /** @param {string[] } layerIDsToExclude List of layer names to exclude from the print */
22
+ constructor ( layerIDsToExclude ) {
23
+ super ( )
24
+ this . layerIDsToExclude = layerIDsToExclude
25
+ this . layerFilter = this . layerFilter . bind ( this )
26
+ }
27
+
28
+ /**
29
+ * Filter out layers that should not be printed. This function is automatically called when the
30
+ * encodeMap is called using this customizer.
31
+ *
32
+ * @param {State } layerState
33
+ * @returns {boolean } True to convert this layer, false to skip it
34
+ */
35
+ layerFilter ( layerState ) {
36
+ if ( this . layerIDsToExclude . includes ( layerState . layer . get ( 'id' ) ) ) {
37
+ return false
38
+ }
39
+ // Call parent layerFilter method for other layers
40
+ return super . layerFilter ( layerState )
41
+ }
42
+ }
43
+
19
44
/**
20
45
* Tool to transform an OpenLayers map into a "spec" for MapFishPrint3 (meaning a big JSON) that can
21
46
* then be used as request body for printing.
@@ -169,6 +194,8 @@ export class PrintError extends Error {
169
194
* Default is `false`
170
195
* @param {CoordinateSystem } [config.projection=null] The projection used by the map, necessary when
171
196
* the grid is to be printed (it can otherwise be null). Default is `null`
197
+ * @param {String[] } [config.excludedLayerIDs=[]] List of the IDs of OpenLayers layer to exclude
198
+ * from the print. Default is `[]`
172
199
*/
173
200
async function transformOlMapToPrintParams ( olMap , config ) {
174
201
const {
@@ -181,6 +208,7 @@ async function transformOlMapToPrintParams(olMap, config) {
181
208
lang = null ,
182
209
printGrid = false ,
183
210
projection = null ,
211
+ excludedLayerIDs = [ ] ,
184
212
} = config
185
213
186
214
if ( ! qrCodeUrl ) {
@@ -202,6 +230,8 @@ async function transformOlMapToPrintParams(olMap, config) {
202
230
throw new PrintError ( 'Missing projection to print the grid' )
203
231
}
204
232
233
+ const customizer = new GeoAdminCustomizer ( excludedLayerIDs )
234
+
205
235
const attributionsOneLine = attributions . length > 0 ? `© ${ attributions . join ( ', ' ) } ` : ''
206
236
207
237
try {
@@ -210,7 +240,7 @@ async function transformOlMapToPrintParams(olMap, config) {
210
240
scale,
211
241
printResolution : PRINTING_RESOLUTION ,
212
242
dpi : PRINTING_RESOLUTION ,
213
- customizer : new BaseCustomizer ( [ 0 , 0 , 10000 , 10000 ] ) ,
243
+ customizer : customizer ,
214
244
} )
215
245
if ( printGrid ) {
216
246
encodedMap . layers . unshift ( {
@@ -239,7 +269,7 @@ async function transformOlMapToPrintParams(olMap, config) {
239
269
layout : layout . name ,
240
270
}
241
271
if ( layersWithLegends . length > 0 ) {
242
- spec . attributes . legends = {
272
+ spec . attributes . legend = {
243
273
name : i18n . global . t ( 'legend' ) ,
244
274
classes : layersWithLegends . map ( ( layer ) => {
245
275
return {
@@ -277,6 +307,8 @@ async function transformOlMapToPrintParams(olMap, config) {
277
307
* Default is `false`
278
308
* @param {CoordinateSystem } [config.projection=null] The projection used by the map, necessary when
279
309
* the grid is to be printed (it can otherwise be null). Default is `null`
310
+ * @param {String[] } [config.excludedLayerIDs=[]] List of IDs of OpenLayers layer to exclude from
311
+ * the print. Default is `[]`
280
312
* @returns {Promise<MFPReportResponse> } A job running on our printing backend (needs to be polled
281
313
* using {@link waitForPrintJobCompletion} to wait until its completion)
282
314
*/
@@ -291,6 +323,7 @@ export async function createPrintJob(map, config) {
291
323
lang = null ,
292
324
printGrid = false ,
293
325
projection = null ,
326
+ excludedLayerIDs = [ ] ,
294
327
} = config
295
328
try {
296
329
const printingSpec = await transformOlMapToPrintParams ( map , {
@@ -303,6 +336,7 @@ export async function createPrintJob(map, config) {
303
336
lang,
304
337
printGrid,
305
338
projection,
339
+ excludedLayerIDs,
306
340
} )
307
341
log . debug ( 'Starting print for spec' , printingSpec )
308
342
return await requestReport ( SERVICE_PRINT_URL , printingSpec )
0 commit comments