import * as L from '@mapgis/leaflet'
import { Projection } from '@mapgis/webclient-common'
/**
* 获取地图范围,仅在4326相关坐标系下生效,其他坐标系在地图视图的坐标系中设置了范围
* @param {Object} layer 基础图层对象
* @return {Object} leaflet的范围对象
* */
function getBounds(layer) {
let bounds = undefined
if (
String(layer.spatialReference.wkid) === '4326' ||
String(layer.spatialReference.wkid) === '4214' ||
String(layer.spatialReference.wkid) === '4490' ||
String(layer.spatialReference.wkid) === '4610'
) {
const corner1 = L.latLng(layer.extent.ymin, layer.extent.xmin)
const corner2 = L.latLng(layer.extent.ymax, layer.extent.xmax)
bounds = L.latLngBounds(corner1, corner2)
} else if (
String(layer.spatialReference.wkid) === '3857' ||
String(layer.spatialReference.wkid) === '900913'
) {
const point1 = Projection.projectMercatorToLonLat({
x: layer.extent.xmin,
y: layer.extent.ymin
})
const corner1 = L.latLng(point1.latitude, point1.longitude)
const point2 = Projection.projectMercatorToLonLat({
x: layer.extent.xmax,
y: layer.extent.ymax
})
const corner2 = L.latLng(point2.latitude, point2.longitude)
bounds = L.latLngBounds(corner1, corner2)
} else {
const projPoints = Projection.projectToGeographyPoints({
points: [
[layer.extent.xmin, layer.extent.ymin],
[layer.extent.xmax, layer.extent.ymax]
],
wkid: layer.spatialReference.wkid,
proj4Defs: layer.spatialReference.wkt
})
const corner1 = L.latLng(projPoints[0][1], projPoints[0][0])
const corner2 = L.latLng(projPoints[1][1], projPoints[1][0])
bounds = L.latLngBounds(corner1, corner2)
}
return bounds
}
/**
* @description: 获取旋转后地图容器像素坐标系bounds
* @param {*} leafletMap
* @return {*}
*/
function getMapRotateBounds(leafletMap) {
const bounds = leafletMap.getBounds()
const nw = bounds.getNorthWest()
const se = bounds.getSouthEast()
const sw = bounds.getSouthWest()
const ne = bounds.getNorthEast()
const nwPoint = leafletMap.latLngToLayerPoint(nw)
const sePoint = leafletMap.latLngToLayerPoint(se)
const swPoint = leafletMap.latLngToLayerPoint(sw)
const nePoint = leafletMap.latLngToLayerPoint(ne)
// 计算旋转坐标
const pxBounds = L.bounds(nwPoint, sePoint)
pxBounds.extend(swPoint)
pxBounds.extend(nePoint)
// 计算屏幕坐标
const size = leafletMap.getSize()
const leftTop = leafletMap.containerPointToLayerPoint([0, 0])
const rightBottom = leafletMap.containerPointToLayerPoint([size.x, size.y])
const screenBounds = L.bounds(leftTop, rightBottom)
pxBounds.extend(screenBounds.getBottomLeft())
pxBounds.extend(screenBounds.getTopRight())
return pxBounds
}
export { getBounds, getMapRotateBounds }