mapillary-loader.js (2430B)
1 import L from 'leaflet'; 2 import {TiledDataLoader} from '~/lib/tiled-data-loader'; 3 import {decodeMvt} from './mvt'; 4 5 class MapillaryLoader extends TiledDataLoader { 6 url = 'https://d25uarhxywzl1j.cloudfront.net/v0.1/{z}/{x}/{y}.mvt'; 7 maxZoom = 14; 8 9 getTileUrl(coords) { 10 const data = { 11 x: coords.x, 12 z: coords.z, 13 y: coords.y 14 }; 15 return L.Util.template(this.url, data); 16 } 17 18 layerTileToDataTileCoords(layerTileCoords) { 19 let z = layerTileCoords.z - 2; 20 let z2 = null; 21 if (z > 6 && z <= 10) { 22 z2 = 6; 23 } else if (z >= 11 && z < 14) { 24 z2 = z - 4; 25 } else if (z < 0) { 26 z2 = 0; 27 } else if (z > this.maxZoom) { 28 z2 = this.maxZoom; 29 } else { 30 return {z, x: layerTileCoords.x, y: layerTileCoords.y}; 31 } 32 33 let multiplier = 1 << (z - z2); 34 return { 35 x: Math.floor(layerTileCoords.x / multiplier), 36 y: Math.floor(layerTileCoords.y / multiplier), 37 z: z2 38 }; 39 } 40 41 makeRequestData(dataTileCoords) { 42 return { 43 url: this.getTileUrl(dataTileCoords), 44 options: { 45 responseType: 'arraybuffer', 46 timeout: 10000, 47 isResponseSuccess: (xhr) => xhr.status === 200 || xhr.status === 403 48 } 49 }; 50 } 51 52 calcAdjustment(layerTileCoords, dataTileCoords) { 53 let adjustment = super.calcAdjustment( 54 {x: layerTileCoords.x, y: layerTileCoords.y, z: layerTileCoords.z - 2}, 55 dataTileCoords 56 ); 57 if (adjustment) { 58 adjustment.offsetX *= 1024; 59 adjustment.offsetY *= 1024; 60 } 61 return adjustment; 62 } 63 64 async processResponse(xhr, originalDataTileCoords) { 65 return this._processResponse(xhr, originalDataTileCoords); 66 } 67 68 async _processResponse(xhr, originalDataTileCoords) { 69 let tileData; 70 if (xhr.status === 200 && xhr.response) { 71 const layers = decodeMvt(xhr.response, 1024); 72 tileData = {}; 73 for (let layer of layers) { 74 tileData[layer.name] = layer.features; 75 } 76 } else { 77 tileData = null; 78 } 79 80 return { 81 tileData, 82 coords: originalDataTileCoords 83 }; 84 } 85 } 86 87 export {MapillaryLoader};