@tinkatinka/static-map 1.5.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -0
- package/dist/base64img.js +3 -5
- package/dist/compactmap.js +2 -6
- package/dist/index.d.ts +18 -2
- package/dist/index.js +158 -146
- package/dist/tilecache.js +10 -13
- package/package.json +6 -11
package/README.md
CHANGED
|
@@ -96,6 +96,22 @@ interface StaticMapCircleOptions {
|
|
|
96
96
|
}
|
|
97
97
|
```
|
|
98
98
|
|
|
99
|
+
#### Rectangles
|
|
100
|
+
```ts
|
|
101
|
+
.addRectangle(bounds; LatLngBounds, options?: Parital<StaticMapRectOptions>);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Options:
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
interface StaticMapRectOptions {
|
|
108
|
+
strokeStyle: string; // css stroke style [default: 'black']
|
|
109
|
+
lineWidth: number; // line width in pixels [default: 1.0]
|
|
110
|
+
lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
|
|
111
|
+
lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
99
115
|
#### Text
|
|
100
116
|
```ts
|
|
101
117
|
.addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>);
|
package/dist/base64img.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const cross_fetch_1 = require("cross-fetch");
|
|
1
|
+
import { fetch } from 'cross-fetch';
|
|
4
2
|
const base64img = async (url, mimetype = 'image/png', userAgent = 'StaticMap/1.0') => {
|
|
5
|
-
const response = await
|
|
3
|
+
const response = await fetch(url, {
|
|
6
4
|
headers: {
|
|
7
5
|
'User-Agent': userAgent
|
|
8
6
|
}
|
|
@@ -15,4 +13,4 @@ const base64img = async (url, mimetype = 'image/png', userAgent = 'StaticMap/1.0
|
|
|
15
13
|
const dataurl = 'data:' + mimetype.trim() + ';base64,' + imgstr;
|
|
16
14
|
return dataurl;
|
|
17
15
|
};
|
|
18
|
-
|
|
16
|
+
export default base64img;
|
package/dist/compactmap.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.compactMap = void 0;
|
|
4
1
|
const isDefined = (value) => (value !== undefined);
|
|
5
|
-
const compactMap = (array, func) => array.map(func).filter(isDefined);
|
|
6
|
-
exports.compactMap = compactMap;
|
|
2
|
+
export const compactMap = (array, func) => array.map(func).filter(isDefined);
|
|
7
3
|
if (!Array.prototype.compactMap) {
|
|
8
4
|
Array.prototype.compactMap = function (func) {
|
|
9
|
-
return
|
|
5
|
+
return compactMap(this, func);
|
|
10
6
|
};
|
|
11
7
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -19,24 +19,37 @@ export interface FillOptions {
|
|
|
19
19
|
fillStyle: string | CanvasGradient | CanvasPattern;
|
|
20
20
|
}
|
|
21
21
|
export type StrokeFillOptions = SetOptional<StrokeOptions, 'strokeStyle'> & SetOptional<FillOptions, 'fillStyle'>;
|
|
22
|
-
export type StaticMapOverlay =
|
|
22
|
+
export type StaticMapOverlay = {
|
|
23
|
+
type: StaticMapOverlayType;
|
|
24
|
+
} & StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapRect | StaticMapText | StaticMapScale;
|
|
25
|
+
type StaticMapOverlayType = 'image' | 'line' | 'circle' | 'rect' | 'text' | 'scale';
|
|
23
26
|
export interface StaticMapImage {
|
|
27
|
+
type: 'image';
|
|
24
28
|
src: string;
|
|
25
29
|
bounds: LatLngBounds;
|
|
26
30
|
}
|
|
27
31
|
export interface StaticMapLine {
|
|
32
|
+
type: 'line';
|
|
28
33
|
points: LatLng[];
|
|
29
34
|
options: StaticMapLineOptions;
|
|
30
35
|
}
|
|
31
36
|
export type StaticMapLineOptions = StrokeOptions;
|
|
32
37
|
export interface StaticMapCircle {
|
|
38
|
+
type: 'circle';
|
|
33
39
|
center: LatLng;
|
|
34
40
|
options: StaticMapCircleOptions;
|
|
35
41
|
}
|
|
36
42
|
export interface StaticMapCircleOptions extends StrokeFillOptions {
|
|
37
43
|
radius: number;
|
|
38
44
|
}
|
|
45
|
+
export type StaticMapRectOptions = StrokeFillOptions;
|
|
46
|
+
export interface StaticMapRect {
|
|
47
|
+
type: 'rect';
|
|
48
|
+
bounds: LatLngBounds;
|
|
49
|
+
options: StaticMapRectOptions;
|
|
50
|
+
}
|
|
39
51
|
export interface StaticMapText {
|
|
52
|
+
type: 'text';
|
|
40
53
|
text: string;
|
|
41
54
|
anchor: LatLng;
|
|
42
55
|
options: StaticMapTextOptions;
|
|
@@ -49,6 +62,7 @@ export interface StaticMapTextOptions extends CanvasTextDrawingStyles, StrokeFil
|
|
|
49
62
|
export type LengthUnits = 'metric' | 'imperial';
|
|
50
63
|
export type LayoutPosition = 'topleft' | 'topright' | 'bottomleft' | 'bottomright';
|
|
51
64
|
export interface StaticMapScale {
|
|
65
|
+
type: 'scale';
|
|
52
66
|
options: StaticMapScaleOptions;
|
|
53
67
|
}
|
|
54
68
|
export interface StaticMapScaleOptions {
|
|
@@ -87,6 +101,7 @@ export declare class StaticMap {
|
|
|
87
101
|
private static defaultOptions;
|
|
88
102
|
private static defaultLineOptions;
|
|
89
103
|
private static defaultCircleOptions;
|
|
104
|
+
private static defaultRectOptions;
|
|
90
105
|
private static defaultTextOptions;
|
|
91
106
|
private static defaultScaleOptions;
|
|
92
107
|
readonly options: StaticMapOptions;
|
|
@@ -106,6 +121,7 @@ export declare class StaticMap {
|
|
|
106
121
|
addLines(points: LatLng[], options?: Partial<StaticMapLineOptions>): StaticMap;
|
|
107
122
|
addLine(src: LatLng, dst: LatLng, options?: Partial<StaticMapLineOptions>): StaticMap;
|
|
108
123
|
addCircle(center: LatLng, options?: Partial<StaticMapCircleOptions>): StaticMap;
|
|
124
|
+
addRectangle(bounds: LatLngBounds, options?: Partial<StaticMapRectOptions>): StaticMap;
|
|
109
125
|
addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>): StaticMap;
|
|
110
126
|
addScale(options?: PartialDeep<StaticMapScaleOptions>): StaticMap;
|
|
111
127
|
private boundsCenter;
|
|
@@ -125,10 +141,10 @@ export declare class StaticMap {
|
|
|
125
141
|
private ang;
|
|
126
142
|
private distance;
|
|
127
143
|
private scaleDistance;
|
|
128
|
-
private overlayType;
|
|
129
144
|
private overlayExtent;
|
|
130
145
|
private calculateExtent;
|
|
131
146
|
private calculateZoom;
|
|
132
147
|
private loadTile;
|
|
133
148
|
private createCanvas;
|
|
134
149
|
}
|
|
150
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,21 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
exports.StaticMap = void 0;
|
|
7
|
-
const canvas_1 = __importDefault(require("canvas"));
|
|
8
|
-
const lodash_merge_1 = __importDefault(require("lodash.merge"));
|
|
9
|
-
const tilecache_1 = require("./tilecache");
|
|
10
|
-
const base64img_1 = __importDefault(require("./base64img"));
|
|
11
|
-
const compactmap_1 = require("./compactmap");
|
|
1
|
+
import Canvas from 'canvas';
|
|
2
|
+
import { merge } from 'lodash-es';
|
|
3
|
+
import { TileCache } from './tilecache.js';
|
|
4
|
+
import base64img from './base64img.js';
|
|
5
|
+
import { compactMap } from './compactmap.js';
|
|
12
6
|
const EARTH_RADIUS_MEAN = 6371008.8;
|
|
13
7
|
const FEET_PER_METER = 3.2808399;
|
|
14
8
|
const FEET_PER_MILE = 5280;
|
|
15
|
-
class StaticMap {
|
|
9
|
+
export class StaticMap {
|
|
10
|
+
static defaultOptions = {
|
|
11
|
+
width: 512,
|
|
12
|
+
height: 512,
|
|
13
|
+
padding: 0,
|
|
14
|
+
scaling: true,
|
|
15
|
+
tileURL: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
16
|
+
tileSize: 256,
|
|
17
|
+
tileMaxZoom: 20,
|
|
18
|
+
grayscale: false
|
|
19
|
+
};
|
|
20
|
+
static defaultLineOptions = {
|
|
21
|
+
strokeStyle: 'black',
|
|
22
|
+
lineWidth: 1.0,
|
|
23
|
+
lineCap: 'round',
|
|
24
|
+
lineJoin: 'round'
|
|
25
|
+
};
|
|
26
|
+
static defaultCircleOptions = {
|
|
27
|
+
radius: 5.0,
|
|
28
|
+
strokeStyle: 'rgba(128, 128, 128, 0.5)',
|
|
29
|
+
lineWidth: 1.0,
|
|
30
|
+
lineCap: 'round',
|
|
31
|
+
lineJoin: 'round',
|
|
32
|
+
fillStyle: 'rgba(0, 0, 0, 0.5)'
|
|
33
|
+
};
|
|
34
|
+
static defaultRectOptions = {
|
|
35
|
+
strokeStyle: 'rgba(128, 128, 128, 0.5)',
|
|
36
|
+
lineWidth: 1.0,
|
|
37
|
+
lineCap: 'round',
|
|
38
|
+
lineJoin: 'round',
|
|
39
|
+
fillStyle: 'rgba(0, 0, 0, 0.5)'
|
|
40
|
+
};
|
|
41
|
+
static defaultTextOptions = {
|
|
42
|
+
px: 0,
|
|
43
|
+
py: 0,
|
|
44
|
+
textAlign: 'center',
|
|
45
|
+
textBaseline: 'middle',
|
|
46
|
+
direction: 'inherit',
|
|
47
|
+
font: '12px sans-serif',
|
|
48
|
+
fontKerning: 'auto',
|
|
49
|
+
lineWidth: 1.0,
|
|
50
|
+
lineCap: 'round',
|
|
51
|
+
lineJoin: 'round',
|
|
52
|
+
fillStyle: 'black'
|
|
53
|
+
};
|
|
54
|
+
static defaultScaleOptions = {
|
|
55
|
+
units: 'metric',
|
|
56
|
+
position: 'bottomleft',
|
|
57
|
+
mx: 4,
|
|
58
|
+
my: 4,
|
|
59
|
+
height: 16,
|
|
60
|
+
maxWidth: 20,
|
|
61
|
+
boxStyle: {
|
|
62
|
+
lineWidth: 1.0,
|
|
63
|
+
lineCap: 'square',
|
|
64
|
+
lineJoin: 'miter',
|
|
65
|
+
strokeStyle: 'black',
|
|
66
|
+
fillStyle: 'rgba(255, 255, 255, 0.3)'
|
|
67
|
+
},
|
|
68
|
+
textStyle: {
|
|
69
|
+
font: '12px sans-serif',
|
|
70
|
+
lineWidth: 1.0,
|
|
71
|
+
lineCap: 'round',
|
|
72
|
+
lineJoin: 'round',
|
|
73
|
+
fillStyle: 'black'
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
options;
|
|
77
|
+
cache;
|
|
78
|
+
overlays;
|
|
16
79
|
constructor(options) {
|
|
17
|
-
this.options = (
|
|
18
|
-
this.cache = this.options.tileCache ? new
|
|
80
|
+
this.options = merge({}, StaticMap.defaultOptions, options);
|
|
81
|
+
this.cache = this.options.tileCache ? new TileCache(this.options.tileCache) : undefined;
|
|
19
82
|
this.overlays = [];
|
|
20
83
|
}
|
|
21
84
|
get extent() {
|
|
@@ -49,13 +112,14 @@ class StaticMap {
|
|
|
49
112
|
return this;
|
|
50
113
|
}
|
|
51
114
|
addImage(src, bounds) {
|
|
52
|
-
return this.addOverlay({ src, bounds });
|
|
115
|
+
return this.addOverlay({ type: 'image', src, bounds });
|
|
53
116
|
}
|
|
54
117
|
addLines(points, options) {
|
|
55
118
|
if (points.length >= 2) {
|
|
56
119
|
return this.addOverlay({
|
|
57
|
-
|
|
58
|
-
|
|
120
|
+
type: 'line',
|
|
121
|
+
points,
|
|
122
|
+
options: merge({}, StaticMap.defaultLineOptions, options)
|
|
59
123
|
});
|
|
60
124
|
}
|
|
61
125
|
return this;
|
|
@@ -65,20 +129,30 @@ class StaticMap {
|
|
|
65
129
|
}
|
|
66
130
|
addCircle(center, options) {
|
|
67
131
|
return this.addOverlay({
|
|
68
|
-
|
|
69
|
-
|
|
132
|
+
type: 'circle',
|
|
133
|
+
center,
|
|
134
|
+
options: merge({}, StaticMap.defaultCircleOptions, options)
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
addRectangle(bounds, options) {
|
|
138
|
+
return this.addOverlay({
|
|
139
|
+
type: 'rect',
|
|
140
|
+
bounds,
|
|
141
|
+
options: merge({}, StaticMap.defaultRectOptions, options)
|
|
70
142
|
});
|
|
71
143
|
}
|
|
72
144
|
addText(text, anchor, options) {
|
|
73
145
|
return this.addOverlay({
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
146
|
+
type: 'text',
|
|
147
|
+
text,
|
|
148
|
+
anchor,
|
|
149
|
+
options: merge({}, StaticMap.defaultTextOptions, options)
|
|
77
150
|
});
|
|
78
151
|
}
|
|
79
152
|
addScale(options) {
|
|
80
153
|
return this.addOverlay({
|
|
81
|
-
|
|
154
|
+
type: 'scale',
|
|
155
|
+
options: merge({}, StaticMap.defaultScaleOptions, options)
|
|
82
156
|
});
|
|
83
157
|
}
|
|
84
158
|
boundsCenter(bounds) {
|
|
@@ -160,25 +234,8 @@ class StaticMap {
|
|
|
160
234
|
d = (d >= 10) ? 10 : (d >= 5) ? 5 : (d >= 3) ? 3 : (d >= 2) ? 2 : 1;
|
|
161
235
|
return p * d;
|
|
162
236
|
}
|
|
163
|
-
overlayType(overlay) {
|
|
164
|
-
if (overlay.bounds !== undefined) {
|
|
165
|
-
return 'image';
|
|
166
|
-
}
|
|
167
|
-
else if (overlay.center !== undefined) {
|
|
168
|
-
return 'circle';
|
|
169
|
-
}
|
|
170
|
-
else if (overlay.anchor !== undefined) {
|
|
171
|
-
return 'text';
|
|
172
|
-
}
|
|
173
|
-
else if (overlay.points !== undefined) {
|
|
174
|
-
return 'line';
|
|
175
|
-
}
|
|
176
|
-
else {
|
|
177
|
-
return 'scale';
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
237
|
overlayExtent(overlay) {
|
|
181
|
-
const type =
|
|
238
|
+
const type = overlay.type;
|
|
182
239
|
switch (type) {
|
|
183
240
|
case 'image':
|
|
184
241
|
return overlay.bounds;
|
|
@@ -186,6 +243,8 @@ class StaticMap {
|
|
|
186
243
|
const c = overlay.center;
|
|
187
244
|
return { min: c, max: c };
|
|
188
245
|
}
|
|
246
|
+
case 'rect':
|
|
247
|
+
return overlay.bounds;
|
|
189
248
|
case 'line': {
|
|
190
249
|
const min = (arr) => arr.reduce((prev, curr) => ((curr < prev) ? curr : prev));
|
|
191
250
|
const max = (arr) => arr.reduce((prev, curr) => ((curr > prev) ? curr : prev));
|
|
@@ -231,7 +290,7 @@ class StaticMap {
|
|
|
231
290
|
lng: max(arr.map(b => b.max.lng))
|
|
232
291
|
}
|
|
233
292
|
});
|
|
234
|
-
return union(
|
|
293
|
+
return union(compactMap(this.overlays, this.overlayExtent.bind(this)));
|
|
235
294
|
}
|
|
236
295
|
calculateZoom(extent) {
|
|
237
296
|
let z = this.options.tileMaxZoom + 1;
|
|
@@ -258,7 +317,7 @@ class StaticMap {
|
|
|
258
317
|
if (this.cache !== undefined) {
|
|
259
318
|
const td = { z: zoom, x: tileX, y: tileY };
|
|
260
319
|
try {
|
|
261
|
-
const csrc = await this.cache.pass(td, () => (
|
|
320
|
+
const csrc = await this.cache.pass(td, () => base64img(src, 'image/png', this.options.userAgent));
|
|
262
321
|
if (csrc !== undefined) {
|
|
263
322
|
src = csrc;
|
|
264
323
|
}
|
|
@@ -269,9 +328,9 @@ class StaticMap {
|
|
|
269
328
|
}
|
|
270
329
|
}
|
|
271
330
|
else {
|
|
272
|
-
src = await (
|
|
331
|
+
src = await base64img(src, 'image/png', this.options.userAgent);
|
|
273
332
|
}
|
|
274
|
-
const image = await
|
|
333
|
+
const image = await Canvas.loadImage(src);
|
|
275
334
|
return image;
|
|
276
335
|
}
|
|
277
336
|
async createCanvas() {
|
|
@@ -280,7 +339,7 @@ class StaticMap {
|
|
|
280
339
|
throw new Error('Undefined extent');
|
|
281
340
|
}
|
|
282
341
|
const { width, height, scaling, backgroundColor, grayscale, tileSize } = this.options;
|
|
283
|
-
const canvas =
|
|
342
|
+
const canvas = Canvas.createCanvas(width, height);
|
|
284
343
|
const ctx = canvas.getContext('2d');
|
|
285
344
|
if (backgroundColor !== undefined) {
|
|
286
345
|
ctx.fillStyle = backgroundColor;
|
|
@@ -323,13 +382,12 @@ class StaticMap {
|
|
|
323
382
|
ctx.putImageData(imgData, 0, 0);
|
|
324
383
|
}
|
|
325
384
|
for (const overlay of this.overlays) {
|
|
326
|
-
const type =
|
|
385
|
+
const type = overlay.type;
|
|
327
386
|
switch (type) {
|
|
328
387
|
case 'image': {
|
|
329
|
-
const imgdata = overlay;
|
|
330
388
|
let img;
|
|
331
389
|
try {
|
|
332
|
-
img = new
|
|
390
|
+
img = new Canvas.Image();
|
|
333
391
|
}
|
|
334
392
|
catch {
|
|
335
393
|
img = new Image();
|
|
@@ -337,80 +395,94 @@ class StaticMap {
|
|
|
337
395
|
}
|
|
338
396
|
const draw = async () => new Promise((resolve, reject) => {
|
|
339
397
|
img.onload = () => {
|
|
340
|
-
const x = this.lngToPx(
|
|
341
|
-
const y = this.latToPy(
|
|
342
|
-
const dx = this.lngToPx(
|
|
343
|
-
const dy = this.latToPy(
|
|
398
|
+
const x = this.lngToPx(overlay.bounds.min.lng, zoom, centerXY.x, scale);
|
|
399
|
+
const y = this.latToPy(overlay.bounds.max.lat, zoom, centerXY.y, scale);
|
|
400
|
+
const dx = this.lngToPx(overlay.bounds.max.lng, zoom, centerXY.x, scale) - x;
|
|
401
|
+
const dy = this.latToPy(overlay.bounds.min.lat, zoom, centerXY.y, scale) - y;
|
|
344
402
|
ctx.drawImage(img, x, y, dx, dy);
|
|
345
403
|
resolve();
|
|
346
404
|
};
|
|
347
405
|
img.onerror = reject;
|
|
348
|
-
img.src =
|
|
406
|
+
img.src = overlay.src;
|
|
349
407
|
});
|
|
350
408
|
await draw();
|
|
351
409
|
break;
|
|
352
410
|
}
|
|
353
411
|
case 'line': {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
const p0 = this.latlngToPxPy(linedata.points[0], zoom, centerXY, scale);
|
|
412
|
+
if (overlay.points.length > 1) {
|
|
413
|
+
const p0 = this.latlngToPxPy(overlay.points[0], zoom, centerXY, scale);
|
|
357
414
|
ctx.beginPath();
|
|
358
415
|
ctx.moveTo(p0.x, p0.y);
|
|
359
|
-
for (const p of
|
|
416
|
+
for (const p of overlay.points.slice(1)) {
|
|
360
417
|
const pxy = this.latlngToPxPy(p, zoom, centerXY, scale);
|
|
361
418
|
ctx.lineTo(pxy.x, pxy.y);
|
|
362
419
|
}
|
|
363
|
-
ctx.strokeStyle =
|
|
364
|
-
ctx.lineWidth =
|
|
365
|
-
ctx.lineCap =
|
|
366
|
-
ctx.lineJoin =
|
|
420
|
+
ctx.strokeStyle = overlay.options.strokeStyle;
|
|
421
|
+
ctx.lineWidth = overlay.options.lineWidth;
|
|
422
|
+
ctx.lineCap = overlay.options.lineCap;
|
|
423
|
+
ctx.lineJoin = overlay.options.lineJoin;
|
|
367
424
|
ctx.stroke();
|
|
368
425
|
}
|
|
369
426
|
break;
|
|
370
427
|
}
|
|
371
428
|
case 'circle': {
|
|
372
|
-
const
|
|
373
|
-
|
|
429
|
+
const ccenter = this.latlngToPxPy(overlay.center, zoom, centerXY, scale);
|
|
430
|
+
ctx.beginPath();
|
|
431
|
+
ctx.arc(ccenter.x, ccenter.y, overlay.options.radius, 0, 2 * Math.PI, false);
|
|
432
|
+
if (overlay.options.fillStyle) {
|
|
433
|
+
ctx.fillStyle = overlay.options.fillStyle;
|
|
434
|
+
ctx.fill();
|
|
435
|
+
}
|
|
436
|
+
if (overlay.options.strokeStyle && overlay.options.lineWidth > 0) {
|
|
437
|
+
ctx.strokeStyle = overlay.options.strokeStyle;
|
|
438
|
+
ctx.lineWidth = overlay.options.lineWidth;
|
|
439
|
+
ctx.stroke();
|
|
440
|
+
}
|
|
441
|
+
break;
|
|
442
|
+
}
|
|
443
|
+
case 'rect': {
|
|
374
444
|
ctx.beginPath();
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
445
|
+
const min = this.latlngToPxPy(overlay.bounds.min, zoom, centerXY, scale);
|
|
446
|
+
const max = this.latlngToPxPy(overlay.bounds.max, zoom, centerXY, scale);
|
|
447
|
+
ctx.rect(min.x, min.y, max.x - min.x, max.y - min.y);
|
|
448
|
+
if (overlay.options.fillStyle) {
|
|
449
|
+
ctx.fillStyle = overlay.options.fillStyle;
|
|
378
450
|
ctx.fill();
|
|
379
451
|
}
|
|
380
|
-
if (
|
|
381
|
-
ctx.strokeStyle =
|
|
382
|
-
ctx.lineWidth =
|
|
452
|
+
if (overlay.options.strokeStyle && overlay.options.lineWidth > 0) {
|
|
453
|
+
ctx.strokeStyle = overlay.options.strokeStyle;
|
|
454
|
+
ctx.lineWidth = overlay.options.lineWidth;
|
|
455
|
+
ctx.lineJoin = overlay.options.lineJoin;
|
|
456
|
+
ctx.lineCap = overlay.options.lineCap;
|
|
383
457
|
ctx.stroke();
|
|
384
458
|
}
|
|
385
459
|
break;
|
|
386
460
|
}
|
|
387
461
|
case 'text': {
|
|
388
|
-
const
|
|
389
|
-
const
|
|
390
|
-
const opts = textdata.options;
|
|
462
|
+
const anchor = this.latlngToPxPy(overlay.anchor, zoom, centerXY, scale);
|
|
463
|
+
const opts = overlay.options;
|
|
391
464
|
ctx.textAlign = opts.textAlign;
|
|
392
465
|
ctx.textBaseline = opts.textBaseline;
|
|
393
466
|
ctx.direction = opts.direction;
|
|
394
467
|
ctx.font = opts.font;
|
|
395
468
|
if (opts.fillStyle) {
|
|
396
469
|
ctx.fillStyle = opts.fillStyle;
|
|
397
|
-
ctx.fillText(
|
|
470
|
+
ctx.fillText(overlay.text, anchor.x + opts.px, anchor.y + opts.py, opts.maxWidth);
|
|
398
471
|
}
|
|
399
472
|
if (opts.strokeStyle) {
|
|
400
473
|
ctx.strokeStyle = opts.strokeStyle;
|
|
401
|
-
ctx.strokeText(
|
|
474
|
+
ctx.strokeText(overlay.text, anchor.x + opts.px, anchor.y + opts.py, opts.maxWidth);
|
|
402
475
|
}
|
|
403
476
|
break;
|
|
404
477
|
}
|
|
405
478
|
case 'scale': {
|
|
406
|
-
const scaledata = overlay;
|
|
407
479
|
const midLeft = { lat: center.lat, lng: extent.min.lng };
|
|
408
480
|
const midRight = { lat: center.lat, lng: extent.max.lng };
|
|
409
481
|
const mapWidth = this.distance(midLeft, midRight);
|
|
410
|
-
const maxDist = mapWidth *
|
|
482
|
+
const maxDist = mapWidth * overlay.options.maxWidth / 100;
|
|
411
483
|
let label;
|
|
412
484
|
let ratio;
|
|
413
|
-
if (
|
|
485
|
+
if (overlay.options.units === 'metric') {
|
|
414
486
|
const scaleLen = this.scaleDistance(maxDist);
|
|
415
487
|
label = (scaleLen > 1000) ? `${scaleLen / 1000} km` : `${scaleLen} m`;
|
|
416
488
|
ratio = scaleLen / maxDist;
|
|
@@ -432,17 +504,17 @@ class StaticMap {
|
|
|
432
504
|
const midLeftP = this.latlngToPxPy(midLeft, zoom, centerXY, scale);
|
|
433
505
|
const midRightP = this.latlngToPxPy(midRight, zoom, centerXY, scale);
|
|
434
506
|
const widthP = Math.abs(midRightP.x - midLeftP.x);
|
|
435
|
-
const dx = widthP * ratio *
|
|
507
|
+
const dx = widthP * ratio * overlay.options.maxWidth / 100;
|
|
436
508
|
let p0 = { x: 0, y: 0 };
|
|
437
|
-
const mpx = Math.max(
|
|
509
|
+
const mpx = Math.max(overlay.options.mx * width / 100, overlay.options.my * height / 100);
|
|
438
510
|
const margin = { x: mpx, y: mpx };
|
|
439
|
-
switch (
|
|
511
|
+
switch (overlay.options.position) {
|
|
440
512
|
case 'bottomleft': {
|
|
441
|
-
p0 = { x: margin.x, y: height - margin.y -
|
|
513
|
+
p0 = { x: margin.x, y: height - margin.y - overlay.options.height };
|
|
442
514
|
break;
|
|
443
515
|
}
|
|
444
516
|
case 'bottomright': {
|
|
445
|
-
p0 = { x: width - dx - margin.x, y: height - margin.y -
|
|
517
|
+
p0 = { x: width - dx - margin.x, y: height - margin.y - overlay.options.height };
|
|
446
518
|
break;
|
|
447
519
|
}
|
|
448
520
|
case 'topleft': {
|
|
@@ -455,8 +527,8 @@ class StaticMap {
|
|
|
455
527
|
}
|
|
456
528
|
default: break;
|
|
457
529
|
}
|
|
458
|
-
const p1 = { x: p0.x + dx, y: p0.y +
|
|
459
|
-
const boxStyle =
|
|
530
|
+
const p1 = { x: p0.x + dx, y: p0.y + overlay.options.height };
|
|
531
|
+
const boxStyle = overlay.options.boxStyle;
|
|
460
532
|
if (boxStyle.fillStyle !== undefined) {
|
|
461
533
|
ctx.fillStyle = boxStyle.fillStyle;
|
|
462
534
|
ctx.fillRect(p0.x, p0.y, Math.abs(p1.x - p0.x), Math.abs(p1.y - p0.y));
|
|
@@ -474,7 +546,7 @@ class StaticMap {
|
|
|
474
546
|
ctx.stroke();
|
|
475
547
|
}
|
|
476
548
|
const anchor = { x: 0.5 * (p0.x + p1.x), y: p1.y - 3 };
|
|
477
|
-
const textStyle =
|
|
549
|
+
const textStyle = overlay.options.textStyle;
|
|
478
550
|
ctx.textAlign = 'center';
|
|
479
551
|
ctx.textBaseline = 'bottom';
|
|
480
552
|
ctx.direction = 'ltr';
|
|
@@ -496,63 +568,3 @@ class StaticMap {
|
|
|
496
568
|
return canvas;
|
|
497
569
|
}
|
|
498
570
|
}
|
|
499
|
-
exports.StaticMap = StaticMap;
|
|
500
|
-
StaticMap.defaultOptions = {
|
|
501
|
-
width: 512,
|
|
502
|
-
height: 512,
|
|
503
|
-
padding: 0,
|
|
504
|
-
scaling: true,
|
|
505
|
-
tileURL: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
506
|
-
tileSize: 256,
|
|
507
|
-
tileMaxZoom: 20,
|
|
508
|
-
grayscale: false
|
|
509
|
-
};
|
|
510
|
-
StaticMap.defaultLineOptions = {
|
|
511
|
-
strokeStyle: 'black',
|
|
512
|
-
lineWidth: 1.0,
|
|
513
|
-
lineCap: 'round',
|
|
514
|
-
lineJoin: 'round'
|
|
515
|
-
};
|
|
516
|
-
StaticMap.defaultCircleOptions = {
|
|
517
|
-
radius: 5.0,
|
|
518
|
-
strokeStyle: 'rgba(128, 128, 128, 0.5)',
|
|
519
|
-
lineWidth: 1.0,
|
|
520
|
-
lineCap: 'round',
|
|
521
|
-
lineJoin: 'round',
|
|
522
|
-
fillStyle: 'rgba(0, 0, 0, 0.5)'
|
|
523
|
-
};
|
|
524
|
-
StaticMap.defaultTextOptions = {
|
|
525
|
-
px: 0,
|
|
526
|
-
py: 0,
|
|
527
|
-
textAlign: 'center',
|
|
528
|
-
textBaseline: 'middle',
|
|
529
|
-
direction: 'inherit',
|
|
530
|
-
font: '12px sans-serif',
|
|
531
|
-
fontKerning: 'auto',
|
|
532
|
-
lineWidth: 1.0,
|
|
533
|
-
lineCap: 'round',
|
|
534
|
-
lineJoin: 'round',
|
|
535
|
-
fillStyle: 'black'
|
|
536
|
-
};
|
|
537
|
-
StaticMap.defaultScaleOptions = {
|
|
538
|
-
units: 'metric',
|
|
539
|
-
position: 'bottomleft',
|
|
540
|
-
mx: 4,
|
|
541
|
-
my: 4,
|
|
542
|
-
height: 16,
|
|
543
|
-
maxWidth: 20,
|
|
544
|
-
boxStyle: {
|
|
545
|
-
lineWidth: 1.0,
|
|
546
|
-
lineCap: 'square',
|
|
547
|
-
lineJoin: 'miter',
|
|
548
|
-
strokeStyle: 'black',
|
|
549
|
-
fillStyle: 'rgba(255, 255, 255, 0.3)'
|
|
550
|
-
},
|
|
551
|
-
textStyle: {
|
|
552
|
-
font: '12px sans-serif',
|
|
553
|
-
lineWidth: 1.0,
|
|
554
|
-
lineCap: 'round',
|
|
555
|
-
lineJoin: 'round',
|
|
556
|
-
fillStyle: 'black'
|
|
557
|
-
}
|
|
558
|
-
};
|
package/dist/tilecache.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
class TileCache {
|
|
1
|
+
import { existsSync, mkdirSync, readFile, writeFile } from 'fs';
|
|
2
|
+
import { join as pathJoin } from 'path';
|
|
3
|
+
import { promisify } from 'util';
|
|
4
|
+
export class TileCache {
|
|
5
|
+
static fnTemplate = '{z}_{x}_{y}.png';
|
|
6
|
+
path;
|
|
8
7
|
constructor(path) {
|
|
9
8
|
this.path = path;
|
|
10
|
-
|
|
9
|
+
mkdirSync(path, { recursive: true });
|
|
11
10
|
}
|
|
12
11
|
async pass(data, generator) {
|
|
13
12
|
let buf = await this.read(data);
|
|
@@ -19,20 +18,18 @@ class TileCache {
|
|
|
19
18
|
}
|
|
20
19
|
async read(data) {
|
|
21
20
|
const p = this.tilePath(data);
|
|
22
|
-
const r =
|
|
21
|
+
const r = existsSync(p) ? await promisify(readFile)(p, { encoding: 'utf-8' }) : undefined;
|
|
23
22
|
return r;
|
|
24
23
|
}
|
|
25
24
|
async write(data, src) {
|
|
26
25
|
const p = this.tilePath(data);
|
|
27
|
-
await
|
|
26
|
+
await promisify(writeFile)(p, src, { encoding: 'utf-8' });
|
|
28
27
|
}
|
|
29
28
|
tilePath(data) {
|
|
30
29
|
const fn = TileCache.fnTemplate
|
|
31
30
|
.replace('{z}', data.z.toString())
|
|
32
31
|
.replace('{x}', data.x.toString())
|
|
33
32
|
.replace('{y}', data.y.toString());
|
|
34
|
-
return (
|
|
33
|
+
return pathJoin(this.path, fn);
|
|
35
34
|
}
|
|
36
35
|
}
|
|
37
|
-
exports.TileCache = TileCache;
|
|
38
|
-
TileCache.fnTemplate = '{z}_{x}_{y}.png';
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tinkatinka/static-map",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Render static map images",
|
|
5
5
|
"author": "Florian Gmeiner <florian@tinkatinka.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"main": "dist/index.js",
|
|
8
9
|
"types": "dist/index.d.ts",
|
|
9
10
|
"files": [
|
|
@@ -26,7 +27,7 @@
|
|
|
26
27
|
"build": "tsc -p tsconfig.build.json",
|
|
27
28
|
"clean": "rm -rf dist/*",
|
|
28
29
|
"lint": "eslint . --ext .ts,.tsx,.js,.jsx",
|
|
29
|
-
"test": "
|
|
30
|
+
"test": "tsc -p tsconfig.test.json && node --experimental-vm-modules --no-warnings --test --test-reporter=spec build/tests/"
|
|
30
31
|
},
|
|
31
32
|
"browser": {
|
|
32
33
|
"node-fetch": false
|
|
@@ -34,24 +35,18 @@
|
|
|
34
35
|
"dependencies": {
|
|
35
36
|
"canvas": "^2.8.0",
|
|
36
37
|
"cross-fetch": "^4.0.0",
|
|
37
|
-
"lodash
|
|
38
|
+
"lodash-es": "^4.17.21"
|
|
38
39
|
},
|
|
39
40
|
"devDependencies": {
|
|
40
|
-
"@types/
|
|
41
|
-
"@types/jest-image-snapshot": "^6.1.0",
|
|
42
|
-
"@types/lodash.merge": "^4.6.6",
|
|
41
|
+
"@types/lodash-es": "^4.17.12",
|
|
43
42
|
"@types/node": "^20.8.9",
|
|
44
43
|
"@typescript-eslint/eslint-plugin": "^6.9.0",
|
|
45
44
|
"eslint": "^8.0.1",
|
|
46
45
|
"eslint-plugin-import": "^2.23.4",
|
|
47
|
-
"jest": "^29.5.0",
|
|
48
|
-
"jest-image-snapshot": "^6.1.0",
|
|
49
|
-
"ts-jest": "^29.0.5",
|
|
50
46
|
"type-fest": "^4.6.0",
|
|
51
47
|
"typescript": "^5.2.2"
|
|
52
48
|
},
|
|
53
49
|
"peerDependencies": {
|
|
54
|
-
"canvas": "^2.8.0"
|
|
55
|
-
"lodash.merge": "^4.6.2"
|
|
50
|
+
"canvas": "^2.8.0"
|
|
56
51
|
}
|
|
57
52
|
}
|