-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.html
151 lines (137 loc) · 4.08 KB
/
viewer.html
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
<html>
<head>
<title>overline viewer</title>
<link
rel="stylesheet"
href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha256-kLaT2GOSpHechhsozzB+flnD+zUyjE2LlfWPgU04xyI="
crossorigin=""
/>
<script
src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha256-WBkoXOwTeyKclOHuWtc+i2uENFpDZ9YPdf5Hf+D7ewM="
crossorigin=""
></script>
<style>
html,
body {
height: 100%;
margin: 0;
}
.leaflet-container {
height: 80%;
width: 100%;
max-width: 100%;
max-height: 100%;
}
</style>
</head>
<body>
<div>
<input type="file" id="load" />
</div>
<div>
<label for="weight">Weight key</label>
<select id="weight"></select>
</div>
<div id="weight-info"></div>
<div><pre id="current-props">Props</pre></div>
<div id="map"></div>
<script>
const map = L.map("map").setView([51.4995, -0.0978], 14);
const tiles = L.tileLayer(
"https://tile.openstreetmap.org/{z}/{x}/{y}.png",
{
maxZoom: 19,
attribution:
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}
).addTo(map);
let gj = null;
let gjLayer = null;
let min = Number.MAX_VALUE;
let max = Number.MIN_VALUE;
document.getElementById("load").onchange = (e) => {
const reader = new FileReader();
reader.onload = (e) => {
try {
gj = JSON.parse(e.target.result);
loadFile();
} catch (err) {
window.alert(`Couldn't load file: ${err}`);
}
};
reader.readAsText(e.target.files[0]);
};
let weightChoices = document.getElementById("weight");
weightChoices.onchange = (e) => {
useKey(e.target.value);
};
function loadFile() {
if (gjLayer) {
gjLayer.remove();
weightChoices.replaceChildren();
}
// Find numeric properties
for (let [key, value] of Object.entries(gj.features[0].properties)) {
if (Number.isFinite(value)) {
let option = document.createElement("option");
option.value = key;
option.textContent = key;
weightChoices.appendChild(option);
}
}
if (weightChoices.children.length > 0) {
useKey(weightChoices.children[0].value);
} else {
window.alert("Warning: no numeric properties, can't visualize this");
}
}
let currentProps = document.getElementById("current-props");
function useKey(key) {
if (gjLayer) {
gjLayer.remove();
}
min = Number.MAX_VALUE;
max = Number.MIN_VALUE;
for (let f of gj.features) {
if (f.properties.hasOwnProperty(key)) {
let value = f.properties[key];
min = Math.min(min, value);
max = Math.max(max, value);
}
}
document.getElementById("weight-info").textContent = `${min} to ${max}`;
gjLayer = L.geoJSON(gj, {
onEachFeature: (feature, layer) => {
layer.setStyle({
color: "red",
weight: weightProperty(feature.properties[key]),
opacity: 0.5,
});
layer.on({
mouseover: (ev) => {
ev.target.setStyle({
opacity: 1.0,
});
currentProps.textContent = JSON.stringify(feature.properties);
},
mouseout: (ev) => {
ev.target.setStyle({
opacity: 0.5,
});
currentProps.textContent = "Props";
},
});
},
}).addTo(map);
map.fitBounds(gjLayer.getBounds());
}
function weightProperty(value) {
let percent = (min != max) ? (value - min) / (max - min) : 1.0;
// Return weight between 10 and 30
return 10 + (30 - 10) * percent;
}
</script>
</body>
</html>