import OL3Parser from "jsts/org/locationtech/jts/io/OL3Parser";
import { OverlayOp } from "jsts/org/locationtech/jts/operation/overlay";
import {
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
} from "ol/geom";
import LinearRing from "ol/geom/LinearRing";
import intersectionSVG from "../../img/intersection.svg";
import TopologyControl from "./topology";
/**
* Control for intersection geometries.
* @extends {Control}
* @alias ole.Intersection
*/
class Intersection extends TopologyControl {
/**
* @inheritdoc
* @param {Object} [options] Control options.
* @param {number} [options.hitTolerance] Select tolerance in pixels
* (default is 10)
*/
constructor(options) {
super({
className: "ole-control-intersection",
image: intersectionSVG,
title: "Intersection",
...options,
});
}
/**
* Intersect given features.
* @param {Array.<ol.Feature>} features Features to inersect.
*/
applyTopologyOperation(features) {
super.applyTopologyOperation(features);
if (features.length < 2) {
return;
}
const parser = new OL3Parser();
parser.inject(
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon,
);
for (let i = 1; i < features.length; i += 1) {
const geom = parser.read(features[0].getGeometry());
const otherGeom = parser.read(features[i].getGeometry());
const intersectGeom = OverlayOp.intersection(geom, otherGeom);
features[0].setGeometry(parser.write(intersectGeom));
features[i].setGeometry(null);
}
}
}
export default Intersection;