Rewritten tile request calculation
This commit is contained in:
parent
0010626c91
commit
f7f8e1ca94
@ -12,34 +12,30 @@ window.requestData = function() {
|
|||||||
requests.abort();
|
requests.abort();
|
||||||
cleanUp();
|
cleanUp();
|
||||||
|
|
||||||
var magic = convertCenterLat(map.getCenter().lat);
|
|
||||||
var R = calculateR(magic);
|
|
||||||
|
|
||||||
var bounds = map.getBounds();
|
var bounds = map.getBounds();
|
||||||
// convert to point values
|
|
||||||
topRight = convertLatLngToPoint(bounds.getNorthEast(), magic, R);
|
var x1 = lngToTile(bounds.getNorthWest().lng, map.getZoom());
|
||||||
bottomLeft = convertLatLngToPoint(bounds.getSouthWest() , magic, R);
|
var x2 = lngToTile(bounds.getNorthEast().lng, map.getZoom());
|
||||||
// how many quadrants intersect the current view?
|
var y1 = latToTile(bounds.getNorthWest().lat, map.getZoom());
|
||||||
quadsX = Math.abs(bottomLeft.x - topRight.x);
|
var y2 = latToTile(bounds.getSouthWest().lat, map.getZoom());
|
||||||
quadsY = Math.abs(bottomLeft.y - topRight.y);
|
|
||||||
|
|
||||||
// will group requests by second-last quad-key quadrant
|
// will group requests by second-last quad-key quadrant
|
||||||
tiles = {};
|
tiles = {};
|
||||||
|
|
||||||
// walk in x-direction, starts right goes left
|
// walk in x-direction, starts right goes left
|
||||||
for(var i = 0; i <= quadsX; i++) {
|
for (var x = x1; x <= x2; x++) {
|
||||||
var x = Math.abs(topRight.x - i);
|
for (var y = y1; y <= y2; y++) {
|
||||||
var qk = pointToQuadKey(x, topRight.y);
|
var tile_id = pointToTileId(map.getZoom(), x, y);
|
||||||
var bnds = convertPointToLatLng(x, topRight.y, magic, R);
|
var bucket = Math.floor(x / 2) + "" + Math.floor(y / 2);
|
||||||
if(!tiles[qk.slice(0, -1)]) tiles[qk.slice(0, -1)] = [];
|
if (!tiles[bucket])
|
||||||
tiles[qk.slice(0, -1)].push(generateBoundsParams(qk, bnds));
|
tiles[bucket] = [];
|
||||||
|
tiles[bucket].push(generateBoundsParams(
|
||||||
// walk in y-direction, starts top, goes down
|
tile_id,
|
||||||
for(var j = 1; j <= quadsY; j++) {
|
tileToLat(y + 1, map.getZoom()),
|
||||||
var qk = pointToQuadKey(x, topRight.y + j);
|
tileToLng(x, map.getZoom()),
|
||||||
var bnds = convertPointToLatLng(x, topRight.y + j, magic, R);
|
tileToLat(y, map.getZoom()),
|
||||||
if(!tiles[qk.slice(0, -1)]) tiles[qk.slice(0, -1)] = [];
|
tileToLng(x + 1, map.getZoom())
|
||||||
tiles[qk.slice(0, -1)].push(generateBoundsParams(qk, bnds));
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,65 +1,45 @@
|
|||||||
|
|
||||||
// MAP DATA REQUEST CALCULATORS //////////////////////////////////////
|
// MAP DATA REQUEST CALCULATORS //////////////////////////////////////
|
||||||
// Ingress Intel splits up requests for map data (portals, links,
|
// Ingress Intel splits up requests for map data (portals, links,
|
||||||
// fields) into tiles. To get data for the current viewport (i.e. what
|
// fields) into tiles. To get data for the current viewport (i.e. what
|
||||||
// is currently visible) it first calculates which tiles intersect.
|
// is currently visible) it first calculates which tiles intersect.
|
||||||
// For all those tiles, it then calculates the lat/lng bounds of that
|
// For all those tiles, it then calculates the lat/lng bounds of that
|
||||||
// tile and a quadkey. Both the bounds and the quadkey are “somewhat”
|
// tile and a quadkey. Both the bounds and the quadkey are “somewhat”
|
||||||
// required to get complete data. No idea how the projection between
|
// required to get complete data.
|
||||||
// lat/lng and tiles works.
|
//
|
||||||
// What follows now are functions that allow conversion between tiles
|
// Convertion functions courtesy of
|
||||||
// and lat/lng as well as calculating the quad key. The variable names
|
// http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
|
||||||
// may be misleading.
|
|
||||||
// The minified source for this code was in gen_dashboard.js after the
|
|
||||||
// “// input 89” line (alternatively: the class was called “Xe”).
|
|
||||||
|
|
||||||
window.convertCenterLat = function(centerLat) {
|
window.lngToTile = function(lng, zoom) {
|
||||||
return Math.round(256 * 0.9999 * Math.abs(1 / Math.cos(centerLat * DEG2RAD)));
|
return Math.floor((lng + 180) / 360 * Math.pow(2, zoom));
|
||||||
}
|
}
|
||||||
|
|
||||||
window.calculateR = function(convCenterLat) {
|
window.latToTile = function(lat, zoom) {
|
||||||
return 1 << window.map.getZoom() - (convCenterLat / 256 - 1);
|
return Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) +
|
||||||
|
1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, zoom));
|
||||||
}
|
}
|
||||||
|
|
||||||
window.convertLatLngToPoint = function(latlng, magic, R) {
|
window.tileToLng = function(x, zoom) {
|
||||||
var x = (magic + latlng.lng * magic / 180)*R;
|
return x / Math.pow(2, zoom) * 360 - 180;
|
||||||
var l = Math.sin(latlng.lat * DEG2RAD);
|
|
||||||
var y = (magic + Math.log((1+l)/(1-l)) * -(magic / (2*Math.PI)))*R;
|
|
||||||
return {x: Math.floor(x/magic), y: Math.floor(y/magic)};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.convertPointToLatLng = function(x, y, magic, R) {
|
window.tileToLat = function(y, zoom) {
|
||||||
var e = {};
|
var n = Math.PI - 2 * Math.PI * y / Math.pow(2, zoom);
|
||||||
e.sw = {
|
return 180 / Math.PI * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
|
||||||
// orig function put together from all over the place
|
|
||||||
// lat: (2 * Math.atan(Math.exp((((y + 1) * magic / R) - (magic/ 2)) / (-1*(magic / (2 * Math.PI))))) - Math.PI / 2) / (Math.PI / 180),
|
|
||||||
// shortened version by your favorite algebra program.
|
|
||||||
lat: (360*Math.atan(Math.exp(Math.PI - Math.PI*(y+1)/R)))/Math.PI - 90,
|
|
||||||
lng: 180*x/R-180
|
|
||||||
};
|
|
||||||
e.ne = {
|
|
||||||
//lat: (2 * Math.atan(Math.exp(((y * magic / R) - (magic/ 2)) / (-1*(magic / (2 * Math.PI))))) - Math.PI / 2) / (Math.PI / 180),
|
|
||||||
lat: (360*Math.atan(Math.exp(Math.PI - Math.PI*y/R)))/Math.PI - 90,
|
|
||||||
lng: 180*(x+1)/R-180
|
|
||||||
};
|
|
||||||
return e;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// calculates the quad key for a given point. The point is not(!) in
|
window.pointToTileId = function(zoom, x, y) {
|
||||||
// lat/lng format.
|
return zoom + "_" + x + "_" + y;
|
||||||
window.pointToQuadKey = function(x, y) {
|
|
||||||
return window.map.getZoom() + "_" + x + "_" + y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// given quadkey and bounds, returns the format as required by the
|
// given tile id and bounds, returns the format as required by the
|
||||||
// Ingress API to request map data.
|
// Ingress API to request map data.
|
||||||
window.generateBoundsParams = function(quadkey, bounds) {
|
window.generateBoundsParams = function(tile_id, minLat, minLng, maxLat, maxLng) {
|
||||||
return {
|
return {
|
||||||
id: quadkey,
|
id: tile_id,
|
||||||
qk: quadkey,
|
qk: tile_id,
|
||||||
minLatE6: Math.round(bounds.sw.lat * 1E6),
|
minLatE6: Math.round(minLat * 1E6),
|
||||||
minLngE6: Math.round(bounds.sw.lng * 1E6),
|
minLngE6: Math.round(minLng * 1E6),
|
||||||
maxLatE6: Math.round(bounds.ne.lat * 1E6),
|
maxLatE6: Math.round(maxLat * 1E6),
|
||||||
maxLngE6: Math.round(bounds.ne.lng * 1E6)
|
maxLngE6: Math.round(maxLng * 1E6)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user