矢量瓦片相关计算函数

42次阅读

共计 2618 个字符,预计需要花费 7 分钟才能阅读完成。

import turf from 'turf'
export default {
  TILE_SIZE: 256,
  /*
  * 获取指定级别的瓦片数目
  */
  _getMapSize(level) {return Math.pow(2, level);
  },
  /**
     * Convert a longitude coordinate (in degrees) to the tile X number at a
     * certain zoom level. 经度转瓦片列号
     * @param longitude
     *            the longitude coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the tile X number of the longitude value.
  */
   longitudeToTileX(longitude, zoom) {let px = this.longitudeToPixelX(longitude, zoom);
       return this.pixelXToTileX(px, zoom);
   },
   /**
      * Convert a latitude coordinate (in degrees) to a tile Y number at a
      * certain zoom level. 纬度转瓦片行号
      * @param latitude
      *            the latitude coordinate that should be converted.
      * @param zoom
      *            the zoom level at which the coordinate should be converted.
      * @return the tile Y number of the latitude value.
   */
   latitudeToTileY(latitude, zoom) {let py = this.latitudeToPixelY(latitude, zoom);
       return this.pixelYToTileY(py, zoom);
   },
/**
     * Convert a latitude coordinate (in degrees) to a pixel Y coordinate at a
     * certain zoom level. 经纬度坐标 (纬度) 转屏幕像素坐标(Y)
     *
     * @param latitude
     *            the latitude coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the pixel Y coordinate of the latitude value.
     */
    latitudeToPixelY(latitude, zoom) {let sinLatitude = Math.sin(latitude * Math.PI / 180);
        return (0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI)) * (this.TILE_SIZE << zoom);
    },
    /**
     * Convert a longitude coordinate (in degrees) to a pixel X coordinate at a
     * certain zoom level. 经纬度坐标 (经度) 转屏幕像素坐标(X)
     *
     * @param longitude
     *            the longitude coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the pixel X coordinate of the longitude value.
     */
    longitudeToPixelX(longitude, zoom) {return (longitude + 180) / 360 * (this.TILE_SIZE << zoom);
    },
   /*
   * 指定级别下,将宏观上的经度转换为该列上的瓦片的像素列号(微观)
  */
  _lngToPixelX(longitude, level) {let x = (longitude + 180) / 360;
    let pixelX = Math.floor(x * this._getMapSize(level) * 256 % 256);
    return pixelX;
  },
  /*
  * 指定级别纬度对应的像素行号
  */
  _latToPixelY(latitude, level) {let sinLatitude = Math.sin(latitude * Math.PI / 180);
    let y = 0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);
    let pixelY = Math.floor(y * this._getMapSize(level) * 256 % 256);
    return pixelY;
  },
/**
     * Convert a pixel X coordinate to the tile X number.
     *
     * @param pixelX
     *            the pixel X coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the tile X number.
     */
    pixelXToTileX(pixelX, zoom) {return Math.floor(Math.min(Math.max(pixelX / this.TILE_SIZE, 0), Math.pow(2, zoom) - 1));
    },
    /**
     * Converts a pixel Y coordinate to the tile Y number.
     *
     * @param pixelY
     *            the pixel Y coordinate that should be converted.
     * @param zoom
     *            the zoom level at which the coordinate should be converted.
     * @return the tile Y number.
     */
    pixelYToTileY(pixelY, zoom) {return Math.floor(Math.min(Math.max(pixelY / this.TILE_SIZE, 0), Math.pow(2, zoom) - 1));
    },
}

正文完
 0