Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polygon & Line self snapping #645

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cypress/integration/polygon.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ describe('Draw & Edit Poly', () => {
.click(450, 100)
.click(450, 150)
.click(400, 150)
.click(390, 140)
.click(390, 120)
.click(390, 100)
.click(450, 100);

Expand Down
5 changes: 2 additions & 3 deletions cypress/integration/toolbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,11 @@ describe('Testing the Toolbar', () => {
.click(450, 100)
.click(450, 150)
.click(400, 150)
.click(390, 140)
.click(390, 120)
.click(390, 100)
.click(450, 100);


cy.get(mapSelector).click(390, 140).then(() => {
cy.get(mapSelector).click(410, 140).then(() => {
expect(testlayer.options.color).to.equal("red");
})
})
Expand Down
43 changes: 33 additions & 10 deletions src/js/Draw/L.PM.Draw.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ Draw.Line = Draw.extend({

// if self-intersection is forbidden, handle it
if (!this.options.allowSelfIntersection) {
this._handleSelfIntersection(true, e.latlng);
this._handleSelfIntersection(true, this._hintMarker.getLatLng());
}
},
hasSelfIntersection() {
Expand Down Expand Up @@ -222,15 +222,6 @@ Draw.Line = Draw.extend({
}
},
_createVertex(e) {
// don't create a vertex if we have a selfIntersection and it is not allowed
if (!this.options.allowSelfIntersection) {
this._handleSelfIntersection(true, e.latlng);

if (this._doesSelfIntersect) {
return;
}
}

// assign the coordinate of the click to the hintMarker, that's necessary for
// mobile where the marker can't follow a cursor
if (!this._hintMarker._snapped) {
Expand All @@ -240,6 +231,15 @@ Draw.Line = Draw.extend({
// get coordinate for new vertex by hintMarker (cursor marker)
const latlng = this._hintMarker.getLatLng();

// don't create a vertex if we have a selfIntersection and it is not allowed
if (!this.options.allowSelfIntersection) {
this._handleSelfIntersection(true, latlng);

if (this._doesSelfIntersect) {
return;
}
}

// check if the first and this vertex have the same latlng
if (latlng.equals(this._layer.getLatLngs()[0])) {
// yes? finish the polygon
Expand All @@ -251,6 +251,11 @@ Draw.Line = Draw.extend({
return;
}

// when last latlng is equal to the current one -> don't create a new point. Can happen while snapping
if(this._layer.getLatLngs().length > 0 && latlng.equals(this._layer.getLatLngs().slice(-1)[0])){
return;
}

// is this the first point?
const first = this._layer.getLatLngs().length === 0;

Expand Down Expand Up @@ -287,11 +292,21 @@ Draw.Line = Draw.extend({
// remove that marker
this._layerGroup.removeLayer(marker);

// remove the marker from the snapping list
const idx = this._otherSnapLayers.indexOf(marker);
if(idx > -1){
this._otherSnapLayers.splice(idx,1);
}

// update layer with new coords
this._layer.setLatLngs(coords);

// sync the hintline again
this._syncHintLine();

if (this.options.snappable) {
this._cleanupSnapping();
}
},
_finishShape() {
// if self intersection is not allowed, do not finish the shape!
Expand Down Expand Up @@ -355,6 +370,14 @@ Draw.Line = Draw.extend({

if (second) {
this._hintMarker.setTooltipContent(getTranslation('tooltips.finishLine'));
// adding layer to the snapping list after a segment is created (two markers needed)
this._otherSnapLayers.push(this._layer);
}

this._otherSnapLayers.push(marker);

if (this.options.snappable) {
this._cleanupSnapping();
}

return marker;
Expand Down
15 changes: 11 additions & 4 deletions src/js/Draw/L.PM.Draw.Polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ Draw.Polygon = Draw.Line.extend({

// add the first vertex to "other snapping layers" so the polygon is easier to finish
this._tempSnapLayerIndex = this._otherSnapLayers.push(marker) - 1;

if (this.options.snappable) {
this._cleanupSnapping();
}
} else {
// add a click event w/ no handler to the marker
// event won't bubble so prevents creation of identical markers in same polygon
// fixes issue where double click during poly creation when allowSelfIntersection: false caused it to break
marker.on('click', () => (1));
this._otherSnapLayers.push(marker);
}

const second = this._layer.getLatLngs().length === 2;
if (second) {
// adding layer to the snapping list after a segment is created (two markers needed)
this._otherSnapLayers.push(this._layer);
}

if (this.options.snappable) {
this._cleanupSnapping();
}

// handle tooltip text
Expand Down
3 changes: 3 additions & 0 deletions src/js/Mixins/Snapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ const SnapMixin = {
const results = this._calcLayerDistances(latlng, layer);

// show indicator lines, it's for debugging
if(!this.debugIndicatorLines[index] || !(this.debugIndicatorLines[index] instanceof L.Polyline)){
this.debugIndicatorLines[index] = L.polyline([], { color: 'red', pmIgnore: true });
}
this.debugIndicatorLines[index].setLatLngs([latlng, results.latlng]);

// save the info if it doesn't exist or if the distance is smaller than the previous one
Expand Down