nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

wikimapia-loader.js (2722B)


      1 import {TiledDataLoader} from '~/lib/tiled-data-loader';
      2 import * as wmUtils from './wm-utils';
      3 
      4 class WikimapiaLoader extends TiledDataLoader {
      5     maxZoom = 15;
      6     minZoom = 1;
      7     tileSize = 1024;
      8 
      9     constructor(tilesBaseUrl, projectObj) {
     10         super();
     11         this._projectObj = projectObj;
     12         this.tilesBaseUrl = tilesBaseUrl;
     13     }
     14 
     15     getFromCache(dataTileCoords) {
     16         dataTileCoords = {...dataTileCoords};
     17         let exactMatch = true;
     18         while (dataTileCoords.z >= 0) {
     19             let key = this.makeTileKey(dataTileCoords);
     20             let res = this._cache.get(key);
     21             if (res.found) {
     22                 if (exactMatch || !res.value.hasChildren) {
     23                     res['coords'] = dataTileCoords;
     24                     return res;
     25                 }
     26                 break;
     27             }
     28             dataTileCoords.z -= 1;
     29             dataTileCoords.x = Math.floor(dataTileCoords.x / 2);
     30             dataTileCoords.y = Math.floor(dataTileCoords.y / 2);
     31             exactMatch = false;
     32         }
     33         return {found: false};
     34     }
     35 
     36     layerTileToDataTileCoords(layerTileCoords) {
     37         let z = layerTileCoords.z - 2;
     38         if (z > this.maxZoom) {
     39             let z2 = this.maxZoom,
     40                 multiplier = 1 << (z - z2);
     41             return {
     42                 x: Math.floor(layerTileCoords.x / multiplier),
     43                 y: Math.floor(layerTileCoords.y / multiplier),
     44                 z: z2
     45             };
     46         } else if (z < this.minZoom) {
     47             let z2 = this.minZoom,
     48                 multiplier = 1 / (1 << (z2 - z));
     49             return {
     50                 x: Math.floor(layerTileCoords.x / multiplier),
     51                 y: Math.floor(layerTileCoords.y / multiplier),
     52                 z: z2
     53             };
     54         }
     55         return {z, x: layerTileCoords.x, y: layerTileCoords.y};
     56     }
     57 
     58     makeRequestData(dataTileCoords) {
     59         let url = this.tilesBaseUrl + wmUtils.makeTilePath(dataTileCoords);
     60         return {
     61             url,
     62             options: {timeout: 20000}
     63         };
     64     }
     65 
     66     processResponse(xhr, requestedCoords) {
     67         return wmUtils.parseTile(xhr.response, this._projectObj, requestedCoords)
     68             .then((tileData) => ({
     69                         tileData,
     70                         coords: tileData.coords
     71             }));
     72     }
     73 
     74     calcAdjustment(layerTileCoords, dataTileCoords) {
     75         const adjustment = super.calcAdjustment(
     76             {x: layerTileCoords.x, y: layerTileCoords.y, z: layerTileCoords.z - 2},
     77             dataTileCoords
     78         );
     79         if (adjustment) {
     80             adjustment.offsetX *= 1024;
     81             adjustment.offsetY *= 1024;
     82         }
     83         return adjustment;
     84     }
     85 }
     86 
     87 export {WikimapiaLoader};