Skip to content

Instantly share code, notes, and snippets.

@Scarysize
Last active February 23, 2018 23:07
Show Gist options
  • Select an option

  • Save Scarysize/abcf22c514d64125111a5b130cf66fb3 to your computer and use it in GitHub Desktop.

Select an option

Save Scarysize/abcf22c514d64125111a5b130cf66fb3 to your computer and use it in GitHub Desktop.
'use strict';
const Point = require('point-geometry');
const LngLat = require('../geo/lng_lat');
const LngLatBounds = require('../geo/lng_lat_bounds');
const intersect = require('turf-intersect');
const bboxPolygon = require('turf-bbox-polygon');
module.exports = createTileIntersect();
/**
* Creates a function to check intersections between a tile and a bounding box.
* Caches for the tiles and bounding box are maintained to avoid calculations.
* @return {function} -
*/
function createTileIntersect() {
const tileCache = {};
const bboxCache = {};
/**
+ * Check if the tile intersects with a boundingBox.
+ * @param {LngLatBounds} boundingBox The bounding box
+ * @param {object} tile The tile
+ * @return {boolean} True if they intersect
+ */
function tileIntersects(boundingBox, tile) {
if (!boundingBox) {
return true;
}
var tileBbox = tileCache[tile.uid];
if (!tileBbox) {
const tileBounds = tileToBounds(tile);
tileBbox = boundsToBbox(tileBounds);
tileCache[tile.uid] = tileBbox;
}
var bboxString = boundingBox.toString();
var bbox = bboxCache[bboxString];
if (!bbox) {
bbox = boundsToBbox(boundingBox);
bboxCache[bboxString] = bbox;
}
return Boolean(intersect(tileBbox, bbox));
}
return tileIntersects;
}
/**
* Unprojects a x/y Point to lng/lat coords.
* @param {Point} point The point
* @param {number} zoom Zoom level
* @return {LngLat} -
*/
function unproject(point, zoom) {
return new LngLat(
xLng(point.x, zoom),
yLat(point.y, zoom)
);
}
/**
* Transform x coordinate to longitude value.
* @param {number} x -
* @param {number} zoom -
* @return {number} -
*/
function xLng(x, zoom) {
return x * 360 / Math.pow(2, zoom) - 180;
}
/**
* Transform y coordinate to latitude value.
* @param {number} y -
* @param {number} zoom -
* @return {number} -
*/
function yLat(y, zoom) {
const y2 = 180 - y * 360 / Math.pow(2, zoom);
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
}
/**
* Transform LngLatBounds to a turf bboxPolygon.
* @param {LngLatBounds} bounds -
* @return {bboxPolygon} -
*/
function boundsToBbox(bounds) {
return bboxPolygon([
bounds.getSouthWest().lng, // minX
bounds.getNorthEast().lat, // minY
bounds.getNorthEast().lng, // maxX
bounds.getSouthWest().lat // maxY
]);
}
/**
* Calculate a tiles LngLatBounds.
* @param {object} tile The tile
* @return {LngLatBounds} -
*/
function tileToBounds(tile) {
const nwPoint = new Point(tile.coord.x, tile.coord.y);
const sePoint = nwPoint.add(new Point(1, 1));
const nw = unproject(nwPoint, tile.coord.z);
const se = unproject(sePoint, tile.coord.z);
// Transform to "common" southwest-northeast bounds
const neLngLat = new LngLat(se.lng, nw.lat);
const swLngLat = new LngLat(nw.lng, se.lat);
return new LngLatBounds(swLngLat, neLngLat);
}
'use strict';
var util = require('../util/util');
var ajax = require('../util/ajax');
var Evented = require('../util/evented');
var loadTileJSON = require('./load_tilejson');
var normalizeURL = require('../util/mapbox').normalizeTileURL;
// or where-ever the module comes from
var testTileIntersection = require('../geo/testTileIntersection');
module.exports = RasterTileSource;
function RasterTileSource(id, options, dispatcher, eventedParent) {
this.id = id;
this.dispatcher = dispatcher;
this.boundingBox = options.boundingBox; // @type {LngLatBounds}
// ...
}
RasterTileSource.prototype = util.inherit(Evented, {
minzoom: 0,
maxzoom: 22,
roundZoom: true,
scheme: 'xyz',
tileSize: 512,
_loaded: false,
// ...
loadTile: function(tile, callback) {
var url = normalizeURL(tile.coord.url(this.tiles, null, this.scheme), this.url, this.tileSize);
// check if it intersects
const intersets = testTileIntersection(this.boundingBox, tile);
if (!intersects) {
return callback(null);
}
tile.request = ajax.getImage(url, done.bind(this));
// ...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment