@mapka/tilestats 0.0.0-stage → 0.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/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,173 @@
1
- # Temporary Holding Version
1
+ # @mapka/tilestats
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Per-layer statistics ("vectorstats") for vector tile archives and feature streams. For every layer it reports exact
4
+ feature counts per zoom, geometry types, and for every attribute its types, number range, distinct values and top
5
+ values, in a JSON format written for people and for AI agents. The format is defined in [SPEC.md](SPEC.md) and has a
6
+ JSON Schema at `@mapka/tilestats/schema/vectorstats-1.json`.
7
+
8
+ - Reads `.pmtiles` and `.mbtiles` archives with MVT or MLT tiles, and any iterable of features.
9
+ - Streams: one tile at a time on one thread, in memory bounded by the limits, not by the size of the archive.
10
+ - 64-bit integers from MLT tiles and feature streams stay exact.
11
+
12
+ Requires Node 26 or newer.
13
+
14
+ ## Install
15
+
16
+ ```sh
17
+ npm install @mapka/tilestats
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ From an archive:
23
+
24
+ ```ts
25
+ import { tilestats } from "@mapka/tilestats";
26
+
27
+ const { layers } = await tilestats("poland.pmtiles");
28
+
29
+ for (const layer of layers) {
30
+ console.log(layer.layer, layer.count, layer.attributes.length);
31
+ }
32
+ ```
33
+
34
+ From features, any `Iterable` or `AsyncIterable` of `{ layer, geometry, properties }`:
35
+
36
+ ```ts
37
+ import { tilestats } from "@mapka/tilestats";
38
+
39
+ const features = [
40
+ { layer: "poi", geometry: "Point", properties: { class: "cafe", rank: 3 } },
41
+ { layer: "poi", geometry: "Point", properties: { class: "bar" } },
42
+ ];
43
+
44
+ console.log(JSON.stringify(await tilestats(features), null, 2));
45
+ ```
46
+
47
+ `tilestats` resolves to the document of [SPEC.md](SPEC.md): a `VectorStats<"zoom">` for an archive and a
48
+ `VectorStats<"total">` for features. Every attribute is a discriminated union on `type`, so narrowing it tells
49
+ TypeScript which of `min`, `max` and `integer` exist.
50
+
51
+ ```ts
52
+ for (const attribute of layer.attributes) {
53
+ switch (attribute.type) {
54
+ case "integer":
55
+ case "number":
56
+ console.log(attribute.attribute, attribute.min, attribute.max);
57
+ break;
58
+ case "mixed-numeric":
59
+ console.log(attribute.attribute, attribute.min, attribute.max, attribute.integer);
60
+ break;
61
+ }
62
+ }
63
+ ```
64
+
65
+ ### Options
66
+
67
+ ```ts
68
+ await tilestats("poland.pmtiles", {
69
+ limits: { maxDistinct: 5000 }, // see below
70
+ });
71
+ ```
72
+
73
+ - `limits` and their defaults: `maxAttributes` 1000 per layer, `maxDistinct` 1000 values kept per attribute,
74
+ `maxTop` 100 top values, `maxExamples` 10, `maxStringLength` 256. Past a limit the output says so: `values.exact`
75
+ is `false` and the distinct count is an estimate (HyperLogLog).
76
+
77
+ Invalid options reject with a `TypeError` naming each bad option.
78
+
79
+ ## Output
80
+
81
+ The output is one JSON document, the same for both inputs apart from how counts are written:
82
+
83
+ ```json
84
+ {
85
+ "version": 1,
86
+ "counts": "zoom",
87
+ "layers": [{
88
+ "layer": "transportation",
89
+ "count": { "4": 812, "14": 1234567 },
90
+ "geometry": { "Point": 0, "LineString": 1200000, "Polygon": 34567 },
91
+ "attributes": [{
92
+ "attribute": "class",
93
+ "present": { "4": 812, "14": 1234000 },
94
+ "type": "string",
95
+ "types": { "string": 1234000, "number": 0, "bigint": 0, "boolean": 0 },
96
+ "values": { "exact": true, "distinct": 23, "top": [["motorway", 50211], ["primary", 40000]] }
97
+ }]
98
+ }]
99
+ }
100
+ ```
101
+
102
+ | Key | On | Meaning |
103
+ |---|---|---|
104
+ | `version` | document | Always `1`. |
105
+ | `counts` | document | `"zoom"` for an archive, `"total"` for features: the shape of every `count` and `present`. |
106
+ | `layers` | document | One entry per layer, sorted by name. |
107
+ | `count` | layer | Features in the layer. |
108
+ | `geometry` | layer | Features per geometry type, Multi* folded into the single type. |
109
+ | `attributes` | layer | One entry per attribute, sorted by name, at most `maxAttributes`. |
110
+ | `present` | attribute | Features that carry a value for the attribute. |
111
+ | `type` | attribute | The kind of the attribute, see below. |
112
+ | `types` | attribute | Values per JSON type: `string`, `number`, `bigint`, `boolean`. |
113
+ | `min`, `max` | attribute | The numeric range, on `integer`, `number` and `mixed-numeric`. |
114
+ | `integer` | attribute | Whether every numeric value is an integer, on `mixed-numeric`. |
115
+ | `values` | attribute | The distinct values: exact counts or an estimate. |
116
+
117
+ `type` tells what the values are and which keys the attribute has:
118
+
119
+ | `type` | Values |
120
+ |---|---|
121
+ | `string` | only strings |
122
+ | `boolean` | only booleans |
123
+ | `integer` | only numbers and bigints, all integers |
124
+ | `number` | only numbers and bigints, at least one not an integer |
125
+ | `mixed` | more than one kind, none numeric |
126
+ | `mixed-numeric` | more than one kind, numbers among them |
127
+
128
+ `values` is exact while an attribute has at most `maxDistinct` distinct values: `distinct` is the true count and `top`
129
+ holds the `maxTop` most frequent as `[value, count]` pairs. Past the limit `exact` is `false`, `distinct` is a
130
+ HyperLogLog estimate, and `examples` holds the `maxExamples` smallest values instead of `top`.
131
+
132
+ `null`, missing keys, `NaN`, the infinities, objects and arrays are absent: not a value and not counted in `present`.
133
+ Every key is always present where its shape names it, and no other keys appear. [SPEC.md](SPEC.md) defines all of it,
134
+ and `@mapka/tilestats/schema/vectorstats-1.json` is the JSON Schema (draft 2020-12) every document validates against.
135
+
136
+ ### What `count` means
137
+
138
+ The document says which kind of input it came from in `counts`. With `"zoom"` (an archive), `count` and each
139
+ attribute's `present` are objects keyed by zoom: the number of features in all tiles of that zoom. A feature that is
140
+ split across tiles counts once per tile, so a count is not a number of map objects, and a tile stored once but used at
141
+ many addresses (ocean, land fill) counts at every address. With `"total"` (features), they
142
+ are plain numbers of rows. See [SPEC.md](SPEC.md).
143
+
144
+ ### Reading bigints exactly
145
+
146
+ A 64-bit integer outside the safe range is written with all its digits, which plain `JSON.parse` rounds to a double.
147
+ To get a `bigint` back, use the reviver's `context.source`:
148
+
149
+ ```ts
150
+ const document = JSON.parse(text, (key, value, context) =>
151
+ typeof value === "number" && !Number.isSafeInteger(value) && /^-?\d+$/.test(context.source)
152
+ ? BigInt(context.source)
153
+ : value,
154
+ );
155
+ ```
156
+
157
+ ## CLI
158
+
159
+ ```sh
160
+ npx tilestats poland.pmtiles > vectorstats.json
161
+ ```
162
+
163
+ - `-h, --help` and `-V, --version`.
164
+
165
+ JSON goes to stdout. A bad argument or option exits with code 1 and says which one.
166
+
167
+ ## Benchmarks
168
+
169
+ To be measured again for the single-threaded streaming reader.
170
+
171
+ ## License
172
+
173
+ Apache-2.0
package/SPEC.md ADDED
@@ -0,0 +1,185 @@
1
+ # vectorstats, version 1
2
+
3
+ vectorstats is the output format of `@mapka/tilestats`: per-layer statistics of a vector tile archive or a feature
4
+ stream, written as JSON for people and for AI agents. This document defines it. The format has its own versioned
5
+ definition because the statistics other tools write for tiles carry no exact counts per zoom, no bigints and no
6
+ statement of how far a number can be trusted.
7
+
8
+ The JSON Schema (draft 2020-12) is `schema/vectorstats-1.json`, shipped with the package. Every document in this format
9
+ validates against it.
10
+
11
+ ## Example
12
+
13
+ ```json
14
+ {
15
+ "version": 1,
16
+ "counts": "zoom",
17
+ "layers": [{
18
+ "layer": "transportation",
19
+ "count": { "4": 812, "10": 96000, "14": 1234567 },
20
+ "geometry": { "Point": 0, "LineString": 1200000, "Polygon": 34567 },
21
+ "attributes": [{
22
+ "attribute": "class",
23
+ "present": { "4": 812, "10": 96000, "14": 1234000 },
24
+ "type": "string",
25
+ "types": { "string": 1234000, "number": 0, "bigint": 0, "boolean": 0 },
26
+ "values": { "exact": true, "distinct": 23, "top": [["motorway", 50211], ["primary", 40000]] }
27
+ }, {
28
+ "attribute": "layer",
29
+ "present": { "12": 30000, "14": 52000 },
30
+ "type": "integer",
31
+ "types": { "string": 0, "number": 52000, "bigint": 0, "boolean": 0 },
32
+ "min": -2,
33
+ "max": 5,
34
+ "values": { "exact": true, "distinct": 9, "top": [[0, 40000], [1, 9000]] }
35
+ }, {
36
+ "attribute": "maxspeed",
37
+ "present": { "14": 49200 },
38
+ "type": "mixed-numeric",
39
+ "types": { "string": 1200, "number": 48000, "bigint": 0, "boolean": 0 },
40
+ "min": 5,
41
+ "max": 140,
42
+ "integer": true,
43
+ "values": { "exact": true, "distinct": 31, "top": [[50, 20000], [30, 9000], ["signals", 800]] }
44
+ }, {
45
+ "attribute": "name",
46
+ "present": { "8": 9000, "14": 640000 },
47
+ "type": "string",
48
+ "types": { "string": 649000, "number": 0, "bigint": 0, "boolean": 0 },
49
+ "values": { "exact": false, "distinct": 182000, "examples": ["Aleja Jana Pawła II", "Aleje Jerozolimskie"] }
50
+ }]
51
+ }]
52
+ }
53
+ ```
54
+
55
+ ## Document
56
+
57
+ The document is an object with exactly `version` (the number `1`), `counts` and `layers`, an array of layers. No other
58
+ keys appear, at any level, and every key a shape names is always present: nothing is optional.
59
+
60
+ `counts` is `"zoom"` for an archive of tiles and `"total"` for a stream of features. It fixes the shape of every
61
+ `count` and `present` in the document, see below.
62
+
63
+ Each layer has `layer` (its name), `count`, `geometry` and `attributes`. Each attribute has `attribute` (its name),
64
+ `present`, `type`, `types`, then `min` and `max` and `integer` where `type` has them, then `values`, in that order.
65
+
66
+ `type` is the kind of the attribute and the discriminator of its shape. Numbers and bigints are both numeric.
67
+
68
+ | `type` | values | range keys |
69
+ |-----------------|---------------------------------------------------|--------------------------|
70
+ | `string` | only strings | none |
71
+ | `boolean` | only booleans | none |
72
+ | `integer` | only numerics, every one an integer | `min`, `max` |
73
+ | `number` | only numerics, at least one not an integer | `min`, `max` |
74
+ | `mixed` | more than one kind, none numeric | none |
75
+ | `mixed-numeric` | more than one kind, numerics among them | `min`, `max`, `integer` |
76
+
77
+ ## Layer count and attribute present
78
+
79
+ `count` (features of the layer) and `present` (features carrying the attribute) are the only per-zoom numbers.
80
+
81
+ - With `counts` `"zoom"` (tiles), each is an object keyed by zoom, ascending, with the zoom as a decimal string. The
82
+ value is the number of features in all tiles of that zoom. A feature split across tiles counts once per tile. Every
83
+ tile counts: an archive that stores one tile and uses it at many addresses (ocean, land fill) counts its features
84
+ once per address, as a map shows them. The keys of `present` are the zooms the attribute appears at. A layer
85
+ without features has `count` `{}`.
86
+ - With `counts` `"total"` (features), each is a plain number of rows.
87
+
88
+ ## Totals
89
+
90
+ Every other count is a total over the whole input, summed over zooms for tiles: `geometry`, `types` and the counts in
91
+ `top`. Use them for ratios ("97 % LineString"), not for absolute sizes.
92
+
93
+ ## geometry
94
+
95
+ An object with the number of features per type, always all three of `Point`, `LineString` and `Polygon`, `0` for a
96
+ type never seen. Multi geometries are folded into their single form. Features without geometry are not counted here.
97
+
98
+ ## types
99
+
100
+ Present on every attribute: it is where a reader tells bigints from numbers. An object with the number of values per
101
+ type, always all four of `string`, `number`, `bigint` and `boolean`, `0` for a type never seen.
102
+
103
+ ## Absent values
104
+
105
+ `null`, a missing key, `NaN` and the infinities are absent: not a value, not a type, not counted in `present`. Tiles
106
+ cannot tell `null` from missing, so a feature stream does not either. Objects and arrays in a feature stream are absent
107
+ too: version 1 has statistics for scalar values only. Per zoom, `count` minus `present` is the number of
108
+ features without the attribute.
109
+
110
+ ## bigint
111
+
112
+ A `bigint` is an integer outside the safe range, beyond plus or minus 2^53 - 1, that arrived as an exact 64-bit
113
+ integer. The type follows the value, not the wire encoding: a 64-bit integer inside the safe range counts as `number`
114
+ in `types`, so the same data encoded in two tile formats gives the same statistics. A JavaScript `number` always
115
+ counts as `number`, even when it is a large integer. An attribute of bigints alone has `type` `integer`.
116
+
117
+ Exact 64-bit integers come from MLT tiles and from feature streams. MVT tiles are read with `@mapbox/vector-tile`, which
118
+ reads every integer as a JavaScript `number`: an MVT integer outside the safe range is the nearest double and counts as
119
+ `number`.
120
+
121
+ In the JSON text a bigint is a JSON number with all its digits, for example `9223372036854775807`. The file is exact.
122
+ A reader using plain `JSON.parse` gets the nearest double, `9223372036854776000`, which is enough for most uses. A
123
+ reader that needs the digits uses the reviver's `context.source`:
124
+
125
+ ```js
126
+ const stats = JSON.parse(text, (key, value, context) =>
127
+ typeof value === "number" && !Number.isSafeInteger(value) && /^-?\d+$/.test(context.source)
128
+ ? BigInt(context.source)
129
+ : value,
130
+ );
131
+ ```
132
+
133
+ This turns every integer written outside the safe range into a `bigint`, in `top`, `examples`, `min` and `max`.
134
+ Integers inside the safe range stay numbers. A JavaScript `number` of magnitude 2^53 or more is also written as plain
135
+ digits (below 10^21, where JavaScript switches to exponent notation), so the reviver turns it into a `bigint` too, and
136
+ `types` is what tells a bigint from a number.
137
+
138
+ ## values
139
+
140
+ `values` describes the distinct values and is itself discriminated by `exact`. `distinct` is the number of distinct
141
+ values. `exact` is true when `distinct` is at most the distinct limit (`maxDistinct`, default 1000). Then every value
142
+ was counted exactly, and `top` holds the `maxTop` (default 100) most frequent values as `[value, count]` pairs, ordered
143
+ by count descending, then by value order. `examples` is absent.
144
+
145
+ Above the limit, `distinct` is a HyperLogLog estimate (see below), `exact` is false, `top` is absent and `examples`
146
+ holds the `maxExamples` (default 10) smallest values in value order, without counts.
147
+
148
+ ## Value order
149
+
150
+ Where order matters, in ties of `top` and in `examples`, values are ordered: booleans (false before true), then numbers
151
+ and bigints together numerically (a number before a bigint of equal value), then strings by code unit. Strings are
152
+ truncated to `maxStringLength` (default 256 code
153
+ units, never between the halves of a surrogate pair) before anything else sees them.
154
+
155
+ ## min, max and integer
156
+
157
+ Present as `type` says: `min` and `max` on `integer`, `number` and `mixed-numeric`, `integer` on `mixed-numeric` only.
158
+ `min` and `max` are taken over numbers and bigints together, exactly; a bigint bound is written with all its digits.
159
+ `integer` is true when every number is an integer; bigints always are. On a purely numeric attribute that flag is the
160
+ choice between `integer` and `number`.
161
+
162
+ ## Limits
163
+
164
+ At most `maxAttributes` (default 1000) attributes are kept per layer, the smallest names by code unit. Layers and
165
+ attributes are arrays sorted by name by code unit, so names that look like integers keep that order and are not
166
+ reordered the way object keys would be.
167
+
168
+ ## HyperLogLog
169
+
170
+ The estimate in `distinct` is part of version 1 and never changes within it. It is the HyperLogLog of
171
+ [distillate](https://github.com/akshay-xp/distillate) 0.19.3, hash variant 0:
172
+
173
+ - Precision 12: m = 4096 registers.
174
+ - A value is hashed as its key, the UTF-8 bytes of a type letter followed by the value: `s` and the string for
175
+ strings, `n` and the decimal text of the number (JavaScript `String(number)`, with -0 as 0) for numbers, `i` and the
176
+ decimal digits for bigints, `b1` or `b0` for booleans. A bigint inside the safe
177
+ range is a number and uses `n`.
178
+ - The hash is MurmurHash3 x86 128-bit with seed 0. Of its first output word `w0`, the top 12 bits are the register
179
+ index; the rank is 1 plus the number of leading zeros of the remaining bits followed by the second word `w1`.
180
+ - The estimate is Ertl's improved raw estimator (Ertl, "New cardinality estimation algorithms for HyperLogLog
181
+ sketches", 2017), which needs no switch to linear counting and has no bias bump at small cardinalities.
182
+ - `distinct` is the estimate rounded to an integer, and at least `maxDistinct + 1`, so an estimated attribute always
183
+ reports more distinct values than the limit.
184
+
185
+ The standard error is about 1.6 %.
package/dist/cli.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env node
2
+ export {};
3
+ //# sourceMappingURL=cli.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env node
2
+ import { readFileSync } from "node:fs";
3
+ import { Command } from "@commander-js/extra-typings";
4
+ import { z } from "zod";
5
+ import { tilestats } from "./tilestats.js";
6
+ const { version } = z
7
+ .object({ version: z.string() })
8
+ .parse(JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")));
9
+ async function write(text) {
10
+ return new Promise((resolve, reject) => {
11
+ process.stdout.write(text, (error) => {
12
+ if (error) {
13
+ reject(error);
14
+ }
15
+ else {
16
+ resolve();
17
+ }
18
+ });
19
+ });
20
+ }
21
+ const program = new Command()
22
+ .name("tilestats")
23
+ .description("Per-layer statistics (vectorstats) of a vector tile archive, as JSON on stdout")
24
+ .version(version)
25
+ .argument("<archive>", "a .pmtiles or .mbtiles archive")
26
+ .showHelpAfterError("(run tilestats --help for usage)")
27
+ .action(async (archive) => {
28
+ await write(`${JSON.stringify(await tilestats(archive))}\n`);
29
+ });
30
+ try {
31
+ await program.parseAsync();
32
+ }
33
+ catch (error) {
34
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
35
+ process.exitCode = 1;
36
+ }
37
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,EAAE,MAAM,6BAA6B,CAAC;AACtD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAE3C,MAAM,EAAE,OAAO,EAAE,GAAG,CAAC;KAClB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC;KAC/B,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,GAAG,CAAC,iBAAiB,EAAE,OAAO,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;AAExF,KAAK,UAAU,KAAK,CAAC,IAAY;IAC/B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,KAAK,EAAE,EAAE;YACnC,IAAI,KAAK,EAAE,CAAC;gBACV,MAAM,CAAC,KAAK,CAAC,CAAC;YAChB,CAAC;iBACI,CAAC;gBACJ,OAAO,EAAE,CAAC;YACZ,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC;AAED,MAAM,OAAO,GAAG,IAAI,OAAO,EAAE;KAC1B,IAAI,CAAC,WAAW,CAAC;KACjB,WAAW,CAAC,gFAAgF,CAAC;KAC7F,OAAO,CAAC,OAAO,CAAC;KAChB,QAAQ,CAAC,WAAW,EAAE,gCAAgC,CAAC;KACvD,kBAAkB,CAAC,kCAAkC,CAAC;KACtD,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;IACxB,MAAM,KAAK,CAAC,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,SAAS,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;AAC/D,CAAC,CAAC,CAAC;AAEL,IAAI,CAAC;IACH,MAAM,OAAO,CAAC,UAAU,EAAE,CAAC;AAC7B,CAAC;AACD,OAAO,KAAK,EAAE,CAAC;IACb,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpF,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC"}
@@ -0,0 +1,4 @@
1
+ export type { CountMode, CountsJSON, GeometryCounts, Limits, Options, OutputValue, RawInteger, TypeCounts, VectorStats, VectorStatsAttribute, VectorStatsLayer, } from "./schema.ts";
2
+ export type { Feature, FeatureInput, GeometryType, Value } from "./tilestats.ts";
3
+ export { tilestats } from "./tilestats.ts";
4
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,SAAS,EACT,UAAU,EACV,cAAc,EACd,MAAM,EACN,OAAO,EACP,WAAW,EACX,UAAU,EACV,UAAU,EACV,WAAW,EACX,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { tilestats } from "./tilestats.js";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC"}