@geogirafe/lib-geoportal
Version:
GeoGirafe is a flexible application to build online geoportals.
65 lines (64 loc) • 2.6 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
import { getCenter, getHeight, getWidth } from 'ol/extent.js';
import LineString from 'ol/geom/LineString.js';
import Polygon from 'ol/geom/Polygon.js';
import { DrawingShape } from './drawingFeature.js';
const EPSILON = 1e-12;
function sqDistance(a, b) {
const dx = a[0] - b[0];
const dy = a[1] - b[1];
return dx * dx + dy * dy;
}
export function calculateCenterAndMinRadius(geometry) {
let center;
let coordinates;
if (geometry instanceof Polygon) {
const ring = geometry.getCoordinates()[0];
const polygonCoords = ring.slice(0, -1);
const x = polygonCoords.reduce((sum, c) => sum + c[0], 0);
const y = polygonCoords.reduce((sum, c) => sum + c[1], 0);
center = [x / polygonCoords.length, y / polygonCoords.length];
coordinates = polygonCoords;
}
else if (geometry instanceof LineString) {
center = geometry.getCoordinateAt(0.5);
coordinates = geometry.getCoordinates();
}
else {
center = getCenter(geometry.getExtent());
}
if (coordinates && coordinates.length > 0) {
const maxSqDistance = Math.max(...coordinates.map((coordinate) => sqDistance(coordinate, center)));
return {
center,
minRadius: Math.sqrt(maxSqDistance) / 3
};
}
return {
center,
minRadius: Math.max(getWidth(geometry.getExtent()), getHeight(geometry.getExtent())) / 3
};
}
export function createScaledAndRotatedGeometry(geometry0, center, minRadius, initialPoint, currentPoint) {
const initialRadius = Math.sqrt(sqDistance(initialPoint, center));
if (initialRadius <= minRadius) {
return geometry0.clone();
}
const currentRadius = Math.sqrt(sqDistance(currentPoint, center));
if (currentRadius <= EPSILON) {
return geometry0.clone();
}
const initialAngle = Math.atan2(initialPoint[1] - center[1], initialPoint[0] - center[0]);
const currentAngle = Math.atan2(currentPoint[1] - center[1], currentPoint[0] - center[0]);
const geometry = geometry0.clone();
geometry.scale(currentRadius / initialRadius, undefined, center);
geometry.rotate(currentAngle - initialAngle, center);
return geometry;
}
export function getGeometryForRendering(feature) {
const modifyGeometry = feature.get('modifyGeometry');
return modifyGeometry?.geometry ?? feature.getGeometry() ?? undefined;
}
export function shouldAllowVertexInsertionForShape(shapeType) {
return shapeType !== DrawingShape.Square && shapeType !== DrawingShape.Rectangle;
}