-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
331 lines (293 loc) · 8.51 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import Camera from "@arcgis/core/Camera";
import { when } from "@arcgis/core/core/reactiveUtils";
import Point from "@arcgis/core/geometry/Point";
import Graphic from "@arcgis/core/Graphic";
import IdentityManager from "@arcgis/core/identity/IdentityManager";
import OAuthInfo from "@arcgis/core/identity/OAuthInfo";
import GraphicsLayer from "@arcgis/core/layers/GraphicsLayer";
import SceneLayer from "@arcgis/core/layers/SceneLayer";
import ClassBreaksRenderer from "@arcgis/core/renderers/ClassBreaksRenderer";
import StatisticDefinition from "@arcgis/core/rest/support/StatisticDefinition";
import SceneView from "@arcgis/core/views/SceneView";
import WebScene from "@arcgis/core/WebScene";
import SketchViewModel from "@arcgis/core/widgets/Sketch/SketchViewModel";
import "@esri/calcite-components";
import "@esri/calcite-components/dist/components/calcite-alert";
import "@esri/calcite-components/dist/components/calcite-loader";
import { HELSINKI_FIELDS } from "./scenes";
const Reveal = (parent as any).Reveal as RevealStatic | null;
export function initView(itemId?: string, camera?: Camera) {
const container = document.getElementById("viewDiv") as HTMLDivElement;
const viewProps: __esri.SceneViewProperties = {
container,
map: itemId ? new WebScene({ portalItem: { id: itemId } }) : new WebScene({ basemap: "topo" }),
qualityProfile: "medium",
popup: { defaultPopupTemplateEnabled: false },
};
const view = new SceneView(camera ? { ...viewProps, camera } : viewProps);
(view as any).pixelRatio = 1.5;
showSpinnerUntilLoaded(view);
return view;
}
function showSpinnerUntilLoaded(view: SceneView): void {
// const container = view.container;
// container.classList.add("loading");
// const loader = container.appendChild(document.createElement("calcite-loader"));
// loader.type = "indeterminate";
// loader.active = true;
// when(
// () => !view.groundView.updating,
// () => {
// container.classList.remove("loading");
// loader.remove();
// },
// { once: true }
// );
}
export function onInit(title: string, cb: () => void) {
if (getCurrentSlide()?.getAttribute("data-slideId") === title) {
cb();
}
}
export function onFragment(id: string, cb: () => void) {
if (!Reveal) {
return;
}
const run = () => {
if (getCurrentFragmentId() === id) {
cb();
}
};
Reveal.addEventListener("fragmenthidden", run);
Reveal.addEventListener("fragmentshown", run);
run();
}
export function onPlayClick(name: string, cb: () => void): void {
getCurrentSlide()
?.querySelector(`[data-fragment-id="${name}"] > .play`)
?.addEventListener("click", cb);
}
export function getCurrentSlide(): HTMLElement | null {
return Reveal?.getCurrentSlide() as HTMLElement;
}
export function getCurrentFragment(): HTMLElement | null {
return getCurrentSlide()?.querySelector(".current-fragment") ?? null;
}
function getCurrentFragmentId(): string | null {
return getCurrentFragment()?.getAttribute("data-fragment-id") ?? null;
}
export function setHeader(header: string, selector: string = ".header"): void {
const headerElement = getCurrentSlide()?.querySelector(selector);
if (!headerElement) {
return;
}
headerElement.textContent = header;
}
export function showAlert(msg: string): void {
const alertElement = document.createElement("calcite-alert");
alertElement.active = true;
const msgElement = alertElement.appendChild(document.createElement("div"));
msgElement.slot = "message";
msgElement.textContent = msg;
document.body.appendChild(alertElement);
}
export function addOAuthSupport(): void {
IdentityManager.registerOAuthInfos([
new OAuthInfo({
appId: "pZzd4uJ0gZddupQh",
popup: true,
popupCallbackUrl: `${import.meta.env.BASE_URL}oauth-callback-api.html`,
}),
]);
(window as any).setOAuthResponseHash = (responseHash: string) => {
IdentityManager.setOAuthResponseHash(responseHash);
};
}
export function throwIfAborted(signal: AbortSignal): void {
if (signal.aborted) {
throw new Error("AbortError");
}
}
export function throwIfNotAbortError(e: any): void {
if (e?.message !== "AbortError") {
throw e;
}
}
export async function getLayerFromView(layerName: string, view: SceneView): Promise<SceneLayer> {
await (view.map as any).load();
return view.map.layers.find((l) => l.title === layerName) as SceneLayer;
}
const yearClasses = [
{
minYear: 1500,
maxYear: 1899,
color: "#bd0026",
label: "<1900",
},
{
minYear: 1900,
maxYear: 1924,
color: "#f03b20",
label: "1900 - 1924",
},
{
minYear: 1925,
maxYear: 1949,
color: "#fd8d3c",
label: "1925 - 1949",
},
{
minYear: 1950,
maxYear: 1974,
color: "#feb24c",
label: "1951 - 1974",
},
{
minYear: 1975,
maxYear: 1999,
color: "#fed976",
label: "1975 - 1999",
},
{
minYear: 2000,
maxYear: 2020,
color: "#ffffb2",
label: "2000 - 2020",
},
];
export function applyYearRenderer(layer: SceneLayer) {
const classBreakInfos = yearClasses.map((year) => {
return {
minValue: year.minYear,
maxValue: year.maxYear,
symbol: {
type: "mesh-3d",
symbolLayers: [
{
type: "fill",
material: {
color: year.color,
colorMixMode: "replace",
},
edges: {
type: "solid",
color: [50, 50, 50, 0.5],
},
},
],
},
};
});
layer.renderer = new ClassBreaksRenderer({
field: HELSINKI_FIELDS.yearField,
defaultSymbol: {
type: "mesh-3d",
symbolLayers: [
{
type: "fill",
material: {
color: "white",
colorMixMode: "replace",
},
edges: {
type: "solid",
color: [50, 50, 50, 0.5],
},
},
],
} as any,
classBreakInfos: classBreakInfos as any,
});
}
export function applySolarAreaRenderer(layer: SceneLayer) {
layer.renderer = {
type: "simple",
symbol: {
type: "mesh-3d",
symbolLayers: [
{
type: "fill",
material: { color: "white", colorMixMode: "replace" },
edges: {
type: "solid",
color: [50, 50, 50, 0.5],
},
},
],
},
visualVariables: [
{
type: "color",
field: HELSINKI_FIELDS.solarAreaField,
legendOptions: {
title: "Solar-Suitable Area (m<sup>2</sup>)",
},
stops: [
{ value: 200, color: "#406f8a", label: "< 100 m<sup>2</sup>" },
{ value: 1000, color: "#ffe23b", label: "> 1000 m<sup>2</sup>" },
],
binSize: 200,
},
],
} as any;
}
export const point = new Point({
spatialReference: { latestWkid: 3857, wkid: 102100 } as any,
x: 2775690.9496367406,
y: 8434900.987816326,
z: 50,
});
export function addEditablePoint(view: SceneView, onUpdate: (pointGraphic: Graphic) => void) {
const graphicsLayer = new GraphicsLayer({
// elevationInfo: { mode: "on-the-ground" },
title: "Sketch GraphicsLayer",
});
const markerSymbol = {
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
color: [226, 119, 40],
outline: {
// autocasts as new SimpleLineSymbol()
color: [255, 255, 255],
width: 2,
},
};
const pointGraphic = new Graphic({
geometry: point,
symbol: markerSymbol,
});
graphicsLayer.add(pointGraphic);
let sketchVM = new SketchViewModel({
layer: graphicsLayer,
view: view,
defaultCreateOptions: {
hasZ: true, // default value
},
defaultUpdateOptions: {
enableZ: true, // default value
},
});
view.on("click", async (e) => {
const hitTestResult = await view.hitTest(e);
if (hitTestResult.results.find((r) => r.graphic === pointGraphic)) {
sketchVM.update(pointGraphic);
}
});
sketchVM.on("update", () => onUpdate(pointGraphic));
return { graphicsLayer, pointGraphic };
}
// setup statistic definition
export const sumLabel = "sum_area";
export const statDefinition: StatisticDefinition[] = [
new StatisticDefinition({
onStatisticField: HELSINKI_FIELDS.solarAreaField,
outStatisticFieldName: sumLabel,
statisticType: "sum",
}),
];
export function addSumArea(view: SceneView) {
const sumContainer = document.getElementById("sumContainer") as HTMLDivElement;
sumContainer.style.display = "block";
sumContainer.style.backgroundColor = "white";
sumContainer.style.padding = "5px";
view.ui.add("sumContainer", "top-right");
}