@tinkatinka/static-map 1.4.5 → 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/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,6 +88,7 @@ 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;
75
93
  private cache?;
76
94
  private overlays;
@@ -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,9 @@ 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
16
  this.options = (0, lodash_merge_1.default)({}, StaticMap.defaultOptions, options);
@@ -72,6 +75,11 @@ class StaticMap {
72
75
  options: (0, lodash_merge_1.default)({}, StaticMap.defaultTextOptions, options)
73
76
  });
74
77
  }
78
+ addScale(options) {
79
+ return this.addOverlay({
80
+ options: (0, lodash_merge_1.default)({}, StaticMap.defaultScaleOptions, options)
81
+ });
82
+ }
75
83
  boundsCenter(bounds) {
76
84
  return {
77
85
  lat: (bounds.max.lat + bounds.min.lat) / 2.0,
@@ -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);
@@ -361,6 +398,90 @@ class StaticMap {
361
398
  }
362
399
  break;
363
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
+ }
364
485
  default:
365
486
  throw new Error(`Unknown overlay type "${type}"`);
366
487
  }
@@ -387,7 +508,7 @@ StaticMap.defaultLineOptions = {
387
508
  };
388
509
  StaticMap.defaultCircleOptions = {
389
510
  radius: 5.0,
390
- strokeStyle: 'rgba(0.5, 0.5, 0.5, 0.5)',
511
+ strokeStyle: 'rgba(128, 128, 128, 0.5)',
391
512
  lineWidth: 1.0,
392
513
  lineCap: 'round',
393
514
  lineJoin: 'round',
@@ -405,3 +526,25 @@ StaticMap.defaultTextOptions = {
405
526
  lineJoin: 'round',
406
527
  fillStyle: 'black'
407
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "1.4.5",
3
+ "version": "1.5.0",
4
4
  "description": "Render static map images",
5
5
  "author": "Florian Gmeiner <florian@tinkatinka.com>",
6
6
  "license": "MIT",
@@ -40,13 +40,14 @@
40
40
  "@types/jest": "^27.0.1",
41
41
  "@types/jest-image-snapshot": "^4.3.0",
42
42
  "@types/lodash.merge": "^4.6.6",
43
- "@types/node": "^16.0.0",
43
+ "@types/node": "^17.0.13",
44
44
  "@typescript-eslint/eslint-plugin": "^5.1.0",
45
45
  "eslint": "^8.0.1",
46
46
  "eslint-plugin-import": "^2.23.4",
47
47
  "jest": "^27.0.6",
48
48
  "jest-image-snapshot": "^4.5.1",
49
49
  "ts-jest": "^27.0.3",
50
+ "type-fest": "^2.12.2",
50
51
  "typescript": "^4.3.4"
51
52
  },
52
53
  "peerDependencies": {