@tinkatinka/static-map 1.4.3 → 1.5.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 CHANGED
@@ -73,10 +73,10 @@ Options:
73
73
 
74
74
  ```ts
75
75
  interface StaticMapLineOptions {
76
- strokeStyle: string; // css stroke style [default: 'black']
77
- lineWidth: number; // line width in pixels [default: 1.0]
78
- lineCap: 'butt' | 'round' | 'square' // [default: 'round']
79
- lineJoin: 'bevel' | 'round' | 'miter' // [default: 'round']
76
+ strokeStyle: string; // css stroke style [default: 'black']
77
+ lineWidth: number; // line width in pixels [default: 1.0]
78
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
79
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
80
80
  }
81
81
  ```
82
82
 
@@ -123,6 +123,39 @@ interface StaticMapTextOptions {
123
123
  .addImage(src: string, bounds: LatLngBounds);
124
124
  ```
125
125
 
126
+ #### Scales
127
+ ```ts
128
+ .addScale(options?: PartialDeep<StaticMapScaleOptions>);
129
+ ```
130
+
131
+ Options:
132
+
133
+ ```ts
134
+ export interface StaticMapScaleOptions {
135
+ units: 'metric' | 'imperial'; // [default: 'metric']
136
+ position: '(bottom|top)(left|right)'; // [default: 'bottomleft']
137
+ height: number; // in pixels [default: 16]
138
+ maxWidth: number; // percent [default: 20]
139
+ mx: number; // margin x in percent [default: 4]
140
+ my: number; // margin y in percent [default: 4]
141
+ boxStyle: { // drawing style of the box
142
+ lineWidth: number; // line width in pixels [default: 1.0]
143
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'square']
144
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'miter']
145
+ strokeStyle: string; // css stroke style [default: 'black']
146
+ fillStyle: string; // css fill style [default: 'rgba(255, 255, 255, 0.3)']
147
+ };
148
+ textStyle: { // drawing style of the label
149
+ font: string; // [default: '12px sans-serif']
150
+ lineWidth: number; // line width in pixels [default: 1.0]
151
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'square']
152
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'miter']
153
+ strokeStyle: string; // css stroke style [default: 'black']
154
+ fillStyle: string; // css fill style [default: 'rgba(255, 255, 255, 0.3)']
155
+ };
156
+ }
157
+ ```
158
+
126
159
  ### Output
127
160
  Can be obtained via async functions
128
161
 
package/dist/base64img.js CHANGED
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const cross_fetch_1 = require("cross-fetch");
4
4
  const base64img = async (url, mimetype = 'image/png', userAgent = 'StaticMap/1.0') => {
5
- const response = await cross_fetch_1.fetch(url, {
5
+ const response = await (0, cross_fetch_1.fetch)(url, {
6
6
  headers: {
7
7
  'User-Agent': userAgent
8
8
  }
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  import Canvas from 'canvas';
3
- declare type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
3
+ import type { SetOptional, PartialDeep } from 'type-fest';
4
4
  export interface LatLng {
5
5
  lat: number;
6
6
  lng: number;
@@ -18,8 +18,8 @@ export interface StrokeOptions {
18
18
  export interface FillOptions {
19
19
  fillStyle: string | CanvasGradient | CanvasPattern;
20
20
  }
21
- export declare type StrokeFillOptions = Optional<StrokeOptions, 'strokeStyle'> & Optional<FillOptions, 'fillStyle'>;
22
- export declare type StaticMapOverlay = StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapText;
21
+ export declare type StrokeFillOptions = SetOptional<StrokeOptions, 'strokeStyle'> & SetOptional<FillOptions, 'fillStyle'>;
22
+ export declare type StaticMapOverlay = StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapText | StaticMapScale;
23
23
  export interface StaticMapImage {
24
24
  src: string;
25
25
  bounds: LatLngBounds;
@@ -46,6 +46,23 @@ export interface StaticMapTextOptions extends CanvasTextDrawingStyles, StrokeFil
46
46
  py: number;
47
47
  maxWidth?: number;
48
48
  }
49
+ export declare type LengthUnits = 'metric' | 'imperial';
50
+ export declare type LayoutPosition = 'topleft' | 'topright' | 'bottomleft' | 'bottomright';
51
+ export interface StaticMapScale {
52
+ options: StaticMapScaleOptions;
53
+ }
54
+ export interface StaticMapScaleOptions {
55
+ units: LengthUnits;
56
+ position: LayoutPosition;
57
+ height: number;
58
+ maxWidth: number;
59
+ mx: number;
60
+ my: number;
61
+ boxStyle: StrokeFillOptions;
62
+ textStyle: StrokeFillOptions & {
63
+ font: string;
64
+ };
65
+ }
49
66
  export interface StaticMapOptions {
50
67
  width: number;
51
68
  height: number;
@@ -71,15 +88,16 @@ export declare class StaticMap {
71
88
  private static defaultLineOptions;
72
89
  private static defaultCircleOptions;
73
90
  private static defaultTextOptions;
91
+ private static defaultScaleOptions;
74
92
  readonly options: StaticMapOptions;
93
+ private cache?;
94
+ private overlays;
95
+ constructor(options?: Partial<StaticMapOptions>);
75
96
  get extent(): LatLngBounds | undefined;
76
97
  get paddingLeft(): number;
77
98
  get paddingRight(): number;
78
99
  get paddingTop(): number;
79
100
  get paddingBottom(): number;
80
- private cache?;
81
- private overlays;
82
- constructor(options?: Partial<StaticMapOptions>);
83
101
  renderToCanvas(): Promise<Canvas.Canvas>;
84
102
  renderToDataURL(): Promise<string>;
85
103
  renderToBuffer(): Promise<Buffer>;
@@ -89,6 +107,7 @@ export declare class StaticMap {
89
107
  addLine(src: LatLng, dst: LatLng, options?: Partial<StaticMapLineOptions>): StaticMap;
90
108
  addCircle(center: LatLng, options?: Partial<StaticMapCircleOptions>): StaticMap;
91
109
  addText(text: string, anchor: LatLng, options?: Partial<StaticMapTextOptions>): StaticMap;
110
+ addScale(options?: PartialDeep<StaticMapScaleOptions>): StaticMap;
92
111
  private boundsCenter;
93
112
  private lngToX;
94
113
  private latToY;
@@ -101,6 +120,11 @@ export declare class StaticMap {
101
120
  private latlngToPxPy;
102
121
  private xToLng;
103
122
  private yToLat;
123
+ private latToRad;
124
+ private lngToRad;
125
+ private ang;
126
+ private distance;
127
+ private scaleDistance;
104
128
  private overlayType;
105
129
  private overlayExtent;
106
130
  private calculateExtent;
@@ -108,4 +132,3 @@ export declare class StaticMap {
108
132
  private loadTile;
109
133
  private createCanvas;
110
134
  }
111
- export {};
package/dist/index.js CHANGED
@@ -8,9 +8,12 @@ const canvas_1 = __importDefault(require("canvas"));
8
8
  const lodash_merge_1 = __importDefault(require("lodash.merge"));
9
9
  const tilecache_1 = require("./tilecache");
10
10
  const base64img_1 = __importDefault(require("./base64img"));
11
+ const EARTH_RADIUS_MEAN = 6371008.8;
12
+ const FEET_PER_METER = 3.2808399;
13
+ const FEET_PER_MILE = 5280;
11
14
  class StaticMap {
12
15
  constructor(options) {
13
- this.options = lodash_merge_1.default({}, StaticMap.defaultOptions, options);
16
+ this.options = (0, lodash_merge_1.default)({}, StaticMap.defaultOptions, options);
14
17
  this.cache = this.options.tileCache ? new tilecache_1.TileCache(this.options.tileCache) : undefined;
15
18
  this.overlays = [];
16
19
  }
@@ -51,7 +54,7 @@ class StaticMap {
51
54
  if (points.length >= 2) {
52
55
  return this.addOverlay({
53
56
  points: points,
54
- options: lodash_merge_1.default({}, StaticMap.defaultLineOptions, options)
57
+ options: (0, lodash_merge_1.default)({}, StaticMap.defaultLineOptions, options)
55
58
  });
56
59
  }
57
60
  return this;
@@ -62,14 +65,19 @@ class StaticMap {
62
65
  addCircle(center, options) {
63
66
  return this.addOverlay({
64
67
  center: center,
65
- options: lodash_merge_1.default({}, StaticMap.defaultCircleOptions, options)
68
+ options: (0, lodash_merge_1.default)({}, StaticMap.defaultCircleOptions, options)
66
69
  });
67
70
  }
68
71
  addText(text, anchor, options) {
69
72
  return this.addOverlay({
70
73
  text: text,
71
74
  anchor: anchor,
72
- options: lodash_merge_1.default({}, StaticMap.defaultTextOptions, options)
75
+ options: (0, lodash_merge_1.default)({}, StaticMap.defaultTextOptions, options)
76
+ });
77
+ }
78
+ addScale(options) {
79
+ return this.addOverlay({
80
+ options: (0, lodash_merge_1.default)({}, StaticMap.defaultScaleOptions, options)
73
81
  });
74
82
  }
75
83
  boundsCenter(bounds) {
@@ -125,6 +133,32 @@ class StaticMap {
125
133
  yToLat(y, zoom) {
126
134
  return Math.atan(Math.sinh(Math.PI * (1 - 2 * y / Math.pow(2, zoom)))) / Math.PI * 180.0;
127
135
  }
136
+ latToRad(lat) {
137
+ return 2.0 * Math.PI * lat / 360.0;
138
+ }
139
+ lngToRad(lng) {
140
+ return 2.0 * Math.PI * lng / 360.0;
141
+ }
142
+ ang(from, to) {
143
+ const l1 = this.lngToRad(from.lng);
144
+ const p1 = this.latToRad(from.lat);
145
+ const l2 = this.lngToRad(to.lng);
146
+ const p2 = this.latToRad(to.lat);
147
+ const dl = Math.abs(l1 - l2);
148
+ const dp = Math.abs(p1 - p2);
149
+ const s1 = Math.sin(dp / 2.0);
150
+ const s2 = Math.sin(dl / 2.0);
151
+ return 2.0 * Math.asin(Math.sqrt(s1 * s1 + Math.cos(p1) * Math.cos(p2) * s2 * s2));
152
+ }
153
+ distance(from, to) {
154
+ return Math.abs(EARTH_RADIUS_MEAN * this.ang(from, to));
155
+ }
156
+ scaleDistance(value) {
157
+ const p = Math.pow(10, Math.floor(value).toString().length - 1);
158
+ let d = value / p;
159
+ d = (d >= 10) ? 10 : (d >= 5) ? 5 : (d >= 3) ? 3 : (d >= 2) ? 2 : 1;
160
+ return p * d;
161
+ }
128
162
  overlayType(overlay) {
129
163
  if (overlay.bounds !== undefined) {
130
164
  return 'image';
@@ -135,9 +169,12 @@ class StaticMap {
135
169
  else if (overlay.anchor !== undefined) {
136
170
  return 'text';
137
171
  }
138
- else {
172
+ else if (overlay.points !== undefined) {
139
173
  return 'line';
140
174
  }
175
+ else {
176
+ return 'scale';
177
+ }
141
178
  }
142
179
  overlayExtent(overlay) {
143
180
  const type = this.overlayType(overlay);
@@ -217,7 +254,7 @@ class StaticMap {
217
254
  if (this.cache !== undefined) {
218
255
  const td = { z: zoom, x: tileX, y: tileY };
219
256
  try {
220
- const csrc = await this.cache.pass(td, () => base64img_1.default(src, 'image/png', this.options.userAgent));
257
+ const csrc = await this.cache.pass(td, () => (0, base64img_1.default)(src, 'image/png', this.options.userAgent));
221
258
  if (csrc !== undefined) {
222
259
  src = csrc;
223
260
  }
@@ -228,7 +265,7 @@ class StaticMap {
228
265
  }
229
266
  }
230
267
  else {
231
- src = await base64img_1.default(src, 'image/png', this.options.userAgent);
268
+ src = await (0, base64img_1.default)(src, 'image/png', this.options.userAgent);
232
269
  }
233
270
  const image = await canvas_1.default.loadImage(src);
234
271
  return image;
@@ -313,6 +350,7 @@ class StaticMap {
313
350
  const linedata = overlay;
314
351
  if (linedata.points.length > 1) {
315
352
  const p0 = this.latlngToPxPy(linedata.points[0], zoom, centerXY, scale);
353
+ ctx.beginPath();
316
354
  ctx.moveTo(p0.x, p0.y);
317
355
  for (const p of linedata.points.slice(1)) {
318
356
  const pxy = this.latlngToPxPy(p, zoom, centerXY, scale);
@@ -360,6 +398,90 @@ class StaticMap {
360
398
  }
361
399
  break;
362
400
  }
401
+ case 'scale': {
402
+ const scaledata = overlay;
403
+ const midLeft = { lat: center.lat, lng: extent.min.lng };
404
+ const midRight = { lat: center.lat, lng: extent.max.lng };
405
+ const mapWidth = this.distance(midLeft, midRight);
406
+ const maxDist = mapWidth * scaledata.options.maxWidth / 100;
407
+ let label;
408
+ let ratio;
409
+ if (scaledata.options.units === 'metric') {
410
+ const scaleLen = this.scaleDistance(maxDist);
411
+ label = (scaleLen > 1000) ? `${scaleLen / 1000} km` : `${scaleLen} m`;
412
+ ratio = scaleLen / maxDist;
413
+ }
414
+ else {
415
+ const maxFeet = maxDist * FEET_PER_METER;
416
+ if (maxFeet > FEET_PER_MILE) {
417
+ const maxMiles = maxFeet / FEET_PER_MILE;
418
+ const scaleLen = this.scaleDistance(maxMiles);
419
+ label = `${scaleLen} mi`;
420
+ ratio = scaleLen / maxMiles;
421
+ }
422
+ else {
423
+ const scaleLen = this.scaleDistance(maxFeet);
424
+ label = `${scaleLen} ft`;
425
+ ratio = scaleLen / maxFeet;
426
+ }
427
+ }
428
+ const dx = width * ratio * scaledata.options.maxWidth / 100;
429
+ let p0 = { x: 0, y: 0 };
430
+ const mpx = Math.max(scaledata.options.mx * width / 100, scaledata.options.my * height / 100);
431
+ const margin = { x: mpx, y: mpx };
432
+ switch (scaledata.options.position) {
433
+ case 'bottomleft': {
434
+ p0 = { x: margin.x, y: height - margin.y - scaledata.options.height };
435
+ break;
436
+ }
437
+ case 'bottomright': {
438
+ p0 = { x: width - dx - margin.x, y: height - margin.y - scaledata.options.height };
439
+ break;
440
+ }
441
+ case 'topleft': {
442
+ p0 = { x: margin.x, y: margin.y };
443
+ break;
444
+ }
445
+ case 'topright': {
446
+ p0 = { x: width - dx - margin.x, y: margin.y };
447
+ break;
448
+ }
449
+ default: break;
450
+ }
451
+ const p1 = { x: p0.x + dx, y: p0.y + scaledata.options.height };
452
+ const boxStyle = scaledata.options.boxStyle;
453
+ if (boxStyle.fillStyle !== undefined) {
454
+ ctx.fillStyle = boxStyle.fillStyle;
455
+ ctx.fillRect(p0.x, p0.y, Math.abs(p1.x - p0.x), Math.abs(p1.y - p0.y));
456
+ }
457
+ if (boxStyle.strokeStyle !== undefined) {
458
+ ctx.beginPath();
459
+ ctx.moveTo(p0.x, p0.y);
460
+ ctx.lineTo(p0.x, p1.y);
461
+ ctx.lineTo(p1.x, p1.y);
462
+ ctx.lineTo(p1.x, p0.y);
463
+ ctx.strokeStyle = boxStyle.strokeStyle;
464
+ ctx.lineWidth = boxStyle.lineWidth;
465
+ ctx.lineCap = boxStyle.lineCap;
466
+ ctx.lineJoin = boxStyle.lineJoin;
467
+ ctx.stroke();
468
+ }
469
+ const anchor = { x: 0.5 * (p0.x + p1.x), y: p1.y - 3 };
470
+ const textStyle = scaledata.options.textStyle;
471
+ ctx.textAlign = 'center';
472
+ ctx.textBaseline = 'bottom';
473
+ ctx.direction = 'ltr';
474
+ ctx.font = textStyle.font;
475
+ if (textStyle.fillStyle) {
476
+ ctx.fillStyle = textStyle.fillStyle;
477
+ ctx.fillText(label, anchor.x, anchor.y, p1.x - p0.x);
478
+ }
479
+ if (textStyle.strokeStyle) {
480
+ ctx.strokeStyle = textStyle.strokeStyle;
481
+ ctx.strokeText(label, anchor.x, anchor.y, p1.x - p0.x);
482
+ }
483
+ break;
484
+ }
363
485
  default:
364
486
  throw new Error(`Unknown overlay type "${type}"`);
365
487
  }
@@ -386,7 +508,7 @@ StaticMap.defaultLineOptions = {
386
508
  };
387
509
  StaticMap.defaultCircleOptions = {
388
510
  radius: 5.0,
389
- strokeStyle: 'rgba(0.5, 0.5, 0.5, 0.5)',
511
+ strokeStyle: 'rgba(128, 128, 128, 0.5)',
390
512
  lineWidth: 1.0,
391
513
  lineCap: 'round',
392
514
  lineJoin: 'round',
@@ -404,3 +526,25 @@ StaticMap.defaultTextOptions = {
404
526
  lineJoin: 'round',
405
527
  fillStyle: 'black'
406
528
  };
529
+ StaticMap.defaultScaleOptions = {
530
+ units: 'metric',
531
+ position: 'bottomleft',
532
+ mx: 4,
533
+ my: 4,
534
+ height: 16,
535
+ maxWidth: 20,
536
+ boxStyle: {
537
+ lineWidth: 1.0,
538
+ lineCap: 'square',
539
+ lineJoin: 'miter',
540
+ strokeStyle: 'black',
541
+ fillStyle: 'rgba(255, 255, 255, 0.3)'
542
+ },
543
+ textStyle: {
544
+ font: '12px sans-serif',
545
+ lineWidth: 1.0,
546
+ lineCap: 'round',
547
+ lineJoin: 'round',
548
+ fillStyle: 'black'
549
+ }
550
+ };
package/dist/tilecache.js CHANGED
@@ -7,7 +7,7 @@ const util_1 = require("util");
7
7
  class TileCache {
8
8
  constructor(path) {
9
9
  this.path = path;
10
- fs_1.mkdirSync(path, { recursive: true });
10
+ (0, fs_1.mkdirSync)(path, { recursive: true });
11
11
  }
12
12
  async pass(data, generator) {
13
13
  let buf = await this.read(data);
@@ -19,19 +19,19 @@ class TileCache {
19
19
  }
20
20
  async read(data) {
21
21
  const p = this.tilePath(data);
22
- const r = fs_1.existsSync(p) ? await util_1.promisify(fs_1.readFile)(p, { encoding: 'utf-8' }) : undefined;
22
+ const r = (0, fs_1.existsSync)(p) ? await (0, util_1.promisify)(fs_1.readFile)(p, { encoding: 'utf-8' }) : undefined;
23
23
  return r;
24
24
  }
25
25
  async write(data, src) {
26
26
  const p = this.tilePath(data);
27
- await util_1.promisify(fs_1.writeFile)(p, src, { encoding: 'utf-8' });
27
+ await (0, util_1.promisify)(fs_1.writeFile)(p, src, { encoding: 'utf-8' });
28
28
  }
29
29
  tilePath(data) {
30
30
  const fn = TileCache.fnTemplate
31
31
  .replace('{z}', data.z.toString())
32
32
  .replace('{x}', data.x.toString())
33
33
  .replace('{y}', data.y.toString());
34
- return path_1.join(this.path, fn);
34
+ return (0, path_1.join)(this.path, fn);
35
35
  }
36
36
  }
37
37
  exports.TileCache = TileCache;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "1.4.3",
3
+ "version": "1.5.0",
4
4
  "description": "Render static map images",
5
5
  "author": "Florian Gmeiner <florian@tinkatinka.com>",
6
6
  "license": "MIT",
@@ -31,19 +31,23 @@
31
31
  "browser": {
32
32
  "node-fetch": false
33
33
  },
34
+ "dependencies": {
35
+ "canvas": "^2.8.0",
36
+ "cross-fetch": "^3.1.4",
37
+ "lodash.merge": "^4.6.2"
38
+ },
34
39
  "devDependencies": {
35
40
  "@types/jest": "^27.0.1",
36
41
  "@types/jest-image-snapshot": "^4.3.0",
37
42
  "@types/lodash.merge": "^4.6.6",
38
- "@types/node": "^16.0.0",
39
- "@typescript-eslint/eslint-plugin": "^4.28.1",
40
- "canvas": "^2.8.0",
41
- "cross-fetch": "^3.1.4",
43
+ "@types/node": "^17.0.13",
44
+ "@typescript-eslint/eslint-plugin": "^5.1.0",
45
+ "eslint": "^8.0.1",
42
46
  "eslint-plugin-import": "^2.23.4",
43
47
  "jest": "^27.0.6",
44
48
  "jest-image-snapshot": "^4.5.1",
45
- "lodash.merge": "^4.6.2",
46
49
  "ts-jest": "^27.0.3",
50
+ "type-fest": "^2.12.2",
47
51
  "typescript": "^4.3.4"
48
52
  },
49
53
  "peerDependencies": {