@geospatial-sdk/core 0.0.5-dev.0.sha-7ea9c6a → 0.0.5-dev.0.sha-9c03c7f

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.
Files changed (46) hide show
  1. package/dist/constant/projections.d.ts +8 -0
  2. package/dist/constant/projections.d.ts.map +1 -1
  3. package/dist/constant/projections.js +8 -0
  4. package/dist/model/events.d.ts +3 -3
  5. package/dist/model/events.d.ts.map +1 -1
  6. package/dist/model/events.js +2 -3
  7. package/dist/model/map-context-diff.d.ts +7 -1
  8. package/dist/model/map-context-diff.d.ts.map +1 -1
  9. package/dist/model/map-context.d.ts +15 -5
  10. package/dist/model/map-context.d.ts.map +1 -1
  11. package/dist/model/resolved-map-state.d.ts +1 -1
  12. package/dist/model/resolved-map-state.d.ts.map +1 -1
  13. package/dist/utils/coordinates.d.ts +52 -0
  14. package/dist/utils/coordinates.d.ts.map +1 -0
  15. package/dist/utils/coordinates.js +91 -0
  16. package/dist/utils/crs.d.ts +30 -0
  17. package/dist/utils/crs.d.ts.map +1 -0
  18. package/dist/utils/crs.js +83 -0
  19. package/dist/utils/geojson.d.ts +16 -0
  20. package/dist/utils/geojson.d.ts.map +1 -0
  21. package/dist/utils/geojson.js +118 -0
  22. package/dist/utils/index.d.ts +3 -0
  23. package/dist/utils/index.d.ts.map +1 -1
  24. package/dist/utils/index.js +3 -0
  25. package/dist/utils/map-context-diff.d.ts.map +1 -1
  26. package/dist/utils/map-context-diff.js +10 -0
  27. package/dist/utils/view.d.ts +2 -2
  28. package/dist/utils/view.d.ts.map +1 -1
  29. package/dist/utils/view.js +16 -29
  30. package/lib/constant/projections.ts +10 -0
  31. package/lib/model/events.ts +4 -4
  32. package/lib/model/map-context-diff.ts +8 -1
  33. package/lib/model/map-context.ts +19 -5
  34. package/lib/model/resolved-map-state.ts +1 -1
  35. package/lib/utils/coordinates.test.ts +172 -0
  36. package/lib/utils/coordinates.ts +157 -0
  37. package/lib/utils/crs.test.ts +128 -0
  38. package/lib/utils/crs.ts +102 -0
  39. package/lib/utils/geojson.test.ts +208 -0
  40. package/lib/utils/geojson.ts +133 -0
  41. package/lib/utils/index.ts +14 -0
  42. package/lib/utils/map-context-diff.test.ts +68 -1
  43. package/lib/utils/map-context-diff.ts +11 -0
  44. package/lib/utils/view.test.ts +23 -0
  45. package/lib/utils/view.ts +19 -44
  46. package/package.json +2 -2
@@ -81,6 +81,29 @@ describe("view", () => {
81
81
  });
82
82
  });
83
83
 
