maplibre-gl-geo-editor 0.11.1 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,6 +37,7 @@ A powerful MapLibre GL plugin for creating and editing geometries. Extends the f
37
37
  - **Union** - Merge multiple polygons into one
38
38
  - **Difference** - Subtract one polygon from another
39
39
  - **Simplify** - Reduce vertices using Douglas-Peucker algorithm
40
+ - **Delete Ring** - Click inside a polygon hole to remove that interior ring
40
41
  - **Lasso** - Select multiple features by drawing a polygon (supports union/difference/drag)
41
42
  - **Reset** - Clear selection and disable active tools (toolbar button)
42
43
 
@@ -242,7 +243,7 @@ map.on('load', () => {
242
243
  drawModes: ['polygon', 'line', 'rectangle', 'circle', 'marker', 'freehand'],
243
244
  editModes: [
244
245
  'select', 'drag', 'change', 'rotate', 'cut', 'delete',
245
- 'scale', 'copy', 'split', 'union', 'difference', 'simplify', 'lasso'
246
+ 'scale', 'copy', 'split', 'union', 'difference', 'simplify', 'delete_ring', 'lasso'
246
247
  ],
247
248
  showFeatureProperties: true, // Show popup with properties on selection
248
249
  fitBoundsOnLoad: true, // Auto-zoom to extent when loading GeoJSON
@@ -516,6 +517,10 @@ map.getContainer().addEventListener('gm:simplify', (e) => {
516
517
  console.log('Simplify result:', e.detail);
517
518
  });
518
519
 
520
+ map.getContainer().addEventListener('gm:deletering', (e) => {
521
+ console.log('Removed ring:', e.detail.ringIndex, e.detail.result);
522
+ });
523
+
519
524
  map.getContainer().addEventListener('gm:lassoend', (e) => {
520
525
  console.log('Lasso selection:', e.detail);
521
526
  });
@@ -196,6 +196,24 @@ function getCoords(coords) {
196
196
  "coords must be GeoJSON Feature, Geometry Object or an Array"
197
197
  );
198
198
  }
199
+ function featureOf(feature2, type, name) {
200
+ if (!feature2) {
201
+ throw new Error("No feature passed");
202
+ }
203
+ if (!name) {
204
+ throw new Error(".featureOf() requires a name");
205
+ }
206
+ if (!feature2 || feature2.type !== "Feature" || !feature2.geometry) {
207
+ throw new Error(
208
+ "Invalid input to " + name + ", Feature with geometry required"
209
+ );
210
+ }
211
+ if (!feature2.geometry || feature2.geometry.type !== type) {
212
+ throw new Error(
213
+ "Invalid input to " + name + ": must be a " + type + ", given " + feature2.geometry.type
214
+ );
215
+ }
216
+ }
199
217
  function getGeom(geojson) {
200
218
  if (geojson.type === "Feature") {
201
219
  return geojson.geometry;
@@ -2263,7 +2281,7 @@ function nearestPointOnLine(lines, inputPoint, options = {}) {
2263
2281
  );
2264
2282
  return closestPt;
2265
2283
  }
2266
- function dot(v1, v2) {
2284
+ function dot$1(v1, v2) {
2267
2285
  const [v1x, v1y, v1z] = v1;
2268
2286
  const [v2x, v2y, v2z] = v2;
2269
2287
  return v1x * v2x + v1y * v2y + v1z * v2z;
@@ -2302,7 +2320,7 @@ function nearestPointOnSegment(posA, posB, posC) {
2302
2320
  const C = lngLatToVector(posC);
2303
2321
  const segmentAxis = cross(A, B2);
2304
2322
  if (segmentAxis[0] === 0 && segmentAxis[1] === 0 && segmentAxis[2] === 0) {
2305
- if (dot(A, B2) > 0) {
2323
+ if (dot$1(A, B2) > 0) {
2306
2324
  return [[...posB], true];
2307
2325
  } else {
2308
2326
  return [[...posC], false];
@@ -2315,14 +2333,14 @@ function nearestPointOnSegment(posA, posB, posC) {
2315
2333
  const intersectionAxis = cross(targetAxis, segmentAxis);
2316
2334
  const I1 = normalize(intersectionAxis);
2317
2335
  const I2 = [-I1[0], -I1[1], -I1[2]];
2318
- const I = dot(C, I1) > dot(C, I2) ? I1 : I2;
2336
+ const I = dot$1(C, I1) > dot$1(C, I2) ? I1 : I2;
2319
2337
  const segmentAxisNorm = normalize(segmentAxis);
2320
- const cmpAI = dot(cross(A, I), segmentAxisNorm);
2321
- const cmpIB = dot(cross(I, B2), segmentAxisNorm);
2338
+ const cmpAI = dot$1(cross(A, I), segmentAxisNorm);
2339
+ const cmpIB = dot$1(cross(I, B2), segmentAxisNorm);
2322
2340
  if (cmpAI >= 0 && cmpIB >= 0) {
2323
2341
  return [vectorToLngLat(I), false];
2324
2342
  }
2325
- if (dot(A, C) > dot(B2, C)) {
2343
+ if (dot$1(A, C) > dot$1(B2, C)) {
2326
2344
  return [[...posA], false];
2327
2345
  } else {
2328
2346
  return [[...posB], true];
@@ -15257,6 +15275,68 @@ function length(geojson, options = {}) {
15257
15275
  0
15258
15276
  );
15259
15277
  }
15278
+ function pointToLineDistance(pt, line, options = {}) {
15279
+ var _a, _b;
15280
+ const method = (_a = options.method) != null ? _a : "geodesic";
15281
+ const units = (_b = options.units) != null ? _b : "kilometers";
15282
+ if (!pt) {
15283
+ throw new Error("pt is required");
15284
+ }
15285
+ if (Array.isArray(pt)) {
15286
+ pt = point(pt);
15287
+ } else if (pt.type === "Point") {
15288
+ pt = feature(pt);
15289
+ } else {
15290
+ featureOf(pt, "Point", "point");
15291
+ }
15292
+ if (!line) {
15293
+ throw new Error("line is required");
15294
+ }
15295
+ if (Array.isArray(line)) {
15296
+ line = lineString(line);
15297
+ } else if (line.type === "LineString") {
15298
+ line = feature(line);
15299
+ } else {
15300
+ featureOf(line, "LineString", "line");
15301
+ }
15302
+ let distance2 = Infinity;
15303
+ const p = pt.geometry.coordinates;
15304
+ segmentEach(line, (segment) => {
15305
+ if (segment) {
15306
+ const a = segment.geometry.coordinates[0];
15307
+ const b = segment.geometry.coordinates[1];
15308
+ const d = distanceToSegment(p, a, b, { method });
15309
+ if (d < distance2) {
15310
+ distance2 = d;
15311
+ }
15312
+ }
15313
+ });
15314
+ return convertLength(distance2, "degrees", units);
15315
+ }
15316
+ function distanceToSegment(p, a, b, options) {
15317
+ if (options.method === "geodesic") {
15318
+ const nearest = nearestPointOnLine(lineString([a, b]).geometry, p, {
15319
+ units: "degrees"
15320
+ });
15321
+ return nearest.properties.dist;
15322
+ }
15323
+ const v = [b[0] - a[0], b[1] - a[1]];
15324
+ const w = [p[0] - a[0], p[1] - a[1]];
15325
+ const c1 = dot(w, v);
15326
+ if (c1 <= 0) {
15327
+ return rhumbDistance(p, a, { units: "degrees" });
15328
+ }
15329
+ const c2 = dot(v, v);
15330
+ if (c2 <= c1) {
15331
+ return rhumbDistance(p, b, { units: "degrees" });
15332
+ }
15333
+ const b2 = c1 / c2;
15334
+ const Pb = [a[0] + b2 * v[0], a[1] + b2 * v[1]];
15335
+ return rhumbDistance(p, Pb, { units: "degrees" });
15336
+ }
15337
+ function dot(u2, v) {
15338
+ return u2[0] * v[0] + u2[1] * v[1];
15339
+ }
15260
15340
  function transformScale(geojson, factor, options) {
15261
15341
  options = options || {};
15262
15342
  if (!isObject$1(options)) throw new Error("options is invalid");
@@ -16743,6 +16823,7 @@ const DEFAULT_EDIT_MODES = [
16743
16823
  "union",
16744
16824
  "difference",
16745
16825
  "simplify",
16826
+ "delete_ring",
16746
16827
  "lasso"
16747
16828
  ];
16748
16829
  const ADVANCED_EDIT_MODES = [
@@ -16753,6 +16834,7 @@ const ADVANCED_EDIT_MODES = [
16753
16834
  "union",
16754
16835
  "difference",
16755
16836
  "simplify",
16837
+ "delete_ring",
16756
16838
  "lasso"
16757
16839
  ];
16758
16840
  const DEFAULT_HELPER_MODES = [
@@ -18577,6 +18659,122 @@ class FreehandFeature {
18577
18659
  this.map = null;
18578
18660
  }
18579
18661
  }
18662
+ class DeleteRingFeature {
18663
+ /**
18664
+ * Find the interior ring of a feature that a clicked point targets.
18665
+ *
18666
+ * A ring is targeted when the point falls inside it (that is, inside the
18667
+ * hole, and not on a filled part of the feature such as an island inside
18668
+ * that hole) or lies within `toleranceKm` of its boundary. When several rings
18669
+ * qualify, the smallest one wins so the innermost hole is picked.
18670
+ *
18671
+ * @param feature - A Polygon or MultiPolygon feature.
18672
+ * @param point - The clicked position as `[lng, lat]`.
18673
+ * @param toleranceKm - Distance from a ring's boundary that still counts as
18674
+ * a hit, in kilometers. Defaults to 0 (only clicks inside the hole).
18675
+ * @returns The ring's location, or null if no interior ring is targeted.
18676
+ */
18677
+ findRingAt(feature2, point$1, toleranceKm = 0) {
18678
+ const polygons = getPolygonParts(feature2);
18679
+ if (!polygons) return null;
18680
+ const clicked = point([point$1[0], point$1[1]]);
18681
+ let onFill = false;
18682
+ try {
18683
+ onFill = booleanPointInPolygon(
18684
+ clicked,
18685
+ feature2
18686
+ );
18687
+ } catch {
18688
+ onFill = false;
18689
+ }
18690
+ let best = null;
18691
+ polygons.forEach((rings, polygonIndex) => {
18692
+ for (let ringIndex = 1; ringIndex < rings.length; ringIndex++) {
18693
+ const ring = rings[ringIndex];
18694
+ if (!isValidRing(ring)) continue;
18695
+ let hit = false;
18696
+ try {
18697
+ const hole = polygon([ring]);
18698
+ hit = !onFill && booleanPointInPolygon(clicked, hole);
18699
+ if (!hit && toleranceKm > 0) {
18700
+ const distance2 = pointToLineDistance(
18701
+ clicked,
18702
+ lineString(ring),
18703
+ { units: "kilometers" }
18704
+ );
18705
+ hit = distance2 <= toleranceKm;
18706
+ }
18707
+ } catch {
18708
+ continue;
18709
+ }
18710
+ if (!hit) continue;
18711
+ const area2 = Math.abs(ringPlanarArea(ring));
18712
+ if (!best || area2 < best.area) {
18713
+ best = { polygonIndex, ringIndex, area: area2 };
18714
+ }
18715
+ }
18716
+ });
18717
+ return best;
18718
+ }
18719
+ /**
18720
+ * Remove one interior ring from a feature.
18721
+ *
18722
+ * @param feature - A Polygon or MultiPolygon feature.
18723
+ * @param location - Which ring to remove, as returned by `findRingAt`.
18724
+ * @returns A new feature without that ring (the input is left unchanged),
18725
+ * or null if the location does not point at an interior ring.
18726
+ */
18727
+ deleteRing(feature2, location) {
18728
+ const polygons = getPolygonParts(feature2);
18729
+ if (!polygons) return null;
18730
+ const { polygonIndex, ringIndex } = location;
18731
+ const rings = polygons[polygonIndex];
18732
+ if (!rings || ringIndex < 1 || ringIndex >= rings.length) return null;
18733
+ const nextPolygons = polygons.map(
18734
+ (part, index) => index === polygonIndex ? part.filter((_, i) => i !== ringIndex).map(cloneRing) : part.map(cloneRing)
18735
+ );
18736
+ const geometry = feature2.geometry.type === "Polygon" ? { type: "Polygon", coordinates: nextPolygons[0] } : { type: "MultiPolygon", coordinates: nextPolygons };
18737
+ return {
18738
+ ...feature2,
18739
+ properties: feature2.properties ? { ...feature2.properties } : {},
18740
+ geometry
18741
+ };
18742
+ }
18743
+ /**
18744
+ * Count the interior rings (holes) of a feature.
18745
+ *
18746
+ * @param feature - Any feature.
18747
+ * @returns The number of holes; 0 for non-polygon geometries.
18748
+ */
18749
+ countRings(feature2) {
18750
+ const polygons = getPolygonParts(feature2);
18751
+ if (!polygons) return 0;
18752
+ return polygons.reduce(
18753
+ (total, rings) => total + Math.max(0, rings.length - 1),
18754
+ 0
18755
+ );
18756
+ }
18757
+ }
18758
+ function getPolygonParts(feature2) {
18759
+ const geometry = feature2?.geometry;
18760
+ if (!geometry) return null;
18761
+ if (geometry.type === "Polygon") return [geometry.coordinates];
18762
+ if (geometry.type === "MultiPolygon") return geometry.coordinates;
18763
+ return null;
18764
+ }
18765
+ function isValidRing(ring) {
18766
+ return Array.isArray(ring) && ring.length >= 4;
18767
+ }
18768
+ function cloneRing(ring) {
18769
+ return ring.map((position) => [...position]);
18770
+ }
18771
+ function ringPlanarArea(ring) {
18772
+ let sum2 = 0;
18773
+ for (let i = 0; i < ring.length - 1; i++) {
18774
+ sum2 += ring[i][0] * ring[i + 1][1] - ring[i + 1][0] * ring[i][1];
18775
+ }
18776
+ return sum2 / 2;
18777
+ }
18580
18778
  function selectFeaturesWithinLasso(features, lasso, mode = "intersects") {
18581
18779
  return features.filter((feature2) => {
18582
18780
  try {
@@ -18694,6 +18892,7 @@ class GeoEditor {
18694
18892
  lassoFeature;
18695
18893
  splitFeature;
18696
18894
  freehandFeature;
18895
+ deleteRingFeature;
18697
18896
  // Event listeners
18698
18897
  boundKeyHandler = null;
18699
18898
  boundClickHandler = null;
@@ -18787,6 +18986,7 @@ class GeoEditor {
18787
18986
  this.lassoFeature = new LassoFeature();
18788
18987
  this.splitFeature = new SplitFeature();
18789
18988
  this.freehandFeature = new FreehandFeature();
18989
+ this.deleteRingFeature = new DeleteRingFeature();
18790
18990
  if (this.options.enableHistory !== false) {
18791
18991
  this.historyManager = new HistoryManager(
18792
18992
  this.options.maxHistorySize,
@@ -18920,6 +19120,10 @@ class GeoEditor {
18920
19120
  */
18921
19121
  setupSelectionHandler() {
18922
19122
  this.boundClickHandler = (e) => {
19123
+ if (this.state.activeEditMode === "delete_ring") {
19124
+ this.deleteRingAt(e.lngLat);
19125
+ return;
19126
+ }
18923
19127
  if (!this.isSelectMode && !this.pendingOperation) {
18924
19128
  return;
18925
19129
  }
@@ -19947,11 +20151,78 @@ class GeoEditor {
19947
20151
  case "simplify":
19948
20152
  this.executeSimplify();
19949
20153
  break;
20154
+ case "delete_ring":
20155
+ this.enableDeleteRingMode();
20156
+ break;
19950
20157
  case "lasso":
19951
20158
  this.enableLassoMode();
19952
20159
  break;
19953
20160
  }
19954
20161
  }
20162
+ /**
20163
+ * Enable delete-ring mode: each click inside a polygon hole removes it.
20164
+ */
20165
+ enableDeleteRingMode() {
20166
+ this.map.getCanvas().style.cursor = "crosshair";
20167
+ }
20168
+ /**
20169
+ * Remove the interior ring (hole) under a click, if any. The innermost hole
20170
+ * containing the click wins; a click just on a hole's edge also counts.
20171
+ *
20172
+ * @param lngLat - The clicked map position.
20173
+ * @returns true when a ring was removed.
20174
+ */
20175
+ deleteRingAt(lngLat) {
20176
+ if (!this.geoman) return false;
20177
+ const point2 = [lngLat.lng, lngLat.lat];
20178
+ const toleranceKm = this.getClickToleranceKm();
20179
+ let hit = null;
20180
+ try {
20181
+ this.geoman.features.forEach((fd) => {
20182
+ const feature2 = fd ? this.getGeomanFeature(fd) : null;
20183
+ if (!feature2?.geometry) return;
20184
+ const ring = this.deleteRingFeature.findRingAt(
20185
+ feature2,
20186
+ point2,
20187
+ toleranceKm
20188
+ );
20189
+ if (ring && (!hit || ring.area < hit.area)) {
20190
+ hit = { feature: feature2, geomanData: fd, ...ring };
20191
+ }
20192
+ });
20193
+ } catch {
20194
+ return false;
20195
+ }
20196
+ if (!hit) return false;
20197
+ const { feature: original, geomanData, polygonIndex, ringIndex } = hit;
20198
+ const updated = this.deleteRingFeature.deleteRing(original, {
20199
+ polygonIndex,
20200
+ ringIndex
20201
+ });
20202
+ if (!updated) return false;
20203
+ if (geomanData.updateGeometry) {
20204
+ geomanData.updateGeometry(updated.geometry);
20205
+ } else if (geomanData.updateGeoJsonGeometry) {
20206
+ geomanData.updateGeoJsonGeometry(updated.geometry);
20207
+ } else {
20208
+ return false;
20209
+ }
20210
+ const updatedId = this.getGeomanIdFromFeature(original);
20211
+ this.state.selectedFeatures = this.state.selectedFeatures.map(
20212
+ (sel) => updatedId && this.getGeomanIdFromFeature(sel.feature) === updatedId ? { ...sel, feature: updated, geomanData } : sel
20213
+ );
20214
+ this.updateSelectionHighlight();
20215
+ this.recordEditOperation(original, updated);
20216
+ this.options.onFeatureEdit?.(updated, original);
20217
+ this.lastEditedFeature = updated;
20218
+ this.emitEvent("gm:deletering", {
20219
+ result: updated,
20220
+ original,
20221
+ polygonIndex,
20222
+ ringIndex
20223
+ });
20224
+ return true;
20225
+ }
19955
20226
  /**
19956
20227
  * Enable union mode (interactive polygon selection)
19957
20228
  */
@@ -21748,6 +22019,7 @@ class GeoEditor {
21748
22019
  union: "Union (select 2+ polygons)",
21749
22020
  difference: "Difference (select 2+ polygons)",
21750
22021
  simplify: "Simplify",
22022
+ delete_ring: "Delete Ring (click a hole)",
21751
22023
  lasso: "Lasso Select"
21752
22024
  };
21753
22025
  return labels[mode] || mode;
@@ -21775,6 +22047,7 @@ class GeoEditor {
21775
22047
  union: '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M4 4h7v7H4V4zm9 0h7v7h-7V4zm-9 9h7v7H4v-7zm9 0h7v7h-7v-7z" fill="currentColor"/></svg>',
21776
22048
  difference: '<svg viewBox="0 0 24 24" width="18" height="18"><rect x="4" y="4" width="10" height="10" fill="none" stroke="currentColor" stroke-width="2"/><rect x="10" y="10" width="10" height="10" fill="none" stroke="currentColor" stroke-width="2"/><path d="M13 7h6v2h-6z" fill="currentColor"/></svg>',
21777
22049
  simplify: '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M4 17l5-5 3 3 6-6" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M5 6h14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
22050
+ delete_ring: '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M3 3h18v18H3z" fill="none" stroke="currentColor" stroke-width="2"/><path d="M8 8h8v8H8z" fill="none" stroke="currentColor" stroke-width="2" stroke-dasharray="2 2"/><path d="M10 10l4 4m0-4l-4 4" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>',
21778
22051
  lasso: '<svg viewBox="0 0 24 24" width="18" height="18"><ellipse cx="12" cy="10" rx="8" ry="6" fill="none" stroke="currentColor" stroke-width="2" stroke-dasharray="4 2"/><circle cx="12" cy="18" r="3" fill="currentColor"/></svg>',
21779
22052
  freehand: '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M3 17.25V21h3.75L17.81 9.94l-3.75-3.75L3 17.25z" fill="none" stroke="currentColor" stroke-width="2"/></svg>',
21780
22053
  circle_marker: '<svg viewBox="0 0 24 24" width="18" height="18"><circle cx="12" cy="12" r="4" fill="currentColor"/></svg>',
@@ -21788,7 +22061,9 @@ class GeoEditor {
21788
22061
  // ============================================================================
21789
22062
  setupKeyboardShortcuts() {
21790
22063
  this.boundKeyHandler = (e) => {
21791
- const isInputField = e.target instanceof HTMLInputElement || e.target instanceof HTMLTextAreaElement || e.target instanceof HTMLElement && e.target.isContentEditable;
22064
+ const target = e.composedPath()[0] ?? e.target;
22065
+ const isInputField = target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement || target instanceof HTMLElement && target.isContentEditable;
22066
+ if (isInputField) return;
21792
22067
  if ((e.ctrlKey || e.metaKey) && e.key === "z" && !e.shiftKey) {
21793
22068
  this.undo();
21794
22069
  e.preventDefault();
@@ -21807,7 +22082,7 @@ class GeoEditor {
21807
22082
  this.pasteFeatures();
21808
22083
  e.preventDefault();
21809
22084
  }
21810
- if ((e.key === "Delete" || e.key === "Backspace") && !isInputField && !(e.target instanceof Element && e.target.closest(`.${CSS_PREFIX}-attribute-panel`))) {
22085
+ if ((e.key === "Delete" || e.key === "Backspace") && !(e.target instanceof Element && e.target.closest(`.${CSS_PREFIX}-attribute-panel`))) {
21811
22086
  this.deleteSelectedFeatures();
21812
22087
  e.preventDefault();
21813
22088
  }
@@ -22508,6 +22783,7 @@ exports.DEFAULT_DRAW_MODES = DEFAULT_DRAW_MODES;
22508
22783
  exports.DEFAULT_EDIT_MODES = DEFAULT_EDIT_MODES;
22509
22784
  exports.DEFAULT_FILE_MODES = DEFAULT_FILE_MODES;
22510
22785
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
22786
+ exports.DeleteRingFeature = DeleteRingFeature;
22511
22787
  exports.DifferenceFeature = DifferenceFeature;
22512
22788
  exports.GeoEditor = GeoEditor;
22513
22789
  exports.GeoEditorLayerAdapter = GeoEditorLayerAdapter;
@@ -22572,4 +22848,4 @@ exports.safeUnion = safeUnion;
22572
22848
  exports.selectFeaturesWithinLasso = selectFeaturesWithinLasso;
22573
22849
  exports.toSelectedFeatures = toSelectedFeatures;
22574
22850
  exports.toggleInSelection = toggleInSelection;
22575
- //# sourceMappingURL=geo-editor-DgXTVVbG.cjs.map
22851
+ //# sourceMappingURL=geo-editor-CnG2QJsG.cjs.map