-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathindex.html
109 lines (97 loc) · 3.29 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>PakData GIS</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://d3js.org/d3.v5.min.js"></script>
<style>
body {
width: 100%;
height: 100%;
background-color: gray;
margin: 0px;
}
svg {
width: 100%;
height: 100vh;
vertical-align: top
}
div.settings {
background-color: ghostwhite;
position: absolute;
vertical-align: top;
}
div.info {
position: absolute;
display: none;
background-color: deepskyblue;
word-wrap: break-word;
}
svg path:hover {
stroke: aqua;
fill: aquamarine;
}
</style>
</head>
<body>
<div class="settings">
<input type="radio" name="level" value="adm0" checked="checked"><label for="adm0">adm0</label>
<input type="radio" name="level" value="adm1"><label for="adm1">adm1</label>
<input type="radio" name="level" value="adm2"><label for="adm2">adm2</label>
<input type="radio" name="level" value="adm3"><label for="adm3">adm3</label>
</div>
<div class="info" style=""></div>
<!-- The svg element on which map is drawn -->
<svg></svg>
<script>
// requests a GeoJSON file and plots
function plotMap(level) {
d3.json("PAK-GeoJSON/PAK_" + level + ".json").then(d => {
var projection = d3.geoMercator()
.fitSize([width, height], d);
var path = d3.geoPath()
.projection(projection);
// removing existing paths
svg.selectAll("path")
.remove();
// plotting anew
svg.selectAll("path")
.data(d.features)
.enter()
.append("path")
.attr("stroke", "#fff")
.attr("d", path)
.on("mousedown", (feature, i, paths) => {
var loc = d3.mouse(document.getElementsByTagName("body")[0]);
info.style("display", "block")
.style("top", loc[1] + "px")
.style("left", loc[0] + "px")
.html("<pre><code>" +
feature.properties.NAME_0 + "\n" +
(feature.properties.NAME_1 || "") + "\n" +
(feature.properties.NAME_2 || "") + "\n" +
(feature.properties.NAME_3 || "") + "\n" +
"</code></pre>");
});
});
}
// setting up initial variables
var width = document.getElementsByTagName("body")[0].clientWidth;
var height = document.getElementsByTagName("body")[0].clientHeight;
var svg = d3.select("svg");
var info = d3.select(".info");
// adding event listeners for options. Clicking a radio button plots map.
d3.selectAll("input")
.on("click", (data, i, inputs) => {
plotMap(inputs[i].value);
})
// event listener for info div when mouse leaves
info.on("mouseleave", () => {
info.style("display", "none");
});
plotMap("adm0");
</script>
</body>
</html>