Spaces:
Sleeping
Sleeping
File size: 7,012 Bytes
fcf3b03 | 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 | // @flow
import {getTileBBox} from '@mapbox/whoots-js';
import EXTENT from '../data/extent';
import Point from '@mapbox/point-geometry';
import MercatorCoordinate from '../geo/mercator_coordinate';
import assert from 'assert';
import {register} from '../util/web_worker_transfer';
export class CanonicalTileID {
z: number;
x: number;
y: number;
key: string;
constructor(z: number, x: number, y: number) {
assert(z >= 0 && z <= 25);
assert(x >= 0 && x < Math.pow(2, z));
assert(y >= 0 && y < Math.pow(2, z));
this.z = z;
this.x = x;
this.y = y;
this.key = calculateKey(0, z, z, x, y);
}
equals(id: CanonicalTileID) {
return this.z === id.z && this.x === id.x && this.y === id.y;
}
// given a list of urls, choose a url template and return a tile URL
url(urls: Array<string>, scheme: ?string) {
const bbox = getTileBBox(this.x, this.y, this.z);
const quadkey = getQuadkey(this.z, this.x, this.y);
return urls[(this.x + this.y) % urls.length]
.replace('{prefix}', (this.x % 16).toString(16) + (this.y % 16).toString(16))
.replace('{z}', String(this.z))
.replace('{x}', String(this.x))
.replace('{y}', String(scheme === 'tms' ? (Math.pow(2, this.z) - this.y - 1) : this.y))
.replace('{quadkey}', quadkey)
.replace('{bbox-epsg-3857}', bbox);
}
getTilePoint(coord: MercatorCoordinate) {
const tilesAtZoom = Math.pow(2, this.z);
return new Point(
(coord.x * tilesAtZoom - this.x) * EXTENT,
(coord.y * tilesAtZoom - this.y) * EXTENT);
}
toString() {
return `${this.z}/${this.x}/${this.y}`;
}
}
export class UnwrappedTileID {
wrap: number;
canonical: CanonicalTileID;
key: string;
constructor(wrap: number, canonical: CanonicalTileID) {
this.wrap = wrap;
this.canonical = canonical;
this.key = calculateKey(wrap, canonical.z, canonical.z, canonical.x, canonical.y);
}
}
export class OverscaledTileID {
overscaledZ: number;
wrap: number;
canonical: CanonicalTileID;
key: string;
posMatrix: Float32Array;
constructor(overscaledZ: number, wrap: number, z: number, x: number, y: number) {
assert(overscaledZ >= z);
this.overscaledZ = overscaledZ;
this.wrap = wrap;
this.canonical = new CanonicalTileID(z, +x, +y);
this.key = calculateKey(wrap, overscaledZ, z, x, y);
}
equals(id: OverscaledTileID) {
return this.overscaledZ === id.overscaledZ && this.wrap === id.wrap && this.canonical.equals(id.canonical);
}
scaledTo(targetZ: number) {
assert(targetZ <= this.overscaledZ);
const zDifference = this.canonical.z - targetZ;
if (targetZ > this.canonical.z) {
return new OverscaledTileID(targetZ, this.wrap, this.canonical.z, this.canonical.x, this.canonical.y);
} else {
return new OverscaledTileID(targetZ, this.wrap, targetZ, this.canonical.x >> zDifference, this.canonical.y >> zDifference);
}
}
/*
* calculateScaledKey is an optimization:
* when withWrap == true, implements the same as this.scaledTo(z).key,
* when withWrap == false, implements the same as this.scaledTo(z).wrapped().key.
*/
calculateScaledKey(targetZ: number, withWrap: boolean): string {
assert(targetZ <= this.overscaledZ);
const zDifference = this.canonical.z - targetZ;
if (targetZ > this.canonical.z) {
return calculateKey(this.wrap * +withWrap, targetZ, this.canonical.z, this.canonical.x, this.canonical.y);
} else {
return calculateKey(this.wrap * +withWrap, targetZ, targetZ, this.canonical.x >> zDifference, this.canonical.y >> zDifference);
}
}
isChildOf(parent: OverscaledTileID) {
if (parent.wrap !== this.wrap) {
// We can't be a child if we're in a different world copy
return false;
}
const zDifference = this.canonical.z - parent.canonical.z;
// We're first testing for z == 0, to avoid a 32 bit shift, which is undefined.
return parent.overscaledZ === 0 || (
parent.overscaledZ < this.overscaledZ &&
parent.canonical.x === (this.canonical.x >> zDifference) &&
parent.canonical.y === (this.canonical.y >> zDifference));
}
children(sourceMaxZoom: number) {
if (this.overscaledZ >= sourceMaxZoom) {
// return a single tile coord representing a an overscaled tile
return [new OverscaledTileID(this.overscaledZ + 1, this.wrap, this.canonical.z, this.canonical.x, this.canonical.y)];
}
const z = this.canonical.z + 1;
const x = this.canonical.x * 2;
const y = this.canonical.y * 2;
return [
new OverscaledTileID(z, this.wrap, z, x, y),
new OverscaledTileID(z, this.wrap, z, x + 1, y),
new OverscaledTileID(z, this.wrap, z, x, y + 1),
new OverscaledTileID(z, this.wrap, z, x + 1, y + 1)
];
}
isLessThan(rhs: OverscaledTileID) {
if (this.wrap < rhs.wrap) return true;
if (this.wrap > rhs.wrap) return false;
if (this.overscaledZ < rhs.overscaledZ) return true;
if (this.overscaledZ > rhs.overscaledZ) return false;
if (this.canonical.x < rhs.canonical.x) return true;
if (this.canonical.x > rhs.canonical.x) return false;
if (this.canonical.y < rhs.canonical.y) return true;
return false;
}
wrapped() {
return new OverscaledTileID(this.overscaledZ, 0, this.canonical.z, this.canonical.x, this.canonical.y);
}
unwrapTo(wrap: number) {
return new OverscaledTileID(this.overscaledZ, wrap, this.canonical.z, this.canonical.x, this.canonical.y);
}
overscaleFactor() {
return Math.pow(2, this.overscaledZ - this.canonical.z);
}
toUnwrapped() {
return new UnwrappedTileID(this.wrap, this.canonical);
}
toString() {
return `${this.overscaledZ}/${this.canonical.x}/${this.canonical.y}`;
}
getTilePoint(coord: MercatorCoordinate) {
return this.canonical.getTilePoint(new MercatorCoordinate(coord.x - this.wrap, coord.y));
}
}
function calculateKey(wrap: number, overscaledZ: number, z: number, x: number, y: number): string {
wrap *= 2;
if (wrap < 0) wrap = wrap * -1 - 1;
const dim = 1 << z;
return (dim * dim * wrap + dim * y + x).toString(36) + z.toString(36) + overscaledZ.toString(36);
}
function getQuadkey(z, x, y) {
let quadkey = '', mask;
for (let i = z; i > 0; i--) {
mask = 1 << (i - 1);
quadkey += ((x & mask ? 1 : 0) + (y & mask ? 2 : 0));
}
return quadkey;
}
register('CanonicalTileID', CanonicalTileID);
register('OverscaledTileID', OverscaledTileID, {omit: ['posMatrix']});
|