Skip to content

Commit

Permalink
Replace class export by methods export
Browse files Browse the repository at this point in the history
  • Loading branch information
lehecht committed Nov 2, 2023
1 parent 398a5ae commit 4abce21
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import ZoomifySource from '@biigle/ol/source/Zoomify';
import ZoomLevel from './annotationCanvas/zoomLevel';
import ZoomToExtentControl from '@biigle/ol/control/ZoomToExtent';
import ZoomToNativeControl from '../ol/ZoomToNativeControl';
import PolygonValidator from '../ol/PolygonValidator';
import * as PolygonValidator from '../ol/PolygonValidator';
import { click as clickCondition } from '@biigle/ol/events/condition';
import { defaults as defaultInteractions } from '@biigle/ol/interaction'
import { getCenter } from '@biigle/ol/extent';
Expand Down
70 changes: 33 additions & 37 deletions resources/assets/js/annotations/ol/PolygonValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,57 @@ import OL3Parser from 'jsts/org/locationtech/jts/io/OL3Parser';
import Polygonizer from 'jsts/org/locationtech/jts/operation/polygonize/Polygonizer';
import Monkey from 'jsts/org/locationtech/jts/monkey'; // Needed for isValid(), normalize()

class PolygonValidator {

/**
* Checks if polygon consists of at least 3 unique points
*
* @param feature containing the polygon
* @returns True if coordinates contains at least 3 unique points, otherwise false
* **/
static isInvalidPolygon(feature) {
let polygon = feature.getGeometry();
let points = polygon.getCoordinates()[0];
return (new Set(points.map(xy => String([xy])))).size < 3;
export function isInvalidPolygon(feature) {
let polygon = feature.getGeometry();
let points = polygon.getCoordinates()[0];
return (new Set(points.map(xy => String([xy])))).size < 3;

}
}

/**
* Makes non-simple polygon simple
*
* @param feature feature containing the (non-simple) polygon
* **/
static makePolygonSimple(feature) {
// Check if polygon is self-intersecting
const parser = new OL3Parser();
parser.inject(
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon
);

// Translate ol geometry into jsts geometry
let jstsPolygon = parser.read(feature.getGeometry());

if (jstsPolygon.isSimple()) {
return feature;
}
export function makePolygonSimple(feature) {
// Check if polygon is self-intersecting
const parser = new OL3Parser();
parser.inject(
Point,
LineString,
LinearRing,
Polygon,
MultiPoint,
MultiLineString,
MultiPolygon
);

// Translate ol geometry into jsts geometry
let jstsPolygon = parser.read(feature.getGeometry());

if (jstsPolygon.isSimple()) {
return feature;
}

// Divide non-simple polygon at cross points into several smaller polygons
// and translate back to ol geometry
let polygons = jstsValidate(jstsPolygon)['array'].map(p => parser.write(p));
// Divide non-simple polygon at cross points into several smaller polygons
// and translate back to ol geometry
let polygons = jstsValidate(jstsPolygon)['array'].map(p => parser.write(p));

if (polygons.length > 1) {
// Select biggest part
let greatestPolygon = getGreatestPolygon(polygons);
// Only change coordinates because object references are in use
feature.getGeometry().setCoordinates(greatestPolygon.getCoordinates());
}
if (polygons.length > 1) {
// Select biggest part
let greatestPolygon = getGreatestPolygon(polygons);
// Only change coordinates because object references are in use
feature.getGeometry().setCoordinates(greatestPolygon.getCoordinates());
}
}

export default PolygonValidator;

/**
* @author Martin Kirk
*
Expand All @@ -78,7 +74,7 @@ export default PolygonValidator;
* @param geom
* @return a geometry
*/

function jstsValidate(geom) {
if (geom instanceof JstsPolygon) {
if (geom.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import Keyboard from '../../../core/keyboard';
import Styles from '../../../annotations/stores/styles';
import VectorLayer from '@biigle/ol/layer/Vector';
import VectorSource from '@biigle/ol/source/Vector';
import PolygonValidator from '../../../annotations/ol/PolygonValidator';
import * as PolygonValidator from "../../../annotations/ol/PolygonValidator";
/**
* Mixin for the videoScreen component that contains logic for the draw interactions.
*
Expand Down Expand Up @@ -173,11 +172,8 @@ export default {
return;
}
// If polygon is self-intersecting, create valid polygon
let validPolygon = PolygonValidator.makePolygonSimple(e.feature);
let coords = validPolygon.getGeometry().getCoordinates();
e.feature.getGeometry().setCoordinates([coords[0]]);
// If polygon is self-intersecting, create simple polygon
PolygonValidator.makePolygonSimple(e.feature);
}
let lastFrame = this.pendingAnnotation.frames[this.pendingAnnotation.frames.length - 1];
Expand Down

0 comments on commit 4abce21

Please sign in to comment.