styled-map-package 2.2.0 → 3.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/bin/smp-download.js +3 -3
- package/bin/smp-mbtiles.js +1 -1
- package/bin/smp-view.js +2 -2
- package/dist/download.cjs +101 -0
- package/dist/download.d.cts +65 -0
- package/dist/{lib/download.d.ts → download.d.ts} +22 -6
- package/dist/download.js +77 -0
- package/dist/from-mbtiles.cjs +91 -0
- package/dist/from-mbtiles.d.cts +17 -0
- package/dist/from-mbtiles.d.ts +17 -0
- package/dist/from-mbtiles.js +57 -0
- package/dist/index.cjs +49 -0
- package/dist/index.d.cts +27 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +18 -0
- package/dist/reader-watch.cjs +135 -0
- package/dist/reader-watch.d.cts +24 -0
- package/dist/reader-watch.d.ts +24 -0
- package/dist/reader-watch.js +101 -0
- package/dist/reader.cjs +167 -0
- package/dist/reader.d.cts +62 -0
- package/dist/{lib/reader.d.ts → reader.d.ts} +16 -5
- package/dist/reader.js +138 -0
- package/dist/reporters.cjs +122 -0
- package/dist/reporters.d.cts +10 -0
- package/dist/{lib/reporters.d.ts → reporters.d.ts} +5 -2
- package/dist/reporters.js +88 -0
- package/dist/server.cjs +79 -0
- package/dist/server.d.cts +37 -0
- package/dist/server.d.ts +37 -0
- package/dist/server.js +45 -0
- package/dist/style-downloader.cjs +312 -0
- package/dist/style-downloader.d.cts +120 -0
- package/dist/{lib/style-downloader.d.ts → style-downloader.d.ts} +23 -13
- package/dist/style-downloader.js +288 -0
- package/dist/tile-downloader.cjs +158 -0
- package/dist/tile-downloader.d.cts +84 -0
- package/dist/{lib/tile-downloader.d.ts → tile-downloader.d.ts} +22 -10
- package/dist/tile-downloader.js +126 -0
- package/dist/{lib/writer.d.ts → types-B4Xn1F9K.d.cts} +74 -14
- package/dist/types-B4Xn1F9K.d.ts +189 -0
- package/dist/utils/errors.cjs +41 -0
- package/dist/utils/errors.d.cts +18 -0
- package/dist/{lib/utils → utils}/errors.d.ts +4 -2
- package/dist/utils/errors.js +16 -0
- package/dist/utils/fetch.cjs +96 -0
- package/dist/utils/fetch.d.cts +51 -0
- package/dist/{lib/utils → utils}/fetch.d.ts +10 -4
- package/dist/utils/fetch.js +62 -0
- package/dist/utils/file-formats.cjs +98 -0
- package/dist/utils/file-formats.d.cts +35 -0
- package/dist/{lib/utils → utils}/file-formats.d.ts +13 -3
- package/dist/utils/file-formats.js +62 -0
- package/dist/utils/geo.cjs +84 -0
- package/dist/utils/geo.d.cts +46 -0
- package/dist/{lib/utils → utils}/geo.d.ts +8 -6
- package/dist/utils/geo.js +56 -0
- package/dist/utils/mapbox.cjs +121 -0
- package/dist/utils/mapbox.d.cts +43 -0
- package/dist/utils/mapbox.d.ts +43 -0
- package/dist/utils/mapbox.js +91 -0
- package/dist/utils/misc.cjs +39 -0
- package/{lib/utils/misc.js → dist/utils/misc.d.cts} +5 -9
- package/dist/{lib/utils → utils}/misc.d.ts +5 -3
- package/dist/utils/misc.js +13 -0
- package/dist/utils/streams.cjs +130 -0
- package/dist/utils/streams.d.cts +73 -0
- package/dist/{lib/utils → utils}/streams.d.ts +14 -10
- package/dist/utils/streams.js +103 -0
- package/dist/utils/style.cjs +126 -0
- package/dist/utils/style.d.cts +69 -0
- package/dist/{lib/utils → utils}/style.d.ts +19 -9
- package/dist/utils/style.js +98 -0
- package/dist/utils/templates.cjs +114 -0
- package/dist/utils/templates.d.cts +78 -0
- package/dist/{lib/utils → utils}/templates.d.ts +24 -14
- package/dist/utils/templates.js +79 -0
- package/dist/writer.cjs +401 -0
- package/dist/writer.d.cts +7 -0
- package/dist/writer.d.ts +7 -0
- package/dist/writer.js +374 -0
- package/package.json +87 -33
- package/dist/lib/from-mbtiles.d.ts +0 -13
- package/dist/lib/index.d.ts +0 -10
- package/dist/lib/reader-watch.d.ts +0 -13
- package/dist/lib/server.d.ts +0 -15
- package/dist/lib/types.d.ts +0 -64
- package/dist/lib/utils/mapbox.d.ts +0 -41
- package/lib/download.js +0 -114
- package/lib/from-mbtiles.js +0 -83
- package/lib/index.js +0 -11
- package/lib/reader-watch.js +0 -133
- package/lib/reader.js +0 -165
- package/lib/reporters.js +0 -92
- package/lib/server.js +0 -81
- package/lib/style-downloader.js +0 -363
- package/lib/tile-downloader.js +0 -188
- package/lib/types.ts +0 -104
- package/lib/utils/errors.js +0 -24
- package/lib/utils/fetch.js +0 -100
- package/lib/utils/file-formats.js +0 -85
- package/lib/utils/geo.js +0 -87
- package/lib/utils/mapbox.js +0 -155
- package/lib/utils/streams.js +0 -165
- package/lib/utils/style.js +0 -174
- package/lib/utils/templates.js +0 -136
- package/lib/writer.js +0 -478
|
@@ -1,27 +1,35 @@
|
|
|
1
|
+
import * as type_fest from 'type-fest';
|
|
2
|
+
import { T as TileInfo, d as GlyphRange, c as TileFormat } from '../types-B4Xn1F9K.js';
|
|
3
|
+
import '@maplibre/maplibre-gl-style-spec';
|
|
4
|
+
import 'geojson';
|
|
5
|
+
import 'stream';
|
|
6
|
+
import 'readable-stream';
|
|
7
|
+
import 'events';
|
|
8
|
+
|
|
1
9
|
/**
|
|
2
10
|
* @param {string} path
|
|
3
11
|
* @returns
|
|
4
12
|
*/
|
|
5
|
-
|
|
13
|
+
declare function getResourceType(path: string): "sprite" | "style" | "tile" | "glyph";
|
|
6
14
|
/**
|
|
7
15
|
* Determine the content type of a file based on its extension.
|
|
8
16
|
*
|
|
9
17
|
* @param {string} path
|
|
10
18
|
*/
|
|
11
|
-
|
|
19
|
+
declare function getContentType(path: string): "application/json; charset=utf-8" | "application/x-protobuf" | "image/png" | "image/jpeg" | "image/webp" | "application/vnd.mapbox-vector-tile";
|
|
12
20
|
/**
|
|
13
21
|
* Get the filename for a tile, given the TileInfo
|
|
14
22
|
*
|
|
15
23
|
* @param {import("type-fest").SetRequired<import("../writer.js").TileInfo, 'format'>} tileInfo
|
|
16
24
|
* @returns
|
|
17
25
|
*/
|
|
18
|
-
|
|
26
|
+
declare function getTileFilename({ sourceId, z, x, y, format }: type_fest.SetRequired<TileInfo, "format">): string;
|
|
19
27
|
/**
|
|
20
28
|
* Get a filename for a sprite file, given the sprite id, pixel ratio and extension
|
|
21
29
|
*
|
|
22
30
|
* @param {{ id: string, pixelRatio: number, ext: '.json' | '.png'}} spriteInfo
|
|
23
31
|
*/
|
|
24
|
-
|
|
32
|
+
declare function getSpriteFilename({ id, pixelRatio, ext }: {
|
|
25
33
|
id: string;
|
|
26
34
|
pixelRatio: number;
|
|
27
35
|
ext: ".json" | ".png";
|
|
@@ -33,14 +41,14 @@ export function getSpriteFilename({ id, pixelRatio, ext }: {
|
|
|
33
41
|
* @param {string} options.fontstack
|
|
34
42
|
* @param {import("../writer.js").GlyphRange} options.range
|
|
35
43
|
*/
|
|
36
|
-
|
|
44
|
+
declare function getGlyphFilename({ fontstack, range }: {
|
|
37
45
|
fontstack: string;
|
|
38
|
-
range:
|
|
46
|
+
range: GlyphRange;
|
|
39
47
|
}): string;
|
|
40
48
|
/**
|
|
41
49
|
* Get the URI template for the sprites in the style
|
|
42
50
|
*/
|
|
43
|
-
|
|
51
|
+
declare function getSpriteUri(id?: string): string;
|
|
44
52
|
/**
|
|
45
53
|
* Get the URI template for tiles in the style
|
|
46
54
|
*
|
|
@@ -49,9 +57,9 @@ export function getSpriteUri(id?: string): string;
|
|
|
49
57
|
* @param {import("../writer.js").TileFormat} opts.format
|
|
50
58
|
* @returns
|
|
51
59
|
*/
|
|
52
|
-
|
|
60
|
+
declare function getTileUri({ sourceId, format }: {
|
|
53
61
|
sourceId: string;
|
|
54
|
-
format:
|
|
62
|
+
format: TileFormat;
|
|
55
63
|
}): string;
|
|
56
64
|
/**
|
|
57
65
|
* Replaces variables in a string with values provided in an object. Variables
|
|
@@ -61,8 +69,10 @@ export function getTileUri({ sourceId, format }: {
|
|
|
61
69
|
* @param {Record<string, string | number>} variables - An object where the keys correspond to variable names and values correspond to the replacement values.
|
|
62
70
|
* @returns {string} The string with the variables replaced by their corresponding values.
|
|
63
71
|
*/
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
72
|
+
declare function replaceVariables(template: string, variables: Record<string, string | number>): string;
|
|
73
|
+
declare const URI_SCHEME: "smp";
|
|
74
|
+
declare const URI_BASE: string;
|
|
75
|
+
declare const STYLE_FILE: "style.json";
|
|
76
|
+
declare const GLYPH_URI: string;
|
|
77
|
+
|
|
78
|
+
export { GLYPH_URI, STYLE_FILE, URI_BASE, URI_SCHEME, getContentType, getGlyphFilename, getResourceType, getSpriteFilename, getSpriteUri, getTileFilename, getTileUri, replaceVariables };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const URI_SCHEME = "smp";
|
|
2
|
+
const URI_BASE = URI_SCHEME + "://maps.v1/";
|
|
3
|
+
const STYLE_FILE = "style.json";
|
|
4
|
+
const SOURCES_FOLDER = "s";
|
|
5
|
+
const SPRITES_FOLDER = "sprites";
|
|
6
|
+
const FONTS_FOLDER = "fonts";
|
|
7
|
+
const TILE_FILE = SOURCES_FOLDER + "/{sourceId}/{z}/{x}/{y}{ext}";
|
|
8
|
+
const SPRITE_FILE = SPRITES_FOLDER + "/{id}/sprite{pixelRatio}{ext}";
|
|
9
|
+
const GLYPH_FILE = FONTS_FOLDER + "/{fontstack}/{range}.pbf.gz";
|
|
10
|
+
const GLYPH_URI = URI_BASE + GLYPH_FILE;
|
|
11
|
+
const pathToResouceType = (
|
|
12
|
+
/** @type {const} */
|
|
13
|
+
{
|
|
14
|
+
[TILE_FILE.split("/")[0] + "/"]: "tile",
|
|
15
|
+
[SPRITE_FILE.split("/")[0] + "/"]: "sprite",
|
|
16
|
+
[GLYPH_FILE.split("/")[0] + "/"]: "glyph"
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
function getResourceType(path) {
|
|
20
|
+
if (path === "style.json") return "style";
|
|
21
|
+
for (const [prefix, type] of Object.entries(pathToResouceType)) {
|
|
22
|
+
if (path.startsWith(prefix)) return type;
|
|
23
|
+
}
|
|
24
|
+
throw new Error(`Unknown resource type for path: ${path}`);
|
|
25
|
+
}
|
|
26
|
+
function getContentType(path) {
|
|
27
|
+
if (path.endsWith(".json")) return "application/json; charset=utf-8";
|
|
28
|
+
if (path.endsWith(".pbf.gz") || path.endsWith(".pbf"))
|
|
29
|
+
return "application/x-protobuf";
|
|
30
|
+
if (path.endsWith(".png")) return "image/png";
|
|
31
|
+
if (path.endsWith(".jpg")) return "image/jpeg";
|
|
32
|
+
if (path.endsWith(".webp")) return "image/webp";
|
|
33
|
+
if (path.endsWith(".mvt.gz") || path.endsWith(".mvt"))
|
|
34
|
+
return "application/vnd.mapbox-vector-tile";
|
|
35
|
+
throw new Error(`Unknown content type for path: ${path}`);
|
|
36
|
+
}
|
|
37
|
+
function getTileFilename({ sourceId, z, x, y, format }) {
|
|
38
|
+
const ext = "." + format + (format === "mvt" ? ".gz" : "");
|
|
39
|
+
return replaceVariables(TILE_FILE, { sourceId, z, x, y, ext });
|
|
40
|
+
}
|
|
41
|
+
function getSpriteFilename({ id, pixelRatio, ext }) {
|
|
42
|
+
return replaceVariables(SPRITE_FILE, {
|
|
43
|
+
id,
|
|
44
|
+
pixelRatio: getPixelRatioString(pixelRatio),
|
|
45
|
+
ext
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
function getGlyphFilename({ fontstack, range }) {
|
|
49
|
+
return replaceVariables(GLYPH_FILE, { fontstack, range });
|
|
50
|
+
}
|
|
51
|
+
function getSpriteUri(id = "default") {
|
|
52
|
+
return URI_BASE + replaceVariables(SPRITE_FILE, { id, pixelRatio: "", ext: "" });
|
|
53
|
+
}
|
|
54
|
+
function getTileUri({ sourceId, format }) {
|
|
55
|
+
const ext = "." + format + (format === "mvt" ? ".gz" : "");
|
|
56
|
+
return URI_BASE + TILE_FILE.replace("{sourceId}", sourceId).replace("{ext}", ext);
|
|
57
|
+
}
|
|
58
|
+
function getPixelRatioString(pixelRatio) {
|
|
59
|
+
return pixelRatio === 1 ? "" : `@${pixelRatio}x`;
|
|
60
|
+
}
|
|
61
|
+
function replaceVariables(template, variables) {
|
|
62
|
+
return template.replace(/{(.*?)}/g, (match, varName) => {
|
|
63
|
+
return varName in variables ? String(variables[varName]) : match;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
export {
|
|
67
|
+
GLYPH_URI,
|
|
68
|
+
STYLE_FILE,
|
|
69
|
+
URI_BASE,
|
|
70
|
+
URI_SCHEME,
|
|
71
|
+
getContentType,
|
|
72
|
+
getGlyphFilename,
|
|
73
|
+
getResourceType,
|
|
74
|
+
getSpriteFilename,
|
|
75
|
+
getSpriteUri,
|
|
76
|
+
getTileFilename,
|
|
77
|
+
getTileUri,
|
|
78
|
+
replaceVariables
|
|
79
|
+
};
|
package/dist/writer.cjs
ADDED
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var writer_exports = {};
|
|
30
|
+
__export(writer_exports, {
|
|
31
|
+
SUPPORTED_SOURCE_TYPES: () => SUPPORTED_SOURCE_TYPES,
|
|
32
|
+
Writer: () => Writer
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(writer_exports);
|
|
35
|
+
var import_maplibre_gl_style_spec = require("@maplibre/maplibre-gl-style-spec");
|
|
36
|
+
var import_bbox = require("@turf/bbox");
|
|
37
|
+
var import_archiver = __toESM(require("archiver"), 1);
|
|
38
|
+
var import_events = require("events");
|
|
39
|
+
var import_filter_obj = require("filter-obj");
|
|
40
|
+
var import_fs = __toESM(require("fs"), 1);
|
|
41
|
+
var import_p_event = require("p-event");
|
|
42
|
+
var import_readable_stream = require("readable-stream");
|
|
43
|
+
var import_stream = require("stream");
|
|
44
|
+
var import_file_formats = require('./utils/file-formats.cjs');
|
|
45
|
+
var import_geo = require('./utils/geo.cjs');
|
|
46
|
+
var import_misc = require('./utils/misc.cjs');
|
|
47
|
+
var import_streams = require('./utils/streams.cjs');
|
|
48
|
+
var import_style = require('./utils/style.cjs');
|
|
49
|
+
var import_templates = require('./utils/templates.cjs');
|
|
50
|
+
const SUPPORTED_SOURCE_TYPES = (
|
|
51
|
+
/** @type {const} */
|
|
52
|
+
[
|
|
53
|
+
"raster",
|
|
54
|
+
"vector",
|
|
55
|
+
"geojson"
|
|
56
|
+
]
|
|
57
|
+
);
|
|
58
|
+
class Writer extends import_events.EventEmitter {
|
|
59
|
+
#archive = (0, import_archiver.default)("zip", { zlib: { level: 9 } }).setMaxListeners(Infinity).on("error", console.error);
|
|
60
|
+
/** @type {Set<string>} */
|
|
61
|
+
#addedFiles = /* @__PURE__ */ new Set();
|
|
62
|
+
/** @type {Set<string>} */
|
|
63
|
+
#fonts = /* @__PURE__ */ new Set();
|
|
64
|
+
/** @type {Set<string>} */
|
|
65
|
+
#addedSpriteIds = /* @__PURE__ */ new Set();
|
|
66
|
+
/** @type {Map<string, SourceInfo>} */
|
|
67
|
+
#sources = /* @__PURE__ */ new Map();
|
|
68
|
+
/** @type {StyleSpecification} */
|
|
69
|
+
#style;
|
|
70
|
+
#outputStream;
|
|
71
|
+
static SUPPORTED_SOURCE_TYPES = SUPPORTED_SOURCE_TYPES;
|
|
72
|
+
/**
|
|
73
|
+
* @param {any} style A v7 or v8 MapLibre style. v7 styles will be migrated to
|
|
74
|
+
* v8. (There are currently no typescript declarations for v7 styles, hence
|
|
75
|
+
* this is typed as `any` and validated internally)
|
|
76
|
+
* @param {object} opts
|
|
77
|
+
* @param {number} [opts.highWaterMark=1048576] The maximum number of bytes to buffer during write
|
|
78
|
+
*/
|
|
79
|
+
constructor(style, { highWaterMark = 1024 * 1024 } = {}) {
|
|
80
|
+
super();
|
|
81
|
+
if (!style || !("version" in style)) {
|
|
82
|
+
throw new Error("Invalid style");
|
|
83
|
+
}
|
|
84
|
+
if (style.version !== 7 && style.version !== 8) {
|
|
85
|
+
throw new Error(`Invalid style: Unsupported version v${style.version}`);
|
|
86
|
+
}
|
|
87
|
+
if (!Array.isArray(style.layers)) {
|
|
88
|
+
throw new Error("Invalid style: missing layers property");
|
|
89
|
+
}
|
|
90
|
+
const styleCopy = (0, import_misc.clone)(style);
|
|
91
|
+
(0, import_maplibre_gl_style_spec.migrate)(styleCopy);
|
|
92
|
+
const errors = (0, import_maplibre_gl_style_spec.validateStyleMin)(styleCopy);
|
|
93
|
+
if (errors.length) {
|
|
94
|
+
throw new AggregateError(errors, "Invalid style");
|
|
95
|
+
}
|
|
96
|
+
this.#style = styleCopy;
|
|
97
|
+
for (const [sourceId, source] of Object.entries(this.#style.sources)) {
|
|
98
|
+
if (source.type !== "geojson") continue;
|
|
99
|
+
this.#addSource(sourceId, source);
|
|
100
|
+
}
|
|
101
|
+
this.#outputStream = new import_readable_stream.PassThrough({ highWaterMark });
|
|
102
|
+
(0, import_readable_stream.pipeline)(this.#archive, this.#outputStream, (err) => {
|
|
103
|
+
if (err) this.emit("error", err);
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @returns {import('stream').Readable} Readable stream of the styled map package
|
|
108
|
+
*/
|
|
109
|
+
get outputStream() {
|
|
110
|
+
return this.#outputStream;
|
|
111
|
+
}
|
|
112
|
+
#getBounds() {
|
|
113
|
+
let bounds;
|
|
114
|
+
let maxzoom = 0;
|
|
115
|
+
for (const { source } of this.#sources.values()) {
|
|
116
|
+
if (source.type === "geojson") {
|
|
117
|
+
if (isEmptyFeatureCollection(source.data)) continue;
|
|
118
|
+
const bbox2 = get2DBBox(source.data.bbox);
|
|
119
|
+
bounds = bounds ? (0, import_geo.unionBBox)([bounds, bbox2]) : [...bbox2];
|
|
120
|
+
} else {
|
|
121
|
+
if (source.maxzoom < maxzoom) continue;
|
|
122
|
+
if (source.maxzoom === maxzoom) {
|
|
123
|
+
bounds = bounds ? (0, import_geo.unionBBox)([bounds, source.bounds]) : source.bounds;
|
|
124
|
+
} else {
|
|
125
|
+
bounds = source.bounds;
|
|
126
|
+
maxzoom = source.maxzoom;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return bounds;
|
|
131
|
+
}
|
|
132
|
+
#getMaxZoom() {
|
|
133
|
+
let maxzoom = 0;
|
|
134
|
+
for (const { source } of this.#sources.values()) {
|
|
135
|
+
const sourceMaxzoom = (
|
|
136
|
+
// For GeoJSON sources, the maxzoom is 16 unless otherwise set
|
|
137
|
+
source.type === "geojson" ? source.maxzoom || 16 : source.maxzoom
|
|
138
|
+
);
|
|
139
|
+
maxzoom = Math.max(maxzoom, sourceMaxzoom);
|
|
140
|
+
}
|
|
141
|
+
return maxzoom;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Add a source definition to the styled map package
|
|
145
|
+
*
|
|
146
|
+
* @param {string} sourceId
|
|
147
|
+
* @param {InputSource} source
|
|
148
|
+
* @returns {SourceInfo}
|
|
149
|
+
*/
|
|
150
|
+
#addSource(sourceId, source) {
|
|
151
|
+
const encodedSourceId = encodeSourceId(this.#sources.size);
|
|
152
|
+
const tileSourceOverrides = {
|
|
153
|
+
minzoom: 0,
|
|
154
|
+
maxzoom: 0,
|
|
155
|
+
bounds: (
|
|
156
|
+
/** @type {import('./utils/geo.js').BBox} */
|
|
157
|
+
[...import_geo.MAX_BOUNDS]
|
|
158
|
+
),
|
|
159
|
+
tiles: (
|
|
160
|
+
/** @type {string[]} */
|
|
161
|
+
[]
|
|
162
|
+
)
|
|
163
|
+
};
|
|
164
|
+
let smpSource;
|
|
165
|
+
switch (source.type) {
|
|
166
|
+
case "raster":
|
|
167
|
+
case "vector":
|
|
168
|
+
smpSource = {
|
|
169
|
+
...(0, import_filter_obj.excludeKeys)(source, ["tiles", "url"]),
|
|
170
|
+
...tileSourceOverrides
|
|
171
|
+
};
|
|
172
|
+
break;
|
|
173
|
+
case "geojson":
|
|
174
|
+
smpSource = {
|
|
175
|
+
...source,
|
|
176
|
+
maxzoom: 0,
|
|
177
|
+
data: typeof source.data !== "string" ? (
|
|
178
|
+
// Add a bbox property to the GeoJSON data if it doesn't already have one
|
|
179
|
+
{ ...source.data, bbox: source.data.bbox || (0, import_bbox.bbox)(source.data) }
|
|
180
|
+
) : (
|
|
181
|
+
// If GeoJSON data is referenced by a URL, start with an empty FeatureCollection
|
|
182
|
+
{
|
|
183
|
+
type: "FeatureCollection",
|
|
184
|
+
features: [],
|
|
185
|
+
bbox: [0, 0, 0, 0]
|
|
186
|
+
}
|
|
187
|
+
)
|
|
188
|
+
};
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
const sourceInfo = {
|
|
192
|
+
source: smpSource,
|
|
193
|
+
encodedSourceId
|
|
194
|
+
};
|
|
195
|
+
this.#sources.set(sourceId, sourceInfo);
|
|
196
|
+
return sourceInfo;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Add a tile to the styled map package
|
|
200
|
+
*
|
|
201
|
+
* @param {Source} tileData
|
|
202
|
+
* @param {TileInfo} opts
|
|
203
|
+
*/
|
|
204
|
+
async addTile(tileData, { z, x, y, sourceId, format }) {
|
|
205
|
+
let sourceInfo = this.#sources.get(sourceId);
|
|
206
|
+
if (!sourceInfo) {
|
|
207
|
+
const source2 = this.#style.sources[sourceId];
|
|
208
|
+
if (!source2) {
|
|
209
|
+
throw new Error(`Source not referenced in style.json: ${sourceId}`);
|
|
210
|
+
}
|
|
211
|
+
if (source2.type !== "raster" && source2.type !== "vector") {
|
|
212
|
+
throw new Error(`Unsupported source type: ${source2.type}`);
|
|
213
|
+
}
|
|
214
|
+
sourceInfo = this.#addSource(sourceId, source2);
|
|
215
|
+
}
|
|
216
|
+
const { source, encodedSourceId } = sourceInfo;
|
|
217
|
+
if (source.type !== "raster" && source.type !== "vector") {
|
|
218
|
+
throw new Error(`Unsupported source type: ${source.type}`);
|
|
219
|
+
}
|
|
220
|
+
if (!format) {
|
|
221
|
+
const tileDataStream = typeof tileData === "string" ? import_fs.default.createReadStream(tileData) : tileData instanceof Uint8Array ? import_stream.Readable.from(tileData) : tileData;
|
|
222
|
+
[format, tileData] = await (0, import_file_formats.getTileFormatFromStream)(tileDataStream);
|
|
223
|
+
}
|
|
224
|
+
if (!sourceInfo.format) {
|
|
225
|
+
sourceInfo.format = format;
|
|
226
|
+
} else if (sourceInfo.format !== format) {
|
|
227
|
+
throw new Error(
|
|
228
|
+
`Tile format mismatch for source ${sourceId}: expected ${sourceInfo.format}, got ${format}`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
const bbox2 = (0, import_geo.tileToBBox)({ z, x, y });
|
|
232
|
+
if (z > source.maxzoom) {
|
|
233
|
+
source.maxzoom = z;
|
|
234
|
+
source.bounds = bbox2;
|
|
235
|
+
} else if (z === source.maxzoom) {
|
|
236
|
+
source.bounds = (0, import_geo.unionBBox)([source.bounds, bbox2]);
|
|
237
|
+
}
|
|
238
|
+
const name = (0, import_templates.getTileFilename)({ sourceId: encodedSourceId, z, x, y, format });
|
|
239
|
+
return this.#append(tileData, { name, store: true });
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Create a write stream for adding tiles to the styled map package
|
|
243
|
+
*
|
|
244
|
+
* @param {object} opts
|
|
245
|
+
* @param {number} [opts.concurrency=16] The number of concurrent writes
|
|
246
|
+
*
|
|
247
|
+
* @returns
|
|
248
|
+
*/
|
|
249
|
+
createTileWriteStream({ concurrency = 16 } = {}) {
|
|
250
|
+
return (0, import_streams.writeStreamFromAsync)(this.addTile.bind(this), { concurrency });
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Add a sprite to the styled map package
|
|
254
|
+
*
|
|
255
|
+
* @param {object} options
|
|
256
|
+
* @param {Source} options.json
|
|
257
|
+
* @param {Source} options.png
|
|
258
|
+
* @param {number} [options.pixelRatio]
|
|
259
|
+
* @param {string} [options.id='default']
|
|
260
|
+
* @returns {Promise<void>}
|
|
261
|
+
*/
|
|
262
|
+
async addSprite({ json, png, pixelRatio = 1, id = "default" }) {
|
|
263
|
+
this.#addedSpriteIds.add(id);
|
|
264
|
+
const jsonName = (0, import_templates.getSpriteFilename)({ id, pixelRatio, ext: ".json" });
|
|
265
|
+
const pngName = (0, import_templates.getSpriteFilename)({ id, pixelRatio, ext: ".png" });
|
|
266
|
+
await Promise.all([
|
|
267
|
+
this.#append(json, { name: jsonName }),
|
|
268
|
+
this.#append(png, { name: pngName })
|
|
269
|
+
]);
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Add glyphs to the styled map package
|
|
273
|
+
*
|
|
274
|
+
* @param {Source} glyphData
|
|
275
|
+
* @param {GlyphInfo} glyphInfo
|
|
276
|
+
* @returns {Promise<void>}
|
|
277
|
+
*/
|
|
278
|
+
addGlyphs(glyphData, { font: fontName, range }) {
|
|
279
|
+
this.#fonts.add(fontName);
|
|
280
|
+
const name = (0, import_templates.getGlyphFilename)({ fontstack: fontName, range });
|
|
281
|
+
return this.#append(glyphData, { name });
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Create a write stream for adding glyphs to the styled map package
|
|
285
|
+
*
|
|
286
|
+
* @param {object} opts
|
|
287
|
+
* @param {number} [opts.concurrency=16] The number of concurrent writes
|
|
288
|
+
* @returns
|
|
289
|
+
*/
|
|
290
|
+
createGlyphWriteStream({ concurrency = 16 } = {}) {
|
|
291
|
+
return (0, import_streams.writeStreamFromAsync)(this.addGlyphs.bind(this), { concurrency });
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Finalize the styled map package and write the style to the archive.
|
|
295
|
+
* This method must be called to complete the archive.
|
|
296
|
+
* You must wait for your destination write stream to 'finish' before using the output.
|
|
297
|
+
*/
|
|
298
|
+
finish() {
|
|
299
|
+
this.#prepareStyle();
|
|
300
|
+
const style = JSON.stringify(this.#style);
|
|
301
|
+
this.#append(style, { name: import_templates.STYLE_FILE });
|
|
302
|
+
this.#archive.finalize();
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Mutates the style object to prepare it for writing to the archive.
|
|
306
|
+
* Deterministic: can be run more than once with the same result.
|
|
307
|
+
*/
|
|
308
|
+
#prepareStyle() {
|
|
309
|
+
if (this.#sources.size === 0) {
|
|
310
|
+
throw new Error("Missing sources: add at least one source");
|
|
311
|
+
}
|
|
312
|
+
if (this.#style.glyphs && this.#fonts.size === 0) {
|
|
313
|
+
throw new Error(
|
|
314
|
+
"Missing fonts: style references glyphs but no fonts added"
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
(0, import_style.replaceFontStacks)(this.#style, [...this.#fonts]);
|
|
318
|
+
if (this.#style.glyphs) {
|
|
319
|
+
this.#style.glyphs = import_templates.GLYPH_URI;
|
|
320
|
+
}
|
|
321
|
+
if (typeof this.#style.sprite === "string") {
|
|
322
|
+
if (!this.#addedSpriteIds.has("default")) {
|
|
323
|
+
throw new Error(
|
|
324
|
+
"Missing sprite: style references sprite but none added"
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
this.#style.sprite = (0, import_templates.getSpriteUri)();
|
|
328
|
+
} else if (Array.isArray(this.#style.sprite)) {
|
|
329
|
+
this.#style.sprite = this.#style.sprite.map(({ id }) => {
|
|
330
|
+
if (!this.#addedSpriteIds.has(id)) {
|
|
331
|
+
throw new Error(
|
|
332
|
+
`Missing sprite: style references sprite ${id} but none added`
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
return { id, url: (0, import_templates.getSpriteUri)(id) };
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
this.#style.sources = {};
|
|
339
|
+
for (const [sourceId, { source, encodedSourceId, format = "mvt" }] of this.#sources) {
|
|
340
|
+
if (source.type === "geojson" && isEmptyFeatureCollection(source.data)) {
|
|
341
|
+
continue;
|
|
342
|
+
}
|
|
343
|
+
this.#style.sources[sourceId] = source;
|
|
344
|
+
if (!("tiles" in source)) continue;
|
|
345
|
+
source.tiles = [(0, import_templates.getTileUri)({ sourceId: encodedSourceId, format })];
|
|
346
|
+
}
|
|
347
|
+
this.#style.layers = this.#style.layers.filter(
|
|
348
|
+
(layer) => !("source" in layer) || !!this.#style.sources[layer.source]
|
|
349
|
+
);
|
|
350
|
+
const metadata = this.#style.metadata || (this.#style.metadata = {});
|
|
351
|
+
const bounds = this.#getBounds();
|
|
352
|
+
if (bounds) {
|
|
353
|
+
metadata["smp:bounds"] = bounds;
|
|
354
|
+
const [w, s, e, n] = bounds;
|
|
355
|
+
this.#style.center = [w + (e - w) / 2, s + (n - s) / 2];
|
|
356
|
+
}
|
|
357
|
+
metadata["smp:maxzoom"] = this.#getMaxZoom();
|
|
358
|
+
metadata["smp:sourceFolders"] = {};
|
|
359
|
+
for (const [sourceId, { encodedSourceId }] of this.#sources) {
|
|
360
|
+
metadata["smp:sourceFolders"][sourceId] = encodedSourceId;
|
|
361
|
+
}
|
|
362
|
+
this.#style.zoom = Math.max(0, this.#getMaxZoom() - 2);
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
*
|
|
366
|
+
* @param {Source} source
|
|
367
|
+
* @param {{ name: string, store?: boolean }} options
|
|
368
|
+
* @returns {Promise<void>}
|
|
369
|
+
*/
|
|
370
|
+
async #append(source, { name, store = false }) {
|
|
371
|
+
if (this.#addedFiles.has(name)) {
|
|
372
|
+
throw new Error(`${name} already added`);
|
|
373
|
+
}
|
|
374
|
+
this.#addedFiles.add(name);
|
|
375
|
+
const onAdded = (0, import_p_event.pEvent)(
|
|
376
|
+
this.#archive,
|
|
377
|
+
"entry",
|
|
378
|
+
(entry) => entry.name === name
|
|
379
|
+
);
|
|
380
|
+
this.#archive.append(convertSource(source), { name, store });
|
|
381
|
+
await onAdded;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
function encodeSourceId(sourceIndex) {
|
|
385
|
+
return sourceIndex.toString(36);
|
|
386
|
+
}
|
|
387
|
+
function convertSource(source) {
|
|
388
|
+
return !Buffer.isBuffer(source) && source instanceof Uint8Array ? Buffer.from(source.buffer, source.byteOffset, source.length) : source;
|
|
389
|
+
}
|
|
390
|
+
function isEmptyFeatureCollection(data) {
|
|
391
|
+
return data.type === "FeatureCollection" && data.features.length === 0;
|
|
392
|
+
}
|
|
393
|
+
function get2DBBox(bbox2) {
|
|
394
|
+
if (bbox2.length === 4) return bbox2;
|
|
395
|
+
return [bbox2[0], bbox2[1], bbox2[3], bbox2[4]];
|
|
396
|
+
}
|
|
397
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
398
|
+
0 && (module.exports = {
|
|
399
|
+
SUPPORTED_SOURCE_TYPES,
|
|
400
|
+
Writer
|
|
401
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { G as GlyphInfo, d as GlyphRange, e as SUPPORTED_SOURCE_TYPES, f as Source, h as SourceInfo, g as SourceInternal, c as TileFormat, T as TileInfo, W as Writer } from './types-B4Xn1F9K.cjs';
|
|
2
|
+
import 'readable-stream';
|
|
3
|
+
import 'stream';
|
|
4
|
+
import 'events';
|
|
5
|
+
import '@maplibre/maplibre-gl-style-spec';
|
|
6
|
+
import 'geojson';
|
|
7
|
+
import 'type-fest';
|
package/dist/writer.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { G as GlyphInfo, d as GlyphRange, e as SUPPORTED_SOURCE_TYPES, f as Source, h as SourceInfo, g as SourceInternal, c as TileFormat, T as TileInfo, W as Writer } from './types-B4Xn1F9K.js';
|
|
2
|
+
import 'readable-stream';
|
|
3
|
+
import 'stream';
|
|
4
|
+
import 'events';
|
|
5
|
+
import '@maplibre/maplibre-gl-style-spec';
|
|
6
|
+
import 'geojson';
|
|
7
|
+
import 'type-fest';
|