@geode/opengeodeweb-front 12.1.0 → 12.2.0-rc.2

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 (57) hide show
  1. package/.build/server/utils/microservices_process.js +1 -1
  2. package/.build/tests/browser/helpers/crs.js +136 -0
  3. package/.build/tests/browser/helpers/crs_flows.js +60 -0
  4. package/.build/tests/browser/helpers/crs_shortlist.js +13 -0
  5. package/.build/tests/browser/helpers/crs_test_data.js +40 -0
  6. package/.build/tests/browser/suites/crs.js +142 -0
  7. package/.build/tests/browser/suites/crs_harmonize.js +63 -0
  8. package/.build/tests/browser/suites/crs_shortlist.js +31 -0
  9. package/.build/tests/browser/suites/index.js +6 -0
  10. package/app/app.config.ts +4 -0
  11. package/app/components/CrsAlign.vue +207 -0
  12. package/app/components/CrsDialog.vue +400 -0
  13. package/app/components/CrsDialogHost.vue +66 -0
  14. package/app/components/CrsSelector.vue +100 -3
  15. package/app/components/PickButton.vue +43 -0
  16. package/app/components/ProjectCrsDialog.vue +109 -0
  17. package/app/components/ToolPanel.vue +5 -2
  18. package/app/components/Viewer/CrsOverlays.vue +29 -0
  19. package/app/components/Viewer/Ui.vue +3 -2
  20. package/app/composables/crs.ts +197 -0
  21. package/app/composables/crs_align.ts +206 -0
  22. package/app/composables/crs_assign.ts +102 -0
  23. package/app/composables/crs_link.ts +22 -0
  24. package/app/composables/crs_shortlist.ts +63 -0
  25. package/app/composables/project_crs.ts +197 -0
  26. package/app/composables/user_setting.ts +140 -0
  27. package/app/plugins/activity_connections.client.ts +9 -0
  28. package/app/stores/activity.ts +130 -0
  29. package/app/stores/back.ts +7 -2
  30. package/app/stores/basemap.ts +19 -2
  31. package/app/stores/cloud.ts +1 -1
  32. package/app/stores/crs.ts +11 -106
  33. package/app/stores/crs_helpers/geographic_list.ts +44 -0
  34. package/app/stores/crs_helpers/project_crs.ts +60 -0
  35. package/app/stores/crs_helpers/requests.ts +60 -0
  36. package/app/utils/activity_connections.ts +36 -0
  37. package/app/utils/activity_params.ts +40 -0
  38. package/app/utils/crs.ts +17 -1
  39. package/app/utils/import_item.ts +87 -0
  40. package/app/utils/import_workflow.ts +36 -94
  41. package/app/utils/log.ts +4 -1
  42. package/app/utils/reload_object.ts +26 -0
  43. package/internal/utils/api_fetch.ts +13 -2
  44. package/internal/utils/track_request.ts +74 -0
  45. package/internal/utils/upload_file.ts +73 -39
  46. package/internal/utils/viewer_call.ts +49 -39
  47. package/package.json +5 -5
  48. package/server/utils/microservices_process.ts +4 -1
  49. package/tests/browser/data/test_lambert93_no_crs.og_pts3d +0 -0
  50. package/tests/browser/data/test_utm31n.og_pts3d +0 -0
  51. package/tests/browser/helpers/crs.ts +250 -0
  52. package/tests/browser/helpers/crs_flows.ts +115 -0
  53. package/tests/browser/helpers/crs_shortlist.ts +27 -0
  54. package/tests/browser/helpers/crs_test_data.ts +34 -0
  55. package/tests/browser/suites/crs.ts +228 -0
  56. package/tests/browser/suites/crs_harmonize.ts +96 -0
  57. package/tests/browser/suites/crs_shortlist.ts +37 -0
