|
1 | 1 | <script lang="ts">
|
2 |
| - //@ts-nocheck |
3 |
| -
|
4 | 2 | import {
|
5 | 3 | MapLibre,
|
6 | 4 | GeoJSON,
|
|
11 | 9 | ControlButton,
|
12 | 10 | ControlGroup,
|
13 | 11 | } from "svelte-maplibre";
|
14 |
| - import { contrastingColor } from "./colors"; |
| 12 | + import { contrastingColor } from "./colors.js"; |
15 | 13 | import { colorbrewer } from "./colorbrewer.js";
|
16 | 14 | import { hoverStateFilter } from "svelte-maplibre/filters.js";
|
17 |
| - import type { maplibregl, ExpressionSpecification } from "maplibre-gl"; |
| 15 | + import type { LngLatLike } from "maplibre-gl"; |
| 16 | + import type { FeatureCollection } from "geojson"; |
18 | 17 | import fullTopo from "./fullTopo.json";
|
19 | 18 | import * as topojson from "topojson-client";
|
20 | 19 | import Tooltip from "./Tooltip.svelte";
|
|
26 | 25 | } from "./mapUtils.js";
|
27 | 26 | import NonStandardControls from "./NonStandardControls.svelte";
|
28 | 27 | import { replaceState } from "$app/navigation";
|
29 |
| -
|
30 |
| - import { joinData } from "./dataJoin"; |
31 | 28 | import { page } from "$app/state";
|
| 29 | + import { joinData } from "./dataJoin.js"; |
32 | 30 |
|
33 | 31 | let {
|
34 | 32 | data,
|
35 |
| - cooperativeGestures, |
| 33 | + cooperativeGestures = true, |
36 | 34 | standardControls = true,
|
37 | 35 | navigationControl,
|
38 | 36 | navigationControlPosition = "top-left",
|
|
52 | 50 | geoType,
|
53 | 51 | year,
|
54 | 52 | metric,
|
55 |
| - breaksType, |
| 53 | + breaksType = "quantile", |
56 | 54 | numberOfBreaks = 5,
|
57 | 55 | fillOpacity = 0.5,
|
58 | 56 | changeOpacityOnHover = true,
|
|
63 | 61 | updateHash = (u) => {
|
64 | 62 | replaceState(u, page.state);
|
65 | 63 | },
|
| 64 | + useInitialHash = true, |
66 | 65 | mapHeight = 200,
|
| 66 | + }: { |
| 67 | + data: object[]; |
| 68 | + cooperativeGestures?: boolean; |
| 69 | + standardControls?: boolean; |
| 70 | + navigationControl?: boolean; |
| 71 | + navigationControlPosition?: string; |
| 72 | + geolocateControl?: boolean; |
| 73 | + geolocateControlPosition?: string; |
| 74 | + fullscreenControl?: boolean; |
| 75 | + fullscreenControlPosition?: string; |
| 76 | + scaleControl?: boolean; |
| 77 | + scaleControlPosition?: string; |
| 78 | + scaleControlUnit?: string; |
| 79 | + styleSheet?: string | URL | object; |
| 80 | + colorPalette?: string; |
| 81 | + showBorder?: boolean; |
| 82 | + maxBorderWidth?: number; |
| 83 | + tooltip?: boolean; |
| 84 | + clickToZoom?: boolean; |
| 85 | + geoType?: string; |
| 86 | + year?: string | number; |
| 87 | + metric?: string; |
| 88 | + breaksType?: string; |
| 89 | + numberOfBreaks?: number; |
| 90 | + fillOpacity?: number; |
| 91 | + changeOpacityOnHover: boolean; |
| 92 | + hoverOpacity?: number; |
| 93 | + center?: LngLatLike | undefined; |
| 94 | + zoom?: number; |
| 95 | + hash?: boolean; |
| 96 | + updateHash?: (URL) => void; |
| 97 | + useInitialHash?: boolean; |
| 98 | + mapHeight?: number; |
67 | 99 | } = $props();
|
68 |
| -
|
| 100 | + $inspect(data); |
69 | 101 | let styleLookup = {
|
70 | 102 | "Carto-light":
|
71 | 103 | "https://basemaps.cartocdn.com/gl/positron-gl-style/style.json",
|
|
74 | 106 | };
|
75 | 107 | let style = $derived(styleLookup[styleSheet] ?? styleSheet);
|
76 | 108 |
|
77 |
| - let mapData = $derived(data?.filter((d) => d.year == year)[0]?.data); |
| 109 | + let mapData = $derived(data?.filter((d) => d["year"] == year)[0]?.data); |
78 | 110 |
|
79 | 111 | let filteredMapData = $derived(
|
80 | 112 | mapData.map((el) => ({
|
|
84 | 116 | })),
|
85 | 117 | );
|
86 | 118 |
|
87 |
| - const geojsonData = $derived( |
| 119 | + const geojsonData: FeatureCollection = $derived( |
88 | 120 | topojson.feature(fullTopo, fullTopo.objects[geoType]),
|
89 | 121 | );
|
90 | 122 |
|
91 | 123 | let filteredGeoJsonData = $derived(filterGeo(geojsonData, year));
|
92 | 124 |
|
93 |
| - let fillColor = $derived(colorbrewer[colorPalette][numberOfBreaks]); |
| 125 | + let fillColors: string[] = $derived( |
| 126 | + colorbrewer[colorPalette][numberOfBreaks], |
| 127 | + ); |
94 | 128 |
|
95 | 129 | let borderColor = "#003300";
|
96 | 130 |
|
|
105 | 139 | : [],
|
106 | 140 | );
|
107 | 141 |
|
108 |
| - let colors = $derived(fillColor.map((d) => contrastingColor(d))); |
| 142 | + let colors = $derived(fillColors.map((d) => contrastingColor(d))); |
109 | 143 | $effect(() => {
|
110 | 144 | //Things can get out of sync when changing source
|
111 | 145 | //this section makes sure that the geojson layers end up below the text layers
|
|
122 | 156 | )?.id;
|
123 | 157 | if (geoJsonLayerIds && labelLayerId) {
|
124 | 158 | for (let layer of geoJsonLayerIds) {
|
125 |
| - map.moveLayer(layer, labelLayerId); |
| 159 | + map?.moveLayer(layer, labelLayerId); |
126 | 160 | }
|
127 | 161 | }
|
128 | 162 |
|
|
157 | 191 | filteredMapData.map((d) => {
|
158 | 192 | return {
|
159 | 193 | ...d,
|
160 |
| - color: getColor(d.metric, breaks, fillColor), |
| 194 | + color: getColor(d.metric, breaks, fillColors), |
161 | 195 | };
|
162 | 196 | }),
|
163 | 197 | );
|
|
194 | 228 | ]);
|
195 | 229 | }
|
196 | 230 | }
|
197 |
| - //Even if hash is false, if the page is loaded with a location hash use that as the initial settings, rather than than the values passed to the component |
| 231 | + //When useInitialHash is true, even if hash is false, if the page is loaded with a location hash use that as the initial settings, rather than the values passed to the component |
198 | 232 | const initialLocationHash = page.url.hash.replace("#", "").split("/");
|
199 | 233 | const useLocationHash = initialLocationHash.length >= 3 ? true : false;
|
| 234 | +
|
| 235 | + center = useInitialHash |
| 236 | + ? useLocationHash |
| 237 | + ? [+initialLocationHash[2], +initialLocationHash[1]] |
| 238 | + : center |
| 239 | + : center; |
| 240 | +
|
| 241 | + zoom = useInitialHash |
| 242 | + ? useLocationHash |
| 243 | + ? +initialLocationHash[0] |
| 244 | + : zoom |
| 245 | + : zoom; |
200 | 246 | </script>
|
201 | 247 |
|
202 | 248 | <div style="height: {mapHeight}px;">
|
|
205 | 251 | bind:loaded
|
206 | 252 | {style}
|
207 | 253 | {standardControls}
|
208 |
| - center={useLocationHash |
209 |
| - ? [initialLocationHash[2], initialLocationHash[1]] |
210 |
| - : center} |
211 |
| - zoom={useLocationHash ? initialLocationHash[0] : zoom} |
| 254 | + {center} |
| 255 | + {zoom} |
212 | 256 | {hash}
|
213 | 257 | {updateHash}
|
214 | 258 | class="map"
|
|
0 commit comments