@polar/plugin-pins
Version:
Pins plugin for POLAR that adds map interactions to client that allow users to indicate a specific point on the map.
52 lines (46 loc) • 1.13 kB
text/typescript
/* eslint-disable @typescript-eslint/naming-convention */
import { transform } from 'ol/proj'
import { getCenter } from 'ol/extent'
import {
Circle,
LinearRing,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
} from 'ol/geom'
const geoms = {
Circle,
LinearRing,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
}
export default function (
sourceEpsg,
targetEpsg,
geometryType,
geometryCoordinates
) {
const Type = geoms[geometryType || 'Point']
const instance = new Type(geometryCoordinates)
let pointCoordinate = getCenter(instance.getExtent())
// return random point if bbox center is not in shape
if (
(geometryType === 'Polygon' || geometryType === 'MultiPolygon') &&
!instance.intersectsCoordinate(pointCoordinate)
) {
pointCoordinate =
geometryType === 'Polygon'
? instance.getInteriorPoint().getFirstCoordinate()
: instance.getInteriorPoints().getFirstCoordinate()
}
return sourceEpsg === targetEpsg
? pointCoordinate
: transform(pointCoordinate, sourceEpsg, targetEpsg)
}