@macrostrat/column-views 3.1.0 → 3.2.0

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 (118) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/age-axis.js +2 -2
  3. package/dist/age-axis.js.map +1 -1
  4. package/dist/age-axis.module.sass.cjs +6 -1
  5. package/dist/age-axis.module.sass.cjs.map +1 -1
  6. package/dist/age-axis.module.sass.js +4 -1
  7. package/dist/age-axis.module.sass.js.map +1 -1
  8. package/dist/age-model-overlay.js +2 -2
  9. package/dist/age-model-overlay.js.map +1 -1
  10. package/dist/age-model-overlay.module.sass.cjs +6 -1
  11. package/dist/age-model-overlay.module.sass.cjs.map +1 -1
  12. package/dist/age-model-overlay.module.sass.js +4 -1
  13. package/dist/age-model-overlay.module.sass.js.map +1 -1
  14. package/dist/column.js +2 -2
  15. package/dist/column.js.map +1 -1
  16. package/dist/column.module.sass.cjs +6 -1
  17. package/dist/column.module.sass.cjs.map +1 -1
  18. package/dist/column.module.sass.js +4 -1
  19. package/dist/column.module.sass.js.map +1 -1
  20. package/dist/correlation-chart/main.js +2 -2
  21. package/dist/correlation-chart/main.js.map +1 -1
  22. package/dist/correlation-chart/main.module.sass.cjs +6 -1
  23. package/dist/correlation-chart/main.module.sass.cjs.map +1 -1
  24. package/dist/correlation-chart/main.module.sass.js +4 -1
  25. package/dist/correlation-chart/main.module.sass.js.map +1 -1
  26. package/dist/data-provider/store.cjs +1 -1
  27. package/dist/data-provider/store.cjs.map +1 -1
  28. package/dist/data-provider/store.js +1 -1
  29. package/dist/data-provider/store.js.map +1 -1
  30. package/dist/facets/carbon-isotopes/isotopes-column.js +2 -2
  31. package/dist/facets/carbon-isotopes/isotopes-column.js.map +1 -1
  32. package/dist/facets/carbon-isotopes/isotopes-column.module.sass.cjs +6 -1
  33. package/dist/facets/carbon-isotopes/isotopes-column.module.sass.cjs.map +1 -1
  34. package/dist/facets/carbon-isotopes/isotopes-column.module.sass.js +4 -1
  35. package/dist/facets/carbon-isotopes/isotopes-column.module.sass.js.map +1 -1
  36. package/dist/facets/detrital-zircon/index.js +2 -2
  37. package/dist/facets/detrital-zircon/index.js.map +1 -1
  38. package/dist/facets/detrital-zircon/index.module.sass.cjs +6 -1
  39. package/dist/facets/detrital-zircon/index.module.sass.cjs.map +1 -1
  40. package/dist/facets/detrital-zircon/index.module.sass.js +4 -1
  41. package/dist/facets/detrital-zircon/index.module.sass.js.map +1 -1
  42. package/dist/facets/fossils/index.js +2 -2
  43. package/dist/facets/fossils/index.js.map +1 -1
  44. package/dist/facets/fossils/taxon-ranges.js +2 -2
  45. package/dist/facets/fossils/taxon-ranges.js.map +1 -1
  46. package/dist/facets/fossils/taxon-ranges.module.sass.cjs +6 -1
  47. package/dist/facets/fossils/taxon-ranges.module.sass.cjs.map +1 -1
  48. package/dist/facets/fossils/taxon-ranges.module.sass.js +4 -1
  49. package/dist/facets/fossils/taxon-ranges.module.sass.js.map +1 -1
  50. package/dist/facets/measurements/base.js +2 -2
  51. package/dist/facets/measurements/base.js.map +1 -1
  52. package/dist/facets/measurements/base.module.sass.cjs +6 -1
  53. package/dist/facets/measurements/base.module.sass.cjs.map +1 -1
  54. package/dist/facets/measurements/base.module.sass.js +4 -1
  55. package/dist/facets/measurements/base.module.sass.js.map +1 -1
  56. package/dist/index.cjs +10 -5
  57. package/dist/index.cjs.map +1 -1
  58. package/dist/index.js +6 -1
  59. package/dist/index.js.map +1 -1
  60. package/dist/maps/_shared/styles.cjs +6 -1
  61. package/dist/maps/_shared/styles.cjs.map +1 -1
  62. package/dist/maps/_shared/styles.js +6 -1
  63. package/dist/maps/_shared/styles.js.map +1 -1
  64. package/dist/maps/column-navigation/mapbox/keyboard-navigation.d.ts +1 -1
  65. package/dist/maps/index.d.ts +1 -0
  66. package/dist/maps/units-overlay/index.cjs +254 -0
  67. package/dist/maps/units-overlay/index.cjs.map +1 -0
  68. package/dist/maps/units-overlay/index.d.ts +14 -0
  69. package/dist/maps/units-overlay/index.js +252 -0
  70. package/dist/maps/units-overlay/index.js.map +1 -0
  71. package/dist/section.js +2 -2
  72. package/dist/section.js.map +1 -1
  73. package/dist/unit-details/panel.js +2 -2
  74. package/dist/unit-details/panel.js.map +1 -1
  75. package/dist/unit-details/panel.module.sass.cjs +6 -1
  76. package/dist/unit-details/panel.module.sass.cjs.map +1 -1
  77. package/dist/unit-details/panel.module.sass.js +4 -1
  78. package/dist/unit-details/panel.module.sass.js.map +1 -1
  79. package/dist/unit-details/popover.js +2 -2
  80. package/dist/unit-details/popover.js.map +1 -1
  81. package/dist/unit-details/popover.module.sass.cjs +6 -1
  82. package/dist/unit-details/popover.module.sass.cjs.map +1 -1
  83. package/dist/unit-details/popover.module.sass.js +4 -1
  84. package/dist/unit-details/popover.module.sass.js.map +1 -1
  85. package/dist/units/boxes.js +2 -2
  86. package/dist/units/boxes.js.map +1 -1
  87. package/dist/units/boxes.module.sass.cjs +6 -1
  88. package/dist/units/boxes.module.sass.cjs.map +1 -1
  89. package/dist/units/boxes.module.sass.js +4 -1
  90. package/dist/units/boxes.module.sass.js.map +1 -1
  91. package/dist/units/colors.cjs +79 -18
  92. package/dist/units/colors.cjs.map +1 -1
  93. package/dist/units/colors.d.ts +17 -4
  94. package/dist/units/colors.js +79 -18
  95. package/dist/units/colors.js.map +1 -1
  96. package/dist/units/composite.js +2 -2
  97. package/dist/units/composite.js.map +1 -1
  98. package/dist/units/composite.module.sass.cjs +6 -1
  99. package/dist/units/composite.module.sass.cjs.map +1 -1
  100. package/dist/units/composite.module.sass.js +4 -1
  101. package/dist/units/composite.module.sass.js.map +1 -1
  102. package/dist/units/index.cjs +2 -0
  103. package/dist/units/index.cjs.map +1 -1
  104. package/dist/units/index.js +3 -0
  105. package/dist/units/index.js.map +1 -1
  106. package/dist/units/resolvers.cjs +11 -0
  107. package/dist/units/resolvers.cjs.map +1 -1
  108. package/dist/units/resolvers.d.ts +5 -0
  109. package/dist/units/resolvers.js +11 -0
  110. package/dist/units/resolvers.js.map +1 -1
  111. package/package.json +10 -9
  112. package/src/data-provider/store.ts +1 -0
  113. package/src/maps/_shared/styles.ts +10 -3
  114. package/src/maps/index.ts +1 -0
  115. package/src/maps/unit-map.stories.ts +64 -0
  116. package/src/maps/units-overlay/index.ts +336 -0
  117. package/src/units/colors.ts +137 -25
  118. package/src/units/resolvers.ts +16 -0
