@geospatial-sdk/maplibre 0.0.5-dev.48 → 0.0.5-dev.50

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.
@@ -5,12 +5,7 @@ import {
5
5
  ViewByZoomAndCenter,
6
6
  } from "@geospatial-sdk/core";
7
7
 
8
- import {
9
- LayerSpecification,
10
- Map,
11
- MapOptions,
12
- StyleSpecification,
13
- } from "maplibre-gl";
8
+ import { LayerSpecification, Map, MapOptions } from "maplibre-gl";
14
9
  import { FeatureCollection, Geometry } from "geojson";
15
10
  import {
16
11
  OgcApiEndpoint,
@@ -19,24 +14,35 @@ import {
19
14
  } from "@camptocamp/ogc-client";
20
15
  import {
21
16
  createDatasetFromGeoJsonLayer,
17
+ generateLayerHashWithoutUpdatableProps,
22
18
  generateLayerId,
23
19
  } from "../helpers/map.helpers.js";
24
- import { Dataset, PartialStyleSpecification } from "../maplibre.models.js";
20
+ import {
21
+ LayerMetadataSpecification,
22
+ PartialStyleSpecification,
23
+ } from "../maplibre.models.js";
25
24
 
26
25
  const featureCollection: FeatureCollection<Geometry | null> = {
27
26
  type: "FeatureCollection",
28
27
  features: [],
29
28
  };
30
29
 
30
+ /**
31
+ * Create a Maplibre layer from a MapContextLayer. Returns null if the layer could not be created.
32
+ * @param layerModel
33
+ */
31
34
  export async function createLayer(
32
35
  layerModel: MapContextLayer,
33
- sourcePosition: number,
34
- ): Promise<PartialStyleSpecification> {
36
+ ): Promise<PartialStyleSpecification | null> {
35
37
  const { type } = layerModel;
38
+ const metadata: LayerMetadataSpecification =
39
+ "id" in layerModel
40
+ ? { layerId: layerModel.id }
41
+ : { layerHash: generateLayerHashWithoutUpdatableProps(layerModel) };
42
+ const layerId = generateLayerId();
36
43
 
37
44
  switch (type) {
38
45
  case "wms": {
39
- const layerId = generateLayerId(layerModel);
40
46
  const sourceId = layerId;
41
47
 
42
48
  const endpoint = await new WmsEndpoint(layerModel.url).isReady();
@@ -50,7 +56,7 @@ export async function createLayer(
50
56
  url = removeSearchParams(url, ["bbox"]);
51
57
  url = `${url.toString()}&BBOX={bbox-epsg-3857}`;
52
58
 
53
- const dataset: Dataset = {
59
+ return {
54
60
  sources: {
55
61
  [sourceId]: {
56
62
  type: "raster",
@@ -63,14 +69,16 @@ export async function createLayer(
63
69
  id: layerId,
64
70
  type: "raster",
65
71
  source: sourceId,
66
- paint: {},
67
- metadata: {
68
- sourcePosition,
72
+ paint: {
73
+ "raster-opacity": layerModel.opacity ?? 1,
69
74
  },
75
+ layout: {
76
+ visibility: layerModel.visibility === false ? "none" : "visible",
77
+ },
78
+ metadata,
70
79
  },
71
80
  ],
72
81
  };
73
- return dataset;
74
82
  }
75
83
  case "wfs": {
76
84
  const entryPoint = await new WfsEndpoint(layerModel.url).isReady();
@@ -78,7 +86,7 @@ export async function createLayer(
78
86
  asJson: true,
79
87
  outputCrs: "EPSG:4326",
80
88
  });
81
- return createDatasetFromGeoJsonLayer(layerModel, url, sourcePosition);
89
+ return createDatasetFromGeoJsonLayer(layerModel, url, metadata);
82
90
  }
83
91
  case "geojson": {
84
92
  let geojson;
@@ -97,42 +105,69 @@ export async function createLayer(
97
105
  geojson = data;
98
106
  }
99
107
  }
100
- return createDatasetFromGeoJsonLayer(layerModel, geojson, sourcePosition);
108
+ return createDatasetFromGeoJsonLayer(layerModel, geojson, metadata);
101
109
  }
102
110
  case "ogcapi": {
103
111
  const ogcEndpoint = new OgcApiEndpoint(layerModel.url);
104
- let layerUrl: string;
105
112
  if (layerModel.useTiles) {
106
113
  console.warn("[Warning] OGC API - Tiles not yet implemented.");
107
- } else {
108
- layerUrl = await ogcEndpoint.getCollectionItemsUrl(
109
- layerModel.collection,
110
- { ...layerModel.options, asJson: true },
111
- );
112
- return createDatasetFromGeoJsonLayer(
113
- layerModel,
114
- layerUrl,
115
- sourcePosition,
116
- );
114
+ return null;
117
115
  }
118
- break;
116
+ const layerUrl = await ogcEndpoint.getCollectionItemsUrl(
117
+ layerModel.collection,
118
+ { ...layerModel.options, asJson: true },
119
+ );
120
+ return createDatasetFromGeoJsonLayer(layerModel, layerUrl, metadata);
119
121
  }
120
122
  case "maplibre-style": {
121
123
  console.warn("[Warning] Maplibre style - Not yet fully implemented.");
122
124
  const style = await fetch(layerModel.styleUrl).then((res) => res.json());
123
125
  style.layers?.forEach(
124
- (layer: LayerSpecification) => (layer.metadata = { sourcePosition }),
126
+ (layer: LayerSpecification) => (layer.metadata = metadata),
125
127
  );
126
128
  return style;
127
129
  }
130
+ case "xyz": {
131
+ const sourceId = layerId;
132
+ return {
133
+ sources: {
134
+ [sourceId]: {
135
+ type: "raster",
136
+ tiles: [layerModel.url],
137
+ tileSize: 256,
138
+ },
139
+ },
140
+ layers: [
141
+ {
142
+ id: layerId,
143
+ type: "raster",
144
+ source: sourceId,
145
+ paint: {
146
+ "raster-opacity": layerModel.opacity ?? 1,
147
+ },
148
+ layout: {
149
+ visibility: layerModel.visibility === false ? "none" : "visible",
150
+ },
151
+ metadata,
152
+ },
153
+ ],
154
+ };
155
+ }
156
+ case "wmts": {
157
+ console.warn(`WMTS layers are not yet supported in Maplibre`, layerModel);
158
+ return null;
159
+ }
160
+ default: {
161
+ console.error(`Layer could not be created`, layerModel);
162
+ return null;
163
+ }
128
164
  }
129
- return {} as StyleSpecification;
130
165
  }
131
166
 
132
167
  /**
133
- * Create an Maplibre map from a context; optionally specify a target (root element) for the map
168
+ * Create a Maplibre map from a context; map options need to be provided
134
169
  * @param context
135
- * @param target
170
+ * @param mapOptions
136
171
  */
137
172
  export async function createMapFromContext(
138
173
  context: MapContext,
@@ -156,7 +191,8 @@ export async function resetMapFromContext(
156
191
 
157
192
  for (let i = 0; i < context.layers.length; i++) {
158
193
  const layerModel = context.layers[i];
159
- const partialMLStyle = await createLayer(layerModel, i);
194
+ const partialMLStyle = await createLayer(layerModel);
195
+ if (!partialMLStyle) continue;
160
196
 
161
197
  if (partialMLStyle.glyphs) {
162
198
  map.setGlyphs(partialMLStyle.glyphs);
@@ -15,7 +15,8 @@ export type LayerSpecificationWithSource = Exclude<
15
15
  >;
16
16
 
17
17
  export interface LayerMetadataSpecification {
18
- sourcePosition: number;
18
+ layerId?: number | string;
19
+ layerHash?: string;
19
20
  }
20
21
 
21
22
  export type LayerContextWithStyle =
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geospatial-sdk/maplibre",
3
- "version": "0.0.5-dev.48+a6a661d",
3
+ "version": "0.0.5-dev.50+c8ee5e3",
4
4
  "description": "Maplibre-related utilities",
5
5
  "keywords": [
6
6
  "maplibre",
@@ -33,7 +33,7 @@
33
33
  "maplibre-gl": "^5.7.3"
34
34
  },
35
35
  "dependencies": {
36
- "@geospatial-sdk/core": "^0.0.5-dev.48+a6a661d"
36
+ "@geospatial-sdk/core": "^0.0.5-dev.50+c8ee5e3"
37
37
  },
38
- "gitHead": "a6a661d277224d3e0acef7e2df817a3c5b7360e1"
38
+ "gitHead": "c8ee5e32059a06e056a5723987baf5e2574bf784"
39
39
  }