-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice.html
More file actions
341 lines (308 loc) · 10 KB
/
slice.html
File metadata and controls
341 lines (308 loc) · 10 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE html>
<!--
Copyright 2016 MarkLogic Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html>
<head>
<title>GeoHash Slice</title>
<style type="text/css">
html, body, div { margin: 0; padding: 0; }
html, body { height: 100%; }
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBWj1dLecWvRSiLz99rdI_P7AaDxoz0odw&libraries=drawing,visualization&v=3"></script>
<script type="text/javascript" src="maplabel.js"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="http://geographiclib.sourceforge.net/scripts/geographiclib.min.js"></script>
<script type="text/javascript">
var theMap;
var geodesics = [];
var labels = [];
var showGeodesics = false;
var showLabels = true;
var geod = GeographicLib.Geodesic.WGS84;
function drawSlices(slices) {
theMap.data.setStyle({
fillColor: 'green',
fillOpacity: 0.2,
strokeColor: 'black',
strokeWeight: 2
});
for (var i=0; i<slices.length; ++i) {
var features = theMap.data.addGeoJson(slices[i]);
}
}
function drawHashes(hashes) {
document.getElementById('wkt_textbox').value = hashes.polygonWkt;
if (hashes.slices) {
drawSlices(hashes.slices);
}
}
function drawHashesWkt(hashes) {
if (hashes.feature) {
//var features = theMap.data.addGeoJson(hashes.feature);
var latlngs = [];
if (hashes.feature.geometry.type == "LineString") {
features[0].getGeometry().getArray().forEach(function(a) {
latlngs.push({ lat: a.lat(), lng: a.lng() });
});
drawGeodesicsLS(latlngs);
} else if (hashes.feature.geometry.type != "Point") {
var geom = features[0].getGeometry();
for (var i = 0; i < geom.getLength(); ++i) {
features[0].getGeometry().getAt(i).getArray().forEach(function(a) {
latlngs.push({ lat: a.lat(), lng: a.lng() });
});
drawGeodesics(latlngs);
latlngs = [];
}
}
} else if (hashes.polygonWkt.charAt(0) == '@') {
//draw the circle
var circleStr = hashes.polygonWkt.substring(1);
var circleData = circleStr.split(/[ |,]/);
var radius = Number(circleData[0]) / 0.000621371;
var center = {
lat: Number(circleData[1]),
lng: Number(circleData[2])
};
var circle = new google.maps.Circle({
map: theMap,
center: center,
radius: radius
});
} else if (hashes.polygonWkt.charAt(0) == '[') {
//draw the box
var rectStr = hashes.polygonWkt.replace(/[\[|\]]/g,"");
var rectData = rectStr.split(",").map(Number);
var rectangle = new google.maps.Rectangle({
map: theMap,
bounds: {
south: rectData[0],
west: rectData[1],
north: rectData[2],
east: rectData[3]
}
});
}
if (hashes.slices) {
drawSlices(hashes.slices);
}
theMap.panTo(new google.maps.LatLng(hashes.center.lat, hashes.center.lng));
}
function fetchHashesPoly(poly) {
var latlngs = [];
var str = "";
poly.getPath().forEach(function(xy, i) {
latlngs[i] = {"lat": xy.lat(), "lng": xy.lng() };
str = str + xy.lat() + "," + xy.lng() + " ";
});
drawGeodesics(latlngs);
fetchHashes(str);
}
function fetchHashesLine(line) {
var latlngs = [];
var str = "LINESTRING(";
line.getPath().forEach(function(xy, i) {
latlngs[i] = {"lat": xy.lat(), "lng": xy.lng() };
str = str + xy.lng() + " " + xy.lat() + ",";
});
str.replace(/,$/, "") + ")";
drawGeodesicsLS(latlngs);
fetchHashes(str);
}
function fetchHashesCirc(circ) {
var radius = circ.getRadius()*0.000621371;
var center = circ.getCenter();
var lat = center.lat();
var lng = center.lng();
var str = "@" + radius + " " + lat + "," + lng;
fetchHashes(str);
}
function fetchHashesRect(rect) {
var bounds = rect.getBounds();
var sw = bounds.getSouthWest();
var s = sw.lat();
var w = sw.lng();
var ne = bounds.getNorthEast();
var n = ne.lat();
var e = ne.lng();
var str = "[" + s + "," + w + "," + n + "," + e + "]";
fetchHashes(str);
}
function fetchHashesMark(mark) {
var pt = mark.getPosition();
var str = pt.lat() + "," + pt.lng();
fetchHashes(str);
}
function fetchHashes(str) {
var precision = $("#precision").val();
var payload = { precision: precision, region: str };
$.ajax({
type: "POST",
url: "slice.xqy",
data: JSON.stringify(payload),
contentType: "application/json",
dataType: "json",
success: drawHashes
});
}
function hashWKT(wkt) {
var precision = $("#precision").val();
var payload = { precision: precision, region: wkt }
$.ajax({
type: "POST",
url: "slice.xqy",
data: JSON.stringify(payload),
contentType: "application/json",
dataType: "json",
success: drawHashesWkt
});
}
function initialize() {
var mapOptions = {
center: { lat: 37.507377, lng: -122.247081 },
zoom: 13
};
theMap = new google.maps.Map(document.getElementById('map-canvas'),mapOptions);
var drawingManager = new google.maps.drawing.DrawingManager();
drawingManager.setMap(theMap);
google.maps.event.addListener(drawingManager, 'polygoncomplete', fetchHashesPoly);
google.maps.event.addListener(drawingManager, 'circlecomplete', fetchHashesCirc);
google.maps.event.addListener(drawingManager, 'polylinecomplete', fetchHashesLine);
google.maps.event.addListener(drawingManager, 'rectanglecomplete', fetchHashesRect);
google.maps.event.addListener(drawingManager, 'markercomplete', fetchHashesMark);
}
google.maps.event.addDomListener(window, 'load', initialize);
function doResize() {
$("#map-canvas").css("height", $(window).height() - $("#text").height());
}
$(doResize);
$(window).resize(doResize);
function drawGeodesics(g) {
for (var i = 0; i < g.length-1; ++i) {
drawGeodesic(g[i].lat,g[i].lng,g[i+1].lat,g[i+1].lng);
}
drawGeodesic(g[g.length-1].lat,g[g.length-1].lng,g[0].lat,g[0].lng);
}
function drawGeodesicsLS(g) {
for (var i = 0; i < g.length-1; ++i) {
drawGeodesic(g[i].lat,g[i].lng,g[i+1].lat,g[i+1].lng);
}
}
function drawGeodesic(lat1, lon1, lat2, lon2) {
var v = geod.Inverse(lat1, lon1, lat2, lon2);
var points = geod.DirectPath(v.lat1, v.lon1, v.azi1, v.s12,
100000, 100);
var geodesicOptions = {
strokeColor: '#0000FF',
strokeOpacity: 1,
strokeWeight: 3,
geodesic: true
}
var geodesic = new google.maps.Polyline(geodesicOptions);
geodesics.push(geodesic);
if (showGeodesics) {
geodesic.setMap(theMap);
} else {
geodesic.setMap(null);
}
var path = geodesic.getPath();
for (var k = 0; k < points.length; ++k) {
path.push(new google.maps.LatLng(points[k].lat, points[k].lon));
}
};
function toggleGeodesics() {
if (showGeodesics) {
showGeodesics = false;
document.getElementById('geod_button').firstChild.data = "Show Geodesic Arcs";
for (var i = 0; i < geodesics.length; ++i) {
geodesics[i].setMap(null);
}
} else {
showGeodesics = true;
document.getElementById('geod_button').firstChild.data = "Hide Geodesic Arcs";
for (var i = 0; i < geodesics.length; ++i) {
geodesics[i].setMap(theMap);
}
}
};
function toggleLabels() {
if (showLabels) {
showLabels = false;
document.getElementById('label_button').firstChild.data = "Show Labels";
for (var i = 0; i < labels.length; ++i) {
labels[i].setMap(null);
}
} else {
showLabels = true;
document.getElementById('label_button').firstChild.data = "Hide Labels";
for (var i = 0; i < labels.length; ++i) {
labels[i].setMap(theMap);
}
}
}
(function(g) {
"use strict";
/*
* split a geodesic line into k equal pieces which are no longer than about
* ds12 (but k cannot exceed maxk, default 20), and returns an array of
* length k + 1 of objects with fields lat, lon, azi.
*/
g.Geodesic.prototype.DirectPath =
function(lat1, lon1, azi1, s12, ds12, maxk) {
var t = this.Direct(lat1, lon1, azi1, s12),
k, points, line, da12, vals, i;
if (!maxk) maxk = 20;
if (!(ds12 > 0))
throw new Error("ds12 must be a positive number");
k = Math.max(1, Math.min(maxk, Math.ceil(Math.abs(t.s12)/ds12)));
points = new Array(k + 1);
points[0] = {lat: t.lat1, lon: t.lon1, azi: t.azi1};
points[k] = {lat: t.lat2, lon: t.lon2, azi: t.azi2};
if (k > 1) {
line = this.Line(t.lat1, t.lon1, t.azi1, g.STANDARD);
da12 = t.a12/k;
for (i = 1; i < k; ++i) {
vals = line.ArcPosition(i * da12);
points[i] = {lat: vals.lat2, lon: vals.lon2, azi: vals.azi2};
}
}
return points;
};
})(GeographicLib.Geodesic);
</script>
</head>
<body>
<div id="text">
<textarea rows="4" cols="80" id="wkt_textbox" placeholder="Input WKT or string serialized cts:region..."></textarea>
<button type="button" onclick="hashWKT(document.getElementById('wkt_textbox').value);">GeoHash</button>
<button type="button" id="label_button" onclick="toggleLabels();">Hide Labels</button>
<button type="button" id="geod_button" onclick="toggleGeodesics();">Show Geodesic Arcs</button>
Precision:
<select id="precision">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6" selected>6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
</div>
<div id="map-canvas"></div>
</body>
</html>