LiveAtlas/src/model/LiveAtlasProjection.ts

60 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-12-16 16:54:41 +00:00
/*
2021-07-25 14:12:40 +00:00
* Copyright 2021 James Lyne
2020-12-16 16:54:41 +00:00
*
* Some portions of this file were taken from https://github.com/webbukkit/dynmap.
* These portions are Copyright 2020 Dynmap Contributors.
*
2021-07-25 14:12:40 +00:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2020-12-16 16:54:41 +00:00
*
2021-07-25 14:12:40 +00:00
* http://www.apache.org/licenses/LICENSE-2.0
2020-12-16 16:54:41 +00:00
*
2021-07-25 14:12:40 +00:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
2020-12-16 16:54:41 +00:00
*/
import {LatLng} from 'leaflet';
2021-07-19 15:40:30 +00:00
import {Coordinate} from "@/index";
export interface LiveAtlasProjectionOptions {
mapToWorld: [number, number, number, number, number, number, number, number, number],
worldToMap: [number, number, number, number, number, number, number, number, number],
nativeZoomLevels: number
}
export class LiveAtlasProjection {
private readonly options: LiveAtlasProjectionOptions
constructor(options: LiveAtlasProjectionOptions) {
this.options = {
mapToWorld: options.mapToWorld || [0, 0, 0, 0, 0, 0, 0, 0],
worldToMap: options.worldToMap || [0, 0, 0, 0, 0, 0, 0, 0],
nativeZoomLevels: options.nativeZoomLevels || 1
}
}
2020-12-12 22:04:56 +00:00
locationToLatLng(location: Coordinate): LatLng {
const wtp = this.options.worldToMap,
lat = wtp[3] * location.x + wtp[4] * location.y + wtp[5] * location.z,
lng = wtp[0] * location.x + wtp[1] * location.y + wtp[2] * location.z;
return new LatLng(
-((128 - lat) / (1 << this.options.nativeZoomLevels)),
lng / (1 << this.options.nativeZoomLevels));
}
2020-12-12 22:04:56 +00:00
latLngToLocation(latLng: LatLng, y: number): Coordinate {
const ptw = this.options.mapToWorld,
lat = latLng.lng * (1 << this.options.nativeZoomLevels),
lon = 128 + latLng.lat * (1 << this.options.nativeZoomLevels),
x = ptw[0] * lat + ptw[1] * lon + ptw[2] * y,
z = ptw[6] * lat + ptw[7] * lon + ptw[8] * y;
return {x: x, y: y, z: z};
}
}