-
-
Notifications
You must be signed in to change notification settings - Fork 662
Expand file tree
/
Copy pathOgcVectorTilesSource.tsx
More file actions
172 lines (166 loc) · 6.14 KB
/
Copy pathOgcVectorTilesSource.tsx
File metadata and controls
172 lines (166 loc) · 6.14 KB
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
import { Input, Label } from "@geolibre/ui";
import { useEffect, useRef, useState } from "react";
import { useTranslation } from "react-i18next";
import { resolveOgcVectorTiles } from "../../../../lib/ogc-vector-tiles";
import { DEFAULT_OGC_VECTOR_TILES_STYLE_URL, DEFAULT_OGC_VECTOR_TILES_URL } from "../constants";
import { createBaseLayer } from "../helpers";
import { AddDataSourceForm, SampleDataSelect, useAddDataSource } from "../shared";
interface OgcSample {
tilesUrl: string;
styleUrl: string;
}
/**
* Adds an OGC API - Tiles (vector) source as a MapLibre vector layer. The user
* supplies a TileJSON metadata URL or a `{z}/{y}/{x}` MVT template, and may add
* a Mapbox/MapLibre style URL to discover the tileset's source layers (an OGC
* API TileJSON often omits them). Rendering uses GeoLibre's default per-source
* layer styling; the style's own paint is not applied.
*/
export function OgcVectorTilesSource({
initialUrl = "",
initialStyleUrl = "",
initialSourceLayers = "",
}: {
initialUrl?: string;
initialStyleUrl?: string;
initialSourceLayers?: string;
}) {
const { t } = useTranslation();
const source = useAddDataSource(t("addData.ogcVectorTiles.defaultName"));
const [tilesUrl, setTilesUrl] = useState(initialUrl);
const [styleUrl, setStyleUrl] = useState(initialStyleUrl);
const [sourceLayersText, setSourceLayersText] = useState(initialSourceLayers);
// Cancel the in-flight metadata/style/collections fetches if the dialog closes
// mid-request, so a slow response cannot add the layer after the user leaves.
const resolveAbortRef = useRef<AbortController | null>(null);
useEffect(
() => () => {
resolveAbortRef.current?.abort();
},
[],
);
const applySample = (sample: OgcSample) => {
setTilesUrl(sample.tilesUrl);
setStyleUrl(sample.styleUrl);
setSourceLayersText("");
};
const handleSubmit = source.runSubmit(async () => {
if (!tilesUrl.trim() && !styleUrl.trim()) {
throw new Error(t("addData.ogcVectorTiles.errorUrl"));
}
const manualLayers = sourceLayersText
.split(",")
.map((layer) => layer.trim())
.filter(Boolean);
resolveAbortRef.current?.abort();
const controller = new AbortController();
resolveAbortRef.current = controller;
const config = await resolveOgcVectorTiles({
tilesUrl: tilesUrl.trim(),
styleUrl: styleUrl.trim() || undefined,
sourceLayers: manualLayers,
signal: controller.signal,
});
// A superseded/cancelled request must not add a layer after the fact.
if (controller.signal.aborted) return;
if (!config.url && !(config.tiles && config.tiles.length > 0)) {
throw new Error(t("addData.ogcVectorTiles.errorNoTiles"));
}
if (config.sourceLayers.length === 0) {
throw new Error(t("addData.ogcVectorTiles.errorNoLayers"));
}
const name = source.layerName.trim() || config.name || t("addData.ogcVectorTiles.defaultName");
source.addAndClose(
createBaseLayer(
name,
"vector-tiles",
{
type: "vector",
...(config.url ? { url: config.url } : {}),
...(config.tiles ? { tiles: config.tiles } : {}),
sourceLayer: config.sourceLayers[0],
sourceLayers: config.sourceLayers,
bounds: config.bounds,
minzoom: config.minzoom,
maxzoom: config.maxzoom,
},
{
bounds: config.bounds,
center: config.center,
minzoom: config.minzoom,
maxzoom: config.maxzoom,
sourceKind: "ogc-vector-tiles",
sourceLayers: config.sourceLayers,
tilesUrl: tilesUrl.trim() || undefined,
styleUrl: styleUrl.trim() || undefined,
},
),
// fitLayer resolves a vector-tiles layer only from bounds (it never reads
// `center` for this type), so only request a fit when bounds are known.
{ fit: Boolean(config.bounds) },
);
});
return (
<AddDataSourceForm
layerName={source.layerName}
onLayerNameChange={source.setLayerName}
beforeLayerId={source.beforeLayerId}
onBeforeLayerIdChange={source.setBeforeLayerId}
onSubmit={handleSubmit}
error={source.error}
submitDisabled={source.isSubmitting}
useServiceIcon
>
<div className="space-y-3">
<div className="space-y-1.5">
<Label htmlFor="ogc-vt-tiles-url">{t("addData.ogcVectorTiles.tilesUrl")}</Label>
<Input
id="ogc-vt-tiles-url"
placeholder={t("addData.ogcVectorTiles.tilesUrlPlaceholder")}
value={tilesUrl}
onChange={(event) => setTilesUrl(event.target.value)}
/>
<p className="text-xs text-muted-foreground">
{t("addData.ogcVectorTiles.tilesUrlHint")}
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="ogc-vt-style-url">{t("addData.ogcVectorTiles.styleUrl")}</Label>
<Input
id="ogc-vt-style-url"
placeholder={t("addData.ogcVectorTiles.styleUrlPlaceholder")}
value={styleUrl}
onChange={(event) => setStyleUrl(event.target.value)}
/>
<p className="text-xs text-muted-foreground">
{t("addData.ogcVectorTiles.styleUrlHint")}
</p>
</div>
<div className="space-y-1.5">
<Label htmlFor="ogc-vt-source-layers">{t("addData.ogcVectorTiles.sourceLayers")}</Label>
<Input
id="ogc-vt-source-layers"
placeholder={t("addData.ogcVectorTiles.sourceLayersPlaceholder")}
value={sourceLayersText}
onChange={(event) => setSourceLayersText(event.target.value)}
/>
<p className="text-xs text-muted-foreground">
{t("addData.ogcVectorTiles.sourceLayersHint")}
</p>
</div>
<SampleDataSelect
samples={[
{
label: t("addData.ogcVectorTiles.sampleLabel"),
value: {
tilesUrl: DEFAULT_OGC_VECTOR_TILES_URL,
styleUrl: DEFAULT_OGC_VECTOR_TILES_STYLE_URL,
},
},
]}
onSelect={applySample}
/>
</div>
</AddDataSourceForm>
);
}