package/src/maps/index.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./column-navigation";
2
2
  export * from "./column-correlation";
3
3
  export * from "./_shared";
4
+ export * from "./units-overlay";
@@ -0,0 +1,64 @@
1
+ import h from "@macrostrat/hyper";
2
+ import { Meta } from "@storybook/react-vite";
3
+ import { InsetMap } from "./_shared";
4
+ import { MacrostratDataProvider } from "@macrostrat/data-provider";
5
+ import { useMemo } from "react";
6
+ import { buildUnitsStyle, MacrostratUnitsOverlay } from "./units-overlay";
7
+
8
+ // @ts-ignore
9
+ const mapboxToken = import.meta.env.VITE_MAPBOX_API_TOKEN;
10
+
11
+ // More on default export: https://storybook.js.org/docs/react/writing-stories/introduction#default-export
12
+ export default {
13
+ title: "Column views/Maps/Unit map",
14
+ component: UnitMapComponent,
15
+ description: "A map of units through time",
16
+ argTypes: {
17
+ time: { control: "number", defaultValue: 100 },
18
+ ageSpan: { control: "number", defaultValue: 0.05 },
19
+ patterns: { control: "boolean", defaultValue: false },
20
+ },
21
+ } as Meta<typeof InsetMap>;
22
+
23
+ function UnitMapComponent(props) {
24
+ const overlayStyles = useMemo(() => {
25
+ let styles: any[] = [
26
+ buildUnitsStyle({ color: "#444", patterns: props.patterns }),
27
+ ];
28
+ return styles;
29
+ }, []);
30
+
31
+ return h(
32
+ MacrostratDataProvider,
33
+ {
34
+ baseURL: "https://dev.macrostrat.org/api/v2",
35
+ },
36
+ h(
37
+ "div",
38
+ h(
39
+ InsetMap,
40
+ {
41
+ style: { width: "800px", height: "600px" },
42
+ accessToken: mapboxToken,
43
+ overlayStyles,
44
+ },
45
+ h(MacrostratUnitsOverlay, props),
46
+ ),
47
+ ),
48
+ );
49
+ }
50
+
51
+ export const UnitMap = {
52
+ args: {
53
+ time: 100,
54
+ ageSpan: 0.05,
55
+ },
56
+ };
57
+
58
+ export const WithPatterns = {
59
+ args: {
60
+ time: 100,
61
+ ageSpan: 0.05,
62
+ patterns: true,
63
+ },
64
+ };
@@ -0,0 +1,336 @@
1
+ import {
2
+ useLithologies,
3
+ useMacrostratColumns,
4
+ } from "@macrostrat/data-provider";
5
+ import { useAPIResult } from "@macrostrat/ui-components";
6
+ import { useMapStyleOperator } from "@macrostrat/mapbox-react";
7
+ import { FeatureCollection } from "geojson";
8
+ import { useMemo } from "react";
9
+ import { mergeStyles, setGeoJSON } from "@macrostrat/mapbox-utils";
10
+ import { UnitLithology, UnitLong } from "@macrostrat/api-types";
11
+ import { flattenLithologies, getMixedColorForData } from "../../units";
12
+ import { getBestFGDCPatternForLithologyList } from "../../units/resolvers";
13
+ import {
14
+ setupStyleImageManager,
15
+ loadStyleImage,
16
+ StyleFragment,
17
+ } from "@macrostrat/map-styles";
18
+ import { asChromaColor, getCSSVariable } from "@macrostrat/color-utils";
19
+ import { buildGeoJSONSource } from "@macrostrat/mapbox-utils";
20
+ import pMap from "p-map";
21
+
22
+ export interface UnitsOverlayProps {
23
+ time: number;
24
+ ageSpan?: number;
25
+ project?: number | number[];
26
+ patterns?: boolean;
27
+ }
28
+
29
+ export function MacrostratUnitsOverlay(props: UnitsOverlayProps) {
30
+ const { time, ageSpan = 0.05, patterns = false } = props;
31
+ const lithMap = useLithologies();
32
+
33
+ useMapStyleOperator(
34
+ (map) => {
35
+ if (!patterns) return;
36
+ setupStyleImageManager(map, { verbose: false });
37
+ },
38
+ [patterns],
39
+ );
40
+
41
+ const params = useMemo(() => {
42
+ let project: number[] | undefined;
43
+ if (Number.isFinite(props.project)) {
44
+ project = [props.project as number];
45
+ } else if (!Array.isArray(props.project)) {
46
+ project = undefined;
47
+ }
48
+ // Project can only be a single project for now
49
+ const projectCompat =
50
+ project != null && project.length > 0 ? project[0] : undefined;
51
+
52
+ return {
53
+ age_bottom: time + ageSpan / 2,
54
+ age_top: time - ageSpan / 2,
55
+ response: "long",
56
+ project: projectCompat,
57
+ };
58
+ }, [time, ageSpan, props.project]);
59
+
60
+ const units: UnitLong[] = useAPIResult("/units", params);
61
+ const columns = useMacrostratColumns(params.project, false);
62
+
63
+ // Set up basic columns layer
64
+ useMapStyleOperator(
65
+ (map) => {
66
+ if (columns == null) {
67
+ return;
68
+ }
69
+ const data: FeatureCollection = {
70
+ type: "FeatureCollection",
71
+ features: columns,
72
+ };
73
+ setGeoJSON(map, "columns", data);
74
+ },
75
+ [columns],
76
+ );
77
+
78
+ useMapStyleOperator(
79
+ (map) => {
80
+ if (units == null || columns == null || lithMap == null) {
81
+ return;
82
+ }
83
+
84
+ const getColorForLithology = (data: UnitLithology): string | null => {
85
+ return lithMap?.get(data.lith_id)?.color ?? null;
86
+ };
87
+
88
+ const unitsMap = postProcessUnits(units, getColorForLithology);
89
+ handleUnitsLayerUpdate(map, unitsMap, columns);
90
+ if (patterns) {
91
+ handlePatternOverlayUpdate(
92
+ map,
93
+ unitsMap,
94
+ columns,
95
+ getColorForLithology,
96
+ ).then(() => {});
97
+ }
98
+ },
99
+ [units, lithMap, columns, patterns],
100
+ );
101
+
102
+ return null;
103
+ }
104
+
105
+ function handleUnitsLayerUpdate(
106
+ map,
107
+ unitsMap: Map<number, UnitsDisplayInfo>,
108
+ columns: any[],
109
+ ) {
110
+ // Update feature states
111
+ for (const column of columns) {
112
+ const col_id = column.properties.col_id;
113
+ const info = unitsMap.get(Number(col_id));
114
+ const color = info?.color;
115
+ map.setFeatureState(
116
+ { source: "columns", id: col_id },
117
+ {
118
+ color,
119
+ shown: color != null,
120
+ },
121
+ );
122
+ }
123
+ }
124
+
125
+ async function handlePatternOverlayUpdate(
126
+ map,
127
+ unitsMap: Map<number, UnitsDisplayInfo>,
128
+ columns: any[],
129
+ getColor: LithologyColorGetter,
130
+ ) {
131
+ // synthesize patterns not yet in cache
132
+
133
+ const colsWithPatterns = [];
134
+ const patternIDs = new Set<string>();
135
+ // Update feature states
136
+ for (const column of columns) {
137
+ const col_id = column.properties.col_id;
138
+ const info = unitsMap.get(Number(col_id));
139
+ if (info == null) continue;
140
+ const pattern = getPatternSpec(info?.liths, getColor);
141
+ if (pattern == null) continue;
142
+
143
+ colsWithPatterns.push({
144
+ type: "Feature",
145
+ geometry: column.geometry,
146
+ properties: {
147
+ col_id,
148
+ pattern,
149
+ },
150
+ });
151
+ patternIDs.add(pattern);
152
+ }
153
+
154
+ const patternsToLoad = patternIDs.values().filter((d) => !map.hasImage(d));
155
+
156
+ const mapper = async (patternID) => {
157
+ try {
158
+ await loadStyleImage(map, patternID, { pixelRatio: 10 });
159
+ } catch (e) {}
160
+ };
161
+ // Load all pattern images
162
+ await pMap(patternsToLoad, mapper, { concurrency: 5 });
163
+
164
+ // Set data for pattern layer
165
+ // This is inefficient but the only way to set per-feature patterns in Mapbox
166
+ const patternData: FeatureCollection = {
167
+ type: "FeatureCollection",
168
+ features: colsWithPatterns,
169
+ };
170
+ setGeoJSON(map, "column-patterns", patternData);
171
+ }
172
+
173
+ const cachedColors = new Map<string, string>();
174
+
175
+ function getDarkenedColor(color: string): string {
176
+ /** Memoized function to get a darkened version of a color for pattern use */
177
+ if (cachedColors.has(color)) {
178
+ return cachedColors.get(color);
179
+ }
180
+ const c1 = asChromaColor(color).set("hsl.l", 0.4).set("hsl.s", 0.8).hex();
181
+ cachedColors.set(color, c1);
182
+ return c1;
183
+ }
184
+
185
+ interface UnitsDisplayInfo {
186
+ col_id: number;
187
+ units: UnitLong[];
188
+ color: string;
189
+ liths: UnitLithology[];
190
+ }
191
+
192
+ type LithologyColorGetter = (val: UnitLithology) => string | null;
193
+
194
+ function postProcessUnits(
195
+ units: UnitLong[],
196
+ getColor: LithologyColorGetter,
197
+ ): Map<number, UnitsDisplayInfo> {
198
+ // Group by col_id
199
+ const unitsByColumn: Map<number, UnitLong[]> = new Map();
200
+ for (const unit of units) {
201
+ const col_id = Number(unit.col_id);
202
+ if (!unitsByColumn.has(col_id)) {
203
+ unitsByColumn.set(col_id, [unit]);
204
+ } else {
205
+ unitsByColumn.set(col_id, [...unitsByColumn.get(col_id), unit]);
206
+ }
207
+ }
208
+
209
+ // Summarize units for display
210
+ const res = new Map<number, UnitsDisplayInfo>();
211
+ for (const [col_id, unitList] of unitsByColumn.entries()) {
212
+ const liths = flattenLithologies(unitList.map((u) => u.lith));
213
+ res.set(col_id, {
214
+ col_id,
215
+ units: unitList,
216
+ liths,
217
+ color: getMixedColorForData(liths, getColor),
218
+ });
219
+ }
220
+
221
+ return res;
222
+ }
223
+
224
+ function getPatternSpec(
225
+ liths: UnitLithology[],
226
+ getColor: LithologyColorGetter,
227
+ ): string | null {
228
+ const patternData = getBestFGDCPatternForLithologyList(liths);
229
+ if (patternData == null) return null;
230
+ let { patternID, lith } = patternData;
231
+ const color = getDarkenedColor(getColor(lith));
232
+ return `fgdc:${patternID}:${color}:transparent`;
233
+ }
234
+
235
+ type UnitsStyleOptions = {
236
+ color?: string;
237
+ patterns?: boolean;
238
+ };
239
+
240
+ export function buildUnitsStyle(opts: UnitsStyleOptions = {}): StyleFragment {
241
+ const { color = null, patterns = false } = opts;
242
+
243
+ let columnBaseColor: any =
244
+ color ?? getCSSVariable("--text-subtle-color", "black");
245
+ const columnSelectedColor = getCSSVariable("--selection-color", "purple");
246
+
247
+ // If color is in the feature state or geojson properties, use that as second choice
248
+
249
+ let columnColor: any = [
250
+ "coalesce",
251
+ ["feature-state", "color"],
252
+ ["get", "color"],
253
+ columnBaseColor,
254
+ ];
255
+
256
+ const baseStyle: StyleFragment = {
257
+ sources: {
258
+ columns: buildGeoJSONSource(),
259
+ },
260
+ version: 8,
261
+ layers: [
262
+ {
263
+ id: "columns-fill",
264
+ type: "fill",
265
+ source: "columns",
266
+ paint: {
267
+ "fill-color": columnColor,
268
+ "fill-opacity": [
269
+ "case",
270
+ ["boolean", ["feature-state", "shown"], false],
271
+ 0.8,
272
+ ["boolean", ["feature-state", "hover"], false],
273
+ 0.3,
274
+ 0.1,
275
+ ],
276
+ },
277
+ },
278
+ {
279
+ id: "columns-line",
280
+ type: "line",
281
+ source: "columns",
282
+ paint: {
283
+ "line-color": columnColor,
284
+ "line-width": 1,
285
+ "line-opacity": 0.5,
286
+ },
287
+ },
288
+ {
289
+ id: "columns-points",
290
+ type: "circle",
291
+ source: "columns",
292
+ paint: {
293
+ "circle-radius": 4,
294
+ "circle-color": [
295
+ "case",
296
+ ["boolean", ["feature-state", "shown"], false],
297
+ columnColor,
298
+ columnBaseColor,
299
+ ],
300
+ "circle-opacity": [
301
+ "case",
302
+ ["boolean", ["feature-state", "shown"], false],
303
+ 0.8,
304
+ ["boolean", ["feature-state", "hover"], false],
305
+ 0.3,
306
+ 0.1,
307
+ ],
308
+ },
309
+ filter: ["==", "$type", "Point"],
310
+ },
311
+ ],
312
+ };
313
+
314
+ if (!patterns) {
315
+ return baseStyle;
316
+ }
317
+
318
+ const patternStyle: StyleFragment = {
319
+ sources: {
320
+ "column-patterns": buildGeoJSONSource(),
321
+ },
322
+ layers: [
323
+ {
324
+ id: "columns-fill-pattern",
325
+ type: "fill",
326
+ source: "column-patterns",
327
+ paint: {
328
+ "fill-pattern": ["get", "pattern"],
329
+ "fill-opacity": 1,
330
+ },
331
+ },
332
+ ],
333
+ };
334
+
335
+ return mergeStyles(baseStyle, patternStyle);
336
+ }
@@ -1,22 +1,93 @@
1
1
  import chroma from "chroma-js";
