@tinkatinka/static-map 3.0.6 → 3.1.2

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
@@ -19,25 +19,26 @@ const map = new StaticMap(...);
19
19
  The constructor can be called with an optional `StaticMapOptions` object, overriding the defaults:
20
20
 
21
21
  ```ts
22
- {
23
- width: number; // width of the image in pixels [default: 512]
24
- height: number; // height of the image in pixels [default: 512]
25
- padding: number; // padding in pixels [default: 0]
26
- paddingX?: number; // horizontal padding (overrides `padding`)
27
- paddingY?: number; // vertical padding (overrides `paddings`)
28
- paddingLeft?: number; // left padding (overrides `paddingX`)
29
- paddingRight?: number; // right padding (overrides `paddingX`)
30
- paddingTop?: number; // top padding (overrides `paddingY`)
22
+ interface StaticMapOptions {
23
+ width: number; // width of the image in pixels [default: 512]
24
+ height: number; // height of the image in pixels [default: 512]
25
+ padding: number; // padding in pixels [default: 0]
26
+ paddingX?: number; // horizontal padding (overrides `padding`)
27
+ paddingY?: number; // vertical padding (overrides `paddings`)
28
+ paddingLeft?: number; // left padding (overrides `paddingX`)
29
+ paddingRight?: number; // right padding (overrides `paddingX`)
30
+ paddingTop?: number; // top padding (overrides `paddingY`)
31
31
  paddingBottom?: number; // bottom padding (overrides `paddingY`)
32
- extent?: LatLngBounds; // extent of the map [default: `undefined`]
33
- scaling: boolean; // scale image to match size and padding exactly [default: `true`]
34
- tileURL: string; // template string for tile URL [default: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png']
35
- tileSize: number; // size of tiles [default: 256]
36
- tileMaxZoom: number; // maximum zoom available for tiles [default: 20]
37
- tileCache?: string; // path to a cache directory for tiles [default: `undefined`]
38
- tileTimeout?: number; // tile fetching timeout [ms] [default: 2000]
39
- backgroundColor?: string; // image background color (CSS string) [default: `undefined`]
40
- grayscale: boolean; // apply a grayscale filter to map tiles (not overlays) [default: `false`]
32
+ extent?: LatLngBounds; // extent of the map [default: `undefined`]
33
+ scaling: boolean; // scale image to match size and padding exactly [default: `true`]
34
+ tileURL: string; // template string for tile URL [default: 'https://tile.openstreetmap.org/{z}/{x}/{y}.png']
35
+ tileSize: number; // size of tiles [default: 256]
36
+ tileMaxZoom: number; // maximum zoom available for tiles [default: 20]
37
+ tileCache?: string; // path to a cache directory for tiles [default: `undefined`]
38
+ tileTimeout?: number; // tile fetching timeout [ms] [default: 2000]
39
+ userAgent?: string; // user agent header to be passed when loading tiles [default: 'StaticMap/3']
40
+ backgroundColor?: string; // image background color (CSS string) [default: `undefined`]
41
+ grayscale: boolean; // apply a grayscale filter to map tiles (not overlays) [default: `false`]
41
42
  }
42
43
  ```
43
44
  **Note**: Either an explicit `extent` of the map *or* at least one overlay (see below) **must** be defined,
@@ -47,22 +48,24 @@ otherwise the image generation will fail.
47
48
 
48
49
  #### LatLng
49
50
  ```ts
50
- {
51
- lat: number; // latitude
52
- lng: number; // longitude
51
+ interface LatLng {
52
+ lat: number; // latitude
53
+ lng: number; // longitude
53
54
  }
54
55
  ```
55
56
 
56
57
  #### LatLngBounds
57
58
  ```ts
58
- {
59
- min: LatLng; // minimum point
60
- max: LatLng; // maximum point
59
+ interface LatLngBounds {
60
+ min: LatLng; // minimum point
61
+ max: LatLng; // maximum point
61
62
  }
62
63
  ```
63
64
 
64
65
  ### Overlays
65
66
 
67
+ Various overlays can be added via the following functions (all are composable, i.e. returning the instance).
68
+
66
69
  #### Lines
67
70
  ```ts
68
71
  .addLine(src: LatLng, dst: LatLng, options?: Partial<StaticMapLineOptions>);
@@ -74,10 +77,10 @@ Options:
74
77
 
75
78
  ```ts
76
79
  interface StaticMapLineOptions {
77
- strokeStyle: string; // css stroke style [default: 'black']
78
- lineWidth: number; // line width in pixels [default: 1.0]
79
- lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
80
- lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
80
+ strokeStyle: string; // css stroke style [default: 'black']
81
+ lineWidth: number; // line width in pixels [default: 1.0]
82
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
83
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
81
84
  }
82
85
  ```
83
86
 
@@ -90,9 +93,9 @@ Options:
90
93
 
91
94
  ```ts
92
95
  interface StaticMapCircleOptions {
93
- radius: number; // in pixels [default: 5.0]
94
- strokeStyle?: string; // css stroke style or undefined for no stroke [default: 'black']
95
- lineWidth: number; // line width in pixels [default: 1.0]
96
+ radius: number; // in pixels [default: 5.0]
97
+ strokeStyle?: string; // css stroke style or undefined for no stroke [default: 'black']
98
+ lineWidth: number; // line width in pixels [default: 1.0]
96
99
  fillStyle?: string; // css fill style or undefined for no fill [default: 'rgba(0, 0, 0, 0.3)']
97
100
  }
98
101
  ```
@@ -106,10 +109,10 @@ Options:
106
109
 
107
110
  ```ts
108
111
  interface StaticMapRectOptions {
109
- strokeStyle: string; // css stroke style [default: 'black']
110
- lineWidth: number; // line width in pixels [default: 1.0]
111
- lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
112
- lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
112
+ strokeStyle: string; // css stroke style [default: 'black']
113
+ lineWidth: number; // line width in pixels [default: 1.0]
114
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
115
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
113
116
  }
114
117
  ```
115
118
 
@@ -122,10 +125,10 @@ Options:
122
125
 
123
126
  ```ts
124
127
  interface StaticMapPolygonOptions {
125
- strokeStyle: string; // css stroke style [default: 'black']
126
- lineWidth: number; // line width in pixels [default: 1.0]
127
- lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
128
- lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
128
+ strokeStyle: string; // css stroke style [default: 'black']
129
+ lineWidth: number; // line width in pixels [default: 1.0]
130
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
131
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
129
132
  }
130
133
  ```
131
134
 
@@ -138,16 +141,22 @@ Options:
138
141
 
139
142
  ```ts
140
143
  interface StaticMapTextOptions {
141
- px: number; // padding x [default: 0]
142
- py: number; // padding y [default: 0]
143
- maxWidth?: number; // text max width [default: undefined]
144
- direction: string; // default: 'ltr'
145
- font: string; // default: '12px sans-serif'
146
- textAlign: string; // default: 'center'
147
- textBaseline: string; // default: 'middle'
148
- strokeStyle?: string; // css stroke style or undefined for no stroke [default: undefined]
149
- lineWidth: number; // line width in pixels [default: 1.0]
144
+ px: number; // padding x [default: 0]
145
+ py: number; // padding y [default: 0]
146
+ maxWidth?: number; // text max width [default: undefined]
147
+ font: string; // default: '12px sans-serif'
148
+ textAlign: 'center' | 'end' | 'left' | 'right' | 'start'; // default: 'center'
149
+ textBaseline: 'alphabetic' | 'bottom' | 'hanging' | 'ideographic' | 'middle' | 'top'; // default: 'middle'
150
+ strokeStyle?: string; // css stroke style or undefined for no stroke [default: undefined]
151
+ lineWidth: number; // line width in pixels [default: 1.0]
150
152
  fillStyle?: string; // css fill style or undefined for no fill [default: 'black']
153
+ direction?: 'ltr' | 'rtl'; // writing direction [default: system or browser default]
154
+ fontKerning: 'auto' | 'none' | 'normal'; // font kerning [default: 'auto']
155
+ fontStretch: 'condensed' | 'expanded' | 'extra-condensed' | 'extra-expanded' | 'normal' | 'semi-condensed' | 'semi-expanded' | 'ultra-condensed' | 'ultra-expanded'; // font stretch [default: 'normal']
156
+ fontVariantCaps: 'all-petite-caps' | 'all-small-caps' | 'normal' | 'petite-caps' | 'small-caps' | 'titling-caps' | 'unicase'; // caps [default: 'normal']
157
+ letterSpacing?: string; // css length [default: undefined = 0px]
158
+ textRendering: 'auto' | 'geometricPrecision' | 'optimizeLegibility' | 'optimizeSpeed'; // [default: 'auto']
159
+ wordSpacing?: string; // css length [default: undefined = 0px]
151
160
  }
