-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
133 lines (117 loc) · 3.94 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/dropzone-complete"></script>
<script src="https://cdn.jsdelivr.net/npm/@turf/turf@6/turf.min.js"></script>
<script src="./zonal.min.js"></script>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0 auto;
max-width: 800px;
padding: 15px;
}
table {
font-size: 12pt;
width: 100%;
}
th {
background: lightskyblue;
}
select {
width: 100%;
}
</style>
</head>
<body>
<h1>Zonal Demo</h1>
<script>
window.state = {
zone_properties: [],
class_properties: []
};
function getSelectedProperties(id) {
const arr = Array.from(document.querySelectorAll(`#${id} option:checked`)).map(option => option.value);
if (arr.length > 0) {
return arr;
}
};
function getAllProps (geojson) {
const seen = new Set();
turf.propEach(geojson, properties => {
Object.keys(properties).forEach(key => {
seen.add(key);
});
});
return seen;
}
function run () {
if (state.zones && state.classes) {
const { table } = zonal.calculate({
zones: state.zones,
zone_properties: getSelectedProperties("zone_properties"),
classes: state.classes,
class_properties: getSelectedProperties("class_properties"),
// class_geometry_type,
// include_zero_count = false
});
// add header
const table_element = document.getElementById("results-table");
const thead = document.createElement("THEAD");
table.columns.forEach(col => {
const th = document.createElement("TH");
th.innerText = col;
thead.appendChild(th);
});
table_element.appendChild(thead);
table.rows.forEach(row => {
const tr = document.createElement("TR");
table.columns.map(column => {
const td = document.createElement("TD");
const value = row[column];
td.textContent = value === null ? "null" : value;
tr.appendChild(td);
});
table_element.appendChild(tr);
});
}
}
</script>
<h3>Zones</h3>
<dropzone-complete id="zones" height="200px" width="100%"></dropzone-complete>
<script>
document.getElementById("zones").addEventListener("change", async function(event) {
if (event?.detail?.file) {
const select = document.getElementById("zone_properties");
state.zones = JSON.parse(await event.detail.file.text());
select.innerHTML = Array.from(getAllProps(state.zones)).sort().map(k => `<option value="${k}">${k}</option>`).join("\n");
}
});
</script>
<h4>Zone Properties</h4>
<select id="zone_properties" name="zone_properties" multiple></select>
<h3>Classes</h3>
<dropzone-complete id="classes" height="200px" width="100%"></dropzone-complete>
<script>
document.getElementById("classes").addEventListener("change", async function(event) {
console.log("event:", event);
if (event?.detail?.file) {
const select = document.getElementById("class_properties");
state.classes = JSON.parse(await event.detail.file.text());
select.innerHTML = Array.from(getAllProps(state.classes)).sort().map(k => `<option value="${k}">${k}</option>`).join("\n");
}
});
</script>
<h4>Class Properties</h4>
<select id="class_properties" name="class_properties" multiple></select>
<button id="execute" style="font-size: 24pt;">Calculate</button>
<script>
document.getElementById("execute").addEventListener("click", run);
</script>
<div>Results Table</div>
<table id="results-table">
</table>
</body>
</html>