New function createOrUpdateCustomSketchLine_

New function for duplicated code handling sketch line for custom polygons
This commit is contained in:
mike-000
2020-11-25 11:59:32 +00:00
committed by GitHub
parent f8df533cb0
commit 8898d540fa
+33 -34
View File
@@ -742,6 +742,31 @@ class Draw extends PointerInteraction {
} }
} }
/**
* @param {import("../geom/Polygon.js").default} geometry Polygon geometry.
* @private
*/
createOrUpdateCustomSketchLine_(geometry) {
if (!this.sketchLine_) {
this.sketchLine_ = new Feature();
}
const ring = geometry.getLinearRing(0);
let sketchLineGeom = this.sketchLine_.getGeometry();
if (!sketchLineGeom) {
sketchLineGeom = new LineString(
ring.getFlatCoordinates(),
ring.getLayout()
);
this.sketchLine_.setGeometry(sketchLineGeom);
} else {
sketchLineGeom.setFlatCoordinates(
ring.getLayout(),
ring.getFlatCoordinates()
);
sketchLineGeom.changed();
}
}
/** /**
* Start the drawing. * Start the drawing.
* @param {import("../MapBrowserEvent.js").default} event Event. * @param {import("../MapBrowserEvent.js").default} event Event.
@@ -812,32 +837,15 @@ class Draw extends PointerInteraction {
const sketchPointGeom = this.sketchPoint_.getGeometry(); const sketchPointGeom = this.sketchPoint_.getGeometry();
sketchPointGeom.setCoordinates(coordinate); sketchPointGeom.setCoordinates(coordinate);
} }
/** @type {LineString} */
let sketchLineGeom;
if ( if (
geometry.getType() === GeometryType.POLYGON && geometry.getType() === GeometryType.POLYGON &&
this.mode_ !== Mode.POLYGON this.mode_ !== Mode.POLYGON
) { ) {
if (!this.sketchLine_) { this.createOrUpdateCustomSketchLine_(
this.sketchLine_ = new Feature(); /** @type {Polygon} */ (geometry)
} );
const ring = geometry.getLinearRing(0);
sketchLineGeom = this.sketchLine_.getGeometry();
if (!sketchLineGeom) {
sketchLineGeom = new LineString(
ring.getFlatCoordinates(),
ring.getLayout()
);
this.sketchLine_.setGeometry(sketchLineGeom);
} else {
sketchLineGeom.setFlatCoordinates(
ring.getLayout(),
ring.getFlatCoordinates()
);
sketchLineGeom.changed();
}
} else if (this.sketchLineCoords_) { } else if (this.sketchLineCoords_) {
sketchLineGeom = this.sketchLine_.getGeometry(); const sketchLineGeom = this.sketchLine_.getGeometry();
sketchLineGeom.setCoordinates(this.sketchLineCoords_); sketchLineGeom.setCoordinates(this.sketchLineCoords_);
} }
this.updateSketchFeatures_(); this.updateSketchFeatures_();
@@ -897,8 +905,6 @@ class Draw extends PointerInteraction {
const geometry = this.sketchFeature_.getGeometry(); const geometry = this.sketchFeature_.getGeometry();
const projection = this.getMap().getView().getProjection(); const projection = this.getMap().getView().getProjection();
let coordinates; let coordinates;
/** @type {LineString} */
let sketchLineGeom;
if (this.mode_ === Mode.LINE_STRING) { if (this.mode_ === Mode.LINE_STRING) {
coordinates = /** @type {LineCoordType} */ (this.sketchCoords_); coordinates = /** @type {LineCoordType} */ (this.sketchCoords_);
coordinates.splice(-2, 1); coordinates.splice(-2, 1);
@@ -912,22 +918,15 @@ class Draw extends PointerInteraction {
} }
} }
this.geometryFunction_(coordinates, geometry, projection); this.geometryFunction_(coordinates, geometry, projection);
if (geometry.getType() === GeometryType.POLYGON && this.sketchLine_) { if (geometry.getType() === GeometryType.POLYGON && this.sketchLine_) {
sketchLineGeom = this.sketchLine_.getGeometry(); this.createOrUpdateCustomSketchLine_(
if (sketchLineGeom) { /** @type {Polygon} */ (geometry)
const ring = geometry.getLinearRing(0); );
sketchLineGeom.setFlatCoordinates(
ring.getLayout(),
ring.getFlatCoordinates()
);
sketchLineGeom.changed();
}
} }
} else if (this.mode_ === Mode.POLYGON) { } else if (this.mode_ === Mode.POLYGON) {
coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0]; coordinates = /** @type {PolyCoordType} */ (this.sketchCoords_)[0];
coordinates.splice(-2, 1); coordinates.splice(-2, 1);
sketchLineGeom = this.sketchLine_.getGeometry(); const sketchLineGeom = this.sketchLine_.getGeometry();
if (coordinates.length >= 2 && this.pointerType_ !== 'mouse') { if (coordinates.length >= 2 && this.pointerType_ !== 'mouse') {
const finishCoordinate = coordinates[coordinates.length - 2].slice(); const finishCoordinate = coordinates[coordinates.length - 2].slice();
coordinates.pop(); coordinates.pop();