Skip to content

Commit

Permalink
Allow removal of holes in polygons while maintaining the minimum vert…
Browse files Browse the repository at this point in the history
…ex count of the geometry (#1475) (patch)

* Allow removal of holes in polygons while maintaining the minimum vertex count of the geometry

* Hotfix

* Add removeLayerBelowMinVertexCount tests

---------

Co-authored-by: Florian Bischof <[email protected]>
  • Loading branch information
Tobikblom and Falke-Design authored Nov 24, 2024
1 parent d7ea514 commit 71b83e5
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 2 deletions.
28 changes: 28 additions & 0 deletions cypress/e2e/line.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,32 @@ describe('Draw & Edit Line', () => {
// draw a line
cy.get(mapSelector).click(150, 250);
});

it('prevents removal of the layer if the vertex count is below minimum (removeLayerBelowMinVertexCount)', () => {
cy.window().then(({ map }) => {
map.pm.setGlobalOptions({ removeLayerBelowMinVertexCount: false });
});

// activate polyline drawing
cy.toolbarButton('polyline')
.click()
.closest('.button-container')
.should('have.class', 'active');

// draw a polyline
cy.get(mapSelector).click(90, 250).click(150, 50).click(150, 50);

// enable global edit mode
cy.toolbarButton('edit')
.click()
.closest('.button-container')
.should('have.class', 'active');

// let's remove one vertex
cy.get('.marker-icon:not(.marker-icon-middle)')
.last()
.trigger('contextmenu');

cy.hasVertexMarkers(2);
});
});
86 changes: 86 additions & 0 deletions cypress/e2e/polygon.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1326,4 +1326,90 @@ describe('Draw & Edit Poly', () => {
expect(hintMarker.getLatLng().lng).to.eq(72.22334801861803);
});
});

it('prevents removal of the layer if the vertex count is below minimum (removeLayerBelowMinVertexCount)', () => {
cy.window().then(({ map }) => {
map.pm.setGlobalOptions({ removeLayerBelowMinVertexCount: false });
});

// activate polygon drawing
cy.toolbarButton('polygon')
.click()
.closest('.button-container')
.should('have.class', 'active');

// draw a polygon
cy.get(mapSelector)
.click(90, 250)
.click(150, 50)
.click(500, 50)
.click(90, 250);

cy.hasLayers(3);

// enable global edit mode
cy.toolbarButton('edit')
.click()
.closest('.button-container')
.should('have.class', 'active');

// let's remove one vertex
cy.get('.marker-icon:not(.marker-icon-middle)')
.last()
.trigger('contextmenu');

cy.hasVertexMarkers(3);
});

it('allows to remove the hole when removeLayerBelowMinVertexCount is false', () => {
cy.window().then(({ map }) => {
map.pm.setGlobalOptions({ removeLayerBelowMinVertexCount: false });
});

// activate polygon drawing
cy.toolbarButton('polygon')
.click()
.closest('.button-container')
.should('have.class', 'active');

// draw a polygon
cy.get(mapSelector)
.click(90, 250)
.click(150, 50)
.click(500, 50)
.click(90, 250);

// activate cutting drawing
cy.toolbarButton('cut')
.click()
.closest('.button-container')
.should('have.class', 'active');

// draw a polygon to cut
cy.get(mapSelector)
.click(170, 80)
.click(320, 80)
.click(170, 170)
.click(170, 80);

cy.hasLayers(3);

// enable global edit mode
cy.toolbarButton('edit')
.click()
.closest('.button-container')
.should('have.class', 'active');

cy.hasVertexMarkers(6);

// let's remove a vertex of the hole
cy.get(mapSelector).trigger('contextmenu', 170, 170);

cy.hasVertexMarkers(3);

// let's remove a vertex of the hole
cy.get(mapSelector).trigger('contextmenu', 90, 250);

cy.hasVertexMarkers(3);
});
});
8 changes: 6 additions & 2 deletions src/js/Edit/L.PM.Edit.Line.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,12 @@ Edit.Line = Edit.extend({
let markerArr =
indexPath.length > 1 ? get(this._markers, parentPath) : this._markers;

// prevent removal of the layer if the vertex count is below minimum
if (!this.options.removeLayerBelowMinVertexCount) {
// define whether marker is part of hole
const isHole =
parentPath[parentPath.length - 1] > 0 && this._layer instanceof L.Polygon;

// prevent removal of the layer if the vertex count is below minimum when not a hole
if (!this.options.removeLayerBelowMinVertexCount && !isHole) {
// if on a line only 2 vertices left or on a polygon 3 vertices left, don't allow to delete
if (
coordsRing.length <= 2 ||
Expand Down

0 comments on commit 71b83e5

Please sign in to comment.