152
161
  ```
153
162
 
@@ -165,26 +174,26 @@ Options:
165
174
 
166
175
  ```ts
167
176
  export interface StaticMapScaleOptions {
168
- units: 'metric' | 'imperial'; // [default: 'metric']
169
- position: '(bottom|top)(left|right)'; // [default: 'bottomleft']
170
- height: number; // in pixels [default: 16]
171
- maxWidth: number; // percent [default: 20]
172
- mx: number; // margin x in percent [default: 4]
173
- my: number; // margin y in percent [default: 4]
174
- boxStyle: { // drawing style of the box
175
- lineWidth: number; // line width in pixels [default: 1.0]
176
- lineCap: 'butt' | 'round' | 'square'; // [default: 'square']
177
- lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'miter']
178
- strokeStyle: string; // css stroke style [default: 'black']
179
- fillStyle: string; // css fill style [default: 'rgba(255, 255, 255, 0.3)']
177
+ units: 'metric' | 'imperial'; // [default: 'metric']
178
+ position: '(bottom|top)(left|right)'; // [default: 'bottomleft']
179
+ height: number; // in pixels [default: 16]
180
+ maxWidth: number; // percent [default: 20]
181
+ mx: number; // margin x in percent [default: 4]
182
+ my: number; // margin y in percent [default: 4]
183
+ boxStyle: { // drawing style of the box
184
+ lineWidth: number; // line width in pixels [default: 1.0]
185
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'square']
186
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'miter']
187
+ strokeStyle?: string; // css stroke style [default: 'black']
188
+ fillStyle?: string; // css fill style [default: 'rgba(255, 255, 255, 0.3)']
180
189
  };
181
- textStyle: { // drawing style of the label
182
- font: string; // [default: '12px sans-serif']
183
- lineWidth: number; // line width in pixels [default: 1.0]
184
- lineCap: 'butt' | 'round' | 'square'; // [default: 'square']
185
- lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'miter']
186
- strokeStyle: string; // css stroke style [default: 'black']
187
- fillStyle: string; // css fill style [default: 'rgba(255, 255, 255, 0.3)']
190
+ textStyle: { // drawing style of the label
191
+ font: string; // [default: '12px sans-serif']
192
+ lineWidth: number; // line width in pixels [default: 1.0]
193
+ lineCap: 'butt' | 'round' | 'square'; // [default: 'round']
194
+ lineJoin: 'bevel' | 'round' | 'miter'; // [default: 'round']
195
+ strokeStyle?: string; // css stroke style [default: 'black']
196
+ fillStyle?: string; // css fill style [default: undefined]
188
197
  };
189
198
  }
190
199
  ```
@@ -200,8 +209,8 @@ Can be obtained via async functions
200
209
  .renderToCanvas(): Promise<Canvas>
201
210
  ```
