-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGeospatialVector.tsx
More file actions
320 lines (292 loc) · 8.21 KB
/
GeospatialVector.tsx
File metadata and controls
320 lines (292 loc) · 8.21 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
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
import React, { useEffect, useRef, useState } from "react";
import Map from "ol/Map";
import View from "ol/View";
import { VisualizationConfigOut } from "../../../openapi/v2";
import VectorLayer from "ol/layer/Vector";
import VectorSource from "ol/source/Vector";
import GeoJSON from "ol/format/GeoJSON";
import TileLayer from "ol/layer/Tile";
import { OSM } from "ol/source";
import { bbox as bboxStrategy } from "ol/loadingstrategy";
import { transformExtent } from "ol/proj";
import { FeatureLike } from "ol/Feature";
type GeospatialProps = {
visConfigEntry?: VisualizationConfigOut;
};
export default function GeospatialVector(props: GeospatialProps) {
const { visConfigEntry } = props;
const [layerWMS, setLayerWMS] = useState<string | undefined>(undefined);
const [layerDL, setLayerDL] = useState<string | undefined>(undefined);
const [layerAttributes, setLayerAttributes] = useState<string[] | undefined>(
undefined
);
const [filterAttribute, setFilterAttribute] = useState<string | undefined>(
undefined
);
const [attributeValues, setAttributeValues] = useState<string[] | undefined>(
undefined
);
const [attributeValue, setAttributeValue] = useState<string | undefined>(
undefined
);
const [vectorRef, setVectorRef] = useState<VectorLayer<any> | undefined>(
undefined
);
const [map, setMap] = useState<Map | undefined>(undefined);
const mapElement = useRef();
function updateFilterAttribute(event) {
setFilterAttribute(event.target.value);
}
function setAttributeValueFn(event) {
setAttributeValue(event.target.value);
}
function clearFilter() {
setFilterAttribute(undefined);
setAttributeValue("Show All");
}
useEffect(() => {
if (visConfigEntry !== undefined) {
if (
visConfigEntry.parameters &&
visConfigEntry.parameters["WMS Layer URL"]
) {
const wms_url = String(visConfigEntry.parameters["WMS Layer URL"]);
setLayerWMS(wms_url);
const attribute_url = wms_url.replace(
"GetFeature",
"describeFeatureType"
);
fetch(attribute_url).then((response) => {
if (response.status === 200) {
response.json().then((json) => {
const attrs: string[] = [];
json["featureTypes"][0]["properties"].forEach((a) => {
attrs.push(a["name"]);
});
setLayerAttributes(attrs);
});
}
});
}
}
}, [visConfigEntry]);
useEffect(() => {
if (layerWMS !== undefined) {
// Determine bounding box extent & center point from URL
let bbox = [0, 0, 0, 0];
const entries = layerWMS.split("&");
entries.forEach((entry) => {
if (entry.startsWith("bbox=")) {
const vals = entry.replace("bbox=", "").split(",");
bbox = vals.map((v) => parseFloat(v));
}
});
bbox = transformExtent(bbox, "EPSG:4326", "EPSG:3857");
const center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
let wms_url = layerWMS;
if (attributeValue != undefined) {
const params = new URLSearchParams(wms_url.split("?")[1]);
params.delete("bbox");
wms_url = `${wms_url.split("?")[0]}?${params.toString()}`;
wms_url += `&CQL_FILTER=${filterAttribute}='${attributeValue}'`;
}
const source = new VectorSource({
url: wms_url,
format: new GeoJSON(),
strategy: bboxStrategy,
});
const vecLayer = new VectorLayer({
source: source,
});
const wms_map = new Map({
target: mapElement.current,
layers: [
new TileLayer({
source: new OSM(),
}),
vecLayer,
],
view: new View({
projection: "EPSG:3857",
center: center,
}),
controls: [],
});
wms_map.getView().fit(bbox);
const info = document.getElementById("info");
let currentFeature: FeatureLike | undefined;
const displayFeatureInfo = function (pixel, target) {
const feature = target.closest(".ol-control")
? undefined
: wms_map.forEachFeatureAtPixel(pixel, function (feature) {
return feature;
});
if (feature && info) {
info.style.left = `${pixel[0]}px`;
info.style.top = `${pixel[1]}px`;
if (feature !== currentFeature) {
info.style.visibility = "visible";
let label =
"<table><th>" + "<td>Field</td>" + "<td>Value</td></th>";
const allProps = feature.getProperties();
for (const key in allProps) {
if (
!["operation_", "sp_region", "price", "prosperty_"].includes(
key
)
)
continue;
label += `<tr><td><b>${key}</b></td><td>${allProps[key]}</td></tr>`;
}
label += "</table>";
info.innerHTML = label;
}
} else {
if (info) info.style.visibility = "hidden";
}
currentFeature = feature;
};
// Interactive behavior
wms_map.on("pointermove", function (evt) {
if (evt.dragging && info) {
info.style.visibility = "hidden";
currentFeature = undefined;
return;
}
const pixel = wms_map.getEventPixel(evt.originalEvent);
displayFeatureInfo(pixel, evt.originalEvent.target);
});
wms_map.on("click", function (evt) {
displayFeatureInfo(evt.pixel, evt.originalEvent.target);
});
wms_map.getTargetElement().addEventListener("pointerleave", function () {
currentFeature = undefined;
if (info) info.style.visibility = "hidden";
});
setVectorRef(vecLayer);
setMap(wms_map);
}
}, [layerWMS]);
useEffect(() => {
if (layerWMS !== undefined) {
// Determine bounding box extent & center point from URL
let bbox = [0, 0, 0, 0];
const entries = layerWMS.split("&");
entries.forEach((entry) => {
if (entry.startsWith("bbox=")) {
const vals = entry.replace("bbox=", "").split(",");
bbox = vals.map((v) => parseFloat(v));
}
});
bbox = transformExtent(bbox, "EPSG:4326", "EPSG:3857");
const center = [(bbox[0] + bbox[2]) / 2, (bbox[1] + bbox[3]) / 2];
let wms_url = layerWMS;
if (
filterAttribute &&
attributeValue != undefined &&
attributeValue != "Show All"
) {
const params = new URLSearchParams(wms_url.split("?")[1]);
params.delete("bbox");
wms_url = `${wms_url.split("?")[0]}?${params.toString()}`;
wms_url += `&CQL_FILTER=${filterAttribute}='${attributeValue}'`;
}
const source = new VectorSource({
url: wms_url,
format: new GeoJSON(),
strategy: bboxStrategy,
});
const vecLayer = new VectorLayer({
source: source,
});
if (map) {
if (vectorRef) map.removeLayer(vectorRef);
map.addLayer(vecLayer);
setVectorRef(vecLayer);
}
}
}, [layerWMS, attributeValue]);
useEffect(() => {
if (layerWMS) {
const params = new URLSearchParams(layerWMS.split("?")[1]);
params.delete("bbox");
params.delete("outputFormat");
let dl_url = `${layerWMS.split("?")[0]}?${params.toString()}`;
if (attributeValue && filterAttribute)
dl_url += `&CQL_FILTER=${filterAttribute}='${attributeValue}'`;
dl_url += "&outputFormat=shape-zip";
setLayerDL(dl_url);
}
}, [attributeValue]);
useEffect(() => {
const values: string[] = ["Show All"];
if (vectorRef && filterAttribute) {
vectorRef
.getSource()
.getFeatures()
.forEach((feat: any) => {
const val = feat["values_"][filterAttribute];
if (!values.includes(val)) values.push(val);
});
setAttributeValues(values.sort());
}
}, [filterAttribute]);
return (() => {
return (
<>
<div
ref={mapElement}
style={{
width: "400px",
height: "300px",
}}
className="map-container"
/>
{layerAttributes ? (
<>
<b>Field Name</b>
<select onChange={updateFilterAttribute}>
{layerAttributes.map((att) => {
return <option value={att}>{att}</option>;
})}
</select>
</>
) : (
<></>
)}
{attributeValues ? (
<>
<br />
<b>Value</b>
<select onChange={setAttributeValueFn} value={attributeValue}>
{attributeValues.map((att) => {
return <option value={att}>{att}</option>;
})}
</select>
<button onClick={clearFilter}>Clear Filter</button>
</>
) : (
<></>
)}
{layerDL ? (
<>
<br />
<a href={layerDL}>Download Data</a>
</>
) : (
<></>
)}
<br />
<div
id="info"
style={{
//position: "absolute",
//display: "inline-block",
height: "auto",
width: "auto",
}}
/>
</>
);
})();
}