Skip to content

Commit

Permalink
Fixed start editing from between line segments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ponlawat-w committed Dec 15, 2023
1 parent e7732af commit 8355e84
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ export default class OSMWaySnap extends PointerInteraction {

/** Layer of snapping ways */
private wayLayer: VectorLayer<VectorSource<Feature<LineString>>>|undefined = undefined;
/** Snap interaction */
private snapInteraction: Snap;
/** Snap interaction to waySource */
private snapWayInteraction: Snap;
/** Snap interaction to source */
private snapSourceInteraction: Snap;

/**
* Constructor
Expand All @@ -118,7 +120,8 @@ export default class OSMWaySnap extends PointerInteraction {
overpassQuery: options.overpassQuery ?? '(way["highway"];>;);',
overpassEndpointURL: options.overpassEndpointURL ?? undefined
});
this.snapInteraction = new Snap({ source: this.waySource });
this.snapWayInteraction = new Snap({ source: this.waySource });
this.snapSourceInteraction = new Snap({ source: this.source });

if (options.createAndAddWayLayer ?? true) {
this.wayLayer = new VectorLayer({ source: this.waySource, style: OSMOverpassWaySource.getDefaultStyle() });
Expand Down Expand Up @@ -147,15 +150,19 @@ export default class OSMWaySnap extends PointerInteraction {
this.waySource.un('featuresloadend', this.waysFeaturesLoadEnded.bind(this));
this.wayLayer && this.map.removeLayer(this.wayLayer);
this.map.removeLayer(this.overlayLayer);
this.map.removeInteraction(this.snapInteraction);
this.map.removeInteraction(this.snapWayInteraction);
this.map.removeInteraction(this.snapSourceInteraction);
}
super.setMap(map);
this.map = map ?? undefined;
if (this.map) {
this.waySource.on('featuresloadend', this.waysFeaturesLoadEnded.bind(this));
this.wayLayer && this.map.getAllLayers().indexOf(this.wayLayer) < 0 && this.map.addLayer(this.wayLayer);
this.map.addLayer(this.overlayLayer);
this.map.getInteractions().getArray().indexOf(this.snapInteraction) < 0 && this.map.addInteraction(this.snapInteraction);
this.map.removeInteraction(this.snapWayInteraction);
this.map.addInteraction(this.snapWayInteraction);
this.map.removeInteraction(this.snapSourceInteraction);
this.map.addInteraction(this.snapSourceInteraction);
}
}

Expand Down Expand Up @@ -296,6 +303,7 @@ export default class OSMWaySnap extends PointerInteraction {
* @param fromCoordinate Coordinate on feature geometry to starts altering, the original vertices after this coordinate will go to draft in overlay layer
*/
private enterEditMode(feature: Feature<LineString>, fromCoordinate: Coordinate) {
feature.getGeometry()!.setCoordinates(LineStringUtils.split(feature.getGeometry()!, fromCoordinate).getCoordinates());
const originalCoordinates = feature.getGeometry()!.getCoordinates();
const fromCoordinateIdx = originalCoordinates.findIndex(c => c[0] === fromCoordinate[0] && c[1] === fromCoordinate[1]);
this.coordinates = originalCoordinates.slice(0, fromCoordinateIdx + 1);
Expand Down
21 changes: 21 additions & 0 deletions tests/interaction.edit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,27 @@ describe('Test OSMWaySnap Interaction: Line Edition', () => {
expect(draftLine.getGeometry()!.getCoordinates()[5]).toEqual([0, 30]);
});

it('enables edit mode by spliting the selected feature on a line segment between vertices', () => {
mouseMove(map, interaction, [0, 5]);
mouseClick(map, interaction, [0, 5]);

const activeCoors = interaction.getActiveFeature()!.getGeometry()!.getCoordinates();
expect(activeCoors.length).toEqual(3);
expect(activeCoors[0]).toEqual([0, -50]);
expect(activeCoors[1]).toEqual([0, 0]);
expect(activeCoors[2]).toEqual([0, 5]);

const draftLine = (interaction as any).draftOriginalLine as Feature<LineString>;
expect(draftLine).toBeDefined();
expect(draftLine.getGeometry()!.getCoordinates().length).toEqual(6);
expect(draftLine.getGeometry()!.getCoordinates()[0]).toEqual([0, 5]);
expect(draftLine.getGeometry()!.getCoordinates()[1]).toEqual([0, 10]);
expect(draftLine.getGeometry()!.getCoordinates()[2]).toEqual([10, 10]);
expect(draftLine.getGeometry()!.getCoordinates()[3]).toEqual([10, 20]);
expect(draftLine.getGeometry()!.getCoordinates()[4]).toEqual([0, 20]);
expect(draftLine.getGeometry()!.getCoordinates()[5]).toEqual([0, 30]);
});

it('does not enable edit mode when not allowed', () => {
interaction.allowEdit = false;

Expand Down

0 comments on commit 8355e84

Please sign in to comment.