@maptiler/client 2.4.0 → 2.5.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # MapTiler Client Changelog
2
2
 
3
+ ## 2.5.1
4
+ ### New Features
5
+ None
6
+
7
+ ### Bug Fixes
8
+ None
9
+
10
+ ### Others
11
+ `Hybrid` map style is no longer deprecated.
12
+
13
+ ## 2.5.0
14
+ ### New Features
15
+ - `at` and `batch` functions compute elevation on the server using MapTiler Elevation API by default
16
+ - Elevation supports Node.js: computed on server when `bufferToPixelData` is not provided
17
+ - Added `computeOn` option to force client/server elevation processing
18
+ - Added `canParsePixelData` function to check if elevation can be computed on the client
19
+
20
+ ### Bug Fixes
21
+ - `bufferToPixelData` can be undefined
22
+
23
+ ### Others
24
+ None
25
+
3
26
  ## 2.4.0
4
27
  ### New Features
5
28
  - Added `elevation` option to Geolocation API
@@ -1562,7 +1562,6 @@ const mapStylePresetList = [
1562
1562
  variants: [
1563
1563
  {
1564
1564
  id: "hybrid",
1565
- deprecated: true,
1566
1565
  name: "Default",
1567
1566
  variantType: "DEFAULT",
1568
1567
  description: "",
@@ -2657,89 +2656,87 @@ function getBufferToPixelDataParser() {
2657
2656
  "An image file buffer to pixel data parser is necessary. Specify it in `config.bufferToPixelData`"
2658
2657
  );
2659
2658
  }
2659
+ function canParsePixelData() {
2660
+ return !!config.bufferToPixelData || typeof window !== "undefined";
2661
+ }
2660
2662
 
2661
- const terrainTileJsonURL = "tiles/terrain-rgb-v2/tiles.json";
2663
+ const TERRAIN_TILESET = "terrain-rgb-v2";
2664
+ const API_BATCH_SIZE = 50;
2665
+ const API_WARN_SIZE = 1e3;
2662
2666
  let terrainTileJson = null;
2663
2667
  const customMessages = {
2664
2668
  403: "Key is missing, invalid or restricted"
2665
2669
  };
2666
- async function fetchTerrainTileJson(apiKey) {
2667
- const endpoint = new URL(terrainTileJsonURL, defaults.maptilerApiURL);
2668
- endpoint.searchParams.set("key", apiKey);
2669
- const urlWithParams = endpoint.toString();
2670
- const res = await callFetch(urlWithParams);
2671
- if (res.ok) {
2672
- terrainTileJson = await res.json();
2673
- return terrainTileJson;
2674
- } else {
2675
- if (!res.ok) {
2676
- throw new ServiceError(res, customMessages[res.status] ?? "");
2677
- }
2678
- }
2679
- }
2680
- async function at(position, options = {}) {
2681
- const apiKey = options.apiKey ?? config.apiKey;
2682
- if (!terrainTileJson) {
2683
- await fetchTerrainTileJson(apiKey);
2670
+ async function computeOnServer(positions, apiKey) {
2671
+ if (positions.length > API_WARN_SIZE) {
2672
+ console.warn(
2673
+ "Computing elevation for complex geometries is discouraged - simplify the geometry before proceeding"
2674
+ );
2684
2675
  }
2685
- const maxZoom = terrainTileJson.maxzoom;
2686
- let zoom = ~~(options.zoom ?? maxZoom);
2687
- if (zoom > maxZoom || zoom < 0) {
2688
- zoom = maxZoom;
2689
- }
2690
- const tileIndex = math.wgs84ToTileIndex(position, zoom, false);
2691
- const tileX = ~~tileIndex[0];
2692
- const tileY = ~~tileIndex[1];
2693
- if (!terrainTileJson.tiles.length) {
2694
- throw new Error("Terrain tileJSON tile list is empty.");
2695
- }
2696
- const tileID = `terrain_${zoom.toString()}_${tileX.toString()}_${tileY.toString()}`;
2697
- let tilePixelData;
2698
- const cache = getTileCache();
2699
- if (cache.has(tileID)) {
2700
- tilePixelData = cache.get(tileID);
2701
- } else {
2702
- const tileURL = terrainTileJson.tiles[0].replace("{x}", tileX.toString()).replace("{y}", tileY.toString()).replace("{z}", zoom.toString());
2703
- const tileRes = await callFetch(tileURL);
2704
- if (!tileRes.ok) {
2705
- throw new ServiceError(tileRes, customMessages[tileRes.status] ?? "");
2676
+ const parts = Math.ceil(positions.length / API_BATCH_SIZE);
2677
+ const respPromises = Array.from({ length: parts }, () => null).map(
2678
+ (_, part) => {
2679
+ const startPos = part * API_BATCH_SIZE;
2680
+ const batch2 = positions.slice(startPos, startPos + API_BATCH_SIZE);
2681
+ const batchEncoded = batch2.map((pos) => pos.join(",")).join(";");
2682
+ const endpoint = new URL(
2683
+ `elevation/${batchEncoded}.json`,
2684
+ defaults.maptilerApiURL
2685
+ );
2686
+ endpoint.searchParams.set("key", apiKey);
2687
+ return callFetch(endpoint.toString());
2706
2688
  }
2707
- const tileBuff = await tileRes.arrayBuffer();
2708
- const tileParser = getBufferToPixelDataParser();
2709
- tilePixelData = await tileParser(tileBuff);
2710
- cache.set(tileID, tilePixelData);
2711
- }
2712
- const pixelX = ~~(tilePixelData.width * (tileIndex[0] % 1));
2713
- const pixelY = ~~(tilePixelData.height * (tileIndex[1] % 1));
2714
- const pixelDataIndex = (pixelY * tilePixelData.width + pixelX) * tilePixelData.components;
2715
- const R = tilePixelData.pixels[pixelDataIndex];
2716
- const G = tilePixelData.pixels[pixelDataIndex + 1];
2717
- const B = tilePixelData.pixels[pixelDataIndex + 2];
2718
- const elevation2 = -1e4 + (R * 256 * 256 + G * 256 + B) * 0.1;
2719
- return [position[0], position[1], elevation2];
2689
+ );
2690
+ const resps = await Promise.allSettled(respPromises);
2691
+ const jsons = await Promise.all(
2692
+ resps.map(async (resp) => {
2693
+ if (resp.status === "rejected") {
2694
+ throw new Error(
2695
+ `Some segments could not be fetched, error: ${resp.reason}`
2696
+ );
2697
+ }
2698
+ if (!resp.value.ok) {
2699
+ throw new Error(
2700
+ `Some segments could not be fetched, response: ${resp.value.status} ${await resp.value.text()}, url: ${resp.value.url}`
2701
+ );
2702
+ }
2703
+ return resp.value.json();
2704
+ })
2705
+ );
2706
+ return jsons.flat();
2720
2707
  }
2721
- async function batch(positions, options = {}) {
2722
- const apiKey = options.apiKey ?? config.apiKey;
2708
+ async function computeOnClient(positions, apiKey, zoom) {
2723
2709
  if (!terrainTileJson) {
2724
- await fetchTerrainTileJson(apiKey);
2710
+ const endpoint = new URL(
2711
+ `tiles/${TERRAIN_TILESET}/tiles.json`,
2712
+ defaults.maptilerApiURL
2713
+ );
2714
+ endpoint.searchParams.set("key", apiKey);
2715
+ const urlWithParams = endpoint.toString();
2716
+ const res = await callFetch(urlWithParams);
2717
+ if (res.ok) {
2718
+ terrainTileJson = await res.json();
2719
+ } else {
2720
+ throw new ServiceError(res, customMessages[res.status] ?? "");
2721
+ }
2725
2722
  }
2726
2723
  const tileParser = getBufferToPixelDataParser();
2727
2724
  const tileURLSchema = terrainTileJson.tiles[0];
2728
2725
  const cache = getTileCache();
2729
2726
  const maxZoom = terrainTileJson.maxzoom;
2730
- let zoom = ~~(options.zoom ?? maxZoom);
2731
- if (zoom > maxZoom || zoom < 0) {
2732
- zoom = maxZoom;
2727
+ let usedZoom = ~~(zoom ?? maxZoom);
2728
+ if (usedZoom > maxZoom || usedZoom < 0) {
2729
+ usedZoom = maxZoom;
2733
2730
  }
2734
2731
  const tileIndicesFloats = positions.map(
2735
- (position) => math.wgs84ToTileIndex(position, zoom, false)
2732
+ (position) => math.wgs84ToTileIndex(position, usedZoom, false)
2736
2733
  );
2737
2734
  const tileIndicesInteger = tileIndicesFloats.map((index) => [
2738
2735
  ~~index[0],
2739
2736
  ~~index[1]
2740
2737
  ]);
2741
2738
  const tileIDs = tileIndicesInteger.map(
2742
- (index) => `terrain_${zoom.toString()}_${index[0].toString()}_${index[1].toString()}`
2739
+ (index) => `terrain_${usedZoom.toString()}_${index[0].toString()}_${index[1].toString()}`
2743
2740
  );
2744
2741
  const uniqueTilesToFetch = Array.from(
2745
2742
  new Set(tileIDs.filter((tileID) => !cache.has(tileID)))
@@ -2795,6 +2792,17 @@ async function batch(positions, options = {}) {
2795
2792
  const elevation2 = -1e4 + (R * 256 * 256 + G * 256 + B) * 0.1;
2796
2793
  return [position[0], position[1], ~~(elevation2 * 1e3) / 1e3];
2797
2794
  });
2795
+ return elevatedPositions;
2796
+ }
2797
+ async function at(position, options = {}) {
2798
+ const elevatedPositions = await batch([position], options);
2799
+ return elevatedPositions[0];
2800
+ }
2801
+ async function batch(positions, options = {}) {
2802
+ if (positions.length === 0)
2803
+ return [];
2804
+ const apiKey = options.apiKey ?? config.apiKey;
2805
+ const elevatedPositions = options.computeOn === "client" ? await computeOnClient(positions, apiKey, options.zoom) : await computeOnServer(positions, apiKey);
2798
2806
  if (options.smoothingKernelSize) {
2799
2807
  const kernelSize = ~~(options.smoothingKernelSize / 2) * 2 + 1;
2800
2808
  const elevations = elevatedPositions.map((pos) => pos[2]);
@@ -2815,6 +2823,7 @@ async function fromLineString(ls, options = {}) {
2815
2823
  if (ls.type !== "LineString") {
2816
2824
  throw new Error("The provided object is not a GeoJSON LineString");
2817
2825
  }
2826
+ options.computeOn ?? (options.computeOn = canParsePixelData() ? "client" : "server");
2818
2827
  const clone = structuredClone(ls);
2819
2828
  const elevatedPositions = await batch(clone.coordinates, options);
2820
2829
  clone.coordinates = elevatedPositions;
@@ -2824,6 +2833,7 @@ async function fromMultiLineString(ls, options = {}) {
2824
2833
  if (ls.type !== "MultiLineString") {
2825
2834
  throw new Error("The provided object is not a GeoJSON MultiLineString");
2826
2835
  }
2836
+ options.computeOn ?? (options.computeOn = canParsePixelData() ? "client" : "server");
2827
2837
  const clone = structuredClone(ls);
2828
2838
  const multiLengths = clone.coordinates.map((poss) => poss.length);
2829
2839
  const flattenPositions = clone.coordinates.flat();
@@ -2854,6 +2864,7 @@ exports.ReferenceMapStyle = ReferenceMapStyle;
2854
2864
  exports.ServiceError = ServiceError;
2855
2865
  exports.areSameLanguages = areSameLanguages;
2856
2866
  exports.bufferToPixelDataBrowser = bufferToPixelDataBrowser;
2867
+ exports.canParsePixelData = canParsePixelData;
2857
2868
  exports.circumferenceAtLatitude = circumferenceAtLatitude;
2858
2869
  exports.config = config;
2859
2870
  exports.coordinates = coordinates;