-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixels-on-osm.html
221 lines (145 loc) · 5.42 KB
/
pixels-on-osm.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<link rel="stylesheet" href="includes/leaflet.css" />
<script src="includes/leaflet.js"></script>
<script src="includes/kdTree-min.js"></script>
<body onload="initialize()">
<div id="map" style="height: 600px;"></div>
<script src="usa.js"></script>
<script src="uk.js"></script>
<script src="arg.js"></script>
<script>
DEGREE = 0.017453292519943295769
EARTH_RADIUS = 3958.761
/**
Converts a list of points representing an equiangular map into a kdtree. Input object parameters:
height: map height
width: map width
points: the list of points in the map
*/
function points2Tree(obj) {
var treepts = []
console.log("OBJ", obj);
for (i in obj.points) {
// convert point number to pixel and add 0.5 for centering (top/left pixels are 0)
var pixx = obj.points[i]%obj.width + 0.5
var pixy = Math.floor(obj.points[i]/obj.width) + 0.5
// now, convert pixels to lng and lat
var lng = -180 + pixx/obj.width*360
var lat = 90 - pixy/obj.height*180
// add to treepts
treepts.push({lat: lat*DEGREE, lng: lng*DEGREE})
}
return new kdTree(treepts, distance, ["lat", "lng"])
}
// create a single canvas object used to generate PNGs
let canvas = document.createElement('canvas');
/**
Return the data URL of a PNG file for a given tile. Input object parameters:
lng: the longitude of the west side of the grid tile
lat: the latitude of the south side of the grid tile
grid: the size of the grid in degrees
*/
function latLngGrid2PNG(obj) {
// set up the canvas
canvas.height = 256;
canvas.width = 256;
let ctx = canvas.getContext('2d');
// distance
usnear = ustree.nearest({lat: obj.lat*DEGREE, lng: obj.lng*DEGREE}, 1)[0][1]
uknear = uktree.nearest({lat: obj.lat*DEGREE, lng: obj.lng*DEGREE}, 1)[0][1]
argnear = argtree.nearest({lat: obj.lat*DEGREE, lng: obj.lng*DEGREE}, 1)[0][1]
// put some stuff on it
let printArray = [
`Longitude: ${obj.lng}`,
`Latitude: ${obj.lat}`,
`US: ${usnear} miles`,
`UK: ${uknear} miles`,
`ARG: ${argnear} miles`,
`EOF`
];
let fontSize = 20;
ctx.font = '20px Arial';
for (let i=0; i < printArray.length; i++) {
ctx.fillText(printArray[i], 5, fontSize*(i+1));
}
return canvas.toDataURL('image/png')
}
function distance(a, b) {
val = EARTH_RADIUS*Math.acos(Math.cos(a.lat)*Math.cos(b.lat)*Math.cos(a.lng-b.lng) + Math.sin(a.lat)*Math.sin(b.lat))
return val
}
const map = L.map('map').setView([37.8, -96], 4);
// call redraw_map if someone moves or zooms
map.on('zoomend', redraw_map);
map.on('moveend', redraw_map);
// create our trees
var ustree = points2Tree(usa)
var uktree = points2Tree(uk)
var argtree = points2Tree(arg)
function initialize(){
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(map);
redraw_map()
}
////// CUT PASTE STARTS HERE
// this is ugly global variable
items = []
function redraw_map() {
let bounds = map.getBounds();
let z = map.getZoom();
// remove items (layers) I added last time
// TODO: only remove layers after adding new ones using 2 var flip
for (i in items) {items[i].remove()}
// grid sizes
if (z <= 1) {return;}
// this is ugly
if (z==2) {grid = 60; gtext = "60 deg";}
if (z==3) {grid = 30; gtext = "30 deg";}
if (z==4) {grid = 15; gtext = "15 deg";}
if (z==5) {grid = 8; gtext = "8 deg";}
if (z==6) {grid = 4; gtext = "4 deg";}
if (z==7) {grid = 2; gtext = "2 deg";}
if (z==8) {grid = 1; gtext = "1 deg";}
if (z==9) {grid = 30/60; gtext = "30'";}
if (z==10) {grid = 15/60; gtext = "15'";}
if (z==11) {grid = 8/60; gtext = "8'";}
if (z==12) {grid = 4/60; gtext = "4'";}
if (z==13) {grid = 2/60; gtext = "2'";}
if (z==14) {grid = 1/60; gtext = "1'";}
if (z==15) {grid = 30/3600; gtext = '30"';}
if (z==16) {grid = 15/3600; gtext = '15"';}
if (z==17) {grid = 8/3600; gtext = '8"';}
if (z==18) {grid = 4/3600; gtext = '4"';}
if (z==19) {grid = 2/3600; gtext = '2"';}
if (z==20) {grid = 1/3600; gtext = '1"';}
if (z==21) {grid = 1/3600/3; gtext = '1/3"';}
if (z==22) {grid = 1/3600/9; gtext = '1/9"';}
// get multiples of grid size
let south = Math.floor(bounds.getSouth()/grid)*grid;
let north = Math.ceil(bounds.getNorth()/grid)*grid;
let west = Math.floor(bounds.getWest()/grid)*grid;
let east = Math.ceil(bounds.getEast()/grid)*grid;
for (i=south; i<=north; i+=grid) {
items.push(L.polyline([[i, -180], [i, 180]], {weight: 1}).addTo(map));
}
for (i=west; i<=east; i+=grid) {
items.push(L.polyline([[-90, i], [90, i]], {weight: 1}).addTo(map));
}
// nested stuff
for (i=south; i<=north; i+=grid) {
for (j=west; j<=east; j+=grid) {
// items.push(L.rectangle([[i, j], [i+grid, j+grid]]).addTo(map))
// L.divIcon({html: 'hello world', position: [i, j]}).addTo(map);
// L.div('Hello, world!', [i,j]).addTo(map);
// L.DivOverlay({[fails]
// L.imageOverlay("red.png", L.latLngBounds([[-180, -90], [180, 90]])).addTo(map);
// L.imageOverlay("red.png", L.latLngBounds([[i, j], [i+grid, j+grid]])).addTo(map);
items.push(L.imageOverlay(latLngGrid2PNG({lat: i+grid/2, lng: j+grid/2, grid: grid}), L.latLngBounds([[i, j], [i+grid, j+grid]])).addTo(map));
items.push(L.imageOverlay("red.png", L.latLngBounds([[i+grid*0.49, j+grid*0.49],
[i+grid*0.51, j+grid*0.51]])).addTo(map));
// items.push(L.marker([i+grid/2,j+grid/2], {icon: icon, title: `z=${z},g=${gtext},x=${j+grid/2},y=${i+grid/2}\ni=${i},j=${j}\nn=${north},s=${south},e=${east},w=${west}`}).addTo(map));
}
}
return;
}
</script>
</script>
</body>