Separate appendCoordinates function from extend

This commit is contained in:
Otto Pellinen
2019-09-13 14:14:58 +03:00
committed by Olivier Guyot
parent 25a5e83910
commit 83c0a258e6
3 changed files with 38 additions and 19 deletions
+6 -6
View File
@@ -1,12 +1,12 @@
--- ---
layout: example.html layout: example.html
title: Draw and Extend Features title: Draw and Append Features
shortdesc: Example of using the extend function of ol/interaction/Draw interaction. shortdesc: Example of using the appendCoordinates function of ol/interaction/Draw interaction.
docs: > docs: >
Example of using the the extend function of Draw interaction. Select a geometry type from the Example of using the the appendCoordinates function of Draw interaction. Select a geometry type from the
dropdown above to start drawing. To finish drawing, click the last dropdown above to start drawing. To finish drawing, click the last point.
point. Click the green LineString Feature to extend coordinates to your drawing. Click the green LineString Feature to append its coordinates to your drawing.
Extending is supported for drawing LineStrings and Polygons. Appending is supported for drawing LineStrings and Polygons.
tags: "draw, edit, freehand, vector" tags: "draw, edit, freehand, vector"
--- ---
<div id="map" class="map"></div> <div id="map" class="map"></div>
+2 -2
View File
@@ -21,7 +21,7 @@ sampleFeatures.push(
[-12000000, 4600000], [-12000000, 4600000],
[-12000000, 4000000], [-12000000, 4000000],
[-10000000, 5600000], [-10000000, 5600000],
[-9000000, 3000000], [-9000000, 3000000],
[-10000000, 4000000], [-10000000, 4000000],
[-11000000, 3000000], [-11000000, 3000000],
[-13000000, 4000000], [-13000000, 4000000],
@@ -77,7 +77,7 @@ map.on('click', (event) => {
// In this demo we remove the new point that was clicked, // In this demo we remove the new point that was clicked,
// and add the whole feature instead: // and add the whole feature instead:
draw.removeLastPoint(); draw.removeLastPoint();
draw.extend(clickedFeature); draw.appendCoordinates(clickedFeature.getGeometry().getCoordinates());
} }
}); });
+30 -11
View File
@@ -903,17 +903,17 @@ class Draw extends PointerInteraction {
} }
/** /**
* Extend an existing geometry by adding additional points. This only works * Extend the geometry that is being drawn, by appending the linear coordinates
* when drawing LineStrings or Polygons. Extending supports only input * given as the parameter to the coordinate array of currently drawn feature..
* features with `LineString` geometries, where the interaction will * This can be used when drawing LineStrings or Polygons. Extending supports
* extend lines by adding points to the end of the coordinates array. * only linear coordinates, such as the coordinates from a LineString
* @param {!Feature<LineString>} feature Feature to be extended. * or a LinearRing of a Polygon.
* @param {!LineCoordType} coordinateExtension Linear coordinates to be appended into
* the coordinate array.
* @api * @api
*/ */
extend(feature) { appendCoordinates(coordinateExtension) {
const lineStringGeometry = feature.getGeometry(); const ending = coordinateExtension[coordinateExtension.length - 1].slice();
const extendCoordinates = lineStringGeometry.getCoordinates();
const ending = extendCoordinates[extendCoordinates.length - 1].slice();
const mode = this.mode_; const mode = this.mode_;
let coordinates = []; let coordinates = [];
@@ -925,9 +925,9 @@ class Draw extends PointerInteraction {
return; return;
} }
// (1) Remove last coordinate, (2) extend coordinate list and (3) clone last coordinate // (1) Remove last coordinate, (2) append coordinate list and (3) clone last coordinate
coordinates.pop(); coordinates.pop();
Array.prototype.push.apply(coordinates, extendCoordinates); Array.prototype.push.apply(coordinates, coordinateExtension);
coordinates.push(ending); coordinates.push(ending);
// Update geometry and sketch line // Update geometry and sketch line
@@ -945,6 +945,25 @@ class Draw extends PointerInteraction {
this.updateSketchFeatures_(); this.updateSketchFeatures_();
} }
/**
* Extend an existing geometry by adding additional points. This only works
* on features with `LineString` geometries, where the interaction will
* extend lines by adding points to the end of the coordinates array.
* @param {!Feature<LineString>} feature Feature to be extended.
* @api
*/
extend(feature) {
const geometry = feature.getGeometry();
const lineString = geometry;
this.sketchFeature_ = feature;
this.sketchCoords_ = lineString.getCoordinates();
const last = this.sketchCoords_[this.sketchCoords_.length - 1];
this.finishCoordinate_ = last.slice();
this.sketchCoords_.push(last.slice());
this.updateSketchFeatures_();
this.dispatchEvent(new DrawEvent(DrawEventType.DRAWSTART, this.sketchFeature_));
}
/** /**
* Redraw the sketch features. * Redraw the sketch features.
* @private * @private