maplibre-gl-geo-editor 0.10.2 → 0.10.4

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.
@@ -16217,6 +16217,60 @@ class HistoryManager {
16217
16217
  function resolveImportedCount(result, fallback) {
16218
16218
  return result?.stats?.success ?? result?.success ?? fallback;
16219
16219
  }
16220
+ const isPolygonFeature = (feature2) => feature2.geometry?.type === "Polygon" || feature2.geometry?.type === "MultiPolygon";
16221
+ const positions = (feature2) => {
16222
+ const polygons = feature2.geometry.type === "Polygon" ? [feature2.geometry.coordinates] : feature2.geometry.coordinates;
16223
+ return polygons.flatMap((polygon2) => polygon2.flatMap((ring) => ring));
16224
+ };
16225
+ const samePosition = (left, right, tolerance) => Math.abs(left[0] - right[0]) <= tolerance && Math.abs(left[1] - right[1]) <= tolerance;
16226
+ function removePolygonOverlaps(feature2, existingFeatures) {
16227
+ if (!isPolygonFeature(feature2)) return null;
16228
+ const masks = existingFeatures.filter(isPolygonFeature);
16229
+ if (masks.length === 0) return feature2;
16230
+ const result = difference2(
16231
+ featureCollection([feature2, ...masks])
16232
+ );
16233
+ if (!result) return null;
16234
+ result.id = feature2.id;
16235
+ result.properties = feature2.properties;
16236
+ return result;
16237
+ }
16238
+ function propagateSharedVertexMoves(oldFeature, newFeature, targetFeatures, tolerance = 1e-10) {
16239
+ if (!isPolygonFeature(oldFeature) || !isPolygonFeature(newFeature)) return [];
16240
+ const before = positions(oldFeature);
16241
+ const after = positions(newFeature);
16242
+ if (before.length !== after.length) return [];
16243
+ const moves = before.flatMap(
16244
+ (from, index) => samePosition(from, after[index], tolerance) ? [] : [{ from, to: after[index] }]
16245
+ );
16246
+ if (moves.length === 0) return [];
16247
+ const changed = [];
16248
+ for (const target of targetFeatures) {
16249
+ if (!isPolygonFeature(target)) continue;
16250
+ let didChange = false;
16251
+ const replace = (position) => {
16252
+ const move = moves.find(
16253
+ ({ from }) => samePosition(position, from, tolerance)
16254
+ );
16255
+ if (!move) return position;
16256
+ didChange = true;
16257
+ return [...move.to];
16258
+ };
16259
+ const geometry = target.geometry.type === "Polygon" ? {
16260
+ ...target.geometry,
16261
+ coordinates: target.geometry.coordinates.map(
16262
+ (ring) => ring.map(replace)
16263
+ )
16264
+ } : {
16265
+ ...target.geometry,
16266
+ coordinates: target.geometry.coordinates.map(
16267
+ (polygon2) => polygon2.map((ring) => ring.map(replace))
16268
+ )
16269
+ };
16270
+ if (didChange) changed.push({ ...target, geometry });
16271
+ }
16272
+ return changed;
16273
+ }
16220
16274
  class CreateFeatureCommand {
16221
16275
  description;
16222
16276
  type = "create";
@@ -16550,7 +16604,11 @@ const ADVANCED_EDIT_MODES = [
16550
16604
  "simplify",
16551
16605
  "lasso"
16552
16606
  ];
16553
- const DEFAULT_HELPER_MODES = ["snapping", "measurements"];
16607
+ const DEFAULT_HELPER_MODES = [
16608
+ "snapping",
16609
+ "topology",
16610
+ "measurements"
16611
+ ];
16554
16612
  const DEFAULT_FILE_MODES = ["open", "save"];
16555
16613
  const DEFAULT_OPTIONS = {
16556
16614
  position: "top-left",
@@ -16563,8 +16621,11 @@ const DEFAULT_OPTIONS = {
16563
16621
  showLabels: false,
16564
16622
  simplifyTolerance: 1e-3,
16565
16623
  snappingEnabled: true,
16624
+ topologyEnabled: false,
16566
16625
  measurementsEnabled: false,
16567
16626
  hideGeomanControl: true,
16627
+ massingHeightProperty: "height",
16628
+ massingDefaultHeight: 10,
16568
16629
  saveFilename: "features.geojson",
16569
16630
  onFeatureCreate: () => {
16570
16631
  },
@@ -18490,6 +18551,10 @@ class GeoEditor {
18490
18551
  pendingOperation = null;
18491
18552
  // Snapping state (independent of other modes)
18492
18553
  snappingEnabled = false;
18554
+ // Topology state (boundary reuse + shared-node editing)
18555
+ topologyEnabled = false;
18556
+ // Prevent geometry updates made by topology handling from being reprocessed.
18557
+ applyingTopology = false;
18493
18558
  // Monotonic token identifying the most recent enableDrawMode() request. Each
18494
18559
  // call bumps it so a tool armed after an async teardown can bail if a newer
18495
18560
  // selection has since superseded it (see enableDrawMode).
@@ -18549,6 +18614,7 @@ class GeoEditor {
18549
18614
  collapsed: this.options.collapsed
18550
18615
  };
18551
18616
  this.snappingEnabled = this.options.snappingEnabled;
18617
+ this.topologyEnabled = this.options.topologyEnabled;
18552
18618
  this.copyFeature = new CopyFeature();
18553
18619
  this.simplifyFeature = new SimplifyFeature({
18554
18620
  tolerance: this.options.simplifyTolerance
@@ -19426,7 +19492,7 @@ class GeoEditor {
19426
19492
  if (mode === "freehand") {
19427
19493
  this.enableFreehandMode();
19428
19494
  } else if (this.geoman) {
19429
- this.geoman.enableDraw(mode);
19495
+ this.geoman.enableDraw(mode === "massing" ? "polygon" : mode);
19430
19496
  setTimeout(() => this.applyVertexMarkerStyles(), 50);
19431
19497
  }
19432
19498
  };
@@ -19592,7 +19658,7 @@ class GeoEditor {
19592
19658
  setupDrawFinishHandlers() {
19593
19659
  if (!this.map) return;
19594
19660
  const handler = (e) => {
19595
- if (this.state.activeDrawMode !== "polygon" && this.state.activeDrawMode !== "line") {
19661
+ if (this.state.activeDrawMode !== "polygon" && this.state.activeDrawMode !== "massing" && this.state.activeDrawMode !== "line") {
19596
19662
  return;
19597
19663
  }
19598
19664
  if (this.finishActiveLineOrPolygonDraw()) {
@@ -19631,7 +19697,7 @@ class GeoEditor {
19631
19697
  * @returns true when a shape was finished, false otherwise.
19632
19698
  */
19633
19699
  finishActiveLineOrPolygonDraw() {
19634
- const mode = this.state.activeDrawMode;
19700
+ const mode = this.state.activeDrawMode === "massing" ? "polygon" : this.state.activeDrawMode;
19635
19701
  if (mode !== "polygon" && mode !== "line") return false;
19636
19702
  if (!this.geoman) return false;
19637
19703
  try {
@@ -20461,6 +20527,10 @@ class GeoEditor {
20461
20527
  * Update Geoman feature properties
20462
20528
  */
20463
20529
  updateFeatureProperties(geomanData, properties) {
20530
+ if (geomanData.updateProperties) {
20531
+ geomanData.updateProperties(properties ?? {});
20532
+ return;
20533
+ }
20464
20534
  const geoJson = geomanData.getGeoJson ? geomanData.getGeoJson() : geomanData.geoJson;
20465
20535
  if (geoJson) {
20466
20536
  geoJson.properties = properties;
@@ -20937,7 +21007,7 @@ class GeoEditor {
20937
21007
  const historyGroup = this.createHistoryToolsGroup();
20938
21008
  toolsWrapper.appendChild(historyGroup);
20939
21009
  }
20940
- if (this.options.helperModes.includes("snapping")) {
21010
+ if (this.options.helperModes.includes("snapping") || this.options.helperModes.includes("topology")) {
20941
21011
  const helperGroup = this.createHelperToolsGroup();
20942
21012
  toolsWrapper.appendChild(helperGroup);
20943
21013
  }
@@ -21067,6 +21137,26 @@ class GeoEditor {
21067
21137
  );
21068
21138
  });
21069
21139
  buttons.appendChild(snappingBtn);
21140
+ if (this.options.helperModes.includes("topology")) {
21141
+ const topologyBtn = document.createElement("button");
21142
+ topologyBtn.className = `${CSS_PREFIX}-tool-button`;
21143
+ topologyBtn.dataset.helper = "topology";
21144
+ topologyBtn.title = "Toggle Topology (shared boundaries and shared-node editing)";
21145
+ topologyBtn.innerHTML = '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M3 3h8v8H3V3zm10 0h8v8h-8V3zM3 13h8v8H3v-8zm10 0h8v8h-8v-8zM9 9h6v6H9V9z" fill="currentColor"/></svg>';
21146
+ topologyBtn.classList.toggle(
21147
+ `${CSS_PREFIX}-tool-button--active`,
21148
+ this.topologyEnabled
21149
+ );
21150
+ topologyBtn.addEventListener("click", (e) => {
21151
+ e.stopPropagation();
21152
+ this.setTopology(!this.topologyEnabled);
21153
+ topologyBtn.classList.toggle(
21154
+ `${CSS_PREFIX}-tool-button--active`,
21155
+ this.topologyEnabled
21156
+ );
21157
+ });
21158
+ buttons.appendChild(topologyBtn);
21159
+ }
21070
21160
  group.appendChild(buttons);
21071
21161
  return group;
21072
21162
  }
@@ -21335,6 +21425,23 @@ class GeoEditor {
21335
21425
  setSnapping(enabled) {
21336
21426
  this.snappingEnabled = enabled;
21337
21427
  this.applySnappingState();
21428
+ this.container?.querySelector('[data-helper="snapping"]')?.classList.toggle(
21429
+ `${CSS_PREFIX}-tool-button--active`,
21430
+ this.snappingEnabled
21431
+ );
21432
+ }
21433
+ isTopologyEnabled() {
21434
+ return this.topologyEnabled;
21435
+ }
21436
+ setTopology(enabled) {
21437
+ this.topologyEnabled = enabled;
21438
+ if (enabled && !this.snappingEnabled) {
21439
+ this.setSnapping(true);
21440
+ }
21441
+ this.container?.querySelector('[data-helper="topology"]')?.classList.toggle(
21442
+ `${CSS_PREFIX}-tool-button--active`,
21443
+ this.topologyEnabled
21444
+ );
21338
21445
  }
21339
21446
  applySnappingState() {
21340
21447
  if (!this.geoman) {
@@ -21455,6 +21562,7 @@ class GeoEditor {
21455
21562
  line: "Line",
21456
21563
  rectangle: "Rectangle",
21457
21564
  polygon: "Polygon",
21565
+ massing: "Building massing",
21458
21566
  freehand: "Freehand",
21459
21567
  // Edit modes
21460
21568
  select: "Select (click features)",
@@ -21479,6 +21587,7 @@ class GeoEditor {
21479
21587
  getModeIcon(mode) {
21480
21588
  const icons = {
21481
21589
  polygon: '<svg viewBox="0 0 24 24" width="18" height="18"><polygon points="12,2 22,8 18,22 6,22 2,8" fill="none" stroke="currentColor" stroke-width="2"/></svg>',
21590
+ massing: '<svg viewBox="0 0 24 24" width="18" height="18"><path d="M4 8l8-4 8 4-8 4-8-4zm0 0v8l8 4 8-4V8M12 12v8" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>',
21482
21591
  line: '<svg viewBox="0 0 24 24" width="18" height="18"><line x1="4" y1="20" x2="20" y2="4" stroke="currentColor" stroke-width="2"/></svg>',
21483
21592
  rectangle: '<svg viewBox="0 0 24 24" width="18" height="18"><rect x="3" y="5" width="18" height="14" fill="none" stroke="currentColor" stroke-width="2"/></svg>',
21484
21593
  circle: '<svg viewBox="0 0 24 24" width="18" height="18"><circle cx="12" cy="12" r="9" fill="none" stroke="currentColor" stroke-width="2"/></svg>',
@@ -21499,7 +21608,7 @@ class GeoEditor {
21499
21608
  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>',
21500
21609
  circle_marker: '<svg viewBox="0 0 24 24" width="18" height="18"><circle cx="12" cy="12" r="4" fill="currentColor"/></svg>',
21501
21610
  ellipse: '<svg viewBox="0 0 24 24" width="18" height="18"><ellipse cx="12" cy="12" rx="10" ry="6" fill="none" stroke="currentColor" stroke-width="2"/></svg>',
21502
- text_marker: '<svg viewBox="0 0 24 24" width="18" height="18"><text x="12" y="16" text-anchor="middle" font-size="14" fill="currentColor">T</text></svg>'
21611
+ text_marker: '<svg viewBox="0 0 24 24" width="18" height="18"><text x="12" y="20" text-anchor="middle" font-size="20" fill="currentColor">T</text></svg>'
21503
21612
  };
21504
21613
  return icons[mode] || `<span>${mode[0].toUpperCase()}</span>`;
21505
21614
  }
@@ -21555,6 +21664,76 @@ class GeoEditor {
21555
21664
  // ============================================================================
21556
21665
  // Geoman Integration
21557
21666
  // ============================================================================
21667
+ /**
21668
+ * Clip a new polygon to the uncovered area. This turns a roughly overlapping
21669
+ * stand boundary into an exact shared boundary with the polygons already in
21670
+ * the editor.
21671
+ */
21672
+ applyTopologyToCreatedFeature(feature2) {
21673
+ if (!this.topologyEnabled || this.applyingTopology || !isPolygonFeature(feature2)) {
21674
+ return feature2;
21675
+ }
21676
+ const featureId2 = this.getGeomanIdFromFeature(feature2);
21677
+ const existing = this.getFeatures().features.filter((candidate) => {
21678
+ if (candidate === feature2) return false;
21679
+ const candidateId = this.getGeomanIdFromFeature(candidate);
21680
+ if (featureId2 && candidateId === featureId2) return false;
21681
+ return JSON.stringify(candidate.geometry) !== JSON.stringify(feature2.geometry);
21682
+ });
21683
+ const topological = removePolygonOverlaps(feature2, existing);
21684
+ const featureData = this.findGeomanDataForFeature(feature2);
21685
+ this.applyingTopology = true;
21686
+ try {
21687
+ if (!topological) {
21688
+ featureData?.delete();
21689
+ return null;
21690
+ }
21691
+ if (JSON.stringify(topological.geometry) !== JSON.stringify(feature2.geometry)) {
21692
+ if (featureData?.updateGeometry) {
21693
+ featureData.updateGeometry(topological.geometry);
21694
+ } else {
21695
+ featureData?.updateGeoJsonGeometry?.(topological.geometry);
21696
+ }
21697
+ }
21698
+ return topological;
21699
+ } finally {
21700
+ this.applyingTopology = false;
21701
+ }
21702
+ }
21703
+ /**
21704
+ * Move matching vertices in adjacent polygons after a shared node is edited.
21705
+ */
21706
+ applyTopologyToEditedFeature(oldFeature, newFeature) {
21707
+ if (!this.topologyEnabled || this.applyingTopology || !isPolygonFeature(oldFeature) || !isPolygonFeature(newFeature)) {
21708
+ return [];
21709
+ }
21710
+ const editedId = this.getGeomanIdFromFeature(newFeature);
21711
+ const targets = this.getFeatures().features.filter((candidate) => {
21712
+ if (candidate === newFeature) return false;
21713
+ const candidateId = this.getGeomanIdFromFeature(candidate);
21714
+ return !(editedId && candidateId === editedId);
21715
+ });
21716
+ const changed = propagateSharedVertexMoves(oldFeature, newFeature, targets);
21717
+ this.applyingTopology = true;
21718
+ const edits = [];
21719
+ try {
21720
+ for (const updated of changed) {
21721
+ const featureData = this.findGeomanDataForFeature(updated);
21722
+ const original = this.getGeomanFeature(featureData);
21723
+ if (!featureData || !original) continue;
21724
+ if (featureData.updateGeometry) {
21725
+ featureData.updateGeometry(updated.geometry);
21726
+ } else {
21727
+ featureData.updateGeoJsonGeometry?.(updated.geometry);
21728
+ }
21729
+ this.options.onFeatureEdit?.(updated, original);
21730
+ edits.push({ oldFeature: original, newFeature: updated });
21731
+ }
21732
+ } finally {
21733
+ this.applyingTopology = false;
21734
+ }
21735
+ return edits;
21736
+ }
21558
21737
  setupGeomanEvents() {
21559
21738
  if (!this.geoman) return;
21560
21739
  this.geoman.setGlobalEventsListener((event) => {
@@ -21564,24 +21743,55 @@ class GeoEditor {
21564
21743
  );
21565
21744
  const eventAction = event.action ?? "";
21566
21745
  if ((eventName === "gm:create" || event.type === "gm:create") && eventFeature) {
21567
- this.lastCreatedFeature = eventFeature;
21568
- this.options.onFeatureCreate?.(eventFeature);
21569
- this.logSelectedFeatureCollection("created", eventFeature);
21570
- this.recordCreateOperation(eventFeature);
21571
- if (this.options.enableAttributeEditing) {
21572
- this.applyDefaultValues(eventFeature);
21746
+ if (this.state.activeDrawMode === "massing" && (eventFeature.geometry.type === "Polygon" || eventFeature.geometry.type === "MultiPolygon")) {
21747
+ eventFeature.properties = {
21748
+ ...eventFeature.properties,
21749
+ [this.options.massingHeightProperty]: this.options.massingDefaultHeight
21750
+ };
21573
21751
  const geomanData = this.findGeomanDataForFeature(eventFeature);
21574
- this.showAttributePanel(eventFeature, geomanData ?? void 0, true);
21752
+ if (geomanData) {
21753
+ this.updateFeatureProperties(geomanData, eventFeature.properties);
21754
+ }
21755
+ }
21756
+ const createdFeature = this.applyTopologyToCreatedFeature(eventFeature);
21757
+ if (!createdFeature) return;
21758
+ this.lastCreatedFeature = createdFeature;
21759
+ this.options.onFeatureCreate?.(createdFeature);
21760
+ this.logSelectedFeatureCollection("created", createdFeature);
21761
+ this.recordCreateOperation(createdFeature);
21762
+ if (this.options.enableAttributeEditing) {
21763
+ this.applyDefaultValues(createdFeature);
21764
+ const geomanData = this.findGeomanDataForFeature(createdFeature);
21765
+ this.showAttributePanel(
21766
+ createdFeature,
21767
+ geomanData ?? void 0,
21768
+ true
21769
+ );
21575
21770
  }
21576
21771
  }
21577
21772
  if (eventAction === "feature_edit_start" && eventFeature) {
21578
21773
  this.pendingEditFeature = clone$1(eventFeature);
21579
21774
  }
21580
21775
  if (eventAction === "feature_edit_end" && eventFeature) {
21776
+ let topologyEdits = [];
21777
+ if (this.pendingEditFeature) {
21778
+ topologyEdits = this.applyTopologyToEditedFeature(
21779
+ this.pendingEditFeature,
21780
+ eventFeature
21781
+ );
21782
+ }
21581
21783
  this.lastEditedFeature = eventFeature;
21582
21784
  this.logSelectedFeatureCollection("edited", eventFeature);
21583
21785
  if (this.pendingEditFeature) {
21584
- this.recordEditOperation(this.pendingEditFeature, eventFeature);
21786
+ if (topologyEdits.length > 0) {
21787
+ this.recordTopologyEditOperation(
21788
+ this.pendingEditFeature,
21789
+ eventFeature,
21790
+ topologyEdits
21791
+ );
21792
+ } else {
21793
+ this.recordEditOperation(this.pendingEditFeature, eventFeature);
21794
+ }
21585
21795
  this.pendingEditFeature = null;
21586
21796
  }
21587
21797
  }
@@ -21693,6 +21903,26 @@ class GeoEditor {
21693
21903
  const command = new EditFeatureCommand(oldFeature, newFeature, context);
21694
21904
  this.historyManager.record(command);
21695
21905
  }
21906
+ /**
21907
+ * Record the edited polygon and every adjacent polygon changed through a
21908
+ * shared node as one undoable operation.
21909
+ */
21910
+ recordTopologyEditOperation(oldFeature, newFeature, propagatedEdits) {
21911
+ if (!this.historyManager || this.historyManager.isExecutingCommand() || this.isPerformingCompositeOperation) {
21912
+ return;
21913
+ }
21914
+ const context = this.getCommandContext();
21915
+ if (!context) return;
21916
+ const commands = [
21917
+ new EditFeatureCommand(oldFeature, newFeature, context),
21918
+ ...propagatedEdits.map(
21919
+ (edit) => new EditFeatureCommand(edit.oldFeature, edit.newFeature, context)
21920
+ )
21921
+ ];
21922
+ this.historyManager.record(
21923
+ new CompositeCommand(commands, "Edit shared polygon nodes")
21924
+ );
21925
+ }
21696
21926
  /**
21697
21927
  * Record a delete operation in history.
21698
21928
  */
@@ -22116,4 +22346,4 @@ exports.safeUnion = safeUnion;
22116
22346
  exports.selectFeaturesWithinLasso = selectFeaturesWithinLasso;
22117
22347
  exports.toSelectedFeatures = toSelectedFeatures;
22118
22348
  exports.toggleInSelection = toggleInSelection;
22119
- //# sourceMappingURL=geo-editor-BP0sEowl.cjs.map
22349
+ //# sourceMappingURL=geo-editor-C6-E5RN2.cjs.map