84
+ it("should preserve the CRS when only a projected WMS bounding box is available", async () => {
85
+ const layer = {
86
+ type: "wms",
87
+ name: "mock_2154",
88
+ url: "http://mock/wms",
89
+ } as MapContextLayerWms;
90
+ const view = await createViewFromLayer(layer);
91
+
92
+ expect(view).toEqual({
93
+ extent: [650796.4, 7060330.6, 690891.3, 7090402.2, "EPSG:2154"],
94
+ });
95
+ });
96
+
97
+ it("should return null when the WMS layer has no bounding boxes", async () => {
98
+ const layer = {
99
+ type: "wms",
100
+ name: "mock_nobbox",
101
+ url: "http://mock/wms",
102
+ } as MapContextLayerWms;
103
+
104
+ await expect(createViewFromLayer(layer)).resolves.toBeNull();
105
+ });
106
+
84
107
  it("should return view for WMTS layer", async () => {
85
108
  const layer = {
86
109
  type: "wmts",
package/lib/utils/view.ts CHANGED
@@ -1,10 +1,5 @@
1
- import { transformExtent } from "ol/proj.js";
2
1
  import { WfsEndpoint, WmsEndpoint, WmtsEndpoint } from "@camptocamp/ogc-client";
3
2
  import { LONLAT_CRS_CODES } from "../constant/projections.js";
4
- import { fromEPSGCode } from "ol/proj/proj4.js";
5
- import GeoJSON from "ol/format/GeoJSON.js";
6
- import { extend } from "ol/extent.js";
7
- import type Feature from "ol/Feature.js";
8
3
  import GeoTIFF from "ol/source/GeoTIFF.js";
9
4
  import {
10
5
  Extent,
@@ -16,13 +11,12 @@ import {
16
11
  MapContextView,
17
12
  ViewByExtent,
18
13
  } from "../model/index.js";
19
- import { FeatureCollection, Geometry } from "geojson";
20
-
21
- const GEOJSON = new GeoJSON();
14
+ import { ensureGeojson, getExtentFromGeojson } from "./geojson.js";
15
+ import { setCrs, transform } from "./coordinates.js";
22
16
 
23
17
  /**
24
- * Creates a view from a layer by extracting its geographic extent. The returned extent is
25
- * always expressed in EPSG:4326 (longitude/latitude) coordinates.
18
+ * Creates a view from a layer by extracting its extent. The returned extent is preferably
19
+ * returned in EPSG:4326; if not available, it will be returned in a CRS available for this layer
26
20
  *
27
21
  * @param layer - The map context layer to extract the extent from
28
22
  * @returns A Promise resolving to a `ViewByExtent` object, or `null` if the extent cannot be determined
@@ -50,7 +44,9 @@ export async function createViewFromLayer(
50
44
  } else if (layer.type === "wmts") {
51
45
  return await getWmtsLayerExtent(layer);
52
46
  } else if (layer.type === "geojson" && layer.data) {
53
- return computeExtentFromGeojson(layer.data);
47
+ return {
48
+ extent: getExtentFromGeojson(ensureGeojson(layer.data)),
49
+ };
54
50
  } else if (layer.type === "wfs") {
55
51
  return await getWfsLayerExtent(layer);
56
52
  } else if (layer.type === "geotiff") {
@@ -60,24 +56,6 @@ export async function createViewFromLayer(
60
56
  }
61
57
  }
62
58
 
63
- function computeExtentFromGeojson(
64
- data: FeatureCollection<Geometry | null> | string,
65
- ): ViewByExtent {
66
- const geojson = typeof data === "string" ? JSON.parse(data) : data;
67
- const features = GEOJSON.readFeatures(geojson) as Feature[];
68
- const extent = features.reduce(
69
- (prev, curr) => {
70
- const geom = curr.getGeometry();
71
- if (!geom) return prev;
72
- return extend(prev, geom.getExtent()) as Extent;
73
- },
74
- [Infinity, Infinity, -Infinity, -Infinity] as Extent,
75
- ) as Extent;
76
- return {
77
- extent,
78
- };
79
- }
80
-
81
59
  async function getWmsLayerExtent(
82
60
  layer: MapContextLayerWms,
83
61
  ): Promise<ViewByExtent | null> {
@@ -89,21 +67,17 @@ async function getWmsLayerExtent(
89
67
  const lonLatCRS = Object.keys(boundingBoxes).find((crs) =>
90
68
  LONLAT_CRS_CODES.includes(crs),
91
69
  );
70
+ // give priority to lon/lat bounding boxes
92
71
  if (lonLatCRS) {
93
72
  return {
94
- extent: boundingBoxes[lonLatCRS] as Extent,
95
- };
96
- } else {
97
- const availableEPSGCode = Object.keys(boundingBoxes)[0];
98
- const proj = await fromEPSGCode(availableEPSGCode);
99
- return {
100
- extent: transformExtent(
101
- boundingBoxes[availableEPSGCode],
102
- proj,
103
- "EPSG:4326",
104
- ) as Extent,
73
+ extent: boundingBoxes[lonLatCRS],
105
74
  };
106
75
  }
76
+ // otherwise take the first extent and apply its CRS
77
+ const availableEPSGCode = Object.keys(boundingBoxes)[0];
78
+ return {
79
+ extent: [...boundingBoxes[availableEPSGCode], availableEPSGCode],
80
+ };
107
81
  }
108
82
 
109
83
  async function getWmtsLayerExtent(
@@ -133,6 +107,7 @@ async function getWfsLayerExtent(
133
107
  };
134
108
  }
135
109
 
110
+ // TODO: DO NOT RELY ON OL FOR THIS!
136
111
  async function getGeotiffLayerExtent(
137
112
  layer: MapContextLayerGeotiff,
138
113
  ): Promise<ViewByExtent | null> {
@@ -149,11 +124,11 @@ async function getGeotiffLayerExtent(
149
124
  resolve(null);
150
125
  return;
151
126
  }
152
- const extent = transformExtent(
153
- tileGrid.getExtent(),
154
- projection,
127
+ const projectionCode = projection.getCode();
128
+ const extent = transform(
129
+ setCrs(tileGrid.getExtent() as Extent, projectionCode),
155
130
  "EPSG:4326",
156
- ) as Extent;
131
+ );
157
132
  resolve({ extent });
158
133
  } else if (source.getState() === "error") {
159
134
  resolve(null);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geospatial-sdk/core",
3
- "version": "0.0.5-dev.0.sha-7ea9c6a",
3
+ "version": "0.0.5-dev.0.sha-9c03c7f",
4
4
  "description": "Core functions and models for the SDK",
5
5
  "keywords": [
6
6
  "geospatial",
@@ -39,5 +39,5 @@
39
39
  "peerDependencies": {
40
40
  "ol": ">9.x"
41
41
  },
42
- "gitHead": "7ea9c6a7869f8da004754a7a9ead00e5cae15884"
42
+ "gitHead": "9c03c7f798ef9e829c74289dbc076efa86e4c66e"
43
43
  }