@@ -0,0 +1,206 @@
1
+ import type { ComputedRef, Ref } from "vue";
2
+ import { onKeyStroke, watchDebounced } from "@vueuse/core";
3
+ import type { CrsPoint } from "@ogw_front/composables/crs";
4
+ import { useViewerStore } from "@ogw_front/stores/viewer";
5
+ import viewer_schemas from "@geode/opengeodeweb-viewer/opengeodeweb_viewer_typed_schemas.js";
6
+
7
+ interface EditablePoint {
8
+ x: number | string;
9
+ y: number | string;
10
+ }
11
+
12
+ // Three pairs: the affine alignment places, rotates, scales and shears the data
13
+ const NB_POINTS = 3;
14
+ // One preview request once the typing pauses, not one per keystroke
15
+ const PREVIEW_DEBOUNCE_MILLISECONDS = 200;
16
+ const preview_schema = viewer_schemas.opengeodeweb_viewer.viewer.preview_points;
17
+
18
+ function emptyPoints(): EditablePoint[] {
19
+ return Array.from({ length: NB_POINTS }, () => ({ x: "", y: "" }));
20
+ }
21
+
22
+ function notPicked(): boolean[] {
23
+ return Array.from({ length: NB_POINTS }, () => false);
24
+ }
25
+
26
+ function toNumber(value: number | string | undefined): number {
27
+ return Number(String(value).replaceAll(",", "."));
28
+ }
29
+
30
+ // "" or a partly typed "-" / "." is not a coordinate yet
31
+ function isCoordinate(value: number | string): boolean {
32
+ return value !== "" && Number.isFinite(toNumber(value));
33
+ }
34
+
35
+ function isFilled(point: EditablePoint): boolean {
36
+ return isCoordinate(point.x) && isCoordinate(point.y);
37
+ }
38
+
39
+ interface UseCrsAlignReturn {
40
+ sourcePoints: Ref<EditablePoint[]>;
41
+ targetPoints: Ref<EditablePoint[]>;
42
+ pickingActive: Ref<boolean>;
43
+ togglePickMode: () => void;
44
+ reset: () => void;
45
+ isComplete: ComputedRef<boolean>;
46
+ toSourceCrsPoints: () => CrsPoint[];
47
+ toTargetCrsPoints: () => CrsPoint[];
48
+ editSourcePoint: (index: number, coord: keyof EditablePoint, value: number | string) => void;
49
+ }
50
+
51
+ interface AlignState {
52
+ sourcePoints: Ref<EditablePoint[]>;
53
+ targetPoints: Ref<EditablePoint[]>;
54
+ pickingActive: Ref<boolean>;
55
+ // Picked z only places the preview markers on the data
56
+ pickedZ: Ref<number[]>;
57
+ // Picked points lie on the displayed data, typed ones are original coordinates
58
+ pickedSources: Ref<boolean[]>;
59
+ }
60
+
61
+ function pairFilled({ sourcePoints, targetPoints }: AlignState, index: number): boolean {
62
+ const source = sourcePoints.value[index];
63
+ const target = targetPoints.value[index];
64
+ return source !== undefined && target !== undefined && isFilled(source) && isFilled(target);
65
+ }
66
+
67
+ function sourceCrsPoints({ sourcePoints, pickedSources }: AlignState): CrsPoint[] {
68
+ return sourcePoints.value.map((point, index) => ({
69
+ x: toNumber(point.x),
70
+ y: toNumber(point.y),
71
+ picked: pickedSources.value[index] ?? false,
72
+ }));
73
+ }
74
+
75
+ function targetCrsPoints({ targetPoints }: AlignState): CrsPoint[] {
76
+ return targetPoints.value.map((point) => ({ x: toNumber(point.x), y: toNumber(point.y) }));
77
+ }
78
+
79
+ async function preview({ sourcePoints, pickedZ }: AlignState): Promise<void> {
80
+ const points = sourcePoints.value
81
+ .map((point, index) => ({
82
+ x: toNumber(point.x),
83
+ y: toNumber(point.y),
84
+ z: pickedZ.value[index] ?? 0,
85
+ filled: isFilled(point),
86
+ }))
87
+ .filter(({ filled }) => filled)
88
+ .map(({ x, y, z }) => ({ x, y, z }));
89
+ await useViewerStore().request({ schema: preview_schema, params: { points, style: "points" } });
90
+ }
91
+
92
+ // A point picked on the data fills the first empty source point
93
+ function placePickedPoint(
94
+ { sourcePoints, pickedZ, pickedSources }: AlignState,
95
+ picked: { x?: number; y?: number; z?: number },
96
+ ): void {
97
+ if (picked.x === undefined || picked.y === undefined) {
98
+ return;
99
+ }
100
+ const firstEmpty = sourcePoints.value.findIndex((point) => !isFilled(point));
101
+ if (firstEmpty === -1) {
102
+ return;
103
+ }
104
+ sourcePoints.value[firstEmpty] = { x: toNumber(picked.x), y: toNumber(picked.y) };
105
+ pickedZ.value[firstEmpty] = toNumber(picked.z);
106
+ pickedSources.value[firstEmpty] = true;
107
+ }
108
+
109
+ // Picking, preview and Escape follow the viewer while the alignment is mounted
110
+ function followViewer(state: AlignState, reset: () => void): void {
111
+ const viewerStore = useViewerStore();
112
+ watch(
113
+ () => viewerStore.picked_point,
114
+ (picked) => {
115
+ if (state.pickingActive.value) {
116
+ placePickedPoint(state, picked);
117
+ }
118
+ },
119
+ { deep: true },
120
+ );
121
+ watchDebounced(
122
+ state.sourcePoints,
123
+ async () => {
124
+ await preview(state);
125
+ },
126
+ {
127
+ deep: true,
128
+ debounce: PREVIEW_DEBOUNCE_MILLISECONDS,
129
+ },
130
+ );
131
+ watch(
132
+ () => viewerStore.picking_mode,
133
+ (pickingMode) => {
134
+ state.pickingActive.value = pickingMode;
135
+ },
136
+ );
137
+ onKeyStroke("Escape", (event) => {
138
+ if (state.pickingActive.value) {
139
+ viewerStore.toggle_picking_mode(false);
140
+ event.stopImmediatePropagation();
141
+ }
142
+ });
143
+ onUnmounted(async () => {
144
+ reset();
145
+ await viewerStore.request({ schema: preview_schema, params: { points: [], style: "points" } });
146
+ });
147
+ }
148
+
149
+ // Source points are picked on the data (only x and y are used) or typed, target points are typed
150
+ function useCrsAlign(): UseCrsAlignReturn {
151
+ const viewerStore = useViewerStore();
152
+ const state: AlignState = {
153
+ sourcePoints: ref<EditablePoint[]>(emptyPoints()),
154
+ targetPoints: ref<EditablePoint[]>(emptyPoints()),
155
+ pickingActive: ref(false),
156
+ pickedZ: ref<number[]>([]),
157
+ pickedSources: ref<boolean[]>(notPicked()),
158
+ };
159
+ const isComplete = computed(() =>
160
+ state.sourcePoints.value.every((_point, index) => pairFilled(state, index)),
161
+ );
162
+
163
+ function editSourcePoint(
164
+ index: number,
165
+ coord: keyof EditablePoint,
166
+ value: number | string,
167
+ ): void {
168
+ const point = state.sourcePoints.value[index];
169
+ if (!point) {
170
+ return;
171
+ }
172
+ point[coord] = value;
173
+ state.pickedSources.value[index] = false;
174
+ }
175
+
176
+ function togglePickMode(): void {
177
+ state.pickingActive.value = !state.pickingActive.value;
178
+ viewerStore.toggle_picking_mode(state.pickingActive.value);
179
+ }
180
+
181
+ function reset(): void {
182
+ if (state.pickingActive.value) {
183
+ viewerStore.toggle_picking_mode(false);
184
+ }
185
+ state.sourcePoints.value = emptyPoints();
186
+ state.targetPoints.value = emptyPoints();
187
+ state.pickedZ.value = [];
188
+ state.pickedSources.value = notPicked();
189
+ }
190
+
191
+ followViewer(state, reset);
192
+
193
+ return {
194
+ sourcePoints: state.sourcePoints,
195
+ targetPoints: state.targetPoints,
196
+ pickingActive: state.pickingActive,
197
+ togglePickMode,
198
+ reset,
199
+ isComplete,
200
+ toSourceCrsPoints: (): CrsPoint[] => sourceCrsPoints(state),
201
+ toTargetCrsPoints: (): CrsPoint[] => targetCrsPoints(state),
202
+ editSourcePoint,
203
+ };
204
+ }
205
+
206
+ export { useCrsAlign };
@@ -0,0 +1,102 @@
1
+ import { type CrsItem, useDataStore } from "@ogw_front/stores/data";
2
+ import type { CRS } from "@geode/opengeodeweb-back/schema_types";
3
+ import type { DataId } from "@geode/opengeodeweb-microservice/schema_types";
4
+ import type { DataIdentified } from "@ogw_front/types/identifiers";
5
+ import { formatCrsCode } from "@ogw_front/utils/crs";
6
+ import { useCrsStore } from "@ogw_front/stores/crs";
7
+ import { useFeedbackStore } from "@ogw_front/stores/feedback";
8
+
9
+ interface AssignedEntry extends DataIdentified {
10
+ name: string;
11
+ crs?: CRS;
12
+ implausible?: boolean;
13
+ }
14
+
15
+ // Unsupported: the data cannot hold a CRS, like a grid, and is left as it is
16
+ interface AssignResponse {
17
+ crs?: CRS;
18
+ implausible?: boolean;
19
+ unsupported?: boolean;
20
+ linked?: { id: string; crs?: CRS; implausible?: boolean; unsupported?: boolean }[];
21
+ }
22
+
23
+ function plausibleEntries(entries: AssignedEntry[]): { data_id: DataId; name: string; crs: CRS }[] {
24
+ return entries.flatMap(({ data_id, name, crs, implausible }) => {
25
+ if (crs === undefined || implausible === true) {
26
+ return [];
27
+ }
28
+ return [{ data_id, name, crs }];
29
+ });
30
+ }
31
+
32
+ function buildAssignedEntries(
33
+ assignedItem: Pick<CrsItem, "data_id" | "name">,
34
+ linked: CrsItem[],
35
+ { crs, implausible, linked: linkedResponses }: AssignResponse,
36
+ ): AssignedEntry[] {
37
+ return [
38
+ { data_id: assignedItem.data_id, name: assignedItem.name, crs, implausible },
39
+ ...linked.map(({ data_id: linkedId, name }) => {
40
+ const response = linkedResponses?.find((entry) => entry.id === linkedId);
41
+ return {
42
+ data_id: linkedId,
43
+ name,
44
+ crs: response?.crs,
45
+ implausible: response?.implausible,
46
+ };
47
+ }),
48
+ ];
49
+ }
50
+
51
+ function implausibleDescription(
52
+ implausibleItems: Pick<CrsItem, "data_id" | "name">[],
53
+ crs: CRS | undefined,
54
+ itemId: string | undefined,
55
+ ): string {
56
+ const mismatch = `do not match ${formatCrsCode(crs)}. Georeference the data?`;
57
+ // Named when linked data were assigned too: the user must know which one to align
58
+ return implausibleItems.length === 1 && implausibleItems[0]?.data_id === itemId
59
+ ? `These coordinates ${mismatch}`
60
+ : `${implausibleItems.map(({ name }) => name).join(", ")}: these coordinates ${mismatch}`;
61
+ }
62
+
63
+ type ImplausibleWarning = (
64
+ implausibleItems: Pick<CrsItem, "data_id" | "name">[],
65
+ crs: CRS | undefined,
66
+ itemId: string | undefined,
67
+ ) => void;
68
+
69
+ // Picking only works in the viewer: the alignment continues in its floating panel
70
+ function useOpenCrsAlignment(): (target: Pick<CrsItem, "data_id">) => Promise<void> {
71
+ const dataStore = useDataStore();
72
+ const crsStore = useCrsStore();
73
+ return async (target) => {
74
+ // The panel needs the whole data: a linked data is only known by its CRS fields
75
+ const full = await dataStore.item(target.data_id);
76
+ crsStore.openCrsAlign(full);
77
+ };
78
+ }
79
+
80
+ function useImplausibleWarning(
81
+ openAlignment: (target: Pick<CrsItem, "data_id">) => Promise<void>,
82
+ ): ImplausibleWarning {
83
+ const feedbackStore = useFeedbackStore();
84
+
85
+ // The label is kept: local coordinates usually need an alignment instead
86
+ return (implausibleItems, crs, itemId) => {
87
+ const [first] = implausibleItems;
88
+ if (!first) {
89
+ return;
90
+ }
91
+ const description = implausibleDescription(implausibleItems, crs, itemId);
92
+ feedbackStore.add_warning(description, {
93
+ label: "Georeference",
94
+ onClick: () => {
95
+ void openAlignment(first);
96
+ },
97
+ });
98
+ };
99
+ }
100
+
101
+ export type { AssignResponse, AssignedEntry };
102
+ export { buildAssignedEntries, plausibleEntries, useImplausibleWarning, useOpenCrsAlignment };
@@ -0,0 +1,22 @@
1
+ import type { ComputedRef } from "vue";
2
+ import type { CrsItem } from "@ogw_front/stores/data";
3
+ import { sameCrsState } from "@ogw_front/utils/crs";
4
+ import { useCrsStore } from "@ogw_front/stores/crs";
5
+
6
+ // Data offered by "Apply also to": only data in the same CRS state can share the frame or the CRS of the item
7
+ function useCrsLinkCandidates(
8
+ item: () => Pick<CrsItem, "data_id" | "crs"> | undefined,
9
+ ): ComputedRef<CrsItem[]> {
10
+ const crsStore = useCrsStore();
11
+ return computed(() => {
12
+ const current = item();
13
+ if (!current) {
14
+ return [];
15
+ }
16
+ return crsStore.crsItems.filter(
17
+ (other) => other.data_id !== current.data_id && sameCrsState(other.crs, current.crs),
18
+ );
19
+ });
20
+ }
21
+
22
+ export { useCrsLinkCandidates };
@@ -0,0 +1,63 @@
1
+ import type { ComputedRef, Ref } from "vue";
2
+ import { type CrsEntry, crsEntrySchema } from "@ogw_front/utils/crs";
3
+ import { defineUserSetting, useUserSetting } from "@ogw_front/composables/user_setting";
4
+ import type { CRS } from "@geode/opengeodeweb-back/schema_types";
5
+ import { z } from "zod";
6
+
7
+ const HISTORY_SIZE = 5;
8
+
9
+ const crsFavoritesSetting = defineUserSetting({
10
+ key: "crsFavorites",
11
+ schema: z.array(crsEntrySchema),
12
+ fallback: [],
13
+ });
14
+ const crsHistorySetting = defineUserSetting({
15
+ key: "crsHistory",
16
+ schema: z.array(crsEntrySchema),
17
+ fallback: [],
18
+ });
19
+
20
+ interface CrsShortlistApi {
21
+ favorites: Ref<CrsEntry[]>;
22
+ recent: ComputedRef<CrsEntry[]>;
23
+ toggleFavorite: (crs: CRS) => void;
24
+ recordUse: (crs: CRS) => void;
25
+ }
26
+
27
+ // Codes are only unique within an authority (e.g. EPSG:4812 and ESRI:4812)
28
+ function crsKey({ authority, code }: CrsEntry): string {
29
+ return `${authority}:${code}`;
30
+ }
31
+
32
+ // Only what the CRS selector shows: the back may add other fields
33
+ function toEntry({ authority, code, name }: CRS): CrsEntry {
34
+ return { authority, code, name };
35
+ }
36
+
37
+ // The favorite CRS and the last ones used, kept across launches for the CRS selectors (user settings)
38
+ function useCrsShortlist(): CrsShortlistApi {
39
+ const favorites = useUserSetting(crsFavoritesSetting);
40
+ const history = useUserSetting(crsHistorySetting);
41
+
42
+ // The whole history, favorites included: the selector shows the two lists in their own row
43
+ const recent = computed(() => history.value);
44
+
45
+ function toggleFavorite(crs: CRS): void {
46
+ const key = crsKey(crs);
47
+ if (favorites.value.some((entry) => crsKey(entry) === key)) {
48
+ favorites.value = favorites.value.filter((entry) => crsKey(entry) !== key);
49
+ return;
50
+ }
51
+ favorites.value = [...favorites.value, toEntry(crs)];
52
+ }
53
+
54
+ function recordUse(crs: CRS): void {
55
+ const key = crsKey(crs);
56
+ const others = history.value.filter((entry) => crsKey(entry) !== key);
57
+ history.value = [toEntry(crs), ...others].slice(0, HISTORY_SIZE);
58
+ }
59
+
60
+ return { favorites, recent, toggleFavorite, recordUse };
61
+ }
62
+
63
+ export { useCrsShortlist };
@@ -0,0 +1,197 @@
1
+ import {
2
+ type AssignResponse,
3
+ buildAssignedEntries,
4
+ useImplausibleWarning,
5
+ useOpenCrsAlignment,
6
+ } from "@ogw_front/composables/crs_assign";
7
+ import { type CrsItem, type DataItem, useDataStore } from "@ogw_front/stores/data";
8
+ import { type FeedbackAction, useFeedbackStore } from "@ogw_front/stores/feedback";
9
+ import { formatCrsCode, sameCrs } from "@ogw_front/utils/crs";
10
+ import type { CRS } from "@geode/opengeodeweb-back/schema_types";
11
+ import { useCrs } from "@ogw_front/composables/crs";
12
+ import { useCrsStore } from "@ogw_front/stores/crs";
13
+
14
+ // Shared by every instance: a batch is harmonized once the previous one is done
15
+ let queue: Promise<void> = Promise.resolve();
16
+
17
+ function byCreation(items: DataItem[]): DataItem[] {
18
+ return items.toSorted((left, right) =>
19
+ (left.created_at ?? "").localeCompare(right.created_at ?? ""),
20
+ );
21
+ }
22
+
23
+ // A failed batch must not block the next ones
24
+ async function settled(promise: Promise<void>): Promise<void> {
25
+ try {
26
+ await promise;
27
+ } catch {
28
+ // The failure is reported by the caller of the batch
29
+ }
30
+ }
31
+
32
+ function names(items: Pick<DataItem, "name">[]): string {
33
+ return items.map((item) => item.name).join(", ");
34
+ }
35
+
36
+ // One dialog can only edit one item: several items are fixed from the data manager
37
+ function editAction(items: Pick<DataItem, "data_id">[]): FeedbackAction | undefined {
38
+ const [item] = items;
39
+ if (items.length !== 1 || item === undefined) {
40
+ return undefined;
41
+ }
42
+ return {
43
+ label: "Edit CRS",
44
+ onClick: (): void => {
45
+ useCrsStore().requestCrsEdit(item.data_id);
46
+ },
47
+ };
48
+ }
49
+
50
+ // The project CRS before the import, else the current one; before any, the first one met
51
+ function referenceCrs(
52
+ projectCrs: CRS | undefined,
53
+ existing: DataItem[],
54
+ imported: DataItem[],
55
+ ): CRS | undefined {
56
+ return (
57
+ projectCrs ??
58
+ useCrsStore().projectCrs ??
59
+ (existing.find((item) => item.crs) ?? imported.find((item) => item.crs))?.crs
60
+ );
61
+ }
62
+
63
+ async function convertImported(
64
+ imported: DataItem[],
65
+ reference: CRS,
66
+ ): Promise<{ converted: DataItem[]; failed: DataItem[]; withoutCrs: DataItem[] }> {
67
+ const converted: DataItem[] = [];
68
+ const failed: DataItem[] = [];
69
+ const withoutCrs: DataItem[] = [];
70
+ // Conversions reload the viewer object: one at a time
71
+ // oxlint-disable no-await-in-loop
72
+ for (const item of imported) {
73
+ if (item.crs === undefined) {
74
+ withoutCrs.push(item);
75
+ } else if (!sameCrs(item.crs, reference)) {
76
+ try {
77
+ await useCrs().convertCrs(item, reference, { focus: false });
78
+ converted.push(item);
79
+ } catch {
80
+ failed.push(item);
81
+ }
82
+ }
83
+ }
84
+ // oxlint-enable no-await-in-loop
85
+ return { converted, failed, withoutCrs };
86
+ }
87
+
88
+ function reportConversions(converted: DataItem[], failed: DataItem[], code: string): void {
89
+ if (converted.length > 0) {
90
+ useFeedbackStore().add_success(`Converted to ${code}: ${names(converted)}`);
91
+ }
92
+ if (failed.length > 0) {
93
+ useFeedbackStore().add_warning(
94
+ `Could not convert to ${code}: ${names(failed)}`,
95
+ editAction(failed),
96
+ );
97
+ }
98
+ }
99
+
100
+ function warnWithoutCrs(withoutCrs: CrsItem[], code: string): void {
101
+ if (withoutCrs.length > 0) {
102
+ useFeedbackStore().add_warning(
103
+ `No CRS, project in ${code}: ${names(withoutCrs)}`,
104
+ editAction(withoutCrs),
105
+ );
106
+ }
107
+ }
108
+
109
+ // The request feedback already shows the error: the caller falls back to the warning
110
+ async function requestAssign(
111
+ first: CrsItem,
112
+ linkedItems: CrsItem[],
113
+ reference: CRS,
114
+ ): Promise<AssignResponse | undefined> {
115
+ try {
116
+ return await useCrs().assignCrs(first, reference, { linkedItems, onlyPlausible: true });
117
+ } catch {
118
+ return undefined;
119
+ }
120
+ }
121
+
122
+ // One request for all of them: data that cannot hold a CRS, like grids, are left out by the back
123
+ async function assignProjectCrs(withoutCrs: CrsItem[], reference: CRS): Promise<void> {
124
+ const [first, ...linkedItems] = withoutCrs;
125
+ if (first === undefined) {
126
+ return;
127
+ }
128
+ const code = formatCrsCode(reference);
129
+ const response = await requestAssign(first, linkedItems, reference);
130
+ if (response === undefined) {
131
+ warnWithoutCrs(withoutCrs, code);
132
+ return;
133
+ }
134
+ const entries = buildAssignedEntries(first, linkedItems, response);
135
+ const assigned = entries.filter((entry) => entry.crs !== undefined);
136
+ if (assigned.length > 0) {
137
+ useFeedbackStore().add_success(`${code} assigned: ${names(assigned)}`);
138
+ }
139
+ useImplausibleWarning(useOpenCrsAlignment())(
140
+ entries.filter((entry) => entry.implausible === true && entry.crs === undefined),
141
+ reference,
142
+ undefined,
143
+ );
144
+ }
145
+
146
+ async function harmonize(ids: string[], projectCrs: CRS | undefined): Promise<void> {
147
+ const items = byCreation(await useDataStore().allItems());
148
+ const imported = items.filter((item) => ids.includes(item.data_id));
149
+ const existing = items.filter((item) => !ids.includes(item.data_id));
150
+ const reference = referenceCrs(projectCrs, existing, imported);
151
+ if (reference === undefined) {
152
+ return;
153
+ }
154
+ const { converted, failed, withoutCrs } = await convertImported(imported, reference);
155
+ const code = formatCrsCode(reference);
156
+ reportConversions(converted, failed, code);
157
+ await assignProjectCrs(withoutCrs, reference);
158
+ }
159
+
160
+ async function harmonizeAfter(
161
+ previous: Promise<void>,
162
+ ids: string[],
163
+ projectCrs: CRS | undefined,
164
+ ): Promise<void> {
165
+ await previous;
166
+ await harmonize(ids, projectCrs);
167
+ }
168
+
169
+ // ProjectCrs: the project CRS taken before the data landed, as the project follows them
170
+ async function harmonizeImportedCrs(ids: string[], projectCrs?: CRS): Promise<void> {
171
+ if (ids.length === 0) {
172
+ return;
173
+ }
174
+ const run = harmonizeAfter(queue, ids, projectCrs);
175
+ queue = settled(run);
176
+ await run;
177
+ }
178
+
179
+ // A project CRS chosen after the import: the data without CRS take it too, when they fit it
180
+ async function adoptProjectCrs(crs: CRS): Promise<void> {
181
+ await assignProjectCrs(
182
+ useCrsStore().crsItems.filter((item) => item.crs === undefined),
183
+ crs,
184
+ );
185
+ }
186
+
187
+ // Imported data in another CRS than the project is converted to it.
188
+ // Data without CRS take the project CRS when their coordinates fit it.
189
+ // Others are likely in a local frame: the warning leads to their georeferencing
190
+ function useProjectCrs(): {
191
+ harmonizeImportedCrs: (ids: string[], projectCrs?: CRS) => Promise<void>;
192
+ adoptProjectCrs: (crs: CRS) => Promise<void>;
193
+ } {
194
+ return { harmonizeImportedCrs, adoptProjectCrs };
195
+ }
196
+
197
+ export { useProjectCrs };