Skip to content

Commit

Permalink
A tiny enhancement to cut holes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
mstahv committed Feb 26, 2024
1 parent 63d108d commit 58607a1
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/org/vaadin/addons/maplibre/PolygonField.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryCollection;
import org.locationtech.jts.geom.Polygon;
Expand All @@ -21,6 +22,7 @@ public PolygonField() {
cutHole.setVisible(false);
cutHole.addThemeVariants(ButtonVariant.LUMO_SMALL, ButtonVariant.LUMO_TERTIARY);
cutHole.addClassName("maplibre-cut-hole");
cutHole.setTooltipText("Cut a new whole to the current polygon.");
cutHole.addClickListener(e -> {
drawControl.setMode(DrawControl.DrawMode.DRAW_POLYGON);
cuttingHole = true;
Expand All @@ -38,9 +40,13 @@ public PolygonField withStyleUrl(String styleUrl) {
this.drawControl.addGeometryChangeListener(e -> {
GeometryCollection geom = e.getGeom();
if(cuttingHole) {
Geometry hole = geom.getGeometryN(1);
polygon = (Polygon) geom.getGeometryN(0).symDifference(hole);
cuttingHole = false;
try {
Geometry hole = geom.getGeometryN(1);
polygon = (Polygon) geom.getGeometryN(0).symDifference(hole);
} catch (Exception ex) {
Notification.show("Cutting hole failed, make sure it is withing the existing Polygon and doesn't conflict other holes.");
}
drawControl.setGeometry(polygon);
} else {
polygon = (Polygon) geom.getGeometryN(0);
Expand Down

0 comments on commit 58607a1

Please sign in to comment.