Skip to content

Commit 639e257

Browse files
authored
Merge pull request #482 from terrestris/add-XYZ-serialzer
feat: adds a XYZ Serializer
2 parents 32c14d7 + 0b48e00 commit 639e257

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed

Diff for: src/serializer/MapFishPrintV3OSMSerializer.ts

+9-20
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1+
import OlLayer from 'ol/layer/Layer';
12
import OlSource from 'ol/source/Source';
23
import OlSourceOSM from 'ol/source/OSM';
3-
import OlLayer from 'ol/layer/Layer';
4-
5-
import BaseSerializer from './BaseSerializer';
6-
7-
export class MapFishPrintV3OSMSerializer implements BaseSerializer {
4+
import MapFishPrintV3XYZSerializer from './MapFishPrintV3XYZSerializer';
85

9-
/**
10-
* The WMS layer type identificator.
11-
*/
12-
static TYPE_OSM: string = 'osm';
6+
export class MapFishPrintV3OSMSerializer extends MapFishPrintV3XYZSerializer {
137

148
validateSource(source: OlSource): source is OlSourceOSM {
159
return source instanceof OlSourceOSM;
1610
}
1711

1812
serialize(olLayer: OlLayer, opts?: any) {
13+
const source = olLayer.getSource();
1914
const optsToApply = {
2015
baseURL: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
2116
customParams: {},
@@ -28,24 +23,18 @@ export class MapFishPrintV3OSMSerializer implements BaseSerializer {
2823
...opts
2924
};
3025

31-
const source = olLayer.getSource();
32-
3326
if (!source || !this.validateSource(source)) {
3427
return;
3528
}
3629

37-
const tileGrid = source.getTileGrid();
38-
const tileGridResolutions = tileGrid?.getResolutions() || [];
30+
const urls = source.getUrls();
31+
const baseUrl = urls ? urls[0] : undefined;
3932

40-
const serialized = {
41-
name: olLayer.get('name'),
42-
opacity: olLayer.getOpacity(),
43-
resolutions: tileGridResolutions,
44-
type: MapFishPrintV3OSMSerializer.TYPE_OSM,
33+
return {
34+
...super.serialize(olLayer, opts),
35+
baseURL: baseUrl,
4536
...optsToApply
4637
};
47-
48-
return serialized;
4938
}
5039
}
5140

Diff for: src/serializer/MapFishPrintV3StamenSerializer.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import OlLayer from 'ol/layer/Layer';
22
import OlSource from 'ol/source/Source';
3-
import StadiaMaps from 'ol/source/StadiaMaps.js';
4-
import MapFishPrintV3OSMSerializer from './MapFishPrintV3OSMSerializer';
3+
import StadiaMaps from 'ol/source/StadiaMaps';
4+
import MapFishPrintV3XYZSerializer from './MapFishPrintV3XYZSerializer';
55

6-
export class MapFishPrintV3StamenSerializer extends MapFishPrintV3OSMSerializer {
6+
7+
export class MapFishPrintV3StamenSerializer extends MapFishPrintV3XYZSerializer {
78

89
validateSource(source: OlSource): source is StadiaMaps {
910
return source instanceof StadiaMaps;

Diff for: src/serializer/MapFishPrintV3XYZSerializer.ts

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import OlSource from 'ol/source/Source';
2+
import OlSourceXYZ from 'ol/source/XYZ';
3+
import OlLayer from 'ol/layer/Layer';
4+
5+
import BaseSerializer from './BaseSerializer';
6+
7+
export class MapFishPrintV3XYZSerializer implements BaseSerializer {
8+
9+
/**
10+
* The XYZ layer type identificator.
11+
* We use OSM for the type because the backend does not
12+
* have XYZ implemented which leads to the map not being printed
13+
*/
14+
static TYPE_XYZ: string = 'osm';
15+
16+
validateSource(source: OlSource): source is OlSourceXYZ {
17+
return source instanceof OlSourceXYZ;
18+
}
19+
20+
serialize(olLayer: OlLayer, opts?: any) {
21+
const optsToApply = {
22+
baseURL: undefined,
23+
customParams: {},
24+
dpi: 72,
25+
failOnError: false,
26+
imageExtension: 'png',
27+
maxExtent: [-20037508.34, -20037508.34, 20037508.34, 20037508.34],
28+
rasterStyle: undefined,
29+
tileSize: [256, 256],
30+
...opts
31+
};
32+
33+
const source = olLayer.getSource();
34+
35+
if (!source || !this.validateSource(source)) {
36+
return;
37+
}
38+
39+
const tileGrid = source.getTileGrid();
40+
const tileGridResolutions = tileGrid?.getResolutions() || [];
41+
const urls = source.getUrls();
42+
const baseUrl = urls ? urls[0] : undefined;
43+
44+
const serialized = {
45+
name: olLayer.get('name'),
46+
opacity: olLayer.getOpacity(),
47+
resolutions: tileGridResolutions,
48+
baseURL: baseUrl,
49+
type: MapFishPrintV3XYZSerializer.TYPE_XYZ,
50+
...optsToApply
51+
};
52+
return serialized;
53+
}
54+
}
55+
56+
export default MapFishPrintV3XYZSerializer;

0 commit comments

Comments
 (0)