-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathPrintProvider.js
904 lines (842 loc) · 35.2 KB
/
PrintProvider.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/**
* Copyright (c) 2008-2012 The Open Source Geospatial Foundation
*
* Published under the BSD license.
* See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text
* of the license.
*/
/**
* @require OpenLayers/Layer.js
* @require OpenLayers/Format/JSON.js
* @require OpenLayers/Format/GeoJSON.js
* @require OpenLayers/BaseTypes/Class.js
*/
/** api: (define)
* module = GeoExt.data
* class = PrintProvider
* base_link = `Ext.util.Observable <http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.Observable>`_
*/
Ext.namespace("GeoExt.data");
/** api: example
* Minimal code to print as much of the current map extent as possible as
* soon as the print service capabilities are loaded, using the first layout
* reported by the print service:
*
* .. code-block:: javascript
*
* var mapPanel = new GeoExt.MapPanel({
* renderTo: "mappanel",
* layers: [new OpenLayers.Layer.WMS("wms", "/geoserver/wms",
* {layers: "topp:tasmania_state_boundaries"})],
* center: [146.56, -41.56],
* zoom: 7
* });
* var printProvider = new GeoExt.data.PrintProvider({
* url: "/geoserver/pdf",
* listeners: {
* "loadcapabilities": function() {
* var printPage = new GeoExt.data.PrintPage({
* printProvider: printProvider
* });
* printPage.fit(mapPanel, true);
* printProvider.print(mapPanel, printPage);
* }
* }
* });
*/
/** api: constructor
* .. class:: PrintProvider
*
* Provides an interface to a Mapfish or GeoServer print module. For printing,
* one or more instances of :class:`GeoExt.data.PrintPage` are also required
* to tell the PrintProvider about the scale and extent (and optionally
* rotation) of the page(s) we want to print.
*/
GeoExt.data.PrintProvider = Ext.extend(Ext.util.Observable, {
/** api: config[url]
* ``String`` Base url of the print service. Only required if
* ``capabilities`` is not provided. This
* is usually something like http://path/to/mapfish/print for Mapfish,
* and http://path/to/geoserver/pdf for GeoServer with the printing
* extension installed. This property requires that the print service is
* at the same origin as the application (or accessible via proxy).
*/
/** private: property[url]
* ``String`` Base url of the print service. Will always have a trailing
* "/".
*/
url: null,
/** api: config[autoLoad]
* ``Boolean`` If set to true, the capabilities will be loaded upon
* instance creation, and ``loadCapabilities`` does not need to be called
* manually. Setting this when ``capabilities`` and no ``url`` is provided
* has no effect. Default is false.
*/
/** api: config[capabilities]
* ``Object`` Capabilities of the print service. Only required if ``url``
* is not provided. This is the object returned by the ``info.json``
* endpoint of the print service, and is usually obtained by including a
* script tag pointing to
* http://path/to/printservice/info.json?var=myvar in the head of the
* html document, making the capabilities accessible as ``window.myvar``.
* This property should be used when no local print service or proxy is
* available, or when you do not listen for the ``loadcapabilities``
* events before creating components that require the PrintProvider's
* capabilities to be available.
*/
/** private: property[capabilities]
* ``Object`` Capabilities as returned from the print service.
*/
capabilities: null,
/** api: config[method]
* ``String`` Either ``POST`` or ``GET`` (case-sensitive). Method to use
* when sending print requests to the servlet. If the print service is at
* the same origin as the application (or accessible via proxy), then
* ``POST`` is recommended. Use ``GET`` when accessing a remote print
* service with no proxy available, but expect issues with character
* encoding and URLs exceeding the maximum length. Default is ``POST``.
*/
/** private: property[method]
* ``String`` Either ``POST`` or ``GET`` (case-sensitive). Method to use
* when sending print requests to the servlet.
*/
method: "POST",
/** api: config[encoding]
* ``String`` The encoding to set in the headers when requesting the print
* service. Prevent character encoding issues, especially when using IE.
* Default is retrieved from document charset or characterSet if existing
* or ``UTF-8`` if not.
*/
encoding: document.charset || document.characterSet || "UTF-8",
/** api: config[timeout]
* ``Number`` Timeout of the POST Ajax request used for the print request
* (in milliseconds). Default of 30 seconds. Has no effect if ``method``
* is set to ``GET``.
*/
timeout: 30000,
/** api: property[customParams]
* ``Object`` Key-value pairs of custom data to be sent to the print
* service. Optional. This is e.g. useful for complex layout definitions
* on the server side that require additional parameters.
*/
customParams: null,
/** api: config[baseParams]
* ``Object`` Key-value pairs of base params to be add to every
* request to the service. Optional.
*/
/** api: property[scales]
* ``Ext.data.JsonStore`` read-only. A store representing the scales
* available.
*
* Fields of records in this store:
*
* * name - ``String`` the name of the scale
* * value - ``Float`` the scale denominator
*/
scales: null,
/** api: property[dpis]
* ``Ext.data.JsonStore`` read-only. A store representing the dpis
* available.
*
* Fields of records in this store:
*
* * name - ``String`` the name of the dpi
* * value - ``Float`` the dots per inch
*/
dpis: null,
/** api: property[layouts]
* ``Ext.data.JsonStore`` read-only. A store representing the layouts
* available.
*
* Fields of records in this store:
*
* * name - ``String`` the name of the layout
* * size - ``Object`` width and height of the map in points
* * rotation - ``Boolean`` indicates if rotation is supported
*/
layouts: null,
/** api: property[dpi]
* ``Ext.data.Record`` the record for the currently used resolution.
* Read-only, use ``setDpi`` to set the value.
*/
dpi: null,
/** api: property[layout]
* ``Ext.data.Record`` the record of the currently used layout. Read-only,
* use ``setLayout`` to set the value.
*/
layout: null,
/** private: method[constructor]
* Private constructor override.
*/
constructor: function(config) {
this.initialConfig = config;
Ext.apply(this, config);
if(!this.customParams) {
this.customParams = {};
}
this.addEvents(
/** api: event[loadcapabilities]
* Triggered when the capabilities have finished loading. This
* event will only fire when ``capabilities`` is not configured.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * capabilities - ``Object`` the capabilities
*/
"loadcapabilities",
/** api: event[layoutchange]
* Triggered when the layout is changed.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * layout - ``Ext.data.Record`` the new layout
*/
"layoutchange",
/** api: event[dpichange]
* Triggered when the dpi value is changed.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * dpi - ``Ext.data.Record`` the new dpi record
*/
"dpichange",
/** api: event[beforeprint]
* Triggered when the print method is called.
* TODO: rename this event to beforeencode
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * map - ``OpenLayers.Map`` the map being printed
* * pages - Array of :class:`GeoExt.data.PrintPage` the print
* pages being printed
* * options - ``Object`` the options to the print command
*/
"beforeprint",
/** api: event[print]
* Triggered when the print document is opened.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * url - ``String`` the url of the print document
*/
"print",
/** api: event[printexception]
* Triggered when using the ``POST`` method, when the print
* backend returns an exception.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * response - ``Object`` the response object of the XHR
*/
"printexception",
/** api: event[beforeencodelayer]
* Triggered before a layer is encoded. This can be used to
* exclude layers from the printing, by having the listener
* return false.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * layer - ``OpenLayers.Layer`` the layer which is about to be
* encoded.
*/
"beforeencodelayer",
/** api: event[encodelayer]
* Triggered when a layer is encoded. This can be used to modify
* the encoded low-level layer object that will be sent to the
* print service.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * layer - ``OpenLayers.Layer`` the layer which is about to be
* encoded.
* * encodedLayer - ``Object`` the encoded layer that will be
* sent to the print service.
*/
"encodelayer",
/** api: events[beforedownload]
* Triggered before the PDF is downloaded. By returning false from
* a listener, the default handling of the PDF can be cancelled
* and applications can take control over downloading the PDF.
* TODO: rename to beforeprint after the current beforeprint event
* has been renamed to beforeencode.
*
* Listener arguments:
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * url - ``String`` the url of the print document
*/
"beforedownload",
/** api: event[beforeencodelegend]
* Triggered before the legend is encoded. If the listener
* returns false, the default encoding based on GeoExt.LegendPanel
* will not be executed. This provides an option for application
* to get legend info from a custom component other than
* GeoExt.LegendPanel.
*
* Listener arguments:
*
* * printProvider - :class:`GeoExt.data.PrintProvider` this
* PrintProvider
* * jsonData - ``Object`` The data that will be sent to the print
* server. Can be used to populate jsonData.legends.
* * legend - ``Object`` The legend supplied in the options which were
* sent to the print function.
*/
"beforeencodelegend"
);
GeoExt.data.PrintProvider.superclass.constructor.apply(this, arguments);
this.scales = new Ext.data.JsonStore({
root: "scales",
sortInfo: {field: "value", direction: "DESC"},
fields: ["name", {name: "value", type: "float"}]
});
this.dpis = new Ext.data.JsonStore({
root: "dpis",
fields: ["name", {name: "value", type: "float"}]
});
this.layouts = new Ext.data.JsonStore({
root: "layouts",
fields: [
"name",
{name: "size", mapping: "map"},
{name: "rotation", type: "boolean"}
]
});
if(config.capabilities) {
this.loadStores();
} else {
if(this.url.split("/").pop()) {
this.url += "/";
}
this.initialConfig.autoLoad && this.loadCapabilities();
}
},
/** api: method[setLayout]
* :param layout: ``Ext.data.Record`` the record of the layout.
*
* Sets the layout for this printProvider.
*/
setLayout: function(layout) {
this.layout = layout;
this.fireEvent("layoutchange", this, layout);
},
/** api: method[setDpi]
* :param dpi: ``Ext.data.Record`` the dpi record.
*
* Sets the dpi for this printProvider.
*/
setDpi: function(dpi) {
this.dpi = dpi;
this.fireEvent("dpichange", this, dpi);
},
/** api: method[print]
* :param map: ``GeoExt.MapPanel`` or ``OpenLayers.Map`` The map to print.
* :param pages: ``Array`` of :class:`GeoExt.data.PrintPage` or
* :class:`GeoExt.data.PrintPage` page(s) to print.
* :param options: ``Object`` of additional options, see below.
*
* Sends the print command to the print service and opens a new window
* with the resulting PDF.
*
* Valid properties for the ``options`` argument:
*
* * ``legend`` - :class:`GeoExt.LegendPanel` If provided, the legend
* will be added to the print document. For the printed result to
* look like the LegendPanel, the following ``!legends`` block
* should be included in the ``items`` of your page layout in the
* print module's configuration file:
*
* .. code-block:: none
*
* - !legends
* maxIconWidth: 0
* maxIconHeight: 0
* classIndentation: 0
* layerSpace: 5
* layerFontSize: 10
*
* * ``overview`` - :class:`OpenLayers.Control.OverviewMap` If provided,
* the layers for the overview map in the printout will be taken from
* the OverviewMap control. If not provided, the print service will
* use the main map's layers for the overview map. Applies only for
* layouts configured to print an overview map.
*/
print: function(map, pages, options) {
if(map instanceof GeoExt.MapPanel) {
map = map.map;
}
pages = pages instanceof Array ? pages : [pages];
options = options || {};
if(this.fireEvent("beforeprint", this, map, pages, options) === false) {
return;
}
var jsonData = Ext.apply({
units: map.getUnits(),
srs: map.baseLayer.projection.getCode(),
layout: this.layout.get("name"),
dpi: this.dpi.get("value")
}, this.customParams);
var pagesLayer = pages[0].feature.layer;
var encodedLayers = [];
// ensure that the baseLayer is the first one in the encoded list
var layers = map.layers.concat();
layers.remove(map.baseLayer);
layers.unshift(map.baseLayer);
Ext.each(layers, function(layer){
if(layer !== pagesLayer && layer.getVisibility() === true) {
var enc = this.encodeLayer(layer);
enc && encodedLayers.push(enc);
}
}, this);
jsonData.layers = encodedLayers;
var encodedPages = [];
Ext.each(pages, function(page) {
encodedPages.push(Ext.apply({
center: [page.center.lon, page.center.lat],
scale: page.scale.get("value"),
rotation: page.rotation
}, page.customParams));
}, this);
jsonData.pages = encodedPages;
if (options.overview) {
var encodedOverviewLayers = [];
Ext.each(options.overview.layers, function(layer) {
var enc = this.encodeLayer(layer);
enc && encodedOverviewLayers.push(enc);
}, this);
jsonData.overviewLayers = encodedOverviewLayers;
}
if(options.legend && !(this.fireEvent("beforeencodelegend", this, jsonData, options.legend) === false)) {
var legend = options.legend;
var rendered = legend.rendered;
if (!rendered) {
legend = legend.cloneConfig({
renderTo: document.body,
hidden: true
});
}
var encodedLegends = [];
legend.items && legend.items.each(function(cmp) {
if(!cmp.hidden) {
var encFn = this.encoders.legends[cmp.getXType()];
// MapFish Print doesn't currently support per-page
// legends, so we use the scale of the first page.
encodedLegends = encodedLegends.concat(
encFn.call(this, cmp, jsonData.pages[0].scale));
}
}, this);
if (!rendered) {
legend.destroy();
}
jsonData.legends = encodedLegends;
}
if(this.method === "GET") {
var url = Ext.urlAppend(this.capabilities.printURL,
"spec=" + encodeURIComponent(Ext.encode(jsonData)));
this.download(url);
} else {
Ext.Ajax.request({
url: this.capabilities.createURL,
timeout: this.timeout,
jsonData: jsonData,
headers: {"Content-Type": "application/json; charset=" + this.encoding},
success: function(response) {
var url = Ext.decode(response.responseText).getURL;
this.download(url);
},
failure: function(response) {
this.fireEvent("printexception", this, response);
},
params: this.initialConfig.baseParams,
scope: this
});
}
},
/** private: method[download]
* :param url: ``String``
*/
download: function(url) {
if (this.fireEvent("beforedownload", this, url) !== false) {
if (Ext.isOpera) {
// Make sure that Opera don't replace the content tab with
// the pdf
window.open(url);
} else {
// This avoids popup blockers for all other browsers
window.location.href = url;
}
}
this.fireEvent("print", this, url);
},
/** api: method[loadCapabilities]
*
* Loads the capabilities from the print service. If this instance is
* configured with either ``capabilities`` or a ``url`` and ``autoLoad``
* set to true, then this method does not need to be called from the
* application.
*/
loadCapabilities: function() {
if (!this.url) {
return;
}
var url = this.url + "info.json";
Ext.Ajax.request({
url: url,
method: "GET",
disableCaching: false,
success: function(response) {
this.capabilities = Ext.decode(response.responseText);
this.loadStores();
},
params: this.initialConfig.baseParams,
scope: this
});
},
/** private: method[loadStores]
*/
loadStores: function() {
this.scales.loadData(this.capabilities);
this.dpis.loadData(this.capabilities);
this.layouts.loadData(this.capabilities);
this.setLayout(this.layouts.getAt(0));
this.setDpi(this.dpis.getAt(0));
this.fireEvent("loadcapabilities", this, this.capabilities);
},
/** private: method[encodeLayer]
* :param layer: ``OpenLayers.Layer``
* :return: ``Object``
*
* Encodes a layer for the print service.
*/
encodeLayer: function(layer) {
var encLayer;
for(var c in this.encoders.layers) {
if(OpenLayers.Layer[c] && layer instanceof OpenLayers.Layer[c]) {
if(this.fireEvent("beforeencodelayer", this, layer) === false) {
return;
}
encLayer = this.encoders.layers[c].call(this, layer);
this.fireEvent("encodelayer", this, layer, encLayer);
break;
}
}
// only return the encLayer object when we have a type. Prevents a
// fallback on base encoders like HTTPRequest.
return (encLayer && encLayer.type) ? encLayer : null;
},
/** private: method[getAbsoluteUrl]
* :param url: ``String``
* :return: ``String``
*
* Converts the provided url to an absolute url.
*/
getAbsoluteUrl: function(url) {
if (Ext.isSafari) {
url = url.replace(/{/g, '%7B');
url = url.replace(/}/g, '%7D');
}
var a;
if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8) {
a = document.createElement("<a href='" + url + "'/>");
a.style.display = "none";
document.body.appendChild(a);
a.href = a.href;
document.body.removeChild(a);
} else {
a = document.createElement("a");
a.href = url;
}
return a.href;
},
/** private: property[encoders]
* ``Object`` Encoders for all print content
*/
encoders: {
"layers": {
"Layer": function(layer) {
var enc = {};
if (layer.options && layer.options.maxScale) {
enc.minScaleDenominator = layer.options.maxScale;
}
if (layer.options && layer.options.minScale) {
enc.maxScaleDenominator = layer.options.minScale;
}
return enc;
},
"WMS": function(layer) {
var enc = this.encoders.layers.HTTPRequest.call(this, layer);
enc.singleTile = layer.singleTile;
Ext.apply(enc, {
type: 'WMS',
layers: [layer.params.LAYERS].join(",").split(","),
format: layer.params.FORMAT,
styles: [layer.params.STYLES].join(",").split(","),
singleTile: layer.singleTile
});
var param;
for(var p in layer.params) {
param = p.toLowerCase();
if(layer.params[p] != null && !layer.DEFAULT_PARAMS[param] &&
"layers,styles,width,height,srs".indexOf(param) == -1) {
if(!enc.customParams) {
enc.customParams = {};
}
enc.customParams[p] = layer.params[p];
}
}
return enc;
},
"OSM": function(layer) {
var enc = this.encoders.layers.TileCache.call(this, layer);
return Ext.apply(enc, {
type: 'OSM',
baseURL: enc.baseURL.substr(0, enc.baseURL.indexOf("$")),
extension: "png"
});
},
"TMS": function(layer) {
var enc = this.encoders.layers.TileCache.call(this, layer);
return Ext.apply(enc, {
type: 'TMS',
format: layer.type
});
},
"TileCache": function(layer) {
var enc = this.encoders.layers.HTTPRequest.call(this, layer);
return Ext.apply(enc, {
type: 'TileCache',
layer: layer.layername,
maxExtent: layer.maxExtent.toArray(),
tileSize: [layer.tileSize.w, layer.tileSize.h],
extension: layer.extension,
resolutions: layer.serverResolutions || layer.resolutions
});
},
"WMTS": function(layer) {
var enc = this.encoders.layers.HTTPRequest.call(this, layer);
enc = Ext.apply(enc, {
type: 'WMTS',
layer: layer.layer,
version: layer.version,
requestEncoding: layer.requestEncoding,
style: layer.style,
dimensions: layer.dimensions,
params: layer.params,
matrixSet: layer.matrixSet
});
if (layer.matrixIds) {
if (layer.requestEncoding == "KVP") {
enc.format = layer.format;
}
enc.matrixIds = []
Ext.each(layer.matrixIds, function(matrixId) {
enc.matrixIds.push({
identifier: matrixId.identifier,
matrixSize: [matrixId.matrixWidth,
matrixId.matrixHeight],
resolution: matrixId.scaleDenominator * 0.28E-3
/ OpenLayers.METERS_PER_INCH
/ OpenLayers.INCHES_PER_UNIT[layer.units],
tileSize: [matrixId.tileWidth, matrixId.tileHeight],
topLeftCorner: [matrixId.topLeftCorner.lon,
matrixId.topLeftCorner.lat]
});
})
return enc;
}
else {
return Ext.apply(enc, {
formatSuffix: layer.formatSuffix,
tileOrigin: [layer.tileOrigin.lon, layer.tileOrigin.lat],
tileSize: [layer.tileSize.w, layer.tileSize.h],
maxExtent: (layer.tileFullExtent != null) ? layer.tileFullExtent.toArray() : layer.maxExtent.toArray(),
zoomOffset: layer.zoomOffset,
resolutions: layer.serverResolutions || layer.resolutions
});
}
},
"KaMapCache": function(layer) {
var enc = this.encoders.layers.KaMap.call(this, layer);
return Ext.apply(enc, {
type: 'KaMapCache',
// group param is mandatory when using KaMapCache
group: layer.params['g'],
metaTileWidth: layer.params['metaTileSize']['w'],
metaTileHeight: layer.params['metaTileSize']['h']
});
},
"KaMap": function(layer) {
var enc = this.encoders.layers.HTTPRequest.call(this, layer);
return Ext.apply(enc, {
type: 'KaMap',
map: layer.params['map'],
extension: layer.params['i'],
// group param is optional when using KaMap
group: layer.params['g'] || "",
maxExtent: layer.maxExtent.toArray(),
tileSize: [layer.tileSize.w, layer.tileSize.h],
resolutions: layer.serverResolutions || layer.resolutions
});
},
"HTTPRequest": function(layer) {
var enc = this.encoders.layers.Layer.call(this, layer);
return Ext.apply(enc, {
baseURL: this.getAbsoluteUrl(layer.url instanceof Array ?
layer.url[0] : layer.url),
opacity: (layer.opacity != null) ? layer.opacity : 1.0
});
},
"Image": function(layer) {
var enc = this.encoders.layers.Layer.call(this, layer);
return Ext.apply(enc, {
type: 'Image',
baseURL: this.getAbsoluteUrl(layer.getURL(layer.extent)),
opacity: (layer.opacity != null) ? layer.opacity : 1.0,
extent: layer.extent.toArray(),
pixelSize: [layer.size.w, layer.size.h],
name: layer.name
});
},
"Vector": function(layer) {
if(!layer.features.length) {
return;
}
var encFeatures = [];
var encStyles = {};
var features = layer.features;
var featureFormat = new OpenLayers.Format.GeoJSON();
var styleFormat = new OpenLayers.Format.JSON();
var nextId = 1;
var styleDict = {};
var feature, style, dictKey, dictItem, styleName;
for(var i=0, len=features.length; i<len; ++i) {
feature = features[i];
style = feature.style || layer.style ||
layer.styleMap.createSymbolizer(feature,
feature.renderIntent);
// don't send unvisible features
if (style.display == 'none') {
continue;
}
dictKey = styleFormat.write(style);
dictItem = styleDict[dictKey];
if(dictItem) {
//this style is already known
styleName = dictItem;
} else {
//new style
styleDict[dictKey] = styleName = nextId++;
if(style.externalGraphic) {
encStyles[styleName] = Ext.applyIf({
externalGraphic: this.getAbsoluteUrl(
style.externalGraphic)}, style);
} else {
encStyles[styleName] = style;
}
}
var featureGeoJson = featureFormat.extract.feature.call(
featureFormat, feature);
featureGeoJson.properties = OpenLayers.Util.extend({
_gx_style: styleName
}, featureGeoJson.properties);
encFeatures.push(featureGeoJson);
}
var enc = this.encoders.layers.Layer.call(this, layer);
return Ext.apply(enc, {
type: 'Vector',
styles: encStyles,
styleProperty: '_gx_style',
geoJson: {
type: "FeatureCollection",
features: encFeatures
},
name: layer.name,
opacity: (layer.opacity != null) ? layer.opacity : 1.0
});
},
"Markers": function(layer) {
var features = [];
for (var i=0, len=layer.markers.length; i<len; i++) {
var marker = layer.markers[i];
var geometry = new OpenLayers.Geometry.Point(marker.lonlat.lon, marker.lonlat.lat);
var style = {externalGraphic: marker.icon.url,
graphicWidth: marker.icon.size.w, graphicHeight: marker.icon.size.h,
graphicXOffset: marker.icon.offset.x, graphicYOffset: marker.icon.offset.y};
var feature = new OpenLayers.Feature.Vector(geometry, {}, style);
features.push(feature);
}
var vector = new OpenLayers.Layer.Vector(layer.name);
vector.addFeatures(features);
var output = this.encoders.layers.Vector.call(this, vector);
vector.destroy();
return output;
},
"Google": function(layer) {
var enc = this.encoders.layers.TileCache.call(this, layer);
return Ext.apply(enc, {
type: 'tiledGoogle',
opacity: 1,
baseURL: 'http://maps.google.com/maps/api/staticmap',
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, 0.29858214169740677, 0.14929107084870338, 0.07464553542435169],
extension: 'png',
format: 'png',
sensor: false,
maptype: layer.type
});
}
},
"legends": {
"gx_wmslegend": function(legend, scale) {
var enc = this.encoders.legends.base.call(this, legend);
var icons = [];
for(var i=1, len=legend.items.getCount(); i<len; ++i) {
var url = legend.items.get(i).url;
if(legend.useScaleParameter === true &&
url.toLowerCase().indexOf(
'request=getlegendgraphic') != -1) {
var split = url.split("?");
var params = Ext.urlDecode(split[1]);
params['SCALE'] = scale;
url = split[0] + "?" + Ext.urlEncode(params);
}
icons.push(this.getAbsoluteUrl(url));
}
enc[0].classes[0] = {
name: "",
icons: icons
};
return enc;
},
"gx_urllegend": function(legend) {
var enc = this.encoders.legends.base.call(this, legend);
enc[0].classes.push({
name: "",
icon: this.getAbsoluteUrl(legend.items.get(1).url)
});
return enc;
},
"base": function(legend){
return [{
name: legend.getLabel(),
classes: []
}];
}
}
}
});