-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
192 lines (177 loc) · 6.45 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport", initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.4.0/dist/leaflet.css"
integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA=="
crossorigin="" />
<script src="https://unpkg.com/leaflet@1.4.0/dist/leaflet.js"
integrity="sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg=="
crossorigin="">
</script>
<script src="QuadTree.js"></script>
<script src="L.GridLayer.MaskCanvas.js"></script>
<style>
#mymap {
height: 760px;
}
</style>
<title>Personal coverage map</title>
</head>
<body>
<h1>Google maps data</h1>
<input id="file" type="file" />
<button id="reset">reset</button>
<div id="mymap"></div>
<script>
// Making a map and tiles
// Setting a higher initial zoom to make effect more obvious
const mymap = L.map('mymap').setView([0, 0], 3);
const attribution =
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors';
const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const tiles = L.tileLayer(tileUrl, { attribution });
tiles.addTo(mymap);
var layer = new L.GridLayer.MaskCanvas({
//opacity: 0.5,
radius: 50,
useAbsoluteRadius: true,
color: '#089',
noMask: true, // true results in normal (filled) circled, instead masked circles
//lineColor: '#A00' // color of the circle outline if noMask is true
});
mymap.addLayer(layer);
function inside(point, vs) {
// ray-casting algorithm based on
// http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
};
function sort_poly(points) {
points.sort((a, b) => a[1] - b[1]);
// Get center y
const cy = (points[0][1] + points[points.length - 1][1]) / 2;
// Sort from right to left
points.sort((a, b) => b[0] - a[0]);
// Get center x
const cx = (points[0][0] + points[points.length - 1][0]) / 2;
// Center point
var center = [cx, cy];
// Pre calculate the angles as it will be slow in the sort
// As the points are sorted from right to left the first point
// is the rightmost
// Starting angle used to reference other angles
var startAng;
points.forEach(point => {
var ang = Math.atan2(point[1] - center[1], point[0] - center[0]);
if (!startAng) {
startAng = ang
} else {
if (ang < startAng) { // ensure that all points are clockwise of the start point
ang += Math.PI * 2;
}
}
point.angle = ang; // add the angle to the point
});
// first sort clockwise
points.sort((a, b) => a.angle - b.angle);
// then reverse the order
const ccwPoints = points.reverse();
return ccwPoints;
}
var poly = [
[90, 180],
[-90, 180],
[-90, -180],
[90, -180]
];
var cnt = 0;
var circles = [];
var df = [];
poly.forEach(f => {
var cir = L.circle(f, 10, {
color: 'blue',
fillColor: '#f03',
fillOpacity: 0.5
});
circles.push(cir);
cir.addTo(mymap);
}
)
function too_close(df, p){
for (var i = 0; i<df.length; i++)
{
if( (df[i][0]-p[0])**2+(df[i][1]-p[1])**2 < 1e-7 )
return true;
}
return false;
}
function add_marks() {
var data= [];
df.forEach(f => {
if (inside(f, poly)) {
if(!too_close(data, f))
data.push(f);
}
})
layer.setData(data);
//mymap.fitBounds(layer.bounds);
}
function onMapClick(e) {
poly[cnt++] = ([e.latlng.lat, e.latlng.lng]);
for (var i = 0; i < cnt; i++)
circles[i].setLatLng(poly[i]);
cnt = cnt % 4;
if (cnt == 0) {
poly = sort_poly(poly);
add_marks();
}
}
mymap.on('click', onMapClick);
function onChange(event) {
var reader = new FileReader();
reader.onload = onReaderLoad;
reader.readAsText(event.target.files[0]);
}
function onReaderLoad(event) {
var data = JSON.parse(event.target.result);
df = [];
data["locations"].forEach(f => {
var latitudeE7 = f["latitudeE7"];
var longitudeE7 = f["longitudeE7"];
if (latitudeE7 > 900000000)
latitudeE7 = latitudeE7 - 4294967296;
if (longitudeE7 > 1800000000)
longitudeE7 = longitudeE7 - 4294967296;
latitudeE7 /= 1e7;
longitudeE7 /= 1e7;
df.push([latitudeE7, longitudeE7]);
});
add_marks();
}
document.getElementById('file').addEventListener('change', onChange);
function reset(){
poly = [
[90, 180],
[-90, 180],
[-90, -180],
[90, -180]
];
for (var i = 0; i < 4; i++)
circles[i].setLatLng(poly[i]);
add_marks();
}
document.getElementById('reset').addEventListener('click', reset);
</script>
</body>
</html>