Source: control/union.js

  1. import OL3Parser from 'jsts/org/locationtech/jts/io/OL3Parser';
  2. import { OverlayOp } from 'jsts/org/locationtech/jts/operation/overlay';
  3. import LinearRing from 'ol/geom/LinearRing';
  4. import {
  5. Point,
  6. LineString,
  7. Polygon,
  8. MultiPoint,
  9. MultiLineString,
  10. MultiPolygon,
  11. } from 'ol/geom';
  12. import TopologyControl from './topology';
  13. import unionSVG from '../../img/union.svg';
  14. /**
  15. * Control for creating a union of geometries.
  16. * @extends {Control}
  17. * @alias ole.Union
  18. */
  19. class Union extends TopologyControl {
  20. /**
  21. * @inheritdoc
  22. * @param {Object} [options] Control options.
  23. * @param {number} [options.hitTolerance] Select tolerance in pixels
  24. * (default is 10)
  25. */
  26. constructor(options) {
  27. super({
  28. title: 'Union',
  29. className: 'ole-control-union',
  30. image: unionSVG,
  31. ...options,
  32. });
  33. }
  34. /**
  35. * Apply a union for given features.
  36. * @param {Array.<ol.Feature>} features Features to union.
  37. */
  38. applyTopologyOperation(features) {
  39. super.applyTopologyOperation(features);
  40. const parser = new OL3Parser();
  41. parser.inject(
  42. Point,
  43. LineString,
  44. LinearRing,
  45. Polygon,
  46. MultiPoint,
  47. MultiLineString,
  48. MultiPolygon,
  49. );
  50. for (let i = 1; i < features.length; i += 1) {
  51. const geom = parser.read(features[0].getGeometry());
  52. const otherGeom = parser.read(features[i].getGeometry());
  53. const unionGeom = OverlayOp.union(geom, otherGeom);
  54. features[0].setGeometry(parser.write(unionGeom));
  55. features[i].setGeometry(null);
  56. }
  57. }
  58. }
  59. export default Union;