/
Utils.js
155 lines (135 loc) · 5.54 KB
/
Utils.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
// The MIT License (MIT)
//
// Copyright (c) 2015-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
/**
* Provides a service with print utility functions.
*
* @class
* @ngdoc service
* @ngname ngeoPrintUtils
* @hidden
*/
export function PrintUtils() {
/**
* @type {number}
*/
this.extentHalfHorizontalDistance_ = -1;
/**
* @type {number}
*/
this.extentHalfVerticalDistance_ = -1;
}
/**
* @hidden
*/
export const INCHES_PER_METER = 39.37;
/**
* The rederance DPI in the PDF
*
* @hidden
*/
export const DOTS_PER_INCH = 72;
/**
* Get the optimal print scale for a map, the map being defined by its
* size (in pixels) and resolution (in map units per pixel).
*
* @param {import('ol/size').Size} mapSize Size of the map on the screen (px).
* @param {number} mapResolution Resolution of the map on the screen.
* @param {import('ol/size').Size} printMapSize Size of the map on the paper (dots).
* @param {number[]} printMapScales Supported map scales on the paper.
* The scales are provided as scale denominators, sorted in ascending order.
* E.g. `[500, 1000, 2000, 4000]`.
* @returns {number} The best scale. `-1` is returned if there is no optimal
* scale, that is the optimal scale is lower than or equal to the first value
* in `printMapScales`.
*/
PrintUtils.prototype.getOptimalScale = function (mapSize, mapResolution, printMapSize, printMapScales) {
const mapWidth = mapSize[0] * mapResolution;
const mapHeight = mapSize[1] * mapResolution;
const scaleWidth = (mapWidth * INCHES_PER_METER * DOTS_PER_INCH) / printMapSize[0];
const scaleHeight = (mapHeight * INCHES_PER_METER * DOTS_PER_INCH) / printMapSize[1];
const scale = Math.min(scaleWidth, scaleHeight);
let optimal = -1;
for (let i = 0, ii = printMapScales.length; i < ii; ++i) {
if (scale > printMapScales[i]) {
optimal = printMapScales[i];
}
}
return optimal;
};
/**
* Get the optimal map resolution for a print scale and a map size.
*
* @param {import('ol/size').Size} mapSize Size of the map on the screen (px).
* @param {import('ol/size').Size} printMapSize Size of the map on the paper (dots).
* @param {number} printMapScale Map scale on the paper.
* @returns {number} The optimal map resolution.
*/
PrintUtils.prototype.getOptimalResolution = function (mapSize, printMapSize, printMapScale) {
const dotsPerMeter = DOTS_PER_INCH * INCHES_PER_METER;
const resolutionX = (printMapSize[0] * printMapScale) / (dotsPerMeter * mapSize[0]);
const resolutionY = (printMapSize[1] * printMapScale) / (dotsPerMeter * mapSize[1]);
const optimalResolution = Math.max(resolutionX, resolutionY);
return optimalResolution;
};
/**
* Get the coordinates of the bottom left corner of the printed map.
*
* @param {import('ol/coordinate').Coordinate} mapCenter Center of the map to print.
* @returns {import('ol/coordinate').Coordinate} The coordinates of the bottom left corner.
*/
PrintUtils.prototype.getBottomLeftCorner = function (mapCenter) {
return [mapCenter[0] - this.extentHalfHorizontalDistance_, mapCenter[1] - this.extentHalfVerticalDistance_];
};
/**
* Get the coordinates of the bottom right corner of the printed map.
*
* @param {import('ol/coordinate').Coordinate} mapCenter Center of the map to print.ç
* @returns {import('ol/coordinate').Coordinate} The coordinates of the bottom right corner.
*/
PrintUtils.prototype.getBottomRightCorner = function (mapCenter) {
return [mapCenter[0] + this.extentHalfHorizontalDistance_, mapCenter[1] - this.extentHalfVerticalDistance_];
};
/**
* Get the coordinates of the up left corner of the printed map.
*
* @param {import('ol/coordinate').Coordinate} mapCenter Center of the map to print.
* @returns {import('ol/coordinate').Coordinate} The coordinates of the up left corner.
*/
PrintUtils.prototype.getUpLeftCorner = function (mapCenter) {
return [mapCenter[0] - this.extentHalfHorizontalDistance_, mapCenter[1] + this.extentHalfVerticalDistance_];
};
/**
* Get the coordinates of the up right corner of the printed map.
*
* @param {import('ol/coordinate').Coordinate} mapCenter Center of the map to print.
* @returns {import('ol/coordinate').Coordinate} The coordinates of the up right corner.
*/
PrintUtils.prototype.getUpRightCorner = function (mapCenter) {
return [mapCenter[0] + this.extentHalfHorizontalDistance_, mapCenter[1] + this.extentHalfVerticalDistance_];
};
/**
* @type {angular.IModule}
* @hidden
*/
const myModule = angular.module('ngeoPrintUtils', []);
myModule.service('ngeoPrintUtils', PrintUtils);
export default myModule;