-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
200 lines (186 loc) · 8.27 KB
/
test.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
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
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY=" crossorigin="" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" integrity="sha256-20nQCchB9co0qIjJZRGuk2/Z9VM+kNiyxNV1lvTlZBo=" crossorigin=""></script>
<script src="https://unpkg.com/[email protected]/dist/VectorTileLayer.umd.js"></script>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<style>
#map {
width: 100vw;
height: 100vh;
position: absolute;
top: 0;
left: 0;
}
h1 {
position: relative;
margin-left: 48px;
margin-top: 0;
color: black;
background-color: transparent;
font-weight: bolder;
font-family: Courier, monospace;
font-size: 3rem;
z-index: 9999;
}
#app {
font-family: sans-serif;
font-size: 18px;
background-color: rgba(255, 255, 255, 0.7);
padding: 8px;
position: absolute;
right: 12px;
top: 12px;
z-index: 9999;
}
#dataset-select {
font-size: 18px;
width: 100%;
}
#admin-radios {
margin-top: 6px;
padding: 6px;
}
input {
margin-right: 16px;
}
label {
vertical-align: middle;
}
</style>
</head>
<body>
<h1>GROUT Test Page</h1>
<div id="map">
</div>
<div id="app">
<label for="dataset-select">Dataset</label>
<select id="dataset-select" v-if="datasets" v-model="selectedDatasetName">
<option v-for="dataset in Object.keys(datasets)" :value="dataset">{{dataset}}</option>
</select>
<div id="admin-radios" v-if="datasets && datasets[selectedDatasetName].admin2Enabled">
<label for="admin1">Admin 1</label>
<input type="radio" id="admin1" v-model="adminLevel" :value="1" />
<label for="admin2">Admin 2</label>
<input type="radio" id="admin2" v-model="adminLevel" :value="2" />
</div>
</div>
<script>
const map = L.map("map", {
maxBoundsViscosity: 1.0 // prevent any dragging outside max bounds
}).setView({ lon: 0, lat: 0 }, 3);
map.options.minZoom = 3;
map.setMaxBounds(map.getBounds());
const groutServers = {
"local": "http://localhost:5000",
"mrcdata": "https://mrcdata.dide.ic.ac.uk/grout"
};
// Background layer
const attribution = "Tiles © Esri — Esri, DeLorme, NAVTEQ; " +
"Boundaries: <a href='https://gadm.org' target='_blank'>GADM</a> version 4.1";
L.tileLayer("https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}", {
attribution }
).addTo(map);
// shared options
const options = {
interactive: true,
maxNativeZoom: 10,
tms: true, // y values are inverted without this!
};
// Country layer options
const countryOptions = {
style: {
weight: 1,
fill: false,
color: "#777"
},
...options
};
let regionLayer = null;
let countryLayer = null;
const { createApp, ref, onMounted, watch, computed } = Vue;
createApp({
setup() {
// Dict of friendly name to { url, dataset, admin2Enabled }
const datasets = ref(null);
const selectedDatasetName = ref("");
const adminLevel = ref(1);
const nameProp = computed(() => `NAME_${adminLevel.value}`);
// Region layer options - assign shade of blue based on first letter of name!
const regionOptions = computed(() => ({
style: (feature, layerName) => {
const shade = Math.min((feature.properties[nameProp.value].charCodeAt(0) - 65)/26, 1) * 255;
return {
fillColor: `rgb(${shade}, ${shade}, 255)`,
fillOpacity: 0.5,
color: "#cccccc",
weight: 1
}
},
...options
}));
onMounted(async () => {
// Populate drop down with available datasets from both servers
const fetchedDatasets = {};
for (let server in groutServers) {
const url = groutServers[server];
try {
const response = await fetch(`${url}/metadata`);
const metadata = await response.json();
if (metadata.status === "success") {
const datasetNames = Object.keys(metadata.data.datasets.tile);
datasetNames.forEach((dataset) => {
const admin2Enabled = metadata.data.datasets.tile[dataset].levels.includes("admin2");
fetchedDatasets[`${server}: ${dataset}`] = {url, dataset, admin2Enabled};
});
}
}
catch(e) {
console.log(`Error fetching from ${url}: ${e}`);
}
}
datasets.value = fetchedDatasets;
if (Object.keys(fetchedDatasets).length) {
selectedDatasetName.value = Object.keys(fetchedDatasets)[0];
}
});
watch([selectedDatasetName, adminLevel], () => {
if (!datasets.value[selectedDatasetName.value].admin2Enabled) {
adminLevel.value = 1;
}
updateMap();
});
const updateMap = () => {
if (regionLayer) {
regionLayer.removeFrom(map);
}
if (countryLayer) {
countryLayer.removeFrom(map);
}
const { url, dataset } = datasets.value[selectedDatasetName.value];
const level = adminLevel.value;
regionLayer = VectorTileLayer(`${url}/tile/${dataset}/admin${level}/{z}/{x}/{-y}`, regionOptions.value)
.on('mouseover', function (e) {
// show a tooltip
const name = e.layer.properties[nameProp.value];
L.popup()
.setContent(name)
.setLatLng(e.latlng)
.openOn(map);
});
regionLayer.addTo(map);
countryLayer = VectorTileLayer(`${url}/tile/${dataset}/admin0/{z}/{x}/{-y}`, countryOptions);
countryLayer.addTo(map);
}
return {
datasets,
selectedDatasetName,
adminLevel
}
}
}).mount('#app')
</script>
</body>
</html>