@india-boundary-corrector/openlayers-layer 0.0.2 → 0.0.4
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 +23 -6
- package/dist/index.cjs +23 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.global.js +163 -71
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +24 -31
- package/dist/index.js.map +1 -1
- package/dist/india_boundary_corrections.pmtiles +0 -0
- package/package.json +6 -5
- package/src/index.d.ts +4 -2
- package/src/index.js +28 -47
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ No bundler required! Just include the script and use the global `IndiaBoundaryCo
|
|
|
21
21
|
```html
|
|
22
22
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
|
|
23
23
|
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
|
|
24
|
-
<script src="https://
|
|
24
|
+
<script src="https://cdn.jsdelivr.net/npm/@india-boundary-corrector/openlayers-layer/dist/index.global.js"></script>
|
|
25
25
|
|
|
26
26
|
<div id="map" style="height: 400px;"></div>
|
|
27
27
|
|
|
@@ -77,7 +77,10 @@ import { IndiaBoundaryCorrectedTileLayer } from '@india-boundary-corrector/openl
|
|
|
77
77
|
|
|
78
78
|
const layer = new IndiaBoundaryCorrectedTileLayer({
|
|
79
79
|
url: 'https://{a-c}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png',
|
|
80
|
-
layerConfig: 'cartodb-dark'
|
|
80
|
+
layerConfig: 'cartodb-dark',
|
|
81
|
+
sourceOptions: {
|
|
82
|
+
attributions: '© CARTO © OpenStreetMap contributors'
|
|
83
|
+
}
|
|
81
84
|
});
|
|
82
85
|
```
|
|
83
86
|
|
|
@@ -98,13 +101,19 @@ const osmDeConfig = new LayerConfig({
|
|
|
98
101
|
|
|
99
102
|
const layer = new IndiaBoundaryCorrectedTileLayer({
|
|
100
103
|
url: 'https://tile.openstreetmap.de/{z}/{x}/{y}.png',
|
|
101
|
-
layerConfig: osmDeConfig
|
|
104
|
+
layerConfig: osmDeConfig,
|
|
105
|
+
sourceOptions: {
|
|
106
|
+
attributions: '© OpenStreetMap contributors'
|
|
107
|
+
}
|
|
102
108
|
});
|
|
103
109
|
|
|
104
110
|
// Or use extraLayerConfigs for auto-detection
|
|
105
111
|
const layer2 = new IndiaBoundaryCorrectedTileLayer({
|
|
106
112
|
url: 'https://tile.openstreetmap.de/{z}/{x}/{y}.png',
|
|
107
|
-
extraLayerConfigs: [osmDeConfig]
|
|
113
|
+
extraLayerConfigs: [osmDeConfig],
|
|
114
|
+
sourceOptions: {
|
|
115
|
+
attributions: '© OpenStreetMap contributors'
|
|
116
|
+
}
|
|
108
117
|
});
|
|
109
118
|
```
|
|
110
119
|
|
|
@@ -114,7 +123,10 @@ const layer2 = new IndiaBoundaryCorrectedTileLayer({
|
|
|
114
123
|
import { indiaBoundaryCorrectedTileLayer } from '@india-boundary-corrector/openlayers-layer';
|
|
115
124
|
|
|
116
125
|
const layer = indiaBoundaryCorrectedTileLayer({
|
|
117
|
-
url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
|
|
126
|
+
url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png',
|
|
127
|
+
sourceOptions: {
|
|
128
|
+
attributions: '© OpenStreetMap contributors'
|
|
129
|
+
}
|
|
118
130
|
});
|
|
119
131
|
```
|
|
120
132
|
|
|
@@ -126,7 +138,8 @@ const layer = indiaBoundaryCorrectedTileLayer({
|
|
|
126
138
|
| `pmtilesUrl` | string | URL to PMTiles file (auto-detected if not provided) |
|
|
127
139
|
| `layerConfig` | LayerConfig \| string | Layer config object or config ID |
|
|
128
140
|
| `extraLayerConfigs` | LayerConfig[] | Additional configs for auto-detection |
|
|
129
|
-
| `tileSize` | number | Tile size in pixels (default: 256) |
|
|
141
|
+
| `tileSize` | number | Tile size in pixels for OpenLayers source (default: 256) |
|
|
142
|
+
| `fallbackOnCorrectionFailure` | boolean | Return original tile if corrections fail (default: true) |
|
|
130
143
|
| `sourceOptions` | Object | Additional options passed to XYZ source |
|
|
131
144
|
| `...layerOptions` | Object | Additional options passed to TileLayer |
|
|
132
145
|
|
|
@@ -158,6 +171,10 @@ layer.on('correctionerror', (e) => {
|
|
|
158
171
|
| `coords` | object | Tile coordinates `{ z, x, y }` |
|
|
159
172
|
| `tileUrl` | string | URL of the tile being loaded |
|
|
160
173
|
|
|
174
|
+
## Bundling
|
|
175
|
+
|
|
176
|
+
If you're bundling your application (Rollup, Webpack, Vite, etc.), you may need to copy the PMTiles data file to your output directory. See **[Bundling the PMTiles Asset](../data/bundling-pmtiles.md)** for instructions.
|
|
177
|
+
|
|
161
178
|
## License
|
|
162
179
|
|
|
163
180
|
Unlicense
|
package/dist/index.cjs
CHANGED
|
@@ -32,7 +32,6 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
IndiaBoundaryCorrectedTileLayer: () => IndiaBoundaryCorrectedTileLayer,
|
|
34
34
|
LayerConfig: () => import_layer_configs2.LayerConfig,
|
|
35
|
-
fetchAndFixTile: () => fetchAndFixTile,
|
|
36
35
|
getPmtilesUrl: () => import_data2.getPmtilesUrl,
|
|
37
36
|
indiaBoundaryCorrectedTileLayer: () => indiaBoundaryCorrectedTileLayer,
|
|
38
37
|
layerConfigs: () => import_layer_configs2.layerConfigs
|
|
@@ -45,30 +44,34 @@ var import_layer_configs = require("@india-boundary-corrector/layer-configs");
|
|
|
45
44
|
var import_tilefixer = require("@india-boundary-corrector/tilefixer");
|
|
46
45
|
var import_layer_configs2 = require("@india-boundary-corrector/layer-configs");
|
|
47
46
|
var import_data2 = require("@india-boundary-corrector/data");
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
{ tileSize, mode: "cors" }
|
|
56
|
-
);
|
|
57
|
-
const blob = new Blob([data], { type: wasFixed ? "image/png" : void 0 });
|
|
58
|
-
return { blob, wasFixed, correctionsFailed, correctionsError };
|
|
47
|
+
function getFetchOptionsFromCrossOrigin(crossOrigin) {
|
|
48
|
+
if (crossOrigin === "anonymous" || crossOrigin === "") {
|
|
49
|
+
return { mode: "cors", credentials: "omit" };
|
|
50
|
+
} else if (crossOrigin === "use-credentials") {
|
|
51
|
+
return { mode: "cors", credentials: "include" };
|
|
52
|
+
}
|
|
53
|
+
return { mode: "same-origin", credentials: "same-origin" };
|
|
59
54
|
}
|
|
60
|
-
function createCorrectedTileLoadFunction(tileFixer, layerConfig,
|
|
55
|
+
function createCorrectedTileLoadFunction(tileFixer, layerConfig, layer, fetchOptions, fallbackOnCorrectionFailure) {
|
|
61
56
|
return async function(imageTile, src) {
|
|
62
57
|
const tileCoord = imageTile.getTileCoord();
|
|
63
58
|
const z = tileCoord[0];
|
|
64
59
|
const x = tileCoord[1];
|
|
65
60
|
const y = tileCoord[2];
|
|
66
61
|
try {
|
|
67
|
-
const {
|
|
62
|
+
const { data, correctionsFailed, correctionsError } = await tileFixer.fetchAndFixTile(
|
|
63
|
+
src,
|
|
64
|
+
z,
|
|
65
|
+
x,
|
|
66
|
+
y,
|
|
67
|
+
layerConfig,
|
|
68
|
+
{ fallbackOnCorrectionFailure, ...fetchOptions }
|
|
69
|
+
);
|
|
68
70
|
if (correctionsFailed) {
|
|
69
71
|
console.warn("[IndiaBoundaryCorrectedTileLayer] Corrections fetch failed:", correctionsError);
|
|
70
72
|
layer.dispatchEvent({ type: "correctionerror", error: correctionsError, coords: { z, x, y }, tileUrl: src });
|
|
71
73
|
}
|
|
74
|
+
const blob = new Blob([data]);
|
|
72
75
|
const image = imageTile.getImage();
|
|
73
76
|
if (typeof image.getContext === "function") {
|
|
74
77
|
const imageBitmap = await createImageBitmap(blob);
|
|
@@ -106,7 +109,8 @@ var IndiaBoundaryCorrectedTileLayer = class extends import_Tile.default {
|
|
|
106
109
|
* @param {string} [options.pmtilesUrl] - URL to PMTiles file (defaults to CDN)
|
|
107
110
|
* @param {Object|string} [options.layerConfig] - LayerConfig or config ID (auto-detected if not provided)
|
|
108
111
|
* @param {Object[]} [options.extraLayerConfigs] - Additional LayerConfigs for matching
|
|
109
|
-
* @param {number} [options.tileSize=256] - Tile size in pixels
|
|
112
|
+
* @param {number} [options.tileSize=256] - Tile size in pixels (for OpenLayers source)
|
|
113
|
+
* @param {boolean} [options.fallbackOnCorrectionFailure=true] - Return original tile if corrections fail
|
|
110
114
|
* @param {Object} [options.sourceOptions] - Additional options for XYZ source
|
|
111
115
|
* @param {Object} [options.layerOptions] - Additional options for TileLayer
|
|
112
116
|
*/
|
|
@@ -117,6 +121,7 @@ var IndiaBoundaryCorrectedTileLayer = class extends import_Tile.default {
|
|
|
117
121
|
layerConfig,
|
|
118
122
|
extraLayerConfigs,
|
|
119
123
|
tileSize = 256,
|
|
124
|
+
fallbackOnCorrectionFailure = true,
|
|
120
125
|
sourceOptions = {},
|
|
121
126
|
...layerOptions
|
|
122
127
|
} = options;
|
|
@@ -129,13 +134,14 @@ var IndiaBoundaryCorrectedTileLayer = class extends import_Tile.default {
|
|
|
129
134
|
} else {
|
|
130
135
|
resolvedConfig = registry.detectFromTemplates([url]);
|
|
131
136
|
}
|
|
132
|
-
const tileFixer =
|
|
137
|
+
const tileFixer = import_tilefixer.TileFixer.getOrCreate(pmtilesUrl ?? (0, import_data.getPmtilesUrl)());
|
|
133
138
|
const source = new import_XYZ.default({
|
|
134
139
|
url,
|
|
135
140
|
tileSize,
|
|
136
141
|
crossOrigin: "anonymous",
|
|
137
142
|
...sourceOptions
|
|
138
143
|
});
|
|
144
|
+
const fetchOptions = getFetchOptionsFromCrossOrigin(sourceOptions.crossOrigin ?? "anonymous");
|
|
139
145
|
super({
|
|
140
146
|
source,
|
|
141
147
|
...layerOptions
|
|
@@ -144,7 +150,7 @@ var IndiaBoundaryCorrectedTileLayer = class extends import_Tile.default {
|
|
|
144
150
|
this._layerConfig = resolvedConfig;
|
|
145
151
|
this._registry = registry;
|
|
146
152
|
if (resolvedConfig) {
|
|
147
|
-
source.setTileLoadFunction(createCorrectedTileLoadFunction(tileFixer, resolvedConfig,
|
|
153
|
+
source.setTileLoadFunction(createCorrectedTileLoadFunction(tileFixer, resolvedConfig, this, fetchOptions, fallbackOnCorrectionFailure));
|
|
148
154
|
}
|
|
149
155
|
if (!resolvedConfig) {
|
|
150
156
|
console.warn("[IndiaBoundaryCorrectedTileLayer] Could not detect layer config from URL. Corrections will not be applied.");
|
|
@@ -171,19 +177,6 @@ var IndiaBoundaryCorrectedTileLayer = class extends import_Tile.default {
|
|
|
171
177
|
getRegistry() {
|
|
172
178
|
return this._registry;
|
|
173
179
|
}
|
|
174
|
-
/**
|
|
175
|
-
* Fetch and fix a tile (exposed for testing).
|
|
176
|
-
* @param {string} src - Tile URL
|
|
177
|
-
* @param {number} z - Zoom level
|
|
178
|
-
* @param {number} x - Tile X coordinate
|
|
179
|
-
* @param {number} y - Tile Y coordinate
|
|
180
|
-
* @returns {Promise<{blob: Blob, wasFixed: boolean}>}
|
|
181
|
-
* @private
|
|
182
|
-
*/
|
|
183
|
-
async _fetchAndFixTile(src, z, x, y) {
|
|
184
|
-
const tileSize = this.getSource().getTileGrid()?.getTileSize(z) || 256;
|
|
185
|
-
return fetchAndFixTile(src, z, x, y, this._tileFixer, this._layerConfig, tileSize);
|
|
186
|
-
}
|
|
187
180
|
};
|
|
188
181
|
function indiaBoundaryCorrectedTileLayer(options) {
|
|
189
182
|
return new IndiaBoundaryCorrectedTileLayer(options);
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.js"],"sourcesContent":["import TileLayer from 'ol/layer/Tile.js';\nimport XYZ from 'ol/source/XYZ.js';\nimport { getPmtilesUrl } from '@india-boundary-corrector/data';\nimport { layerConfigs } from '@india-boundary-corrector/layer-configs';\nimport { BoundaryCorrector as TileFixer } from '@india-boundary-corrector/tilefixer';\n\n// Re-export for convenience\nexport { layerConfigs, LayerConfig } from '@india-boundary-corrector/layer-configs';\nexport { getPmtilesUrl } from '@india-boundary-corrector/data';\n\n/**\n * Handle tile fetching and correction application logic.\n * This method is extracted for testability.\n * @param {string} src - URL of the raster tile\n * @param {number} z - Zoom level\n * @param {number} x - Tile X coordinate\n * @param {number} y - Tile Y coordinate\n * @param {TileFixer} tileFixer - TileFixer instance\n * @param {Object} layerConfig - Layer configuration\n * @param {number} tileSize - Tile size in pixels\n * @returns {Promise<{blob: Blob, wasFixed: boolean, correctionsFailed: boolean, correctionsError: Error|null}>}\n */\nasync function fetchAndFixTile(src, z, x, y, tileFixer, layerConfig, tileSize) {\n const { data, wasFixed, correctionsFailed, correctionsError } = await tileFixer.fetchAndFixTile(\n src, z, x, y, layerConfig, { tileSize, mode: 'cors' }\n );\n const blob = new Blob([data], { type: wasFixed ? 'image/png' : undefined });\n return { blob, wasFixed, correctionsFailed, correctionsError };\n}\n\n/**\n * Create a custom tileLoadFunction that applies boundary corrections.\n * @param {TileFixer} tileFixer - The TileFixer instance\n * @param {Object} layerConfig - The layer configuration\n * @param {number} tileSize - Tile size in pixels\n * @param {IndiaBoundaryCorrectedTileLayer} layer - The layer instance for event dispatching\n * @returns {Function} Custom tile load function\n */\nfunction createCorrectedTileLoadFunction(tileFixer, layerConfig, tileSize, layer) {\n return async function(imageTile, src) {\n const tileCoord = imageTile.getTileCoord();\n const z = tileCoord[0];\n const x = tileCoord[1];\n const y = tileCoord[2];\n\n // TODO: Pass AbortSignal to fetchAndFixTile to cancel in-flight requests when tiles\n // go off-screen. OpenLayers' tileLoadFunction doesn't provide an AbortController,\n // so this would require custom tracking. Deferred due to complexity - will revisit\n // if performance becomes an issue during rapid panning.\n try {\n const { blob, correctionsFailed, correctionsError } = await fetchAndFixTile(src, z, x, y, tileFixer, layerConfig, tileSize);\n\n if (correctionsFailed) {\n // TODO: If abort is implemented, check for AbortError here and skip emitting correctionerror\n console.warn('[IndiaBoundaryCorrectedTileLayer] Corrections fetch failed:', correctionsError);\n layer.dispatchEvent({ type: 'correctionerror', error: correctionsError, coords: { z, x, y }, tileUrl: src });\n }\n\n const image = imageTile.getImage();\n \n // Check if image is a canvas (OffscreenCanvas) or HTMLImageElement\n if (typeof image.getContext === 'function') {\n // OffscreenCanvas path\n const imageBitmap = await createImageBitmap(blob);\n image.width = imageBitmap.width;\n image.height = imageBitmap.height;\n const ctx = image.getContext('2d');\n ctx.drawImage(imageBitmap, 0, 0);\n imageBitmap.close?.();\n image.dispatchEvent(new Event('load'));\n } else {\n // HTMLImageElement path - use blob URL\n const blobUrl = URL.createObjectURL(blob);\n image.onload = () => {\n URL.revokeObjectURL(blobUrl);\n };\n image.onerror = () => {\n URL.revokeObjectURL(blobUrl);\n };\n image.src = blobUrl;\n }\n } catch (err) {\n // Don't emit correctionerror for tile fetch/processing errors - only for PMTiles failures (handled above)\n console.warn('[IndiaBoundaryCorrectedTileLayer] Tile fetch failed:', err);\n // Fall back to original tile\n const image = imageTile.getImage();\n if (typeof image.src !== 'undefined') {\n // HTMLImageElement - load original tile\n image.src = src;\n } else if (typeof image.dispatchEvent === 'function') {\n // OffscreenCanvas - can't fall back, signal error\n image.dispatchEvent(new Event('error'));\n }\n }\n };\n}\n\n/**\n * Extended OpenLayers TileLayer with India boundary corrections.\n * Extends ol/layer/Tile with a custom XYZ source that applies corrections.\n */\nexport class IndiaBoundaryCorrectedTileLayer extends TileLayer {\n /**\n * @param {Object} options - Layer options\n * @param {string} options.url - Tile URL template with {x}, {y}, {z} placeholders\n * @param {string} [options.pmtilesUrl] - URL to PMTiles file (defaults to CDN)\n * @param {Object|string} [options.layerConfig] - LayerConfig or config ID (auto-detected if not provided)\n * @param {Object[]} [options.extraLayerConfigs] - Additional LayerConfigs for matching\n * @param {number} [options.tileSize=256] - Tile size in pixels\n * @param {Object} [options.sourceOptions] - Additional options for XYZ source\n * @param {Object} [options.layerOptions] - Additional options for TileLayer\n */\n constructor(options) {\n const {\n url,\n pmtilesUrl,\n layerConfig,\n extraLayerConfigs,\n tileSize = 256,\n sourceOptions = {},\n ...layerOptions\n } = options;\n\n // Initialize registry and resolve layer config\n const registry = layerConfigs.createMergedRegistry(extraLayerConfigs);\n let resolvedConfig;\n \n if (typeof layerConfig === 'string') {\n resolvedConfig = registry.get(layerConfig);\n } else if (layerConfig) {\n resolvedConfig = layerConfig;\n } else {\n // Auto-detect from URL\n resolvedConfig = registry.detectFromTemplates([url]);\n }\n\n // Create TileFixer\n const tileFixer = new TileFixer(pmtilesUrl ?? getPmtilesUrl());\n\n // Create XYZ source (tileLoadFunction set after super() to access 'this')\n const source = new XYZ({\n url,\n tileSize,\n crossOrigin: 'anonymous',\n ...sourceOptions,\n });\n\n super({\n source,\n ...layerOptions\n });\n\n this._tileFixer = tileFixer;\n this._layerConfig = resolvedConfig;\n this._registry = registry;\n\n // Set tileLoadFunction after super() so we can pass 'this' for event dispatching\n if (resolvedConfig) {\n source.setTileLoadFunction(createCorrectedTileLoadFunction(tileFixer, resolvedConfig, tileSize, this));\n }\n\n if (!resolvedConfig) {\n console.warn('[IndiaBoundaryCorrectedTileLayer] Could not detect layer config from URL. Corrections will not be applied.');\n }\n }\n\n /**\n * Get the TileFixer instance.\n * @returns {TileFixer}\n */\n getTileFixer() {\n return this._tileFixer;\n }\n\n /**\n * Get the resolved LayerConfig.\n * @returns {Object|null}\n */\n getLayerConfig() {\n return this._layerConfig;\n }\n\n /**\n * Get the registry.\n * @returns {LayerConfigRegistry}\n */\n getRegistry() {\n return this._registry;\n }\n\n /**\n * Fetch and fix a tile (exposed for testing).\n * @param {string} src - Tile URL\n * @param {number} z - Zoom level\n * @param {number} x - Tile X coordinate\n * @param {number} y - Tile Y coordinate\n * @returns {Promise<{blob: Blob, wasFixed: boolean}>}\n * @private\n */\n async _fetchAndFixTile(src, z, x, y) {\n const tileSize = this.getSource().getTileGrid()?.getTileSize(z) || 256;\n return fetchAndFixTile(src, z, x, y, this._tileFixer, this._layerConfig, tileSize);\n }\n}\n\n// Export for testing\nexport { fetchAndFixTile };\n\n/**\n * Factory function to create an IndiaBoundaryCorrectedTileLayer.\n * @param {Object} options - Layer options (see IndiaBoundaryCorrectedTileLayer constructor)\n * @returns {IndiaBoundaryCorrectedTileLayer}\n */\nexport function indiaBoundaryCorrectedTileLayer(options) {\n return new IndiaBoundaryCorrectedTileLayer(options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,kBAAsB;AACtB,iBAAgB;AAChB,kBAA8B;AAC9B,2BAA6B;AAC7B,uBAA+C;AAG/C,IAAAA,wBAA0C;AAC1C,IAAAC,eAA8B;AAc9B,eAAe,gBAAgB,KAAK,GAAG,GAAG,GAAG,WAAW,aAAa,UAAU;AAC7E,QAAM,EAAE,MAAM,UAAU,mBAAmB,iBAAiB,IAAI,MAAM,UAAU;AAAA,IAC9E;AAAA,IAAK;AAAA,IAAG;AAAA,IAAG;AAAA,IAAG;AAAA,IAAa,EAAE,UAAU,MAAM,OAAO;AAAA,EACtD;AACA,QAAM,OAAO,IAAI,KAAK,CAAC,IAAI,GAAG,EAAE,MAAM,WAAW,cAAc,OAAU,CAAC;AAC1E,SAAO,EAAE,MAAM,UAAU,mBAAmB,iBAAiB;AAC/D;AAUA,SAAS,gCAAgC,WAAW,aAAa,UAAU,OAAO;AAChF,SAAO,eAAe,WAAW,KAAK;AACpC,UAAM,YAAY,UAAU,aAAa;AACzC,UAAM,IAAI,UAAU,CAAC;AACrB,UAAM,IAAI,UAAU,CAAC;AACrB,UAAM,IAAI,UAAU,CAAC;AAMrB,QAAI;AACF,YAAM,EAAE,MAAM,mBAAmB,iBAAiB,IAAI,MAAM,gBAAgB,KAAK,GAAG,GAAG,GAAG,WAAW,aAAa,QAAQ;AAE1H,UAAI,mBAAmB;AAErB,gBAAQ,KAAK,+DAA+D,gBAAgB;AAC5F,cAAM,cAAc,EAAE,MAAM,mBAAmB,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I,WAAW;AAAA,IAC3C,WAAW,aAAa;AACtB,uBAAiB;AAAA,IACnB,OAAO;AAEL,uBAAiB,SAAS,oBAAoB,CAAC,GAAG,CAAC;AAAA,IACrD;AAGA,UAAM,YAAY,IAAI,iBAAAC,kBAAU,kBAAc,2BAAc,CAAC;AAG7D,UAAM,SAAS,IAAI,WAAAC,QAAI;AAAA,MACrB;AAAA,MACA;AAAA,MACA,aAAa;AAAA,MACb,GAAG;AAAA,IACL,CAAC;AAED,UAAM;AAAA,MACJ;AAAA,MACA,GAAG;AAAA,IACL,CAAC;AAED,SAAK,aAAa;AAClB,SAAK,eAAe;AACpB,SAAK,YAAY;AAGjB,QAAI,gBAAgB;AAClB,aAAO,oBAAoB,gCAAgC,WAAW,gBAAgB,UAAU,IAAI,CAAC;AAAA,IACvG;AAEA,QAAI,CAAC,gBAAgB;AACnB,cAAQ,KAAK,4GAA4G;AAAA,IAC3H;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAe;AACb,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,iBAAiB;AACf,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,cAAc;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAM,iBAAiB,KAAK,GAAG,GAAG,GAAG;AACnC,UAAM,WAAW,KAAK,UAAU,EAAE,YAAY,GAAG,YAAY,CAAC,KAAK;AACnE,WAAO,gBAAgB,KAAK,GAAG,GAAG,GAAG,KAAK,YAAY,KAAK,cAAc,QAAQ;AAAA,EACnF;AACF;AAUO,SAAS,gCAAgC,SAAS;AACvD,SAAO,IAAI,gCAAgC,OAAO;AACpD;","names":["import_layer_configs","import_data","TileLayer","TileFixer","XYZ"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.js"],"sourcesContent":["import TileLayer from 'ol/layer/Tile.js';\nimport XYZ from 'ol/source/XYZ.js';\nimport { getPmtilesUrl } from '@india-boundary-corrector/data';\nimport { layerConfigs } from '@india-boundary-corrector/layer-configs';\nimport { TileFixer } from '@india-boundary-corrector/tilefixer';\n\n// Re-export for convenience\nexport { layerConfigs, LayerConfig } from '@india-boundary-corrector/layer-configs';\nexport { getPmtilesUrl } from '@india-boundary-corrector/data';\n\n/**\n * Derive fetch options from crossOrigin attribute (matches OpenLayers behavior).\n * @param {string|null} crossOrigin - The crossOrigin attribute value\n * @returns {{mode: RequestMode, credentials: RequestCredentials}}\n */\nfunction getFetchOptionsFromCrossOrigin(crossOrigin) {\n if (crossOrigin === 'anonymous' || crossOrigin === '') {\n return { mode: 'cors', credentials: 'omit' };\n } else if (crossOrigin === 'use-credentials') {\n return { mode: 'cors', credentials: 'include' };\n }\n return { mode: 'same-origin', credentials: 'same-origin' };\n}\n\n/**\n * Create a custom tileLoadFunction that applies boundary corrections.\n * @param {TileFixer} tileFixer - The TileFixer instance\n * @param {Object} layerConfig - The layer configuration\n * @param {IndiaBoundaryCorrectedTileLayer} layer - The layer instance for event dispatching\n * @param {Object} fetchOptions - Fetch options (mode, credentials)\n * @param {boolean} fallbackOnCorrectionFailure - Whether to return original tile if corrections fail\n * @returns {Function} Custom tile load function\n */\nfunction createCorrectedTileLoadFunction(tileFixer, layerConfig, layer, fetchOptions, fallbackOnCorrectionFailure) {\n return async function(imageTile, src) {\n const tileCoord = imageTile.getTileCoord();\n const z = tileCoord[0];\n const x = tileCoord[1];\n const y = tileCoord[2];\n\n try {\n const { data, correctionsFailed, correctionsError } = await tileFixer.fetchAndFixTile(\n src, z, x, y, layerConfig, { fallbackOnCorrectionFailure, ...fetchOptions }\n );\n\n if (correctionsFailed) {\n console.warn('[IndiaBoundaryCorrectedTileLayer] Corrections fetch failed:', correctionsError);\n layer.dispatchEvent({ type: 'correctionerror', error: correctionsError, coords: { z, x, y }, tileUrl: src });\n }\n\n const blob = new Blob([data]);\n const image = imageTile.getImage();\n \n // Check if image is a canvas (OffscreenCanvas) or HTMLImageElement\n if (typeof image.getContext === 'function') {\n // OffscreenCanvas path\n const imageBitmap = await createImageBitmap(blob);\n image.width = imageBitmap.width;\n image.height = imageBitmap.height;\n const ctx = image.getContext('2d');\n ctx.drawImage(imageBitmap, 0, 0);\n imageBitmap.close?.();\n image.dispatchEvent(new Event('load'));\n } else {\n // HTMLImageElement path - use blob URL\n const blobUrl = URL.createObjectURL(blob);\n image.onload = () => {\n URL.revokeObjectURL(blobUrl);\n };\n image.onerror = () => {\n URL.revokeObjectURL(blobUrl);\n };\n image.src = blobUrl;\n }\n } catch (err) {\n console.warn('[IndiaBoundaryCorrectedTileLayer] Tile fetch failed:', err);\n // Fall back to original tile\n const image = imageTile.getImage();\n if (typeof image.src !== 'undefined') {\n // HTMLImageElement - load original tile\n image.src = src;\n } else if (typeof image.dispatchEvent === 'function') {\n // OffscreenCanvas - can't fall back, signal error\n image.dispatchEvent(new Event('error'));\n }\n }\n };\n}\n\n/**\n * Extended OpenLayers TileLayer with India boundary corrections.\n * Extends ol/layer/Tile with a custom XYZ source that applies corrections.\n */\nexport class IndiaBoundaryCorrectedTileLayer extends TileLayer {\n /**\n * @param {Object} options - Layer options\n * @param {string} options.url - Tile URL template with {x}, {y}, {z} placeholders\n * @param {string} [options.pmtilesUrl] - URL to PMTiles file (defaults to CDN)\n * @param {Object|string} [options.layerConfig] - LayerConfig or config ID (auto-detected if not provided)\n * @param {Object[]} [options.extraLayerConfigs] - Additional LayerConfigs for matching\n * @param {number} [options.tileSize=256] - Tile size in pixels (for OpenLayers source)\n * @param {boolean} [options.fallbackOnCorrectionFailure=true] - Return original tile if corrections fail\n * @param {Object} [options.sourceOptions] - Additional options for XYZ source\n * @param {Object} [options.layerOptions] - Additional options for TileLayer\n */\n constructor(options) {\n const {\n url,\n pmtilesUrl,\n layerConfig,\n extraLayerConfigs,\n tileSize = 256,\n fallbackOnCorrectionFailure = true,\n sourceOptions = {},\n ...layerOptions\n } = options;\n\n // Initialize registry and resolve layer config\n const registry = layerConfigs.createMergedRegistry(extraLayerConfigs);\n let resolvedConfig;\n \n if (typeof layerConfig === 'string') {\n resolvedConfig = registry.get(layerConfig);\n } else if (layerConfig) {\n resolvedConfig = layerConfig;\n } else {\n // Auto-detect from URL\n resolvedConfig = registry.detectFromTemplates([url]);\n }\n\n // Create TileFixer\n const tileFixer = TileFixer.getOrCreate(pmtilesUrl ?? getPmtilesUrl());\n\n // Create XYZ source\n const source = new XYZ({\n url,\n tileSize,\n crossOrigin: 'anonymous',\n ...sourceOptions,\n });\n\n // Derive fetch options from crossOrigin (matches OpenLayers behavior)\n // Default to 'anonymous' if not specified in sourceOptions\n const fetchOptions = getFetchOptionsFromCrossOrigin(sourceOptions.crossOrigin ?? 'anonymous');\n\n super({\n source,\n ...layerOptions\n });\n\n this._tileFixer = tileFixer;\n this._layerConfig = resolvedConfig;\n this._registry = registry;\n\n // Set tileLoadFunction after super() so we can pass 'this' for event dispatching\n if (resolvedConfig) {\n source.setTileLoadFunction(createCorrectedTileLoadFunction(tileFixer, resolvedConfig, this, fetchOptions, fallbackOnCorrectionFailure));\n }\n\n if (!resolvedConfig) {\n console.warn('[IndiaBoundaryCorrectedTileLayer] Could not detect layer config from URL. Corrections will not be applied.');\n }\n }\n\n /**\n * Get the TileFixer instance.\n * @returns {TileFixer}\n */\n getTileFixer() {\n return this._tileFixer;\n }\n\n /**\n * Get the resolved LayerConfig.\n * @returns {Object|null}\n */\n getLayerConfig() {\n return this._layerConfig;\n }\n\n /**\n * Get the registry.\n * @returns {LayerConfigRegistry}\n */\n getRegistry() {\n return this._registry;\n }\n}\n\n/**\n * Factory function to create an IndiaBoundaryCorrectedTileLayer.\n * @param {Object} options - Layer options (see IndiaBoundaryCorrectedTileLayer constructor)\n * @returns {IndiaBoundaryCorrectedTileLayer}\n */\nexport function indiaBoundaryCorrectedTileLayer(options) {\n return new IndiaBoundaryCorrectedTileLayer(options);\n}\n"],"mappings":";;;;;;;;;;;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