Map testing, custom table based on Tanstack Table
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
import { Coordinate, distance, rotate } from "ol/coordinate";
|
||||
import { Extent, getCenter } from "ol/extent";
|
||||
import { Extent, getCenter, getHeight, getWidth } from "ol/extent";
|
||||
import { LineString, Polygon, SimpleGeometry } from "ol/geom";
|
||||
import { addCoordinateTransforms, addProjection, get, getTransform, Projection, ProjectionLike, transform } from "ol/proj";
|
||||
import proj4 from "proj4";
|
||||
|
||||
@ -119,9 +120,87 @@ function calculateExtent(bottomLeft: Coordinate, topLeft: Coordinate, topRight:
|
||||
return extent;
|
||||
}
|
||||
|
||||
function getTilesPerSide(zoom: number) {
|
||||
return Math.pow(2, zoom)
|
||||
}
|
||||
|
||||
function normalize(value: number, min: number, max: number) {
|
||||
return (value - min) / (max - min)
|
||||
}
|
||||
|
||||
function getTileIndex(normalized: number, tilesPerSide: number) {
|
||||
return Math.floor(normalized * tilesPerSide)
|
||||
}
|
||||
|
||||
function getGridCellPosition(x: number, y: number, extent: Extent, zoom: number) {
|
||||
const tilesPerSide = getTilesPerSide(zoom);
|
||||
const minX = extent[0]
|
||||
const minY = extent[1]
|
||||
const maxX = extent[2]
|
||||
const maxY = extent[3]
|
||||
|
||||
// Normalize the coordinates
|
||||
const xNormalized = normalize(x, minX, maxX);
|
||||
const yNormalized = normalize(y, minY, maxY);
|
||||
|
||||
// Get tile indices
|
||||
const tileX = getTileIndex(xNormalized, tilesPerSide);
|
||||
const tileY = getTileIndex(1 - yNormalized, tilesPerSide);
|
||||
|
||||
return { tileX, tileY };
|
||||
}
|
||||
|
||||
function calculateCenter(geometry: SimpleGeometry) {
|
||||
let center, coordinates, minRadius;
|
||||
const type = geometry.getType();
|
||||
if (type === 'Polygon') {
|
||||
let x = 0;
|
||||
let y = 0;
|
||||
let i = 0;
|
||||
coordinates = (geometry as Polygon).getCoordinates()[0].slice(1);
|
||||
coordinates.forEach(function (coordinate) {
|
||||
x += coordinate[0];
|
||||
y += coordinate[1];
|
||||
i++;
|
||||
});
|
||||
center = [x / i, y / i];
|
||||
} else if (type === 'LineString') {
|
||||
center = (geometry as LineString).getCoordinateAt(0.5);
|
||||
coordinates = geometry.getCoordinates();
|
||||
} else {
|
||||
center = getCenter(geometry.getExtent());
|
||||
}
|
||||
let sqDistances;
|
||||
if (coordinates) {
|
||||
sqDistances = coordinates.map(function (coordinate: Coordinate) {
|
||||
const dx = coordinate[0] - center[0];
|
||||
const dy = coordinate[1] - center[1];
|
||||
return dx * dx + dy * dy;
|
||||
});
|
||||
minRadius = Math.sqrt(Math.max.apply(Math, sqDistances)) / 3;
|
||||
} else {
|
||||
minRadius =
|
||||
Math.max(
|
||||
getWidth(geometry.getExtent()),
|
||||
getHeight(geometry.getExtent()),
|
||||
) / 3;
|
||||
}
|
||||
return {
|
||||
center: center,
|
||||
coordinates: coordinates,
|
||||
minRadius: minRadius,
|
||||
sqDistances: sqDistances,
|
||||
};
|
||||
}
|
||||
|
||||
export {
|
||||
rotateProjection,
|
||||
calculateRotationAngle,
|
||||
calculateExtent,
|
||||
calculateCentroid
|
||||
calculateCentroid,
|
||||
getTilesPerSide,
|
||||
normalize,
|
||||
getTileIndex,
|
||||
getGridCellPosition,
|
||||
calculateCenter
|
||||
}
|
Reference in New Issue
Block a user