@florasync/leaflet-geokit 0.10.0 → 0.11.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.
@@ -0,0 +1,38 @@
1
+ import type { StyleSpecification } from "@maplibre/maplibre-gl-style-spec";
2
+ import type { BasemapAdapter } from "./lib/providers.js";
3
+ /** Register once per application; leases share a cache. Do not also register the
4
+ * `pmtiles` scheme yourself. Release all leases after removing all adapter maps.
5
+ * Hosts already managing the scheme may omit this helper entirely.
6
+ */
7
+ export declare function registerPMTilesProtocol(): () => void;
8
+ /** Stable event surface of the renderer, without importing its private worker
9
+ * declarations into strict NodeNext consumers. The runtime value is a MapLibre
10
+ * Map; hosts needing its full API may narrow it using their own renderer types.
11
+ */
12
+ export interface PMTilesMap {
13
+ on(type: "error", listener: (event: {
14
+ error: Error;
15
+ }) => void): unknown;
16
+ on(type: "load" | "remove", listener: () => void): unknown;
17
+ off(type: "error", listener: (event: {
18
+ error: Error;
19
+ }) => void): unknown;
20
+ off(type: "load" | "remove", listener: () => void): unknown;
21
+ }
22
+ export interface PMTilesBasemapOptions {
23
+ /** Trusted MapLibre style; use pmtiles://https://... vector source URLs.
24
+ * Source-layer names must match the archive. No vendor schema is assumed.
25
+ */
26
+ style: StyleSpecification | string;
27
+ /** Trusted HTML required by your data license; shown by Leaflet. */
28
+ attribution: string;
29
+ /** Called on mount, before asynchronous load/error events. Attach listeners
30
+ * here for readiness and explicit host fallback. Never implies offline support.
31
+ */
32
+ onMap?: (map: PMTilesMap) => void;
33
+ }
34
+ /** Optional concrete MapLibre bridge. Use /external and the same Leaflet module.
35
+ * Leaflet retains interaction, drawing, and view ownership (no pitch/rotation).
36
+ * Protocol registration is explicit and application-owned.
37
+ */
38
+ export declare function createPMTilesBasemapAdapter(options: PMTilesBasemapOptions): BasemapAdapter;
@@ -0,0 +1,153 @@
1
+ # Optional Protomaps / PMTiles basemaps
2
+
3
+ GeoKit's `/pmtiles` entrypoint supplies a concrete MapLibre-to-Leaflet adapter.
4
+ The core and `/external` entrypoints do not import renderer dependencies. Install
5
+ these optional peers only when using this entrypoint:
6
+
7
+ ```sh
8
+ npm install maplibre-gl@5.24.0 @maplibre/maplibre-gl-leaflet@0.1.4 pmtiles@4.5.0
9
+ ```
10
+
11
+ These exact versions, Leaflet 1.9.4 and Leaflet.draw 1.0.4 are exercised by the
12
+ browser fixture. MapLibre 6 is not claimed as verified. The bridge's named
13
+ `maplibreGL` export and `getMaplibreMap()` API are used, not historical global
14
+ `L.maplibreGL` registration. Use **one** shared Leaflet and MapLibre installation;
15
+ use GeoKit `/external`, not the core bundle's private Leaflet copy.
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import L from "leaflet";
21
+ import "leaflet-draw";
22
+ import "leaflet/dist/leaflet.css";
23
+ import "leaflet-draw/dist/leaflet.draw.css";
24
+ import "maplibre-gl/dist/maplibre-gl.css";
25
+ import { LeafletDrawMapElement } from "@florasync/leaflet-geokit/external";
26
+ import {
27
+ createPMTilesBasemapAdapter,
28
+ registerPMTilesProtocol,
29
+ } from "@florasync/leaflet-geokit/pmtiles";
30
+
31
+ const releaseProtocol = registerPMTilesProtocol();
32
+ const element = new LeafletDrawMapElement();
33
+ element.leafletInstance = L; // before connecting
34
+ // Configure latitude/longitude/zoom, draw tools and licensed raster fallback.
35
+ element.setAttribute("min-zoom", "1");
36
+ const adapter = createPMTilesBasemapAdapter({
37
+ attribution: "Your dataset's required attribution (trusted HTML)",
38
+ style: {
39
+ version: 8,
40
+ sources: {
41
+ region: {
42
+ type: "vector",
43
+ url: "pmtiles://https://your-host/region.pmtiles",
44
+ },
45
+ },
46
+ layers: [
47
+ {
48
+ id: "water",
49
+ type: "fill",
50
+ source: "region",
51
+ "source-layer": "water",
52
+ paint: { "fill-color": "#a0c8e8" },
53
+ },
54
+ ],
55
+ },
56
+ onMap(map) {
57
+ map.on("error", (event) => {
58
+ // Host decides whether to report, retry or restore raster. No paid fallback.
59
+ console.warn("Basemap failed", event.error);
60
+ });
61
+ },
62
+ });
63
+ element.addEventListener("leaflet-draw:ready", () => {
64
+ element.setBasemapAdapter(adapter);
65
+ });
66
+ document.body.append(element);
67
+
68
+ // Later: removes the renderer, not your Leaflet-drawn features.
69
+ element.setBasemapAdapter(null);
70
+ element.remove();
71
+ releaseProtocol();
72
+ ```
73
+
74
+ Supply your own MapLibre style object or URL. **PMTiles is archive transport, not
75
+ a basemap schema or a style generator.** For a Protomaps basemap archive, use the
76
+ matching Protomaps style/source-layer schema, glyphs, sprites and data attribution.
77
+ GeoKit deliberately does not guess those from an archive name. Remote style URLs,
78
+ attribution HTML, URLs and callbacks must be trusted application input.
79
+
80
+ `onMap` runs immediately after mount, before asynchronous `load`/`error` events;
81
+ use it to attach listeners. The public `PMTilesMap` type exposes `on`/`off` for
82
+ `load`, `error` (with `event.error: Error`), and `remove`. The runtime object is a
83
+ MapLibre map; hosts needing additional renderer APIs may assert their own
84
+ `maplibre-gl` `Map` type. Style objects retain full validation through the
85
+ standalone MapLibre style specification. This keeps renderer-internal worker
86
+ declarations out of strict NodeNext consumers without `skipLibCheck`.
87
+ Each mount gets a fresh layer and MapLibre map. The
88
+ bridge owns `Map.remove()` on raster switch/disconnect; do not remove that map
89
+ yourself. `onMap` exceptions roll back mounting. A synchronous WebGL constructor
90
+ failure removes the half-created container and the existing raster stays active.
91
+ Async network/render errors do not automatically change providers.
92
+
93
+ `registerPMTilesProtocol()` returns an idempotent release function. Multiple
94
+ calls share the protocol/cache; final release throws while GeoKit PMTiles layers
95
+ are mounted, so callers may retry after removal. It has **exclusive ownership of
96
+ the MapLibre `pmtiles` scheme**. Do not combine it with your own registration or
97
+ multiple bundled copies of this optional entrypoint. If your application already
98
+ registers `new Protocol().tile` with MapLibre, omit this helper and keep protocol
99
+ teardown entirely application-owned (including other non-GeoKit maps).
100
+
101
+ ## Runnable example and verification
102
+
103
+ From a checkout:
104
+
105
+ ```sh
106
+ npm ci --ignore-scripts
107
+ npm run build
108
+ npm run dev
109
+ # Open http://localhost:5173/pmtiles.html
110
+ ```
111
+
112
+ `pmtiles.html` / `examples/pmtiles.ts` use the **built** optional and external
113
+ entrypoints with a local, licensed synthetic archive. No external map server,
114
+ API key, fonts or sprites are required. Draw a polygon, switch to local raster,
115
+ then return to PMTiles. The separate test server uses ports 5197 and 5198 for a
116
+ real cross-origin range/CORS check:
117
+
118
+ ```sh
119
+ npm run typecheck
120
+ npm run test:unit:focused -- tests/pmtiles.spec.ts tests/providers.spec.ts tests/component-tile-provider.spec.ts tests/map-controller.spec.ts tests/component-events.spec.ts
121
+ npm run build
122
+ npm run test:e2e:pmtiles
123
+ node scripts/verify-pmtiles-bundle.mjs
124
+ ```
125
+
126
+ Chromium with SwiftShader exercises real MapLibre workers, the PMTiles decoder,
127
+ HTTP 206/Content-Range/CORS, rendered feature queries and actual WebGL framebuffer
128
+ pixels. It also exercises pointer drawing, feature/ID preservation across repeated
129
+ switches, view synchronization, two simultaneous maps, renderer remove events,
130
+ DOM cleanup, guarded protocol release, HTTP failure/raster fallback and actual
131
+ WebGL-disabled browser constructor failure/rollback. Artifacts
132
+ are in `test-results/pmtiles/` (screenshot and range/pixel JSON).
133
+ See [fixture provenance](../test-data/pmtiles/README.md).
134
+
135
+ ## Boundaries
136
+
137
+ - **No offline claim.** Capability remains `unknown`. Local HTTP fixture success
138
+ does not test offline caches, service workers, range caches or permission to
139
+ redistribute an archive. Network-disabled behavior is unverified.
140
+ - Archive hosting must support byte ranges and CORS, including exposed
141
+ `Content-Range`/`ETag`; do not gzip the whole archive at the HTTP layer.
142
+ - Leaflet owns drawing and interaction; the basemap is noninteractive in tilePane.
143
+ Pitch/rotation are not supported. Follow upstream minZoom 1 and latitude/bounds
144
+ guidance for your data; unconstrained global wrap/poles are not certified.
145
+ - This fixture is not a production Protomaps global basemap. Large archives,
146
+ production hosting, raster archives, fonts/sprites, high-DPI/touch, mobile GPUs,
147
+ WebGL context loss, restrictive worker/CSP configurations and memory profiling
148
+ still need application-specific validation. DOM/remove-event tests are not a
149
+ heap/worker leak audit.
150
+
151
+ Upstream references: [Leaflet bridge](https://github.com/maplibre/maplibre-gl-leaflet),
152
+ [PMTiles + MapLibre](https://docs.protomaps.com/pmtiles/maplibre),
153
+ [Protomaps basemaps](https://docs.protomaps.com/basemaps/).
package/docs/providers.md CHANGED
@@ -65,70 +65,12 @@ capabilities report `requested/active: "adapter"` without vendor configuration.
65
65
 
66
66
  ## MapLibre and PMTiles execution path
67
67
 
68
- This release ships a **dependency-injected feasibility shim**, not a production
69
- WebGL renderer. The next optional adapter package can use the existing public
70
- `setBasemapAdapter` method without changing web component consumers.
71
-
72
- Host-side experiment (not installed by this slice):
73
-
74
- 1. Create a separate adapter/example package. Select and pin mutually compatible
75
- `leaflet`, `maplibre-gl`, `@maplibre/maplibre-gl-leaflet`, and `pmtiles` versions.
76
- The current upstream bridge documentation uses a named `maplibreGL` export;
77
- older bridge versions use different registration patterns. Recheck the selected
78
- release, rather than copying a historical global `L.maplibreGL` call.
79
- 2. Use GeoKit's external entrypoint and one shared Leaflet instance. Load Leaflet,
80
- draw and MapLibre styles, configure MapLibre's worker/CSP requirements, and
81
- supply `leafletInstance` before connecting the element.
82
- 3. Register PMTiles once per host application. Feed the bridge a style whose source
83
- is `type: "vector", url: "pmtiles://https://your-host/region.pmtiles"`.
84
- PMTiles is a tile archive transport, not a renderer; it also supports raster
85
- archives. This vector bridge contract describes the rendering path, not every
86
- source inside a MapLibre style.
87
- 4. Wrap the bridge factory in the shipped shim:
88
-
89
- ```ts
90
- import { maplibreGL } from "@maplibre/maplibre-gl-leaflet";
91
- import maplibregl from "maplibre-gl";
92
- import { Protocol } from "pmtiles";
93
- import { createMapLibreBasemapAdapter } from "@florasync/leaflet-geokit/external";
94
-
95
- // Host initialization: once, using the SAME MapLibre module as the bridge.
96
- const protocol = new Protocol();
97
- maplibregl.addProtocol("pmtiles", protocol.tile);
98
-
99
- // After GeoKit ready; /maps/style.json contains the PMTiles source.
100
- mapElement.setBasemapAdapter(
101
- createMapLibreBasemapAdapter({
102
- createLayer: () => maplibreGL({ style: "/maps/style.json" }),
103
- attribution: "Your licensed dataset attribution",
104
- offline: "host-managed",
105
- apiKey: "none",
106
- }),
107
- );
108
-
109
- // Remove all maps/bridges BEFORE host-global protocol teardown.
110
- mapElement.setBasemapAdapter(null);
111
- maplibregl.removeProtocol("pmtiles");
112
- ```
113
-
114
- This recipe is sourced from upstream APIs, but the optional packages and actual
115
- archive rendering are **not exercised by this slice**. Before production promotion:
116
-
117
- - Verify HTTP range requests, CORS, archive content type, source-layer names,
118
- fonts, sprites and licensing with an owned fixture archive.
119
- - Verify offline mode with network disabled and all required assets provisioned.
120
- `host-managed` declares potential, not a cache implementation or permission.
121
- - Exercise pan/zoom, high-DPI, touch, resize, WebGL loss, worker/CSP failures,
122
- renderer error fallback, repeated swaps and multiple simultaneous maps.
123
- - Keep drawing above the basemap and pointer events with Leaflet. The upstream
124
- bridge has no pitch/rotation support and recommends minZoom 1 plus bounds
125
- restrictions to avoid renderer synchronization issues. Test those explicitly.
126
- - Confirm no duplicate Leaflet/MapLibre module instances and no worker/listener
127
- leaks on removal. Do not remove a global PMTiles protocol still used by another map.
128
-
129
- Sources:
130
- [MapLibre Leaflet bridge](https://github.com/maplibre/maplibre-gl-leaflet),
131
- [PMTiles protocol integration](https://docs.protomaps.com/pmtiles/maplibre).
68
+ The generic `createMapLibreBasemapAdapter` remains dependency-injected. For a
69
+ concrete, optional MapLibre/PMTiles implementation, use the new
70
+ [`/pmtiles` entrypoint](pmtiles.md). It leaves renderer dependencies out of core
71
+ and retains Leaflet.draw. The optional browser suite exercises a real licensed
72
+ PMTiles archive, range/CORS requests, actual WebGL pixels, drawing and teardown.
73
+ Offline operation and production Protomaps styles/assets are not implied.
132
74
 
133
75
  ## Capability diagnostics
134
76
 
@@ -188,4 +130,5 @@ External raster requests are intercepted with deterministic image fixtures; HERE
188
130
  credentials and service access are not tested. The vector fixture is deliberately
189
131
  an injected Leaflet layer, not an imitation MapLibre renderer. It proves the shim
190
132
  boundary, attribution, diagnostics, public switching and real pointer drawing with
191
- feature preservation. Actual WebGL/PMTiles is the optional follow-on gate above.
133
+ feature preservation. Actual WebGL/PMTiles is covered separately by
134
+ `npm run test:e2e:pmtiles`; see [optional adapter verification](pmtiles.md).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@florasync/leaflet-geokit",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Framework-agnostic Leaflet + Leaflet.draw web component with TypeScript, verbose logging, and tests.",
5
5
  "type": "module",
6
6
  "main": "./dist/leaflet-geokit.umd.js",
@@ -35,6 +35,10 @@
35
35
  "./react-bundled": {
36
36
  "types": "./dist/types/react-bundled/index.d.ts",
37
37
  "import": "./dist/react-bundled/index.js"
38
+ },
39
+ "./pmtiles": {
40
+ "types": "./dist/types/pmtiles.d.ts",
41
+ "import": "./dist/leaflet-geokit.pmtiles.es.js"
38
42
  }
39
43
  },
40
44
  "files": [
@@ -61,7 +65,8 @@
61
65
  "docs/grower-geometry.md",
62
66
  "docs/grower-measurement.md",
63
67
  "docs/snapping-and-measurements.md",
64
- "CHANGELOG.md"
68
+ "CHANGELOG.md",
69
+ "docs/pmtiles.md"
65
70
  ],
66
71
  "sideEffects": [
67
72
  "**/*.css"
@@ -79,7 +84,7 @@
79
84
  "homepage": "https://github.com/FloraSync/leaflet-geokit#readme",
80
85
  "scripts": {
81
86
  "dev": "vite",
82
- "build": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json --emitDeclarationOnly && tsc-alias -p tsconfig.build.json && node scripts/resolve-declaration-imports.mjs && vite build && vite build -c vite.config.external.ts && vite build -c vite.config.django.ts && vite build -c vite.config.preact.ts && vite build -c vite.config.preact-bundled.ts && vite build -c vite.config.react.ts && vite build -c vite.config.react-bundled.ts",
87
+ "build": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true })\" && tsc -p tsconfig.build.json --emitDeclarationOnly && tsc-alias -p tsconfig.build.json && node scripts/resolve-declaration-imports.mjs && vite build && vite build -c vite.config.external.ts && vite build -c vite.config.django.ts && vite build -c vite.config.preact.ts && vite build -c vite.config.preact-bundled.ts && vite build -c vite.config.react.ts && vite build -c vite.config.react-bundled.ts && vite build -c vite.config.pmtiles.ts",
83
88
  "build:analyze": "npm run build:analyze:main && npm run build:analyze:external && npm run build:analyze:django && npm run build:analyze:preact && npm run build:analyze:preact-bundled && npm run build:analyze:react && npm run build:analyze:react-bundled",
84
89
  "build:analyze:main": "BUNDLE_ANALYZE=1 BUNDLE_ANALYZE_REPORT=dist/stats/main.html vite build",
85
90
  "build:analyze:external": "BUNDLE_ANALYZE=1 BUNDLE_ANALYZE_REPORT=dist/stats/external.html vite build -c vite.config.external.ts",
@@ -108,7 +113,8 @@
108
113
  "pack:dry": "npm pack --dry-run",
109
114
  "test:consumer": "node scripts/verify-package.mjs",
110
115
  "release:dry": "npm run typecheck && npm run test:unit && npm run build && npm pack --dry-run && npm run test:consumer",
111
- "test:e2e:snapping": "playwright test --config=playwright.snapping.config.ts"
116
+ "test:e2e:snapping": "playwright test --config=playwright.snapping.config.ts",
117
+ "test:e2e:pmtiles": "playwright test -c playwright.pmtiles.config.ts"
112
118
  },
113
119
  "keywords": [
114
120
  "leaflet",
@@ -126,6 +132,7 @@
126
132
  "npm": ">=9"
127
133
  },
128
134
  "dependencies": {
135
+ "@maplibre/maplibre-gl-style-spec": "^24.10.0",
129
136
  "@turf/area": "^7.4.0",
130
137
  "@turf/bbox": "^7.4.0",
131
138
  "@turf/boolean-valid": "^7.4.0",
@@ -142,6 +149,9 @@
142
149
  "uuid": "^11.1.1"
143
150
  },
144
151
  "peerDependencies": {
152
+ "@maplibre/maplibre-gl-leaflet": "^0.1.4",
153
+ "maplibre-gl": "^5.24.0",
154
+ "pmtiles": "^4.5.0",
145
155
  "preact": "^10.24.0",
146
156
  "react": "^18.3.1",
147
157
  "react-dom": "^18.3.1"
@@ -155,10 +165,20 @@
155
165
  },
156
166
  "react-dom": {
157
167
  "optional": true
168
+ },
169
+ "maplibre-gl": {
170
+ "optional": true
171
+ },
172
+ "@maplibre/maplibre-gl-leaflet": {
173
+ "optional": true
174
+ },
175
+ "pmtiles": {
176
+ "optional": true
158
177
  }
159
178
  },
160
179
  "devDependencies": {
161
180
  "@eslint/js": "^10.0.1",
181
+ "@maplibre/maplibre-gl-leaflet": "0.1.4",
162
182
  "@playwright/test": "^1.58.2",
163
183
  "@preact/preset-vite": "^2.8.0",
164
184
  "@testing-library/dom": "^10.4.0",
@@ -179,6 +199,8 @@
179
199
  "happy-dom": "^20.6.3",
180
200
  "husky": "^9.1.7",
181
201
  "lint-staged": "^16.4.0",
202
+ "maplibre-gl": "5.24.0",
203
+ "pmtiles": "4.5.0",
182
204
  "preact": "^10.28.3",
183
205
  "prettier": "^3.3.2",
184
206
  "react": "^18.3.1",