2
- import { UnitLong } from "@macrostrat/api-types";
2
+ import {
3
+ Environment,
4
+ Lithology,
5
+ UnitLithology,
6
+ UnitLong,
7
+ } from "@macrostrat/api-types";
3
8
 
4
9
  interface KeyOptions {
5
- key?: string;
6
- id_key?: string;
10
+ key?: "lith" | "environ";
11
+ id_key?: "lith_id" | "environ_id";
7
12
  }
8
13
 
9
14
  export function getMixedUnitColor(
10
15
  unit: UnitLong,
11
- lithMap,
16
+ lithMap: Map<number, { color: string }>,
12
17
  inDarkMode = false,
13
18
  asBackground = true,
14
19
  keyOpts: KeyOptions = null,
15
20
  ): string | null {
16
21
  const { key = "lith", id_key = "lith_id" } = keyOpts ?? {};
17
22
  const liths = unit[key];
18
- if (liths == null) {
19
- return "gray";
23
+ const getColor = (data: UnitLithology): string | null => {
24
+ return lithMap?.get(data[id_key])?.color ?? null;
25
+ };
26
+
27
+ return getMixedColorForData(liths, getColor, {
28
+ inDarkMode,
29
+ asBackground,
30
+ key: id_key,
31
+ });
32
+ }
33
+
34
+ interface ColorBuilderOptions {
35
+ key?: "lith_id" | "environ_id";
36
+ inDarkMode?: boolean;
37
+ asBackground?: boolean;
38
+ }
39
+
40
+ export function flattenLithologies<
41
+ T extends { prop: number | null; lith_id?: number; environ_id?: number },
42
+ >(liths: T[][], normalize: boolean = true): T[] {
43
+ /** Unify proportions in lithology/environment data across several units, normalizing if needed.
44
+ * If normalize is true, proportions are scaled to sum to 1 within each unit (with remaining proportions
45
+ * distributed equally among liths without specified proportions). This helps ensure that units with a
46
+ * few lithologies with specified proportions don't skew the color mixing.
47
+ *
48
+ * Lithologies are deduplicated based on their identifying property (lith_id or environ_id) if that is present.
49
+ */
50
+ if (liths == null || liths.length === 0) {
51
+ return [];
52
+ }
53
+
54
+ const unitCount = liths.length;
55
+
56
+ let newLiths: T[] = [];
57
+ for (const unitLiths of liths) {
58
+ let l1 = unitLiths;
59
+ if (normalize) {
60
+ l1 = addProportionsToAllData(unitLiths);
61
+ }
62
+ // Divide proportions by number of units to balance influence
63
+ for (const lith of l1) {
64
+ if (lith.prop == null || isNaN(lith.prop) || lith.prop <= 0) {
65
+ continue;
66
+ }
67
+ lith.prop = lith.prop / unitCount;
68
+ }
69
+ newLiths.push(...l1);
70
+ }
71
+ if (newLiths[0]?.lith_id != null) {
72
+ newLiths = mergeLikeData(newLiths, "lith_id");
73
+ } else if (newLiths[0]?.environ_id != null) {
74
+ newLiths = mergeLikeData(newLiths, "environ_id");
75
+ }
76
+ // Sort by proportion descending
77
+ newLiths.sort((a, b) => b.prop - a.prop);
78
+ return newLiths;
79
+ }
80
+
81
+ export function getMixedColorForData<
82
+ T extends UnitLithology | Lithology | Environment,
83
+ >(
84
+ liths: T[] | null,
85
+ getColor: (val: T) => string | null,
86
+ options: ColorBuilderOptions = {},
87
+ ): string | null {
88
+ const { inDarkMode = false, asBackground = true } = options;
89
+ if (liths == null || liths.length === 0) {
90
+ return null;
20
91
  }
21
92
  const lithData = liths
22
93
  .map((d) => {
@@ -25,33 +96,19 @@ export function getMixedUnitColor(
25
96
  prop = null;
26
97
  }
27
98
  return {
28
- lith_id: d[id_key],
99
+ ...d,
29
100
  prop,
30
- color: lithMap?.get(d[id_key])?.color,
101
+ color: getColor?.(d) ?? d.color,
31
102
  };
32
103
  })
33
104
  .filter((d) => d.color != null);
34
105
 
35
- const lithsWithProp = lithData.filter((d) => d.prop != null);
36
- const lithsWithoutProp = lithData.filter((d) => d.prop == null);
37
-
38
- const totalProp = lithsWithProp.reduce((sum, d) => sum + d.prop, 0);
39
- if (totalProp < 1 && lithsWithoutProp.length > 0) {
40
- const remainingProp = 1 - totalProp;
41
- // Distribute remaining proportion equally among liths without prop
42
- const equalProp = remainingProp / lithsWithoutProp.length;
43
- lithsWithoutProp.forEach((d) => {
44
- lithsWithProp.push({
45
- ...d,
46
- prop: equalProp,
47
- });
48
- });
49
- }
50
-
51
- if (lithsWithProp.length == 0) {
106
+ if (lithData.length == 0) {
52
107
  return null;
53
108
  }
54
109
 
110
+ const lithsWithProp = addProportionsToAllData(lithData);
111
+
55
112
  // Mix colors proportionally
56
113
  const baseColor = chroma.average(
57
114
  lithsWithProp.map((d) => d.color),
@@ -69,3 +126,58 @@ export function getMixedUnitColor(
69
126
  return baseColor.set("hsl.l", 0.9).hex();
70
127
  }
71
128
  }
129
+
130
+ function addProportionsToAllData<T extends { prop?: number }>(liths: T[]): T[] {
131
+ /** Make sure that all lithologies/environments have a defined proportion,
132
+ * and that proportions sum to 1. If some liths lack a defined proportion,
133
+ * the remaining proportion is distributed equally among them.
134
+ *
135
+ * NOTE: This function does not normalize proportions if they already sum to more than 1.
136
+ */
137
+ const lithsWithProp = liths.filter((d) => d.prop != null);
138
+ const lithsWithoutProp = liths.filter((d) => d.prop == null);
139
+
140
+ const totalProp = lithsWithProp.reduce((sum, d) => sum + d.prop, 0);
141
+ let overallTotal = 1;
142
+
143
+ if (totalProp < overallTotal && lithsWithoutProp.length > 0) {
144
+ const remainingProp = overallTotal - totalProp;
145
+ // Distribute remaining proportion equally among liths without prop
146
+ const equalProp = remainingProp / lithsWithoutProp.length;
147
+ lithsWithoutProp.forEach((d) => {
148
+ lithsWithProp.push({
149
+ ...d,
150
+ prop: equalProp,
151
+ } as T);
152
+ });
153
+ }
154
+ return lithsWithProp as T[];
155
+ }
156
+
157
+ function mergeLikeData<
158
+ T extends { prop: number; lith_id?: number; environ_id?: number },
159
+ >(liths: T[], key: "lith_id" | "environ_id"): T[] {
160
+ /** Merge lithology/environment data with the same identifying property (lith_id or environ_id),
161
+ * summing their proportions.
162
+ *
163
+ * TODO: handle attribute merging
164
+ */
165
+ const mergedMap: Map<number, T> = new Map();
166
+
167
+ for (const lith of liths) {
168
+ const id = lith[key];
169
+ if (id == null) continue;
170
+
171
+ if (mergedMap.has(id)) {
172
+ const existing = mergedMap.get(id);
173
+ mergedMap.set(id, {
174
+ ...existing,
175
+ prop: existing.prop + lith.prop,
176
+ });
177
+ } else {
178
+ mergedMap.set(id, { ...lith });
179
+ }
180
+ }
181
+
182
+ return Array.from(mergedMap.values());
183
+ }
@@ -1,3 +1,6 @@
1
+ import { Lithology, UnitLithology } from "@macrostrat/api-types";
2
+ import { LithologyCore } from "@macrostrat/api-types/src/lithologies";
3
+
1
4
  const symbolIndex = {
2
5
  "dolomite-limestone": 641,
3
6
  lime_mudstone: 627,
@@ -54,6 +57,19 @@ const symbolIndex = {
54
57
  diamictite: 681,
55
58
  };
56
59
 
60
+ export function getBestFGDCPatternForLithologyList<
61
+ T extends UnitLithology | Lithology,
62
+ >(liths: T[]): { lith: T; patternID: string } | null {
63
+ let lithsSorted = [...liths];
64
+ lithsSorted.sort((a, b) => b.prop - a.prop);
65
+ let firstLith = lithsSorted[0];
66
+ let sym = symbolIndex[firstLith.name];
67
+ if (sym != null) {
68
+ return { lith: firstLith, patternID: `${sym}` };
69
+ }
70
+ return null;
71
+ }
72
+
57
73
  const resolveID = (d: any) => {
58
74
  if (d.lith == null || d.lith.length == 0) {
59
75
  return null;