@tinkatinka/static-map 1.4.4 → 1.5.1

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
 
@@ -0,0 +1,6 @@
1
+ export declare const compactMap: <T, R>(array: T[], func: (value: T, idx: number) => R | undefined) => R[];
2
+ declare global {
3
+ interface Array<T> {
4
+ compactMap<R>(func: (value: T, idx: number) => R | undefined): Array<R>;
5
+ }
6
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.compactMap = void 0;
4
+ const isDefined = (value) => (value !== undefined);
5
+ const compactMap = (array, func) => array.map(func).filter(isDefined);
6
+ exports.compactMap = compactMap;
7
+ if (!Array.prototype.compactMap) {
8
+ Array.prototype.compactMap = function (func) {
9
+ return (0, exports.compactMap)(this, func);
10
+ };
11
+ }
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,6 +8,10 @@ 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 compactmap_1 = require("./compactmap");
12
+ const EARTH_RADIUS_MEAN = 6371008.8;
13
+ const FEET_PER_METER = 3.2808399;
14
+ const FEET_PER_MILE = 5280;
11
15
  class StaticMap {
12
16
  constructor(options) {
13
17
  this.options = (0, lodash_merge_1.default)({}, StaticMap.defaultOptions, options);
@@ -72,6 +76,11 @@ class StaticMap {
72
76
  options: (0, lodash_merge_1.default)({}, StaticMap.defaultTextOptions, options)
73
77
  });
74
78
  }
79
+ addScale(options) {
80
+ return this.addOverlay({
81
+ options: (0, lodash_merge_1.default)({}, StaticMap.defaultScaleOptions, options)
82
+ });
83
+ }
75
84
  boundsCenter(bounds) {
76
85
  return {
77
86
  lat: (bounds.max.lat + bounds.min.lat) / 2.0,
@@ -125,6 +134,32 @@ class StaticMap {
125
134
  yToLat(y, zoom) {
126
135
  return Math.atan(Math.sinh(Math.PI * (1 - 2 * y / Math.pow(2, zoom)))) / Math.PI * 180.0;
127
136
  }
137
+ latToRad(lat) {
138
+ return 2.0 * Math.PI * lat / 360.0;
139
+ }
140
+ lngToRad(lng) {
141
+ return 2.0 * Math.PI * lng / 360.0;
142
+ }
143
+ ang(from, to) {
144
+ const l1 = this.lngToRad(from.lng);
145
+ const p1 = this.latToRad(from.lat);
146
+ const l2 = this.lngToRad(to.lng);
147
+ const p2 = this.latToRad(to.lat);
148
+ const dl = Math.abs(l1 - l2);
149
+ const dp = Math.abs(p1 - p2);
150
+ const s1 = Math.sin(dp / 2.0);
151
+ const s2 = Math.sin(dl / 2.0);
152
+ return 2.0 * Math.asin(Math.sqrt(s1 * s1 + Math.cos(p1) * Math.cos(p2) * s2 * s2));
153
+ }
154
+ distance(from, to) {
155
+ return Math.abs(EARTH_RADIUS_MEAN * this.ang(from, to));
156
+ }
157
+ scaleDistance(value) {
158
+ const p = Math.pow(10, Math.floor(value).toString().length - 1);
159
+ let d = value / p;
160
+ d = (d >= 10) ? 10 : (d >= 5) ? 5 : (d >= 3) ? 3 : (d >= 2) ? 2 : 1;
161
+ return p * d;
162
+ }
128
163
  overlayType(overlay) {
129
164
  if (overlay.bounds !== undefined) {
130
165
  return 'image';
@@ -135,9 +170,12 @@ class StaticMap {
135
170
  else if (overlay.anchor !== undefined) {
136
171
  return 'text';
137
172
  }
138
- else {
173
+ else if (overlay.points !== undefined) {
139
174
  return 'line';
140
175
  }
176
+ else {
177
+ return 'scale';
178
+ }
141
179
  }
142
180
  overlayExtent(overlay) {
143
181
  const type = this.overlayType(overlay);
@@ -167,6 +205,9 @@ class StaticMap {
167
205
  const a = overlay.anchor;
168
206
  return { min: a, max: a };
169
207
  }
208
+ case 'scale': {
209
+ return undefined;
210
+ }
170
211
  default:
171
212
  throw new Error(`Unknown overlay type "${type}"`);
172
213
  }
@@ -190,7 +231,7 @@ class StaticMap {
190
231
  lng: max(arr.map(b => b.max.lng))
191
232
  }
192
233
  });
193
- return union(this.overlays.map(this.overlayExtent.bind(this)));
234
+ return union((0, compactmap_1.compactMap)(this.overlays, this.overlayExtent.bind(this)));
194
235
  }
195
236
  calculateZoom(extent) {
196
237
  let z = this.options.tileMaxZoom + 1;
@@ -361,6 +402,93 @@ class StaticMap {
361
402
  }
362
403
  break;
363
404
  }
405
+ case 'scale': {
406
+ const scaledata = overlay;
407
+ const midLeft = { lat: center.lat, lng: extent.min.lng };
408
+ const midRight = { lat: center.lat, lng: extent.max.lng };
409
+ const mapWidth = this.distance(midLeft, midRight);
410
+ const maxDist = mapWidth * scaledata.options.maxWidth / 100;
411
+ let label;
412
+ let ratio;
413
+ if (scaledata.options.units === 'metric') {
414
+ const scaleLen = this.scaleDistance(maxDist);
415
+ label = (scaleLen > 1000) ? `${scaleLen / 1000} km` : `${scaleLen} m`;
416
+ ratio = scaleLen / maxDist;
417
+ }
418
+ else {
419
+ const maxFeet = maxDist * FEET_PER_METER;
420
+ if (maxFeet > FEET_PER_MILE) {
421
+ const maxMiles = maxFeet / FEET_PER_MILE;
422
+ const scaleLen = this.scaleDistance(maxMiles);
423
+ label = `${scaleLen} mi`;
424
+ ratio = scaleLen / maxMiles;
425
+ }
426
+ else {
427
+ const scaleLen = this.scaleDistance(maxFeet);
428
+ label = `${scaleLen} ft`;
429
+ ratio = scaleLen / maxFeet;
430
+ }
431
+ }
432
+ const midLeftP = this.latlngToPxPy(midLeft, zoom, centerXY, scale);
433
+ const midRightP = this.latlngToPxPy(midRight, zoom, centerXY, scale);
434
+ const widthP = Math.abs(midRightP.x - midLeftP.x);
435
+ const dx = widthP * ratio * scaledata.options.maxWidth / 100;
436
+ let p0 = { x: 0, y: 0 };
437
+ const mpx = Math.max(scaledata.options.mx * width / 100, scaledata.options.my * height / 100);
438
+ const margin = { x: mpx, y: mpx };
439
+ switch (scaledata.options.position) {
440
+ case 'bottomleft': {
441
+ p0 = { x: margin.x, y: height - margin.y - scaledata.options.height };
442
+ break;
443
+ }
444
+ case 'bottomright': {
445
+ p0 = { x: width - dx - margin.x, y: height - margin.y - scaledata.options.height };
446
+ break;
447
+ }
448
+ case 'topleft': {
449
+ p0 = { x: margin.x, y: margin.y };
450
+ break;
451
+ }
452
+ case 'topright': {
453
+ p0 = { x: width - dx - margin.x, y: margin.y };
454
+ break;
455
+ }
456
+ default: break;
457
+ }
458
+ const p1 = { x: p0.x + dx, y: p0.y + scaledata.options.height };
459
+ const boxStyle = scaledata.options.boxStyle;
460
+ if (boxStyle.fillStyle !== undefined) {
461
+ ctx.fillStyle = boxStyle.fillStyle;
462
+ ctx.fillRect(p0.x, p0.y, Math.abs(p1.x - p0.x), Math.abs(p1.y - p0.y));
463
+ }
464
+ if (boxStyle.strokeStyle !== undefined) {
465
+ ctx.beginPath();
466
+ ctx.moveTo(p0.x, p0.y);
467
+ ctx.lineTo(p0.x, p1.y);
468
+ ctx.lineTo(p1.x, p1.y);
469
+ ctx.lineTo(p1.x, p0.y);
470
+ ctx.strokeStyle = boxStyle.strokeStyle;
471
+ ctx.lineWidth = boxStyle.lineWidth;
472
+ ctx.lineCap = boxStyle.lineCap;
473
+ ctx.lineJoin = boxStyle.lineJoin;
474
+ ctx.stroke();
475
+ }
476
+ const anchor = { x: 0.5 * (p0.x + p1.x), y: p1.y - 3 };
477
+ const textStyle = scaledata.options.textStyle;
478
+ ctx.textAlign = 'center';
479
+ ctx.textBaseline = 'bottom';
480
+ ctx.direction = 'ltr';
481
+ ctx.font = textStyle.font;
482
+ if (textStyle.fillStyle) {
483
+ ctx.fillStyle = textStyle.fillStyle;
484
+ ctx.fillText(label, anchor.x, anchor.y, p1.x - p0.x);
485
+ }
486
+ if (textStyle.strokeStyle) {
487
+ ctx.strokeStyle = textStyle.strokeStyle;
488
+ ctx.strokeText(label, anchor.x, anchor.y, p1.x - p0.x);
489
+ }
490
+ break;
491
+ }
364
492
  default:
365
493
  throw new Error(`Unknown overlay type "${type}"`);
366
494
  }
@@ -387,7 +515,7 @@ StaticMap.defaultLineOptions = {
387
515
  };
388
516
  StaticMap.defaultCircleOptions = {
389
517
  radius: 5.0,
390
- strokeStyle: 'rgba(0.5, 0.5, 0.5, 0.5)',
518
+ strokeStyle: 'rgba(128, 128, 128, 0.5)',
391
519
  lineWidth: 1.0,
392
520
  lineCap: 'round',
393
521
  lineJoin: 'round',
@@ -405,3 +533,25 @@ StaticMap.defaultTextOptions = {
405
533
  lineJoin: 'round',
406
534
  fillStyle: 'black'
407
535
  };
536
+ StaticMap.defaultScaleOptions = {
537
+ units: 'metric',
538
+ position: 'bottomleft',
539
+ mx: 4,
540
+ my: 4,
541
+ height: 16,
542
+ maxWidth: 20,
543
+ boxStyle: {
544
+ lineWidth: 1.0,
545
+ lineCap: 'square',
546
+ lineJoin: 'miter',
547
+ strokeStyle: 'black',
548
+ fillStyle: 'rgba(255, 255, 255, 0.3)'
549
+ },
550
+ textStyle: {
551
+ font: '12px sans-serif',
552
+ lineWidth: 1.0,
553
+ lineCap: 'round',
554
+ lineJoin: 'round',
555
+ fillStyle: 'black'
556
+ }
557
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "1.4.4",
3
+ "version": "1.5.1",
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
- "devDependencies": {
35
- "@types/jest": "^27.0.1",
36
- "@types/jest-image-snapshot": "^4.3.0",
37
- "@types/lodash.merge": "^4.6.6",
38
- "@types/node": "^16.0.0",
39
- "@typescript-eslint/eslint-plugin": "^4.28.1",
34
+ "dependencies": {
40
35
  "canvas": "^2.8.0",
41
36
  "cross-fetch": "^3.1.4",
37
+ "lodash.merge": "^4.6.2"
38
+ },
39
+ "devDependencies": {
40
+ "@types/jest": "^28.0.0",
41
+ "@types/jest-image-snapshot": "^5.1.0",
42
+ "@types/lodash.merge": "^4.6.6",
43
+ "@types/node": "^18.0.0",
44
+ "@typescript-eslint/eslint-plugin": "^5.1.0",
45
+ "eslint": "^8.0.1",
42
46
  "eslint-plugin-import": "^2.23.4",
43
- "jest": "^27.0.6",
44
- "jest-image-snapshot": "^4.5.1",
45
- "lodash.merge": "^4.6.2",
46
- "ts-jest": "^27.0.3",
47
+ "jest": "^28.0.0",
48
+ "jest-image-snapshot": "^5.1.0",
49
+ "ts-jest": "^28.0.0",
50
+ "type-fest": "^2.12.2",
47
51
  "typescript": "^4.3.4"
48
52
  },
49
53
  "peerDependencies": {