202
211
 
203
- All of them will fail if the `.extent` of the map is not explicitly defined or cannot be computed (because there are
204
- no overlays).
212
+ All of them will fail if the `.extent` of the map is not explicitly defined or cannot be computed
213
+ (because there are no overlays).
205
214
 
206
215
  ## Examples
207
216
 
@@ -261,8 +270,8 @@ const buffer = await canvas.toBuffer();
261
270
  ```
262
271
 
263
272
  ## Attribution
264
- Depending on which tiles are being used, attribution of the data has to be provided. When using the static map image
265
- you are responsible to do so.
273
+ Depending on which tiles are being used, attribution of the data has to be provided. When using the static map
274
+ image you are responsible to do so.
266
275
 
267
276
  E.g. see [OpenStreetMap](https://www.openstreetmap.org/copyright) for information regarding the copyright and
268
277
  attribution of OpenStreetMap tiles.
@@ -1,2 +1,2 @@
1
1
  export declare function mimeFromURL(url: string): "image/png" | "image/jpeg" | "image/svg+xml" | "application/octet-stream";
2
- export declare function base64img(url: string, userAgent?: string, timeout?: number): Promise<string>;
2
+ export declare function base64img(url: string, userAgent: string, timeout?: number): Promise<string>;
package/dist/base64img.js CHANGED
@@ -13,7 +13,7 @@ export function mimeFromURL(url) {
13
13
  return 'application/octet-stream';
14
14
  }
15
15
  }
16
- export async function base64img(url, userAgent = 'StaticMap/3', timeout = 0) {
16
+ export async function base64img(url, userAgent, timeout = 0) {
17
17
  let timeoutId;
18
18
  try {
19
19
  const controller = new AbortController();
package/dist/index.d.ts CHANGED
@@ -18,10 +18,7 @@ export interface FillOptions {
18
18
  fillStyle: string | CanvasGradient | CanvasPattern;
19
19
  }
20
20
  export type StrokeFillOptions = SetOptional<StrokeOptions, 'strokeStyle'> & SetOptional<FillOptions, 'fillStyle'>;
21
- export type StaticMapOverlay = {
22
- type: StaticMapOverlayType;
23
- } & StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapRect | StaticMapPolygon | StaticMapText | StaticMapScale;
24
- type StaticMapOverlayType = 'image' | 'line' | 'circle' | 'rect' | 'polygon' | 'text' | 'scale';
21
+ export type StaticMapOverlay = StaticMapImage | StaticMapLine | StaticMapCircle | StaticMapRect | StaticMapPolygon | StaticMapText | StaticMapScale;
25
22
  export interface StaticMapImage {
26
23
  type: 'image';
27
24
  src: string;
@@ -98,8 +95,8 @@ export interface StaticMapOptions {
98
95
  tileSize: number;
99
96
  tileMaxZoom: number;
100
97
  tileCache?: string;
101
- userAgent?: string;
102
98
  tileTimeout: number;
99
+ userAgent: string;
103
100
  backgroundColor?: string;
104
101
  grayscale: boolean;
105
102
  }
@@ -111,14 +108,15 @@ export declare class StaticMap {
111
108
  private static defaultTextOptions;
112
109
  private static defaultScaleOptions;
113
110
  readonly options: StaticMapOptions;
114
- private cache?;
115
- private overlays;
111
+ private readonly cache?;
112
+ private readonly overlays;
116
113
  constructor(options?: Partial<StaticMapOptions>);
117
114
  get extent(): LatLngBounds | undefined;
118
115
  get paddingLeft(): number;
119
116
  get paddingRight(): number;
120
117
  get paddingTop(): number;
121
118
  get paddingBottom(): number;
119
+ setOptions(options: Partial<Omit<StaticMapOptions, 'tileCache'>>): StaticMap;
122
120
  renderToCanvas(): Promise<Canvas.Canvas>;
123
121
  renderToDataURL(): Promise<string>;
124
122
  renderToBuffer(): Promise<Buffer>;
@@ -154,4 +152,3 @@ export declare class StaticMap {
154
152
  private loadTile;
155
153
  private createCanvas;
156
154
  }
157
- export {};
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ export class StaticMap {
16
16
  tileSize: 256,
17
17
  tileMaxZoom: 20,
18
18
  tileTimeout: 2000,
19
+ userAgent: 'StaticMap/3',
19
20
  grayscale: false
20
21
  };
21
22
  static defaultLineOptions = {
@@ -100,6 +101,10 @@ export class StaticMap {
100
101
  get paddingBottom() {
101
102
  return this.options.paddingBottom ?? this.options.paddingY ?? this.options.padding;
102
103
  }
104
+ setOptions(options) {
105
+ merge(this.options, options);
106
+ return this;
107
+ }
103
108
  async renderToCanvas() {
104
109
  return this.createCanvas();
105
110
  }
@@ -330,7 +335,12 @@ export class StaticMap {
330
335
  .replace('{y}', tileY.toString());
331
336
  let src = url;
332
337
  if (this.cache !== undefined) {
333
- const td = { z: zoom, x: tileX, y: tileY };
338
+ const td = {
339
+ z: zoom,
340
+ x: tileX,
341
+ y: tileY,
342
+ url: this.options.tileURL
343
+ };
334
344
  try {
335
345
  const csrc = await this.cache.pass(td, () => base64img(src, this.options.userAgent, this.options.tileTimeout));
336
346
  if (csrc !== undefined) {
@@ -2,6 +2,7 @@ export interface TileData {
2
2
  z: number;
3
3
  x: number;
4
4
  y: number;
5
+ url: string;
5
6
  }
6
7
  export declare class TileCache {
7
8
  static fnTemplate: string;
package/dist/tilecache.js CHANGED
@@ -1,8 +1,22 @@
1
1
  import { mkdirSync } from 'fs';
2
2
  import { readFile, writeFile } from 'fs/promises';
3
3
  import { join as pathJoin } from 'path';
4
+ function cyrb53(str, seed = 0) {
5
+ let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
6
+ for (let i = 0, ch; i < str.length; i++) {
7
+ ch = str.charCodeAt(i);
8
+ h1 = Math.imul(h1 ^ ch, 2654435761);
9
+ h2 = Math.imul(h2 ^ ch, 1597334677);
10
+ }
11
+ h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
12
+ h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
13
+ h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
14
+ h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
15
+ return (h2 >>> 0).toString(16).padStart(8, '0') + (h1 >>> 0).toString(16).padStart(8, '0');
16
+ }
17
+ ;
4
18
  export class TileCache {
5
- static fnTemplate = '{z}_{x}_{y}.png';
19
+ static fnTemplate = '{url}_{z}_{x}_{y}.png';
6
20
  path;
7
21
  constructor(path) {
8
22
  this.path = path;
@@ -20,7 +34,7 @@ export class TileCache {
20
34
  const p = this.tilePath(data);
21
35
  try {
22
36
  const r = await readFile(p, { encoding: 'utf-8' });
23
- return r;
37
+ return (r.length > 0) ? r : undefined;
24
38
  }
25
39
  catch {
26
40
  return undefined;
@@ -32,6 +46,7 @@ export class TileCache {
32
46
  }
33
47
  tilePath(data) {
34
48
  const fn = TileCache.fnTemplate
49
+ .replace('{url}', cyrb53(data.url))
35
50
  .replace('{z}', data.z.toString())
36
51
  .replace('{x}', data.x.toString())
37
52
  .replace('{y}', data.y.toString());
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tinkatinka/static-map",
3
- "version": "3.0.6",
3
+ "version": "3.1.2",
4
4
  "description": "Render static map images",
5
5
  "author": "Florian Gmeiner <florian@tinkatinka.com>",
6
6
  "license": "MIT",