terra-draw 0.0.1-alpha.4 → 0.0.1-alpha.6
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/dist/adapters/google-maps.adapter.d.ts +2 -1
- package/dist/adapters/leaflet.adapter.d.ts +3 -5
- package/dist/adapters/mapbox-gl.adapter.d.ts +2 -1
- package/dist/common.d.ts +17 -18
- package/dist/modes/base.mode.d.ts +11 -8
- package/dist/modes/circle/circle.mode.d.ts +11 -3
- package/dist/modes/freehand/freehand.mode.d.ts +11 -3
- package/dist/modes/linestring/linestring.mode.d.ts +9 -3
- package/dist/modes/point/point.mode.d.ts +11 -3
- package/dist/modes/polygon/behaviors/closing-points.behavior.d.ts +19 -0
- package/dist/modes/polygon/polygon.mode.d.ts +16 -4
- package/dist/modes/render/render.mode.d.ts +23 -0
- package/dist/modes/select/select.mode.d.ts +18 -2
- package/dist/modes/static/static.mode.d.ts +18 -1
- package/dist/terra-draw.cjs +1 -1
- package/dist/terra-draw.cjs.map +1 -1
- package/dist/terra-draw.d.ts +3 -2
- package/dist/terra-draw.modern.js +1 -1
- package/dist/terra-draw.modern.js.map +1 -1
- package/dist/terra-draw.module.js +1 -1
- package/dist/terra-draw.module.js.map +1 -1
- package/dist/terra-draw.umd.js +1 -1
- package/dist/terra-draw.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/terra-draw.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ import { TerraDrawFreehandMode } from "./modes/freehand/freehand.mode";
|
|
|
7
7
|
import { TerraDrawLineStringMode } from "./modes/linestring/linestring.mode";
|
|
8
8
|
import { TerraDrawPointMode } from "./modes/point/point.mode";
|
|
9
9
|
import { TerraDrawPolygonMode } from "./modes/polygon/polygon.mode";
|
|
10
|
+
import { TerraDrawRenderMode } from "./modes/render/render.mode";
|
|
10
11
|
import { TerraDrawSelectMode } from "./modes/select/select.mode";
|
|
11
12
|
import { GeoJSONStoreFeatures } from "./store/store";
|
|
12
13
|
declare type ChangeListener = (ids: string[], type: string) => void;
|
|
@@ -33,7 +34,7 @@ declare class TerraDraw {
|
|
|
33
34
|
data?: GeoJSONStoreFeatures[];
|
|
34
35
|
});
|
|
35
36
|
private getModeStyles;
|
|
36
|
-
|
|
37
|
+
setModeStyles(mode: string, styles: TerraDrawAdapterStyling): void;
|
|
37
38
|
getSnapshot(): GeoJSONStoreFeatures[];
|
|
38
39
|
get enabled(): boolean;
|
|
39
40
|
set enabled(_: boolean);
|
|
@@ -44,4 +45,4 @@ declare class TerraDraw {
|
|
|
44
45
|
on<T extends TerraDrawEvents>(event: T, callback: TerraDrawEventListeners[T]): void;
|
|
45
46
|
off<T extends TerraDrawEvents>(event: TerraDrawEvents, callback: TerraDrawEventListeners[T]): void;
|
|
46
47
|
}
|
|
47
|
-
export { TerraDraw, TerraDrawSelectMode, TerraDrawPointMode, TerraDrawLineStringMode, TerraDrawPolygonMode, TerraDrawCircleMode, TerraDrawFreehandMode, TerraDrawGoogleMapsAdapter, TerraDrawMapboxGLAdapter, TerraDrawLeafletAdapter, };
|
|
48
|
+
export { TerraDraw, TerraDrawSelectMode, TerraDrawPointMode, TerraDrawLineStringMode, TerraDrawPolygonMode, TerraDrawCircleMode, TerraDrawFreehandMode, TerraDrawRenderMode, TerraDrawGoogleMapsAdapter, TerraDrawMapboxGLAdapter, TerraDrawLeafletAdapter, };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(this,arguments)}function e(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}class i{constructor(t){this._heldKeys=new Set,this._cursor=void 0,this._cursorStyleSheet=void 0,this._coordinatePrecision=void 0,this._lib=void 0,this._map=void 0,this._onMouseMoveListener=void 0,this._onMouseMoveCallback=void 0,this._onClickListener=void 0,this._onRightClickListener=void 0,this._onClickCallback=void 0,this._onKeyUpListener=void 0,this._onDragStartListener=void 0,this._onDragListener=void 0,this._onDragEndListener=void 0,this._layers=!1,this.getMapContainer=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this._lib=t.lib,this._map=t.map,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9,this.getMapContainer=()=>this._map.getDiv(),this.project=(t,e)=>{const i=this._map.getBounds();if(void 0===i)throw new Error("cannot get bounds");const o=new this._lib.LatLng(i.getNorthEast().lat(),i.getSouthWest().lng()),n=this._map.getProjection();if(void 0===n)throw new Error("cannot get projection");const s=n.fromLatLngToPoint(o);if(null===s)throw new Error("cannot get projectedNorthWest");const r=n.fromLatLngToPoint({lng:t,lat:e});if(null===r)throw new Error("cannot get projected lng lat");const a=this._map.getZoom();if(void 0===a)throw new Error("cannot get zoom");const h=Math.pow(2,a);return{x:Math.floor((r.x-s.x)*h),y:Math.floor((r.y-s.y)*h)}},this.unproject=(t,e)=>{const i=this._map.getProjection();if(void 0===i)throw new Error("cannot get projection");const o=this._map.getBounds();if(void 0===o)throw new Error("cannot get bounds");const n=i.fromLatLngToPoint(o.getNorthEast());if(null===n)throw new Error("cannot get topRight");const s=i.fromLatLngToPoint(o.getSouthWest());if(null===s)throw new Error("cannot get bottomLeft");const r=this._map.getZoom();if(void 0===r)throw new Error("zoom get bounds");const a=Math.pow(2,r),h=new google.maps.Point(t/a+s.x,e/a+n.y),c=i.fromPointToLatLng(h);if(null===c)throw new Error("zoom get bounds");return{lng:c.lng(),lat:c.lat()}},this.setCursor=t=>{if(t!==this._cursor){if(this._cursorStyleSheet&&(this._cursorStyleSheet.remove(),this._cursorStyleSheet=void 0),"unset"!==t){const e=this.getMapContainer(),i=document.createElement("style");i.type="text/css",i.innerHTML=`#${e.id} [aria-label="Map"] { cursor: ${t} !important; }`,document.getElementsByTagName("head")[0].appendChild(i),this._cursorStyleSheet=i}this._cursor=t}}}circlePath(t,e,i){return"M "+t+" "+e+" m -"+i+", 0 a "+i+","+i+" 0 1,0 "+2*i+",0 a "+i+","+i+" 0 1,0 -"+2*i+",0"}register(t){this._onClickCallback=i=>{i.latLng&&t.onClick({lng:e(i.latLng.lng(),this._coordinatePrecision),lat:e(i.latLng.lat(),this._coordinatePrecision),containerX:i.domEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.domEvent.clientY-this.getMapContainer().offsetTop,button:0===i.domEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._onClickListener=this._map.addListener("click",this._onClickCallback),this._onRightClickListener=this._map.addListener("rightclick",this._onClickCallback),this._onMouseMoveCallback=i=>{i.latLng&&t.onMouseMove({lng:e(i.latLng.lng(),this._coordinatePrecision),lat:e(i.latLng.lat(),this._coordinatePrecision),containerX:i.domEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.domEvent.clientY-this.getMapContainer().offsetTop,button:0===i.domEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._onMouseMoveListener=this._map.addListener("mousemove",this._onMouseMoveCallback),this._onKeyUpListener=e=>{t.onKeyUp({key:e.key})},this.getMapContainer().addEventListener("keyup",this._onKeyUpListener);let i="not-dragging";this._onDragStartListener=t=>{i="pre-dragging"};const o=this.getMapContainer();o.addEventListener("mousedown",this._onDragStartListener),this._onDragListener=n=>{const s={x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop},{lng:r,lat:a}=this.unproject(s.x,s.y),h={lng:e(r,this._coordinatePrecision),lat:e(a,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]};"pre-dragging"===i?(i="dragging",t.onDragStart(h,t=>{this._map.setOptions({draggable:!1})})):"dragging"===i&&t.onDrag(h)},o.addEventListener("mousemove",this._onDragListener),this._onDragEndListener=n=>{if("dragging"===i){const i={x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop},{lng:s,lat:r}=this.unproject(i.x,i.y);t.onDragEnd({lng:e(s,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]},t=>{this._map.setOptions({draggable:t})})}i="not-dragging"},o.addEventListener("mouseup",this._onDragEndListener)}unregister(){this._onClickListener&&(this._onClickCallback=void 0,this._onClickListener.remove(),this._onClickListener=void 0),this._onRightClickListener&&(this._onClickCallback=void 0,this._onRightClickListener.remove(),this._onRightClickListener=void 0),this._onMouseMoveListener&&(this._onMouseMoveCallback=void 0,this._onMouseMoveListener.remove(),this._onMouseMoveListener=void 0),this._onKeyUpListener&&(this.getMapContainer().removeEventListener("keyup",this._onKeyUpListener),this._onKeyUpListener=void 0)}render(t,e){this._layers?(t.deletedIds.forEach(t=>{const e=this._map.data.getFeatureById(t);e&&this._map.data.remove(e)}),t.updated.forEach(t=>{if(!t||!t.id)throw new Error("Feature is not valid");const e=this._map.data.getFeatureById(t.id);if(!e)throw new Error("Feature could not be found by Google Maps API");switch(e.forEachProperty((t,i)=>{e.setProperty(i,void 0)}),Object.keys(t.properties).forEach(i=>{e.setProperty(i,t.properties[i])}),t.geometry.type){case"Point":{const i=t.geometry.coordinates;e.setGeometry(new google.maps.Data.Point(new google.maps.LatLng(i[1],i[0])))}break;case"LineString":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=i[t],n=new google.maps.LatLng(e[1],e[0]);o.push(n)}e.setGeometry(new google.maps.Data.LineString(o))}break;case"Polygon":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=[];for(let o=0;o<i[t].length;o++){const n=new google.maps.LatLng(i[t][o][1],i[t][o][0]);e.push(n)}o.push(e)}e.setGeometry(new google.maps.Data.Polygon(o))}}}),t.created.forEach(t=>{this._map.data.addGeoJson(t)})):(this._map.data.addListener("click",t=>{this._onClickCallback&&this._onClickCallback(t)}),this._map.data.addListener("mousemove",t=>{this._onMouseMoveCallback&&this._onMouseMoveCallback(t)}));const i={type:"FeatureCollection",features:[...t.created]};this._map.data.addGeoJson(i),this._map.data.setStyle(t=>{const i=t.getProperty("mode"),o=t.getGeometry();if(!o)throw new Error("Google Maps geometry not found");const n=o.getType(),s=t.getProperty("selected"),r=Boolean(t.getProperty("selectionPoint")),a=Boolean(t.getProperty("midPoint"));switch(n){case"Point":const t=s||r,o=a;return{clickable:!1,icon:{path:this.circlePath(0,0,t?e[i].selectionPointWidth:o?e[i].midPointWidth:e[i].pointWidth),fillColor:t?e[i].selectedColor:o?e[i].midPointColor:e[i].pointColor,fillOpacity:1,strokeColor:t?e[i].selectedPointOutlineColor:o?e[i].midPointOutlineColor:void 0,strokeWeight:t||o?2:0,rotation:0,scale:1}};case"LineString":return{strokeColor:s?e[i].selectedColor:e[i].lineStringColor,strokeWeight:e[i].lineStringWidth};case"Polygon":return{strokeColor:s?e[i].selectedColor:e[i].polygonOutlineColor,strokeWeight:e[i].polygonOutlineWidth,fillOpacity:e[i].polygonFillOpacity,fillColor:s?e[i].selectedColor:e[i].polygonFillColor}}throw Error("Unknown feature type")}),this._layers=!0}}class o{constructor(t){this._heldKeys=new Set,this._lib=void 0,this._coordinatePrecision=void 0,this._map=void 0,this._onMouseMoveListener=void 0,this._onClickListener=void 0,this._onKeyUpListener=void 0,this._onKeyDownListener=void 0,this._onDragStartListener=void 0,this._onDragListener=void 0,this._onDragEndListener=void 0,this._layer=void 0,this._midPointPaneZIndexStyleSheet=void 0,this._midPointPane="midPointPane",this._selectionPaneZIndexStyleSheet=void 0,this._selectedPane="selectedPane",this.project=void 0,this.unproject=void 0,this.setCursor=void 0,this.getMapContainer=void 0,this._lib=t.lib,this._map=t.map,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9,this.getMapContainer=()=>this._map.getContainer(),this.project=(t,e)=>{const{x:i,y:o}=this._map.latLngToContainerPoint({lng:t,lat:e});return{x:i,y:o}},this.unproject=(t,e)=>{const{lng:i,lat:o}=this._map.containerPointToLatLng({x:t,y:e});return{lng:i,lat:o}},this.setCursor=t=>{"unset"===t?this.getMapContainer().style.removeProperty("cursor"):this.getMapContainer().style.cursor=t}}createPaneStyleSheet(t,e){const i=document.createElement("style");return i.type="text/css",i.innerHTML=`.leaflet-${t} {z-index: ${e};}`,document.getElementsByTagName("head")[0].appendChild(i),this._map.createPane(t),i}register(t){this._selectionPaneZIndexStyleSheet||(this._selectionPaneZIndexStyleSheet=this.createPaneStyleSheet(this._selectedPane,10)),this._midPointPaneZIndexStyleSheet||(this._midPointPaneZIndexStyleSheet=this.createPaneStyleSheet(this._midPointPane,20));const i=this.getMapContainer();let o="not-dragging";this._onClickListener=i=>{"not-dragging"!==o&&"pre-dragging"!==o||t.onClick({lng:e(i.latlng.lng,this._coordinatePrecision),lat:e(i.latlng.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("mouseup",this._onClickListener),this._map.on("contextmenu",this._onClickListener),this._onMouseMoveListener=i=>{i.originalEvent.preventDefault(),t.onMouseMove({lng:e(i.latlng.lng,this._coordinatePrecision),lat:e(i.latlng.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("mousemove",this._onMouseMoveListener),this._onDragStartListener=t=>{o="pre-dragging"},i.addEventListener("pointerdown",this._onDragStartListener),this._onDragListener=n=>{const s={x:n.clientX-i.offsetLeft,y:n.clientY-i.offsetTop},{lng:r,lat:a}=this._map.containerPointToLatLng(s),h={lng:e(r,this._coordinatePrecision),lat:e(a,this._coordinatePrecision),containerX:n.clientX-i.offsetLeft,containerY:n.clientY-i.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]};"pre-dragging"===o?(o="dragging",t.onDragStart(h,t=>{t?this._map.dragging.enable():this._map.dragging.disable()})):"dragging"===o&&t.onDrag(h)},i.addEventListener("pointermove",this._onDragListener),this._onDragEndListener=n=>{if(n.preventDefault(),"dragging"===o){const s={x:n.clientX-i.offsetLeft,y:n.clientY-i.offsetTop},{lng:r,lat:a}=this._map.containerPointToLatLng(s);return t.onDragEnd({lng:e(r,this._coordinatePrecision),lat:e(a,this._coordinatePrecision),containerX:n.clientX-i.offsetLeft,containerY:n.clientY-i.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]},t=>{t?this._map.dragging.enable():this._map.dragging.disable()}),o="after-dragging",void this._map.dragging.enable()}o="not-dragging",this._map.dragging.enable()},i.addEventListener("pointerup",this._onDragEndListener),this._onKeyUpListener=e=>{e.preventDefault(),this._heldKeys.delete(e.key),t.onKeyUp({key:e.key})},i.addEventListener("keyup",this._onKeyUpListener),this._onKeyDownListener=e=>{e.preventDefault(),this._heldKeys.add(e.key),t.onKeyDown({key:e.key})},i.addEventListener("keydown",this._onKeyDownListener)}unregister(){this._onClickListener&&(this._map.off("contextmenu",this._onClickListener),this._map.off("click",this._onClickListener),this._onClickListener=void 0),this._onMouseMoveListener&&(this._map.off("click",this._onClickListener),this._onClickListener=void 0);const t=this._map.getPane(this._selectedPane);t&&t.remove()}render(t,e){const i=[...t.created,...t.updated,...t.unchanged];this._layer&&this._map.removeLayer(this._layer);const o=this._lib.geoJSON({type:"FeatureCollection",features:i},{pointToLayer:(t,i)=>{if(!t.properties)throw new Error("Feature has no properties");const o=e[t.properties.mode],n=t.properties.selected||t.properties.selectionPoint,s=t.properties.midPoint;return this._lib.circleMarker(i,{radius:n?o.selectionPointWidth:s?o.midPointWidth:o.pointWidth,fillColor:n?o.selectedColor:s?o.midPointColor:o.pointColor,stroke:n||s,color:n?o.selectedPointOutlineColor:s?o.midPointOutlineColor:o.pointOutlineColor,weight:n||s?2:0,fillOpacity:.8,pane:n?this._selectedPane:s?this._midPointPane:void 0,interactive:!1})},style:t=>{if(!t||!t.properties)return{};const i=e[t.properties.mode];return"LineString"===t.geometry.type?{interactive:!1,color:t.properties.selected?i.selectedColor:i.lineStringColor,weight:i.lineStringWidth}:"Polygon"===t.geometry.type?{interactive:!1,fillOpacity:i.polygonFillOpacity,fillColor:t.properties.selected?i.selectedColor:i.polygonFillColor,weight:i.polygonOutlineWidth,stroke:!0,color:t.properties.selected?i.selectedColor:i.polygonOutlineColor}:{}}});this._map.addLayer(o),this._layer=o}}class n{constructor(t){this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this.getMapContainer=void 0,this._heldKeys=new Set,this._coordinatePrecision=void 0,this._map=void 0,this._onMouseMoveListener=void 0,this._onClickListener=void 0,this._onDragStartListener=void 0,this._onDragListener=void 0,this._onDragEndListener=void 0,this._onKeyDownListener=void 0,this._onKeyUpListener=void 0,this._rendered={},this._map=t.map,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9,this.project=(t,e)=>{const{x:i,y:o}=this._map.project({lng:t,lat:e});return{x:i,y:o}},this.unproject=(t,e)=>{const{lng:i,lat:o}=this._map.unproject({x:t,y:e});return{lng:i,lat:o}},this.setCursor=t=>{this._map.getCanvas().style.cursor=t},this.getMapContainer=()=>this._map.getContainer()}_addGeoJSONSource(t,e){this._map.addSource(t,{type:"geojson",data:{type:"FeatureCollection",features:e}})}_addFillLayer(t,e,i){return this._map.addLayer({id:t,source:t,type:"fill",filter:["all",["match",["geometry-type"],"Polygon",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"fill-color":["get","polygonFillColor"],"fill-opacity":i.polygonFillOpacity}})}_addFillOutlineLayer(t,e,i,o){const n=this._map.addLayer({id:t+"outline",source:t,type:"line",filter:["all",["match",["geometry-type"],"Polygon",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"line-width":i.polygonOutlineWidth,"line-color":["get","polygonOutlineColor"]}});return o&&this._map.moveLayer(t,o),n}_addLineLayer(t,e,i,o){const n=this._map.addLayer({id:t,source:t,type:"line",filter:["all",["match",["geometry-type"],"LineString",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"line-width":i.lineStringWidth,"line-color":["get","lineStringColor"]}});return o&&this._map.moveLayer(t,o),n}_addPointLayer(t,e,i,o){const n=this._map.addLayer({id:t,source:t,type:"circle",filter:["all",["match",["geometry-type"],"Point",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"circle-stroke-color":["get","selectedPointOutlineColor"],"circle-stroke-width":2,"circle-radius":["get","pointWidth"],"circle-color":["get","pointColor"]}});return o&&this._map.moveLayer(t,o),n}_addLayer(t,e,i,o,n){"Point"===i&&this._addPointLayer(t,e,o,n),"LineString"===i&&this._addLineLayer(t,e,o,n),"Polygon"===i&&(this._addFillLayer(t,e,o),this._addFillOutlineLayer(t,e,o,n))}_addGeoJSONLayer(t,e,i,o){const n=`td-${t}-${e.toLowerCase()}`;return this._addGeoJSONSource(n,i),this._addLayer(n,t,e,o),n}_setGeoJSONLayerData(t,e,i){const o=`td-${t}-${e.toLowerCase()}`;return this._map.getSource(o).setData({type:"FeatureCollection",features:i}),o}register(t){this._onClickListener=i=>{t.onClick({lng:e(i.lngLat.lng,this._coordinatePrecision),lat:e(i.lngLat.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("click",this._onClickListener),this._map.on("contextmenu",this._onClickListener),this._onMouseMoveListener=i=>{t.onMouseMove({lng:e(i.lngLat.lng,this._coordinatePrecision),lat:e(i.lngLat.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("mousemove",this._onMouseMoveListener);let i="not-dragging";this._onDragStartListener=t=>{i="pre-dragging"};const o=this.getMapContainer();o.addEventListener("mousedown",this._onDragStartListener),this._onDragListener=n=>{const{lng:s,lat:r}=this._map.unproject({x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop}),a={lng:e(s,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]};"pre-dragging"===i?(i="dragging",t.onDragStart(a,t=>{t?this._map.dragPan.enable():this._map.dragPan.disable()})):"dragging"===i&&t.onDrag(a)},o.addEventListener("mousemove",this._onDragListener),this._onDragEndListener=n=>{if("dragging"===i){const i={x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop},{lng:s,lat:r}=this._map.unproject(i);t.onDragEnd({lng:e(s,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]},t=>{t?this._map.dragPan.enable():this._map.dragPan.disable()})}i="not-dragging"},o.addEventListener("mouseup",this._onDragEndListener),this._onKeyUpListener=e=>{e.preventDefault(),this._heldKeys.delete(e.key),t.onKeyUp({key:e.key})},o.addEventListener("keyup",this._onKeyUpListener),this._onKeyDownListener=e=>{e.preventDefault(),this._heldKeys.add(e.key),t.onKeyDown({key:e.key})},o.addEventListener("keydown",this._onKeyDownListener)}unregister(){this._onClickListener&&(this._map.off("contextmenue",this._onClickListener),this._map.off("click",this._onClickListener),this._onClickListener=void 0),this._onMouseMoveListener&&(this._map.off("mousemove",this._onMouseMoveListener),this._onMouseMoveListener=void 0),this._onKeyUpListener&&this._map.getCanvas().removeEventListener("keypress",this._onKeyUpListener),this._onDragStartListener&&this._map.getCanvas().removeEventListener("mousedown",this._onDragStartListener),this._onDragListener&&this._map.getCanvas().removeEventListener("mousemove",this._onDragListener),this._onDragEndListener&&this._map.getCanvas().removeEventListener("mouseup",this._onDragEndListener)}render(t,e){const i=[...t.created,...t.updated,...t.unchanged],o={};for(let t=0;t<i.length;t++){const n=i[t];Object.keys(e).forEach(t=>{const i=e[t];n.properties.mode===t&&(o[t]||(o[t]={points:[],linestrings:[],polygons:[]}),"Point"===n.geometry.type?(n.properties.selected||n.properties.selectionPoint?(n.properties.pointColor=i.selectedColor,n.properties.selectedPointOutlineColor=i.selectedPointOutlineColor,n.properties.pointWidth=i.selectionPointWidth):n.properties.midPoint?(n.properties.pointColor=i.midPointColor,n.properties.selectedPointOutlineColor=i.midPointOutlineColor,n.properties.pointWidth=i.midPointWidth):(n.properties.pointColor=i.pointColor,n.properties.selectedPointOutlineColor=i.pointColor,n.properties.pointWidth=i.pointWidth),o[t].points.push(n)):"LineString"===n.geometry.type?(n.properties.lineStringColor=n.properties.selected?i.selectedColor:i.lineStringColor,o[t].linestrings.push(n)):"Polygon"===n.geometry.type&&(n.properties.selected?(n.properties.polygonFillColor=i.selectedColor,n.properties.polygonOutlineColor=i.selectedColor):(n.properties.polygonFillColor=i.polygonFillColor,n.properties.polygonOutlineColor=i.polygonOutlineColor),o[t].polygons.push(n)))})}Object.keys(e).forEach(t=>{const i=e[t];if(!o[t]||!i)return;const{points:n,linestrings:s,polygons:r}=o[t];if(this._rendered[t]){const e=this._setGeoJSONLayerData(t,"Point",n);this._setGeoJSONLayerData(t,"LineString",s),this._setGeoJSONLayerData(t,"Polygon",r),this._map.moveLayer(e)}else this._addGeoJSONLayer(t,"Point",n,i),this._addGeoJSONLayer(t,"LineString",s,i),this._addGeoJSONLayer(t,"Polygon",r,i),this._rendered[t]=!0})}}function s(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),n=i(t[0]),s=i(e[1]),r=s-o,a=i(e[0])-n,h=Math.sin(r/2)*Math.sin(r/2)+Math.cos(o)*Math.cos(s)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(h),Math.sqrt(1-h))*6371e3/1e3}function r(t){return t%360*Math.PI/180}function a(t){return t%(2*Math.PI)*180/Math.PI}function h(t,e,i){const o=r(t[0]),n=r(t[1]),s=r(i),h=function(t){return t/6371.0088}(e),c=Math.asin(Math.sin(n)*Math.cos(h)+Math.cos(n)*Math.sin(h)*Math.cos(s));return[a(o+Math.atan2(Math.sin(s)*Math.sin(h)*Math.cos(n),Math.cos(h)-Math.sin(n)*Math.sin(c))),a(c)]}function c(t){const{center:e,radiusKilometers:i}=t,o=t.steps?t.steps:64,n=[];for(let t=0;t<o;t++)n.push(h(e,i,-360*t/o));return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:{}}}class d{get state(){return this._state}set state(t){throw new Error("Please use the modes lifecycle methods")}get styling(){return this._styling}set styling(t){if("object"!=typeof t)throw new Error("Styling must be an object");this.onStyleChange([],"styling"),this._styling=t}registerBehaviors(t){}constructor(e){this._state=void 0,this._styling=void 0,this.behaviors=[],this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.onStyleChange=void 0,this.store=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this._state="unregistered",this._styling=e&&e.styling?t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#3f97e0",pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,selectedColor:"#26a9c8",selectedPointOutlineColor:"#ffffff",selectionPointWidth:6,midPointColor:"#3f97e0",midPointOutlineColor:"#ffffff",midPointWidth:4,closingPointColor:"#3f97e0",closingPointOutlineColor:"#ffffff",closingPointWidth:4},e.styling):{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#3f97e0",pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,selectedColor:"#26a9c8",selectedPointOutlineColor:"#ffffff",selectionPointWidth:6,midPointColor:"#3f97e0",midPointOutlineColor:"#ffffff",midPointWidth:4,closingPointColor:"#3f97e0",closingPointOutlineColor:"#ffffff",closingPointWidth:4},this.pointerDistance=e&&e.pointerDistance||40,this.coordinatePrecision=e&&e.coordinatePrecision||9}setStarted(){if("stopped"!==this._state&&"registered"!==this._state)throw new Error("Mode must be unregistered or stopped to start");this._state="started"}setStopped(){if("started"!==this._state)throw new Error("Mode must be started to be stopped");this._state="stopped"}register(t){if("unregistered"!==this._state)throw new Error("Can not register unless mode is unregistered");this._state="registered",this.store=t.store,this.store.registerOnChange(t.onChange),this.project=t.project,this.unproject=t.unproject,this.onSelect=t.onSelect,this.onDeselect=t.onDeselect,this.setCursor=t.setCursor,this.onStyleChange=t.onChange,this.registerBehaviors({mode:t.mode,store:this.store,project:this.project,unproject:this.unproject,pointerDistance:this.pointerDistance,coordinatePrecision:this.coordinatePrecision})}onDeselect(t){}onSelect(t){}}class l extends d{constructor(t){super(t),this.mode="circle",this.center=void 0,this.clickCount=0,this.currentCircleId=void 0,this.keyEvents=void 0,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onClick(t){if(0===this.clickCount){this.center=[t.lng,t.lat];const e=c({center:this.center,radiusKilometers:1e-5}),[i]=this.store.create([{geometry:e.geometry,properties:{mode:this.mode}}]);this.currentCircleId=i,this.clickCount++}else this.center=void 0,this.currentCircleId=void 0,this.clickCount=0}onMouseMove(t){if(1===this.clickCount&&this.center&&this.currentCircleId){const e=s(this.center,[t.lng,t.lat]),i=c({center:this.center,radiusKilometers:e});this.store.updateGeometry([{id:this.currentCircleId,geometry:i.geometry}])}}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentCircleId&&this.store.delete([this.currentCircleId])}catch(t){}this.center=void 0,this.currentCircleId=void 0,this.clickCount=0}}class p extends d{constructor(t){super(t),this.mode="freehand",this.startingClick=!1,this.currentId=void 0,this.skip=0,this.everyNthMouseEvent=void 0,this.keyEvents=void 0,this.everyNthMouseEvent=t&&t.everyNthMouseEvent||10,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onMouseMove(t){if(this.currentId&&!1!==this.startingClick){if(this.skip>this.everyNthMouseEvent){this.skip=0;const e=this.store.getGeometryCopy(this.currentId);e.coordinates[0].pop(),this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[...e.coordinates[0],[t.lng,t.lat],e.coordinates[0][0]]]}}])}this.skip++}}onClick(t){if(!1===this.startingClick){const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);return this.currentId=e,void(this.startingClick=!0)}this.startingClick=!1,this.currentId=void 0}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.startingClick=!1}}function g(t){let e;if("Polygon"===t.geometry.type)e=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");e=[t.geometry.coordinates]}const i=[];for(let t=0;t<e.length;t++)for(let i=0;i<e[t].length-1;i++)for(let o=0;o<e.length;o++)for(let s=0;s<e[o].length-1;s++)n(t,i,o,s);return i.length>0;function o(t){return t<0||t>1}function n(t,n,s,r){const a=e[t][n],h=e[t][n+1],c=e[s][r],d=e[s][r+1],l=function(t,e,i,o){if(u(t,i)||u(t,o)||u(e,i)||u(o,i))return null;const n=t[0],s=t[1],r=e[0],a=e[1],h=i[0],c=i[1],d=o[0],l=o[1],p=(n-r)*(c-l)-(s-a)*(h-d);return 0===p?null:[((n*a-s*r)*(h-d)-(n-r)*(h*l-c*d))/p,((n*a-s*r)*(c-l)-(s-a)*(h*l-c*d))/p]}(a,h,c,d);if(null===l)return;let p,g;p=h[0]!==a[0]?(l[0]-a[0])/(h[0]-a[0]):(l[1]-a[1])/(h[1]-a[1]),g=d[0]!==c[0]?(l[0]-c[0])/(d[0]-c[0]):(l[1]-c[1])/(d[1]-c[1]),o(p)||o(g)||(l.toString(),i.push(l))}}function u(t,e){return t[0]===e[0]&&t[1]===e[1]}const y=(t,e)=>{const{x:i,y:o}=t,{x:n,y:s}=e,r=n-i,a=s-o;return Math.sqrt(a*a+r*r)};class m{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:n,coordinatePrecision:s}){this.store=void 0,this.mode=void 0,this.project=void 0,this.unproject=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.store=t,this.mode=e,this.project=i,this.unproject=o,this.pointerDistance=n,this.coordinatePrecision=s}}class f extends m{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t,o=this.pointerDistance/2;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[this.unproject(e-o,i-o),this.unproject(e+o,i-o),this.unproject(e+o,i+o),this.unproject(e-o,i+o),this.unproject(e-o,i-o)].map(t=>[t.lng,t.lat])]}}}}class _ extends m{constructor(t){super(t)}measure(t,e){const{x:i,y:o}=this.project(e[0],e[1]);return y({x:i,y:o},{x:t.containerX,y:t.containerY})}}class v extends m{constructor(t,e,i){super(t),this.config=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)),this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),n={coord:void 0,minDist:Infinity};return o.forEach(e=>{let i;if("Polygon"===e.geometry.type)i=e.geometry.coordinates[0];else{if("LineString"!==e.geometry.type)return;i=e.geometry.coordinates}i.forEach(e=>{const i=this.pixelDistance.measure(t,e);i<n.minDist&&i<this.pointerDistance&&(n.coord=e)})}),n.coord}}class P extends d{constructor(t){super(t),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.allowSelfIntersections=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.snapping=void 0,this.snappingEnabled=!(!t||void 0===t.snapping)&&t.snapping,this.allowSelfIntersections=!t||void 0===t.allowSelfIntersections||t.allowSelfIntersections,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}registerBehaviors(t){this.snapping=new v(t,new _(t),new f(t))}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onMouseMove(t){if(!this.currentId||0===this.currentCoordinate)return;const e=this.store.getGeometryCopy(this.currentId);e.coordinates.pop();const i=this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId);this.store.updateGeometry([{id:this.currentId,geometry:{type:"LineString",coordinates:[...e.coordinates,i||[t.lng,t.lat]]}}])}onClick(t){const e=this.currentId&&this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId)||[t.lng,t.lat];if(0===this.currentCoordinate){const[t]=this.store.create([{geometry:{type:"LineString",coordinates:[e,e]},properties:{mode:this.mode}}]);this.currentId=t,this.currentCoordinate++}else if(1===this.currentCoordinate&&this.currentId){const t=this.store.getGeometryCopy(this.currentId);this.store.updateGeometry([{id:this.currentId,geometry:{type:"LineString",coordinates:[t.coordinates[0],e,e]}}]),this.currentCoordinate++}else if(this.currentId){const i=this.store.getGeometryCopy(this.currentId),[o,n]=i.coordinates[i.coordinates.length-2],{x:s,y:r}=this.project(o,n);if(y({x:s,y:r},{x:t.containerX,y:t.containerY})<this.pointerDistance)i.coordinates.pop(),this.store.updateGeometry([{id:this.currentId,geometry:{type:"LineString",coordinates:[...i.coordinates]}}]),this.currentCoordinate=0,this.currentId=void 0;else{const t={type:"LineString",coordinates:[...i.coordinates,e]};if(!this.allowSelfIntersections&&g({type:"Feature",geometry:t,properties:{}}))return;this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.currentCoordinate++}}}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.currentCoordinate=0}}class C extends d{constructor(t){super(t),this.mode="point"}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onClick(t){if(!this.store)throw new Error("Mode must be registered first");this.store.create([{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode}}])}onMouseMove(){}onKeyDown(){}onKeyUp(){}cleanUp(){}onDragStart(){}onDrag(){}onDragEnd(){}}function M(t,e){return t[0]===e[0]&&t[1]===e[1]}class L extends m{constructor(t){super(t),this._startEndPoints=[]}get ids(){return this._startEndPoints.concat()}set ids(t){}create(t,e){if(this.ids.length)throw new Error("Opening and closing points already creating");this._startEndPoints=this.store.create([{geometry:{type:"Point",coordinates:t[0]},properties:{mode:e}}])}delete(){this.ids.length&&(this.store.delete(this.ids),this._startEndPoints=[])}update(t){this.store.updateGeometry([{id:this.ids[0],geometry:{type:"Point",coordinates:t[0]}}])}}class x extends d{constructor(t){super(t),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.allowSelfIntersections=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.isClosed=!1,this.snapping=void 0,this.pixelDistance=void 0,this.startEndPoint=void 0,this.snappingEnabled=!(!t||void 0===t.snapping)&&t.snapping,this.allowSelfIntersections=!t||void 0===t.allowSelfIntersections||t.allowSelfIntersections,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}registerBehaviors(t){this.pixelDistance=new _(t),this.snapping=new v(t,this.pixelDistance,new f(t)),this.startEndPoint=new L(t)}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onMouseMove(t){if(this.setCursor("crosshair"),!this.currentId||0===this.currentCoordinate)return;const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0,i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(e&&(t.lng=e[0],t.lat=e[1]),1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),n=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat+n],i[0]]}else if(2===this.currentCoordinate)o=[i[0],i[1],[t.lng,t.lat],i[0]];else{const e=this.pixelDistance.measure(t,i[0]),n=this.pixelDistance.measure(t,i[i.length-2]);e<this.pointerDistance||n<this.pointerDistance?(this.setCursor("pointer"),this.isClosed?o=[...i]:(o=[...i.slice(0,-2),i[0],i[0]],this.isClosed=!0)):(this.isClosed&&(this.isClosed=!1),o=[...i.slice(0,-2),[t.lng,t.lat],i[0]])}this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[o]}}]),this.startEndPoint.ids.length&&this.startEndPoint.update(o)}onClick(t){const e=this.currentId&&this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0;if(0===this.currentCoordinate){e&&(t.lng=e[0],t.lat=e[1]);const[i]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=i,this.currentCoordinate++}else if(1===this.currentCoordinate&&this.currentId){e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId);if(M([t.lng,t.lat],i.coordinates[0][0]))return;this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]]]}}]),this.currentCoordinate++}else if(2===this.currentCoordinate&&this.currentId){e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId);if(2===this.currentCoordinate&&(console.log("creating"),this.startEndPoint.create(i.coordinates[0],"polygon")),M([t.lng,t.lat],i.coordinates[0][1]))return;this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[i.coordinates[0][0],i.coordinates[0][1],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]]]}}]),this.currentCoordinate++}else if(this.currentId){const i=this.store.getGeometryCopy(this.currentId);if(this.pixelDistance.measure(t,i.coordinates[0][0])<this.pointerDistance)this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[...i.coordinates[0].slice(0,-2),i.coordinates[0][0]]]}}]),this.currentCoordinate=0,this.currentId=void 0,this.startEndPoint.delete();else{if(e&&(t.lng=e[0],t.lat=e[1]),M([t.lng,t.lat],i.coordinates[0][this.currentCoordinate-1]))return;const o=function(t=[[[0,0],[0,1],[1,1],[1,0],[0,0]]]){return{type:"Feature",geometry:{type:"Polygon",coordinates:t},properties:{}}}([[...i.coordinates[0].slice(0,-1),[t.lng,t.lat],i.coordinates[0][0]]]);if(this.currentCoordinate>2&&!this.allowSelfIntersections&&g(o))return;this.store.updateGeometry([{id:this.currentId,geometry:o.geometry}]),this.currentCoordinate++}}}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onKeyDown(){}onDragStart(){this.setCursor("unset")}onDrag(){}onDragEnd(){this.setCursor("crosshair")}cleanUp(){try{this.currentId&&this.store.delete([this.currentId]),this.startEndPoint.ids.length&&this.startEndPoint.delete()}catch(t){}this.currentId=void 0,this.currentCoordinate=0}}function E(t,i,o){const n=s(t,i),c=function(t,e){const i=r(t[0]),o=r(e[0]),n=r(t[1]),s=r(e[1]),h=Math.sin(o-i)*Math.cos(s),c=Math.cos(n)*Math.sin(s)-Math.sin(n)*Math.cos(s)*Math.cos(o-i);return a(Math.atan2(h,c))}(t,i),d=h(t,n/2,c);return[e(d[0],o),e(d[1],o)]}function k(t,e){const i=[];for(let o=0;o<t.length-1;o++){const n=E(t[o],t[o+1],e);i.push(n)}return i}class S extends m{constructor(t,e){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e}get ids(){return this._midPoints.concat()}set ids(t){}insert(t,e){const i=this.store.getGeometryCopy(t),{midPointFeatureId:o,midPointSegment:n}=this.store.getPropertiesCopy(t),s=this.store.getGeometryCopy(o),r="Polygon"===s.type?s.coordinates[0]:s.coordinates;r.splice(n+1,0,i.coordinates),s.coordinates="Polygon"===s.type?[r]:r,this.store.updateGeometry([{id:o,geometry:s}]),this.store.delete([...this._midPoints,...this.selectionPointBehavior.ids]),this.create(r,o,e),this.selectionPointBehavior.create(r,s.type,o)}create(t,e,i){if(!this.store.has(e))throw new Error("Store does not have feature with this id");this._midPoints=this.store.create(function(t,e,i){return k(t,i).map((t,i)=>({geometry:{type:"Point",coordinates:t},properties:e(i)}))}(t,t=>({mode:this.mode,midPoint:!0,midPointSegment:t,midPointFeatureId:e}),i))}delete(){this._midPoints.length&&(this.store.delete(this._midPoints),this._midPoints=[])}getUpdated(t){if(0!==this._midPoints.length)return k(t,this.coordinatePrecision).map((t,e)=>({id:this._midPoints[e],geometry:{type:"Point",coordinates:t}}))}}class w extends m{constructor(t){super(t),this._selectionPoints=[]}get ids(){return this._selectionPoints.concat()}set ids(t){}create(t,e,i){this._selectionPoints=this.store.create(function(t,e,i){const o=[],n="Polygon"===e?t.length-1:t.length;for(let e=0;e<n;e++)o.push({geometry:{type:"Point",coordinates:t[e]},properties:i(e)});return o}(t,e,t=>({mode:this.mode,selectionPoint:!0,selectionPointFeatureId:i,index:t})))}delete(){this.ids.length&&(this.store.delete(this.ids),this._selectionPoints=[])}getUpdated(t){if(0!==this._selectionPoints.length)return this._selectionPoints.map((e,i)=>({id:e,geometry:{type:"Point",coordinates:t[i]}}))}getOneUpdated(t,e){if(void 0!==this._selectionPoints[t])return{id:this._selectionPoints[t],geometry:{type:"Point",coordinates:e}}}}function D(t,e){let i=!1;for(let r=0,a=e.length;r<a;r++){const a=e[r];for(let e=0,r=a.length,h=r-1;e<r;h=e++)(n=a[e])[1]>(o=t)[1]!=(s=a[h])[1]>o[1]&&o[0]<(s[0]-n[0])*(o[1]-n[1])/(s[1]-n[1])+n[0]&&(i=!i)}var o,n,s;return i}const I=(t,e,i)=>{const o=t=>t*t,n=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=n(e,i);if(0===o)return n(t,e);let s=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return s=Math.max(0,Math.min(1,s)),n(t,{x:e.x+s*(i.x-e.x),y:e.y+s*(i.y-e.y)})})(t,e,i))};class b extends m{constructor(t,e,i){super(t),this.config=void 0,this.createClickBoundingBox=void 0,this.pixelDistance=void 0,this.config=t,this.createClickBoundingBox=e,this.pixelDistance=i}find(t,e){let i,o,n=Infinity,s=Infinity;const r=this.createClickBoundingBox.create(t),a=this.store.search(r);for(let r=0;r<a.length;r++){const h=a[r],c=h.geometry;if("Point"===c.type){const r=!e&&h.properties.midPoint;if(h.properties.selectionPoint||r)continue;const a=this.pixelDistance.measure(t,c.coordinates);h.properties.midPoint&&a<this.pointerDistance&&a<s?(s=a,o=h):!h.properties.midPoint&&a<this.pointerDistance&&a<n&&(n=a,i=h)}else if("LineString"===c.type)for(let e=0;e<c.coordinates.length-1;e++){const o=c.coordinates[e],s=c.coordinates[e+1],r=I({x:t.containerX,y:t.containerY},this.project(o[0],o[1]),this.project(s[0],s[1]));r<this.pointerDistance&&r<n&&(n=r,i=h)}else"Polygon"===c.type&&D([t.lng,t.lat],c.coordinates)&&(n=0,i=h)}return{clickedFeature:i,clickedMidPoint:o}}}class B extends m{constructor(t,e,i,o){super(t),this.config=void 0,this.featuresAtMouseEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.dragPosition=void 0,this.config=t,this.featuresAtMouseEvent=e,this.selectionPoints=i,this.midPoints=o}get position(){return this.dragPosition?this.dragPosition.concat():void 0}set position(t){if(void 0!==t){if(!Array.isArray(t)||2!==t.length||"number"!=typeof t[0]||"number"!=typeof t[1])throw new Error("Position must be [number, number] array");this.dragPosition=t.concat()}else this.dragPosition=void 0}drag(t,e){const{clickedFeature:i}=this.featuresAtMouseEvent.find(t,!0);if(!i||i.id!==e)return;const o=this.store.getGeometryCopy(e),n=[t.lng,t.lat];if("Polygon"===o.type||"LineString"===o.type){let t,i;if("Polygon"===o.type?(t=o.coordinates[0],i=t.length-1):"LineString"===o.type&&(t=o.coordinates,i=t.length),void 0===i||!t||!this.dragPosition)return!1;for(let e=0;e<i;e++){const i=t[e],o=[this.dragPosition[0]-n[0],this.dragPosition[1]-n[1]];t[e]=[i[0]-o[0],i[1]-o[1]]}"Polygon"===o.type&&(t[t.length-1]=[t[0][0],t[0][1]]);const s=this.selectionPoints.getUpdated(t)||[],r=this.midPoints.getUpdated(t)||[];this.store.updateGeometry([{id:e,geometry:o},...s,...r])}else"Point"===o.type&&this.store.updateGeometry([{id:e,geometry:{type:"Point",coordinates:n}}])}}class O extends m{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o}drag(t,e){const i=this.store.getGeometryCopy(e);let o;if("LineString"===i.type)o=i.coordinates;else{if("Polygon"!==i.type)return!1;o=i.coordinates[0]}const n={dist:Infinity,index:-1,isFirstOrLastPolygonCoord:!1};for(let e=0;e<o.length;e++){const s=this.pixelDistance.measure(t,o[e]);if(s<this.pointerDistance&&s<n.dist){const t="Polygon"===i.type&&(e===o.length-1||0===e);n.dist=s,n.index=t?0:e,n.isFirstOrLastPolygonCoord=t}}if(-1===n.index)return!1;const s=[t.lng,t.lat];if(n.isFirstOrLastPolygonCoord){const t=o.length-1;o[0]=s,o[t]=s}else o[n.index]=s;const r=this.selectionPoints.getOneUpdated(n.index,s),a=r?[r]:[],h=this.midPoints.getUpdated(o)||[];return this.store.updateGeometry([{id:e,geometry:i},...a,...h]),!0}}function K(t){let e=0,i=0,o=0;return("Polygon"===t.geometry.type?t.geometry.coordinates[0].slice(0,-1):t.geometry.coordinates).forEach(t=>{e+=t[0],i+=t[1],o++},!0),[e/o,i/o]}function X(t,e){const i=t,o=e,n=r(i[1]),s=r(o[1]);let h=r(o[0]-i[0]);h>Math.PI&&(h-=2*Math.PI),h<-Math.PI&&(h+=2*Math.PI);const c=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),d=(a(Math.atan2(h,c))+360)%360;return d>180?-(360-d):d}function Y(t,e,i){const o=e/6371008.8,n=t[0]*Math.PI/180,s=r(t[1]),a=r(i),h=o*Math.cos(a);let c=s+h;Math.abs(c)>Math.PI/2&&(c=c>0?Math.PI-c:-Math.PI-c);const d=Math.log(Math.tan(c/2+Math.PI/4)/Math.tan(s/2+Math.PI/4)),l=Math.abs(d)>1e-11?h/d:Math.cos(s),p=[(180*(n+o*Math.sin(a)/l)/Math.PI+540)%360-180,180*c/Math.PI];return p[0]+=p[0]-t[0]>180?-360:t[0]-p[0]>180?360:0,p}function j(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=e[1]*Math.PI/180,o=t[1]*Math.PI/180,n=o-i;let s=Math.abs(t[0]-e[0])*Math.PI/180;s>Math.PI&&(s-=2*Math.PI);const r=Math.log(Math.tan(o/2+Math.PI/4)/Math.tan(i/2+Math.PI/4)),a=Math.abs(r)>1e-11?n/r:Math.cos(i);return 6371008.8*Math.sqrt(n*n+a*a*s*s)}class F extends m{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastBearing=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastBearing=void 0}rotate(t,i){const o=this.store.getGeometryCopy(i);if("Polygon"!==o.type&&"LineString"!==o.type)return;const n=[t.lng,t.lat],s=X(K({type:"Feature",geometry:o,properties:{}}),n);if(!this.lastBearing)return void(this.lastBearing=s+180);let r;if(function(t,e){if(0===e)return t;const i=K(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const o=X(i,t)+e,n=j(i,t),s=Y(i,n,o);t[0]=s[0],t[1]=s[1]})}({type:"Feature",geometry:o,properties:{}},-(this.lastBearing-(s+180))),"Polygon"===o.type)r=o.coordinates[0];else{if("LineString"!==o.type)return;r=o.coordinates}r.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const a=this.midPoints.getUpdated(r)||[],h=this.selectionPoints.getUpdated(r)||[];this.store.updateGeometry([{id:i,geometry:o},...h,...a]),this.lastBearing=s+180}}class G extends m{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastDistance=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastDistance=void 0}scale(t,i){const o=this.store.getGeometryCopy(i);if("Polygon"!==o.type&&"LineString"!==o.type)return;const n=[t.lng,t.lat],r=s(K({type:"Feature",geometry:o,properties:{}}),n);if(!this.lastDistance)return void(this.lastDistance=r);let a;if(function(t,e){if(1===e)return t;const i=K(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const o=j(i,t),n=X(i,t),s=Y(i,o*e,n);t[0]=s[0],t[1]=s[1]})}({type:"Feature",geometry:o,properties:{}},1-(this.lastDistance-r)/r),"Polygon"===o.type)a=o.coordinates[0];else{if("LineString"!==o.type)return;a=o.coordinates}a.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const h=this.midPoints.getUpdated(a)||[],c=this.selectionPoints.getUpdated(a)||[];this.store.updateGeometry([{id:i,geometry:o},...c,...h]),this.lastDistance=r}}class U extends d{constructor(t){super(t),this.mode="select",this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags=void 0,this.keyEvents=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.featuresAtMouseEvent=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.dragFeature=void 0,this.dragCoordinate=void 0,this.rotateFeature=void 0,this.scaleFeature=void 0,this.flags=t&&t.flags?t.flags:{},this.keyEvents=t&&t.keyEvents?t.keyEvents:{deselect:"Escape",delete:"Delete",rotate:"r",scale:"s"},this.dragEventThrottle=t&&void 0!==t.dragEventThrottle&&t.dragEventThrottle||5}registerBehaviors(t){this.pixelDistance=new _(t),this.clickBoundingBox=new f(t),this.featuresAtMouseEvent=new b(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new w(t),this.midPoints=new S(t,this.selectionPoints),this.rotateFeature=new F(t,this.selectionPoints,this.midPoints),this.scaleFeature=new G(t,this.selectionPoints,this.midPoints),this.dragFeature=new B(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints),this.dragCoordinate=new O(t,this.pixelDistance,this.selectionPoints,this.midPoints)}deselect(){this.store.updateProperty(this.selected.map(t=>({id:t,property:"selected",value:!1}))),this.onDeselect(this.selected[0]),this.selected=[],this.selectionPoints.delete(),this.midPoints.delete()}deleteSelected(){this.store.delete(this.selected),this.selected=[]}onRightClick(t){if(!this.selectionPoints.ids.length)return;let e,i=Infinity;if(this.selectionPoints.ids.forEach(o=>{const n=this.store.getGeometryCopy(o),s=this.pixelDistance.measure(t,n.coordinates);s<this.pointerDistance&&s<i&&(i=s,e=this.store.getPropertiesCopy(o))}),!e)return;const o=e.selectionPointFeatureId,n=e.index,s=this.store.getPropertiesCopy(o),r=this.flags[s.mode];if(!(r&&r.feature&&r.feature.coordinates&&r.feature.coordinates.deletable))return;const a=this.store.getGeometryCopy(o);let h;if("Polygon"===a.type){if(h=a.coordinates[0],h.length<=4)return}else if("LineString"===a.type&&(h=a.coordinates,h.length<=3))return;h&&("Polygon"===a.type&&0===n||n===h.length-1?(h.shift(),h.pop(),h.push([h[0][0],h[0][1]])):h.splice(n,1),this.store.delete([...this.midPoints.ids,...this.selectionPoints.ids]),this.store.updateGeometry([{id:o,geometry:a}]),this.selectionPoints.create(h,a.type,o),r&&r.feature&&r.feature.coordinates&&r.feature.coordinates.midpoints&&this.midPoints.create(h,o,this.coordinatePrecision))}onLeftClick(t){const{clickedFeature:e,clickedMidPoint:i}=this.featuresAtMouseEvent.find(t,this.selected.length>0);if(this.selected.length&&i)this.midPoints.insert(i.id,this.coordinatePrecision);else if(e){const{mode:t}=this.store.getPropertiesCopy(e.id),i=this.selected[0];if(i){if(i===e.id)return;this.deselect()}const o=this.flags[t];if(!o||!o.feature)return;this.selected=[e.id],this.store.updateProperty([{id:e.id,property:"selected",value:!0}]),this.onSelect(e.id);const{type:n,coordinates:s}=this.store.getGeometryCopy(e.id);let r;"LineString"===n?r=s:"Polygon"===n&&(r=s[0]),r&&o&&o.feature.coordinates&&(this.selectionPoints.create(r,n,e.id),o.feature.coordinates.midpoints&&this.midPoints.create(r,e.id,this.coordinatePrecision))}else if(this.selected.length)return void this.deselect()}start(){this.setStarted()}stop(){this.setStopped(),this.cleanUp()}onClick(t){"right"!==t.button?"left"===t.button&&this.onLeftClick(t):this.onRightClick(t)}onKeyDown(){}onKeyUp(t){if(t.key===this.keyEvents.delete){if(!this.selected.length)return;this.onDeselect(this.selected[0]),this.deleteSelected(),this.selectionPoints.delete(),this.midPoints.delete()}else t.key===this.keyEvents.deselect&&this.cleanUp()}cleanUp(){this.selected.length&&this.deselect()}onDragStart(t,e){if(!this.selected.length)return;const i=this.store.getPropertiesCopy(this.selected[0]),o=this.flags[i.mode];o&&o.feature&&(o.feature.draggable||o.feature.coordinates&&o.feature.coordinates.draggable)&&(this.dragEventCount=0,this.setCursor("grabbing"),this.dragFeature.position=[t.lng,t.lat],e(!1))}onDrag(t){const e=this.selected[0];if(!e||!this.dragFeature.position)return;const i=this.store.getPropertiesCopy(e),o=this.flags[i.mode];if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle!=0)if(o&&o.feature&&o.feature.rotateable&&t.heldKeys.includes("r"))this.rotateFeature.rotate(t,e);else if(o&&o.feature&&o.feature.scaleable&&t.heldKeys.includes("s"))this.scaleFeature.scale(t,e);else{if(o&&o.feature&&o.feature.coordinates&&o.feature.coordinates.draggable&&this.dragCoordinate.drag(t,e))return;o&&o.feature&&o.feature.draggable&&(this.dragFeature.drag(t,e),this.dragFeature.position=[t.lng,t.lat])}}onDragEnd(t,e){this.setCursor("grab"),this.dragFeature.position=void 0,this.rotateFeature.reset(),this.scaleFeature.reset(),e(!0)}onMouseMove(t){if(!this.selected.length||this.dragFeature.position)return;let e=!1;this.midPoints.ids.forEach(i=>{if(e)return;const o=this.store.getGeometryCopy(i);this.pixelDistance.measure(t,o.coordinates)<this.pointerDistance&&(e=!0)}),this.selectionPoints.ids.forEach(i=>{const o=this.store.getGeometryCopy(i);this.pixelDistance.measure(t,o.coordinates)<this.pointerDistance&&(e=!1)}),this.setCursor(e?"crosshair":"unset")}}class T extends d{constructor(...t){super(...t),this.mode="static"}start(){}stop(){}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}}const W=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)})};function N(t,e,i,o,n){for(;o>i;){if(o-i>600){const s=o-i+1,r=e-i+1,a=Math.log(s),h=.5*Math.exp(2*a/3),c=.5*Math.sqrt(a*h*(s-h)/s)*(r-s/2<0?-1:1);N(t,e,Math.max(i,Math.floor(e-r*h/s+c)),Math.min(o,Math.floor(e+(s-r)*h/s+c)),n)}const s=t[e];let r=i,a=o;for(A(t,i,e),n(t[o],s)>0&&A(t,i,o);r<a;){for(A(t,r,a),r++,a--;n(t[r],s)<0;)r++;for(;n(t[a],s)>0;)a--}0===n(t[i],s)?A(t,i,a):(a++,A(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function A(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function J(t,e){$(t,0,t.children.length,e,t)}function $(t,e,i,o,n){n||(n=et([])),n.minX=Infinity,n.minY=Infinity,n.maxX=-Infinity,n.maxY=-Infinity;for(let s=e;s<i;s++){const e=t.children[s];R(n,t.leaf?o(e):e)}return n}function R(t,e){return t.minX=Math.min(t.minX,e.minX),t.minY=Math.min(t.minY,e.minY),t.maxX=Math.max(t.maxX,e.maxX),t.maxY=Math.max(t.maxY,e.maxY),t}function Z(t,e){return t.minX-e.minX}function q(t,e){return t.minY-e.minY}function z(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function H(t){return t.maxX-t.minX+(t.maxY-t.minY)}function Q(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),n=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,n-i)*Math.max(0,s-o)}function V(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function tt(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function et(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function it(t,e,i,o,n){const s=[e,i];for(;s.length;){if((i=s.pop())-(e=s.pop())<=o)continue;const r=e+Math.ceil((i-e)/o/2)*o;N(t,r,e,i,n),s.push(e,r,r,i)}}class ot{constructor(t){this._maxEntries=void 0,this._minEntries=void 0,this.data=void 0,this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(.4*this._maxEntries)),this.clear()}search(t){let e=this.data;const i=[];if(!tt(t,e))return i;const o=this.toBBox,n=[];for(;e;){for(let s=0;s<e.children.length;s++){const r=e.children[s],a=e.leaf?o(r):r;tt(t,a)&&(e.leaf?i.push(r):V(t,a)?this._all(r,i):n.push(r))}e=n.pop()}return i}collides(t){let e=this.data;if(tt(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const n=e.children[o],s=e.leaf?this.toBBox(n):n;if(tt(t,s)){if(e.leaf||V(t,s))return!0;i.push(n)}}e=i.pop()}}return!1}load(t){if(t.length<this._minEntries){for(let e=0;e<t.length;e++)this.insert(t[e]);return}let e=this._build(t.slice(),0,t.length-1,0);if(this.data.children.length)if(this.data.height===e.height)this._splitRoot(this.data,e);else{if(this.data.height<e.height){const t=this.data;this.data=e,e=t}this._insert(e,this.data.height-e.height-1,!0)}else this.data=e}insert(t){this._insert(t,this.data.height-1)}clear(){this.data=et([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],n=[];let s,r,a=!1;for(;e||o.length;){if(e||(e=o.pop(),r=o[o.length-1],s=n.pop(),a=!0),e.leaf){const i=e.children.indexOf(t);-1!==i&&(e.children.splice(i,1),o.push(e),this._condense(o))}a||e.leaf||!V(e,i)?r?(s++,e=r.children[s],a=!1):e=null:(o.push(e),n.push(s),s=0,r=e,e=e.children[0])}}toBBox(t){return t}compareMinX(t,e){return t.minX-e.minX}compareMinY(t,e){return t.minY-e.minY}_all(t,e){const i=[];for(;t;)t.leaf?e.push(...t.children):i.push(...t.children),t=i.pop();return e}_build(t,e,i,o){const n=i-e+1;let s,r=this._maxEntries;if(n<=r)return s=et(t.slice(e,i+1)),J(s,this.toBBox),s;o||(o=Math.ceil(Math.log(n)/Math.log(r)),r=Math.ceil(n/Math.pow(r,o-1))),s=et([]),s.leaf=!1,s.height=o;const a=Math.ceil(n/r),h=a*Math.ceil(Math.sqrt(r));it(t,e,i,h,this.compareMinX);for(let n=e;n<=i;n+=h){const e=Math.min(n+h-1,i);it(t,n,e,a,this.compareMinY);for(let i=n;i<=e;i+=a){const n=Math.min(i+a-1,e);s.children.push(this._build(t,i,n,o-1))}}return J(s,this.toBBox),s}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,r=Infinity;for(let a=0;a<e.children.length;a++){const h=e.children[a],c=z(h),d=(n=t,s=h,(Math.max(s.maxX,n.maxX)-Math.min(s.minX,n.minX))*(Math.max(s.maxY,n.maxY)-Math.min(s.minY,n.minY))-c);d<r?(r=d,o=c<o?c:o,i=h):d===r&&c<o&&(o=c,i=h)}e=i||e.children[0]}var n,s;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),n=[],s=this._chooseSubtree(o,this.data,e,n);for(s.children.push(t),R(s,o);e>=0&&n[e].children.length>this._maxEntries;)this._split(n,e),e--;this._adjustParentBBoxes(o,n,e)}_split(t,e){const i=t[e],o=i.children.length,n=this._minEntries;this._chooseSplitAxis(i,n,o);const s=this._chooseSplitIndex(i,n,o),r=et(i.children.splice(s,i.children.length-s));r.height=i.height,r.leaf=i.leaf,J(i,this.toBBox),J(r,this.toBBox),e?t[e-1].children.push(r):this._splitRoot(i,r)}_splitRoot(t,e){this.data=et([t,e]),this.data.height=t.height+1,this.data.leaf=!1,J(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,n=Infinity,s=Infinity;for(let r=e;r<=i-e;r++){const e=$(t,0,r,this.toBBox),a=$(t,r,i,this.toBBox),h=Q(e,a),c=z(e)+z(a);h<n?(n=h,o=r,s=c<s?c:s):h===n&&c<s&&(s=c,o=r)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:Z,n=t.leaf?this.compareMinY:q;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,n)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const n=this.toBBox,s=$(t,0,e,n),r=$(t,i-e,i,n);let a=H(s)+H(r);for(let o=e;o<i-e;o++){const e=t.children[o];R(s,t.leaf?n(e):e),a+=H(s)}for(let o=i-e-1;o>=e;o--){const e=t.children[o];R(r,t.leaf?n(e):e),a+=H(r)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)R(e[o],t)}_condense(t){for(let e,i=t.length-1;i>=0;i--)0===t[i].children.length?i>0?(e=t[i-1].children,e.splice(e.indexOf(t[i]),1)):this.clear():J(t[i],this.toBBox)}}class nt{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new ot(t&&t.maxEntries?t.maxEntries:9),this.idToNode=new Map,this.nodeToId=new Map}setMaps(t,e){this.idToNode.set(String(t.id),e),this.nodeToId.set(e,String(t.id))}toBBox(t){const e=[],i=[];let o;if("Polygon"===t.geometry.type)o=t.geometry.coordinates[0];else if("LineString"===t.geometry.type)o=t.geometry.coordinates;else{if("Point"!==t.geometry.type)throw new Error("Not a valid feature to turn into a bounding box");o=[t.geometry.coordinates]}for(let t=0;t<o.length;t++)i.push(o[t][1]),e.push(o[t][0]);const n=Math.min(...i),s=Math.max(...i);return{minX:Math.min(...e),minY:n,maxX:Math.max(...e),maxY:s}}insert(t){if(this.idToNode.get(String(t.id)))throw new Error("Feature already exists");const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}load(t){const e=[],i=new Set;t.forEach(t=>{const o=this.toBBox(t);if(this.setMaps(t,o),i.has(String(t.id)))throw new Error(`Duplicate feature ID found ${t.id}`);i.add(String(t.id)),e.push(o)}),this.tree.load(e)}update(t){this.remove(t.id);const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}remove(t){const e=this.idToNode.get(t);if(!e)throw new Error(`${t} not inserted into the spatial index`);this.tree.remove(e)}clear(){this.tree.clear()}search(t){return this.tree.search(this.toBBox(t)).map(t=>this.nodeToId.get(t))}collides(t){return this.tree.collides(this.toBBox(t))}}class st{constructor(t){this.tracked=void 0,this.spatialIndex=void 0,this.store=void 0,this._onChange=()=>{},this.store={},this.spatialIndex=new nt,this.tracked=!t||!1!==t.tracked,t&&t.data&&this.load(t.data,t.validateFeature)}getId(){return W()}clone(t){return JSON.parse(JSON.stringify(t))}has(t){return Boolean(this.store[t])}load(t,e){if(0===t.length)return;const i=this.clone(t);i.forEach(t=>{t.id||(t.id=W()),this.tracked&&(t.properties.createdAt||(t.properties.createdAt=+new Date),t.properties.updatedAt||(t.properties.updatedAt=+new Date))});const o=[];i.forEach(t=>{e&&e(t),this.store[t.id]=t,o.push(t.id)}),this.spatialIndex.load(i),this._onChange(o,"create")}search(t,e){const i=this.spatialIndex.search(t).map(t=>this.store[t]);return this.clone(e?i.filter(e):i)}registerOnChange(t){this._onChange=(e,i)=>{t(e,i)}}getGeometryCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get geometry copy`);return this.clone(e.geometry)}getPropertiesCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get properties copy`);return this.clone(e.properties)}updateProperty(t){const e=[];t.forEach(({id:t,property:i,value:o})=>{const n=this.store[t];if(!n)throw new Error(`No feature with this (${t}), can not update geometry`);e.push(t),n.properties[i]=o,this.tracked&&(n.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}updateGeometry(t){const e=[];t.forEach(({id:t,geometry:i})=>{e.push(t);const o=this.store[t];if(!o)throw new Error(`No feature with this (${t}), can not update geometry`);o.geometry=this.clone(i),this.spatialIndex.update(o),this.tracked&&(o.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}create(e){const i=[];return e.forEach(({geometry:e,properties:o})=>{let n,s=t({},o);this.tracked&&(n=+new Date,o?(s.createdAt="number"==typeof o.createdAt?o.createdAt:n,s.updatedAt="number"==typeof o.updatedAt?o.updatedAt:n):s={createdAt:n,updatedAt:n});const r=this.getId(),a={id:r,type:"Feature",geometry:e,properties:s};this.store[r]=a,this.spatialIndex.insert(a),i.push(r)}),this._onChange&&this._onChange([...i],"create"),i}delete(t){t.forEach(t=>{if(!this.store[t])throw new Error("No feature with this id, can not delete");delete this.store[t],this.spatialIndex.remove(t)}),this._onChange&&this._onChange([...t],"delete")}copyAll(){return this.clone(Object.keys(this.store).map(t=>this.store[t]))}}class rt{constructor(e){this._modes=void 0,this._mode=void 0,this._adapter=void 0,this._enabled=!1,this._store=void 0,this._eventListeners=void 0,this._adapter=e.adapter,this._mode=new T,this._modes=t({},e.modes,{static:this._mode}),this._eventListeners={change:[],select:[],deselect:[]},this._store=e.data?new st({data:e.data}):new st;const i=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},o=(t,e)=>{this._eventListeners.change.forEach(i=>{i(t,e)});const{changed:o,unchanged:n}=i(t);"create"===e?this._adapter.render({created:o,deletedIds:[],unchanged:n,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:n,updated:o},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:n,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:n,updated:[]},this.getModeStyles())},n=t=>{this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:o}=i([t]);this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:e},this.getModeStyles())},s=t=>{this._eventListeners.deselect.forEach(t=>{t()});const{changed:e,unchanged:o}=i([t]);e&&this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:e},this.getModeStyles())};if(Object.keys(this._modes).forEach(t=>{this._modes[t].register({mode:t,store:this._store,setCursor:this._adapter.setCursor,project:this._adapter.project,unproject:this._adapter.unproject,onChange:o,onSelect:n,onDeselect:s})}),e.data){const t=this._store.copyAll().filter(t=>!(t.properties&&!Object.keys(this._modes).includes(t.properties.mode)&&(this._store.delete([t.id]),1)));this._adapter.render({created:t,deletedIds:[],unchanged:[],updated:[]},this.getModeStyles())}}getModeStyles(){const t={};return Object.keys(this._modes).forEach(e=>{t[e]=this._modes[e].styling}),t}setModeStyling(t,e){this._modes[t].styling=e}getSnapshot(){return this._store.copyAll()}get enabled(){return this._enabled}set enabled(t){throw new Error("Enabled is read only")}getCurrentMode(){return this._mode.mode}changeMode(t){if(!this._modes[t])throw new Error("No mode with this name present");this._mode.stop(),this._mode=this._modes[t],this._mode.start()}start(){this._enabled=!0,this._adapter.register({onClick:t=>{this._mode.onClick(t)},onMouseMove:t=>{this._mode.onMouseMove(t)},onKeyDown:t=>{this._mode.onKeyDown(t)},onKeyUp:t=>{this._mode.onKeyUp(t)},onDragStart:(t,e)=>{this._mode.onDragStart(t,e)},onDrag:t=>{this._mode.onDrag(t)},onDragEnd:(t,e)=>{this._mode.onDragEnd(t,e)}})}stop(){this._enabled=!1,this._adapter.unregister()}on(t,e){const i=this._eventListeners[t];i.includes(e)||i.push(e)}off(t,e){const i=this._eventListeners[t];i.includes(e)&&i.splice(i.indexOf(e),1)}}export{rt as TerraDraw,l as TerraDrawCircleMode,p as TerraDrawFreehandMode,i as TerraDrawGoogleMapsAdapter,o as TerraDrawLeafletAdapter,P as TerraDrawLineStringMode,n as TerraDrawMapboxGLAdapter,C as TerraDrawPointMode,x as TerraDrawPolygonMode,U as TerraDrawSelectMode};
|
|
1
|
+
function t(){return t=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var i=arguments[e];for(var o in i)Object.prototype.hasOwnProperty.call(i,o)&&(t[o]=i[o])}return t},t.apply(this,arguments)}function e(t,e=9){const i=Math.pow(10,e);return Math.round(t*i)/i}class i{constructor(t){this._heldKeys=new Set,this._cursor=void 0,this._cursorStyleSheet=void 0,this._coordinatePrecision=void 0,this._lib=void 0,this._map=void 0,this._onMouseMoveListener=void 0,this._onMouseMoveCallback=void 0,this._onClickListener=void 0,this._onRightClickListener=void 0,this._onClickCallback=void 0,this._onKeyUpListener=void 0,this._onDragStartListener=void 0,this._onDragListener=void 0,this._onDragEndListener=void 0,this._layers=!1,this.getMapContainer=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this._lib=t.lib,this._map=t.map,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9,this.getMapContainer=()=>this._map.getDiv(),this.project=(t,e)=>{const i=this._map.getBounds();if(void 0===i)throw new Error("cannot get bounds");const o=new this._lib.LatLng(i.getNorthEast().lat(),i.getSouthWest().lng()),n=this._map.getProjection();if(void 0===n)throw new Error("cannot get projection");const s=n.fromLatLngToPoint(o);if(null===s)throw new Error("cannot get projectedNorthWest");const r=n.fromLatLngToPoint({lng:t,lat:e});if(null===r)throw new Error("cannot get projected lng lat");const a=this._map.getZoom();if(void 0===a)throw new Error("cannot get zoom");const h=Math.pow(2,a);return{x:Math.floor((r.x-s.x)*h),y:Math.floor((r.y-s.y)*h)}},this.unproject=(t,e)=>{const i=this._map.getProjection();if(void 0===i)throw new Error("cannot get projection");const o=this._map.getBounds();if(void 0===o)throw new Error("cannot get bounds");const n=i.fromLatLngToPoint(o.getNorthEast());if(null===n)throw new Error("cannot get topRight");const s=i.fromLatLngToPoint(o.getSouthWest());if(null===s)throw new Error("cannot get bottomLeft");const r=this._map.getZoom();if(void 0===r)throw new Error("zoom get bounds");const a=Math.pow(2,r),h=new google.maps.Point(t/a+s.x,e/a+n.y),l=i.fromPointToLatLng(h);if(null===l)throw new Error("zoom get bounds");return{lng:l.lng(),lat:l.lat()}},this.setCursor=t=>{if(t!==this._cursor){if(this._cursorStyleSheet&&(this._cursorStyleSheet.remove(),this._cursorStyleSheet=void 0),"unset"!==t){const e=this.getMapContainer(),i=document.createElement("style");i.type="text/css",i.innerHTML=`#${e.id} [aria-label="Map"] { cursor: ${t} !important; }`,document.getElementsByTagName("head")[0].appendChild(i),this._cursorStyleSheet=i}this._cursor=t}}}circlePath(t,e,i){return"M "+t+" "+e+" m -"+i+", 0 a "+i+","+i+" 0 1,0 "+2*i+",0 a "+i+","+i+" 0 1,0 -"+2*i+",0"}register(t){this._onClickCallback=i=>{i.latLng&&t.onClick({lng:e(i.latLng.lng(),this._coordinatePrecision),lat:e(i.latLng.lat(),this._coordinatePrecision),containerX:i.domEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.domEvent.clientY-this.getMapContainer().offsetTop,button:0===i.domEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._onClickListener=this._map.addListener("click",this._onClickCallback),this._onRightClickListener=this._map.addListener("rightclick",this._onClickCallback),this._onMouseMoveCallback=i=>{i.latLng&&t.onMouseMove({lng:e(i.latLng.lng(),this._coordinatePrecision),lat:e(i.latLng.lat(),this._coordinatePrecision),containerX:i.domEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.domEvent.clientY-this.getMapContainer().offsetTop,button:0===i.domEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._onMouseMoveListener=this._map.addListener("mousemove",this._onMouseMoveCallback),this._onKeyUpListener=e=>{t.onKeyUp({key:e.key})},this.getMapContainer().addEventListener("keyup",this._onKeyUpListener);let i="not-dragging";this._onDragStartListener=t=>{i="pre-dragging"};const o=this.getMapContainer();o.addEventListener("mousedown",this._onDragStartListener),this._onDragListener=n=>{const s={x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop},{lng:r,lat:a}=this.unproject(s.x,s.y),h={lng:e(r,this._coordinatePrecision),lat:e(a,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]};"pre-dragging"===i?(i="dragging",t.onDragStart(h,t=>{this._map.setOptions({draggable:!1})})):"dragging"===i&&t.onDrag(h)},o.addEventListener("mousemove",this._onDragListener),this._onDragEndListener=n=>{if("dragging"===i){const i={x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop},{lng:s,lat:r}=this.unproject(i.x,i.y);t.onDragEnd({lng:e(s,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]},t=>{this._map.setOptions({draggable:t})})}i="not-dragging"},o.addEventListener("mouseup",this._onDragEndListener)}unregister(){this._onClickListener&&(this._onClickCallback=void 0,this._onClickListener.remove(),this._onClickListener=void 0),this._onRightClickListener&&(this._onClickCallback=void 0,this._onRightClickListener.remove(),this._onRightClickListener=void 0),this._onMouseMoveListener&&(this._onMouseMoveCallback=void 0,this._onMouseMoveListener.remove(),this._onMouseMoveListener=void 0),this._onKeyUpListener&&(this.getMapContainer().removeEventListener("keyup",this._onKeyUpListener),this._onKeyUpListener=void 0)}render(t,e){this._layers?(t.deletedIds.forEach(t=>{const e=this._map.data.getFeatureById(t);e&&this._map.data.remove(e)}),t.updated.forEach(t=>{if(!t||!t.id)throw new Error("Feature is not valid");const e=this._map.data.getFeatureById(t.id);if(!e)throw new Error("Feature could not be found by Google Maps API");switch(e.forEachProperty((t,i)=>{e.setProperty(i,void 0)}),Object.keys(t.properties).forEach(i=>{e.setProperty(i,t.properties[i])}),t.geometry.type){case"Point":{const i=t.geometry.coordinates;e.setGeometry(new google.maps.Data.Point(new google.maps.LatLng(i[1],i[0])))}break;case"LineString":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=i[t],n=new google.maps.LatLng(e[1],e[0]);o.push(n)}e.setGeometry(new google.maps.Data.LineString(o))}break;case"Polygon":{const i=t.geometry.coordinates,o=[];for(let t=0;t<i.length;t++){const e=[];for(let o=0;o<i[t].length;o++){const n=new google.maps.LatLng(i[t][o][1],i[t][o][0]);e.push(n)}o.push(e)}e.setGeometry(new google.maps.Data.Polygon(o))}}}),t.created.forEach(t=>{this._map.data.addGeoJson(t)})):(this._map.data.addListener("click",t=>{this._onClickCallback&&this._onClickCallback(t)}),this._map.data.addListener("mousemove",t=>{this._onMouseMoveCallback&&this._onMouseMoveCallback(t)}));const i={type:"FeatureCollection",features:[...t.created]};this._map.data.addGeoJson(i),this._map.data.setStyle(t=>{const i=t.getProperty("mode"),o=t.getGeometry();if(!o)throw new Error("Google Maps geometry not found");const n=o.getType(),s={};t.forEachProperty((t,e)=>{s[e]=t});const r=e[i]({type:"Feature",geometry:{type:n,coordinates:[]},properties:s});switch(n){case"Point":return{clickable:!1,icon:{path:this.circlePath(0,0,r.pointWidth),fillColor:r.pointColor,fillOpacity:1,strokeColor:r.pointOutlineColor,strokeWeight:r.pointOutlineWidth,rotation:0,scale:1}};case"LineString":return{strokeColor:r.lineStringColor,strokeWeight:r.lineStringWidth};case"Polygon":return{strokeColor:r.polygonOutlineColor,strokeWeight:r.polygonOutlineWidth,fillOpacity:r.polygonFillOpacity,fillColor:r.polygonFillColor}}throw Error("Unknown feature type")}),this._layers=!0}}class o{constructor(t){this._heldKeys=new Set,this._lib=void 0,this._coordinatePrecision=void 0,this._map=void 0,this._onMouseMoveListener=void 0,this._onClickListener=void 0,this._onKeyUpListener=void 0,this._onKeyDownListener=void 0,this._onDragStartListener=void 0,this._onDragListener=void 0,this._onDragEndListener=void 0,this._layer=void 0,this._panes={},this.project=void 0,this.unproject=void 0,this.setCursor=void 0,this.getMapContainer=void 0,this._lib=t.lib,this._map=t.map,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9,this.getMapContainer=()=>this._map.getContainer(),this.project=(t,e)=>{const{x:i,y:o}=this._map.latLngToContainerPoint({lng:t,lat:e});return{x:i,y:o}},this.unproject=(t,e)=>{const{lng:i,lat:o}=this._map.containerPointToLatLng({x:t,y:e});return{lng:i,lat:o}},this.setCursor=t=>{"unset"===t?this.getMapContainer().style.removeProperty("cursor"):this.getMapContainer().style.cursor=t}}createPaneStyleSheet(t,e){const i=document.createElement("style");return i.type="text/css",i.innerHTML=`.leaflet-${t} {z-index: ${e};}`,document.getElementsByTagName("head")[0].appendChild(i),this._map.createPane(t),i}register(t){const i=this.getMapContainer();let o="not-dragging";this._onClickListener=i=>{"not-dragging"!==o&&"pre-dragging"!==o||t.onClick({lng:e(i.latlng.lng,this._coordinatePrecision),lat:e(i.latlng.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("mouseup",this._onClickListener),this._map.on("contextmenu",this._onClickListener),this._onMouseMoveListener=i=>{i.originalEvent.preventDefault(),t.onMouseMove({lng:e(i.latlng.lng,this._coordinatePrecision),lat:e(i.latlng.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("mousemove",this._onMouseMoveListener),this._onDragStartListener=t=>{o="pre-dragging"},i.addEventListener("pointerdown",this._onDragStartListener),this._onDragListener=n=>{const s={x:n.clientX-i.offsetLeft,y:n.clientY-i.offsetTop},{lng:r,lat:a}=this._map.containerPointToLatLng(s),h={lng:e(r,this._coordinatePrecision),lat:e(a,this._coordinatePrecision),containerX:n.clientX-i.offsetLeft,containerY:n.clientY-i.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]};"pre-dragging"===o?(o="dragging",t.onDragStart(h,t=>{t?this._map.dragging.enable():this._map.dragging.disable()})):"dragging"===o&&t.onDrag(h)},i.addEventListener("pointermove",this._onDragListener),this._onDragEndListener=n=>{if(n.preventDefault(),"dragging"===o){const s={x:n.clientX-i.offsetLeft,y:n.clientY-i.offsetTop},{lng:r,lat:a}=this._map.containerPointToLatLng(s);return t.onDragEnd({lng:e(r,this._coordinatePrecision),lat:e(a,this._coordinatePrecision),containerX:n.clientX-i.offsetLeft,containerY:n.clientY-i.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]},t=>{t?this._map.dragging.enable():this._map.dragging.disable()}),o="after-dragging",void this._map.dragging.enable()}o="not-dragging",this._map.dragging.enable()},i.addEventListener("pointerup",this._onDragEndListener),this._onKeyUpListener=e=>{e.preventDefault(),this._heldKeys.delete(e.key),t.onKeyUp({key:e.key})},i.addEventListener("keyup",this._onKeyUpListener),this._onKeyDownListener=e=>{e.preventDefault(),this._heldKeys.add(e.key),t.onKeyDown({key:e.key})},i.addEventListener("keydown",this._onKeyDownListener)}unregister(){this._onClickListener&&(this._map.off("contextmenu",this._onClickListener),this._map.off("click",this._onClickListener),this._onClickListener=void 0),this._onMouseMoveListener&&(this._map.off("click",this._onClickListener),this._onClickListener=void 0),Object.keys(this._panes).forEach(t=>{const e=this._map.getPane(t);e&&e.remove()})}render(t,e){const i=[...t.created,...t.updated,...t.unchanged];this._layer&&this._map.removeLayer(this._layer);const o=this._lib.geoJSON({type:"FeatureCollection",features:i},{pointToLayer:(t,i)=>{if(!t.properties)throw new Error("Feature has no properties");if("string"!=typeof t.properties.mode)throw new Error("Feature mode is not a string");const o=(0,e[t.properties.mode])(t),n=String(o.zIndex);return this._panes[n]||(this._panes[n]=this.createPaneStyleSheet(n,o.zIndex)),this._lib.circleMarker(i,{radius:o.pointWidth,stroke:o.pointOutlineWidth||!1,color:o.pointOutlineColor,weight:o.pointOutlineWidth,fillOpacity:.8,fillColor:o.pointColor,pane:n,interactive:!1})},style:t=>{if(!t||!t.properties)return{};const i=t,o=(0,e[i.properties.mode])(i);return"LineString"===i.geometry.type?{interactive:!1,color:o.lineStringColor,weight:o.lineStringWidth}:"Polygon"===i.geometry.type?{interactive:!1,fillOpacity:o.polygonFillOpacity,fillColor:o.polygonFillColor,weight:o.polygonOutlineWidth,stroke:!0,color:o.polygonFillColor}:{}}});this._map.addLayer(o),this._layer=o}}class n{constructor(t){this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this.getMapContainer=void 0,this._heldKeys=new Set,this._coordinatePrecision=void 0,this._map=void 0,this._onMouseMoveListener=void 0,this._onClickListener=void 0,this._onDragStartListener=void 0,this._onDragListener=void 0,this._onDragEndListener=void 0,this._onKeyDownListener=void 0,this._onKeyUpListener=void 0,this._rendered={},this._map=t.map,this._coordinatePrecision="number"==typeof t.coordinatePrecision?t.coordinatePrecision:9,this.project=(t,e)=>{const{x:i,y:o}=this._map.project({lng:t,lat:e});return{x:i,y:o}},this.unproject=(t,e)=>{const{lng:i,lat:o}=this._map.unproject({x:t,y:e});return{lng:i,lat:o}},this.setCursor=t=>{this._map.getCanvas().style.cursor=t},this.getMapContainer=()=>this._map.getContainer()}_addGeoJSONSource(t,e){this._map.addSource(t,{type:"geojson",data:{type:"FeatureCollection",features:e}})}_addFillLayer(t,e){return this._map.addLayer({id:t,source:t,type:"fill",filter:["all",["match",["geometry-type"],"Polygon",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"fill-color":["get","polygonFillColor"],"fill-opacity":["get","polygonFillOpacity"]}})}_addFillOutlineLayer(t,e,i){const o=this._map.addLayer({id:t+"outline",source:t,type:"line",filter:["all",["match",["geometry-type"],"Polygon",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"line-width":["get","polygonOutlineWidth"],"line-color":["get","polygonOutlineColor"]}});return i&&this._map.moveLayer(t,i),o}_addLineLayer(t,e,i){const o=this._map.addLayer({id:t,source:t,type:"line",filter:["all",["match",["geometry-type"],"LineString",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"line-width":["get","lineStringWidth"],"line-color":["get","lineStringColor"]}});return i&&this._map.moveLayer(t,i),o}_addPointLayer(t,e,i){const o=this._map.addLayer({id:t,source:t,type:"circle",filter:["all",["match",["geometry-type"],"Point",!0,!1],["match",["get","mode"],e,!0,!1]],paint:{"circle-stroke-color":["get","pointOutlineColor"],"circle-stroke-width":["get","pointOutlineWidth"],"circle-radius":["get","pointWidth"],"circle-color":["get","pointColor"]}});return i&&this._map.moveLayer(t,i),o}_addLayer(t,e,i,o){"Point"===i&&this._addPointLayer(t,e,o),"LineString"===i&&this._addLineLayer(t,e,o),"Polygon"===i&&(this._addFillLayer(t,e),this._addFillOutlineLayer(t,e,o))}_addGeoJSONLayer(t,e,i){const o=`td-${t}-${e.toLowerCase()}`;return this._addGeoJSONSource(o,i),this._addLayer(o,t,e),o}_setGeoJSONLayerData(t,e,i){const o=`td-${t}-${e.toLowerCase()}`;return this._map.getSource(o).setData({type:"FeatureCollection",features:i}),o}register(t){this._onClickListener=i=>{t.onClick({lng:e(i.lngLat.lng,this._coordinatePrecision),lat:e(i.lngLat.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("click",this._onClickListener),this._map.on("contextmenu",this._onClickListener),this._onMouseMoveListener=i=>{t.onMouseMove({lng:e(i.lngLat.lng,this._coordinatePrecision),lat:e(i.lngLat.lat,this._coordinatePrecision),containerX:i.originalEvent.clientX-this.getMapContainer().offsetLeft,containerY:i.originalEvent.clientY-this.getMapContainer().offsetTop,button:0===i.originalEvent.button?"left":"right",heldKeys:[...this._heldKeys]})},this._map.on("mousemove",this._onMouseMoveListener);let i="not-dragging";this._onDragStartListener=t=>{i="pre-dragging"};const o=this.getMapContainer();o.addEventListener("mousedown",this._onDragStartListener),this._onDragListener=n=>{const{lng:s,lat:r}=this._map.unproject({x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop}),a={lng:e(s,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]};"pre-dragging"===i?(i="dragging",t.onDragStart(a,t=>{t?this._map.dragPan.enable():this._map.dragPan.disable()})):"dragging"===i&&t.onDrag(a)},o.addEventListener("mousemove",this._onDragListener),this._onDragEndListener=n=>{if("dragging"===i){const i={x:n.clientX-o.offsetLeft,y:n.clientY-o.offsetTop},{lng:s,lat:r}=this._map.unproject(i);t.onDragEnd({lng:e(s,this._coordinatePrecision),lat:e(r,this._coordinatePrecision),containerX:n.clientX-o.offsetLeft,containerY:n.clientY-o.offsetTop,button:0===n.button?"left":"right",heldKeys:[...this._heldKeys]},t=>{t?this._map.dragPan.enable():this._map.dragPan.disable()})}i="not-dragging"},o.addEventListener("mouseup",this._onDragEndListener),this._onKeyUpListener=e=>{e.preventDefault(),this._heldKeys.delete(e.key),t.onKeyUp({key:e.key})},o.addEventListener("keyup",this._onKeyUpListener),this._onKeyDownListener=e=>{e.preventDefault(),this._heldKeys.add(e.key),t.onKeyDown({key:e.key})},o.addEventListener("keydown",this._onKeyDownListener)}unregister(){this._onClickListener&&(this._map.off("contextmenue",this._onClickListener),this._map.off("click",this._onClickListener),this._onClickListener=void 0),this._onMouseMoveListener&&(this._map.off("mousemove",this._onMouseMoveListener),this._onMouseMoveListener=void 0),this._onKeyUpListener&&this._map.getCanvas().removeEventListener("keypress",this._onKeyUpListener),this._onDragStartListener&&this._map.getCanvas().removeEventListener("mousedown",this._onDragStartListener),this._onDragListener&&this._map.getCanvas().removeEventListener("mousemove",this._onDragListener),this._onDragEndListener&&this._map.getCanvas().removeEventListener("mouseup",this._onDragEndListener)}render(t,e){const i=[...t.created,...t.updated,...t.unchanged],o={};Object.keys(e).forEach(t=>{o[t]||(o[t]={points:[],linestrings:[],polygons:[]})});for(let t=0;t<i.length;t++){const n=i[t];Object.keys(e).forEach(t=>{const{properties:i}=n;if(i.mode!==t)return;const s=e[t](n);"Point"===n.geometry.type?(i.pointColor=s.pointColor,i.pointOutlineColor=s.pointOutlineColor,i.pointOutlineWidth=s.pointOutlineWidth,i.pointWidth=s.pointWidth,o[t].points.push(n)):"LineString"===n.geometry.type?(i.lineStringColor=s.lineStringColor,i.lineStringWidth=s.lineStringWidth,o[t].linestrings.push(n)):"Polygon"===n.geometry.type&&(i.polygonFillColor=s.polygonFillColor,i.polygonFillOpacity=s.polygonFillOpacity,i.polygonOutlineColor=s.polygonOutlineColor,i.polygonOutlineWidth=s.polygonOutlineWidth,o[t].polygons.push(n))})}Object.keys(e).forEach(t=>{if(!o[t])return;const{points:e,linestrings:i,polygons:n}=o[t];if(this._rendered[t]){const o=this._setGeoJSONLayerData(t,"Point",e);this._setGeoJSONLayerData(t,"LineString",i),this._setGeoJSONLayerData(t,"Polygon",n),this._map.moveLayer(o)}else this._addGeoJSONLayer(t,"Point",e),this._addGeoJSONLayer(t,"LineString",i),this._addGeoJSONLayer(t,"Polygon",n),this._rendered[t]=!0}),this._map.style&&(this._map._frame&&(this._map._frame.cancel(),this._map._frame=null),this._map._render())}}function s(t,e){const i=t=>t*Math.PI/180,o=i(t[1]),n=i(t[0]),s=i(e[1]),r=s-o,a=i(e[0])-n,h=Math.sin(r/2)*Math.sin(r/2)+Math.cos(o)*Math.cos(s)*Math.sin(a/2)*Math.sin(a/2);return 2*Math.atan2(Math.sqrt(h),Math.sqrt(1-h))*6371e3/1e3}function r(t){return t%360*Math.PI/180}function a(t){return t%(2*Math.PI)*180/Math.PI}function h(t,e,i){const o=r(t[0]),n=r(t[1]),s=r(i),h=function(t){return t/6371.0088}(e),l=Math.asin(Math.sin(n)*Math.cos(h)+Math.cos(n)*Math.sin(h)*Math.cos(s));return[a(o+Math.atan2(Math.sin(s)*Math.sin(h)*Math.cos(n),Math.cos(h)-Math.sin(n)*Math.sin(l))),a(l)]}function l(t){const{center:e,radiusKilometers:i}=t,o=t.steps?t.steps:64,n=[];for(let t=0;t<o;t++)n.push(h(e,i,-360*t/o));return n.push(n[0]),{type:"Feature",geometry:{type:"Polygon",coordinates:[n]},properties:{}}}class d{get state(){return this._state}set state(t){throw new Error("Please use the modes lifecycle methods")}get styles(){return this._styles}set styles(t){if("object"!=typeof t)throw new Error("Styling must be an object");this.onStyleChange([],"styling"),this._styles=t}registerBehaviors(t){}constructor(e){this._state=void 0,this._styles=void 0,this.behaviors=[],this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.onStyleChange=void 0,this.store=void 0,this.unproject=void 0,this.project=void 0,this.setCursor=void 0,this._state="unregistered",this._styles=e&&e.styles?t({},e.styles):{},this.pointerDistance=e&&e.pointerDistance||40,this.coordinatePrecision=e&&e.coordinatePrecision||9}setStarted(){if("stopped"!==this._state&&"registered"!==this._state)throw new Error("Mode must be unregistered or stopped to start");this._state="started"}setStopped(){if("started"!==this._state)throw new Error("Mode must be started to be stopped");this._state="stopped"}register(t){if("unregistered"!==this._state)throw new Error("Can not register unless mode is unregistered");this._state="registered",this.store=t.store,this.store.registerOnChange(t.onChange),this.project=t.project,this.unproject=t.unproject,this.onSelect=t.onSelect,this.onDeselect=t.onDeselect,this.setCursor=t.setCursor,this.onStyleChange=t.onChange,this.registerBehaviors({mode:t.mode,store:this.store,project:this.project,unproject:this.unproject,pointerDistance:this.pointerDistance,coordinatePrecision:this.coordinatePrecision})}onDeselect(t){}onSelect(t){}styleFeature(t){}}class c extends d{constructor(t){super(t),this.mode="circle",this.center=void 0,this.clickCount=0,this.currentCircleId=void 0,this.keyEvents=void 0,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onClick(t){if(0===this.clickCount){this.center=[t.lng,t.lat];const e=l({center:this.center,radiusKilometers:1e-5}),[i]=this.store.create([{geometry:e.geometry,properties:{mode:this.mode}}]);this.currentCircleId=i,this.clickCount++}else this.center=void 0,this.currentCircleId=void 0,this.clickCount=0}onMouseMove(t){if(1===this.clickCount&&this.center&&this.currentCircleId){const e=s(this.center,[t.lng,t.lat]),i=l({center:this.center,radiusKilometers:e});this.store.updateGeometry([{id:this.currentCircleId,geometry:i.geometry}])}}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentCircleId&&this.store.delete([this.currentCircleId])}catch(t){}this.center=void 0,this.currentCircleId=void 0,this.clickCount=0}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(this.styles.fillColor&&(i.polygonFillColor=this.styles.fillColor),this.styles.outlineColor&&(i.polygonOutlineColor=this.styles.outlineColor),this.styles.outlineWidth&&(i.polygonOutlineWidth=this.styles.outlineWidth),this.styles.fillOpacity&&(i.polygonFillOpacity=this.styles.fillOpacity),i):i}}class p extends d{constructor(t){super(t),this.mode="freehand",this.startingClick=!1,this.currentId=void 0,this.skip=0,this.everyNthMouseEvent=void 0,this.keyEvents=void 0,this.everyNthMouseEvent=t&&t.everyNthMouseEvent||10,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onMouseMove(t){if(this.currentId&&!1!==this.startingClick){if(this.skip>this.everyNthMouseEvent){this.skip=0;const e=this.store.getGeometryCopy(this.currentId);e.coordinates[0].pop(),this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[...e.coordinates[0],[t.lng,t.lat],e.coordinates[0][0]]]}}])}this.skip++}}onClick(t){if(!1===this.startingClick){const[e]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);return this.currentId=e,void(this.startingClick=!0)}this.startingClick=!1,this.currentId=void 0}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.startingClick=!1}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Polygon"===e.geometry.type&&e.properties.mode===this.mode?(this.styles.fillColor&&(i.polygonFillColor=this.styles.fillColor),this.styles.outlineColor&&(i.polygonOutlineColor=this.styles.outlineColor),this.styles.outlineWidth&&(i.polygonOutlineWidth=this.styles.outlineWidth),this.styles.fillOpacity&&(i.polygonFillOpacity=this.styles.fillOpacity),i):i}}function g(t){let e;if("Polygon"===t.geometry.type)e=t.geometry.coordinates;else{if("LineString"!==t.geometry.type)throw new Error("Self intersects only accepts Polygons and LineStrings");e=[t.geometry.coordinates]}const i=[];for(let t=0;t<e.length;t++)for(let i=0;i<e[t].length-1;i++)for(let o=0;o<e.length;o++)for(let s=0;s<e[o].length-1;s++)n(t,i,o,s);return i.length>0;function o(t){return t<0||t>1}function n(t,n,s,r){const a=e[t][n],h=e[t][n+1],l=e[s][r],d=e[s][r+1],c=function(t,e,i,o){if(u(t,i)||u(t,o)||u(e,i)||u(o,i))return null;const n=t[0],s=t[1],r=e[0],a=e[1],h=i[0],l=i[1],d=o[0],c=o[1],p=(n-r)*(l-c)-(s-a)*(h-d);return 0===p?null:[((n*a-s*r)*(h-d)-(n-r)*(h*c-l*d))/p,((n*a-s*r)*(l-c)-(s-a)*(h*c-l*d))/p]}(a,h,l,d);if(null===c)return;let p,g;p=h[0]!==a[0]?(c[0]-a[0])/(h[0]-a[0]):(c[1]-a[1])/(h[1]-a[1]),g=d[0]!==l[0]?(c[0]-l[0])/(d[0]-l[0]):(c[1]-l[1])/(d[1]-l[1]),o(p)||o(g)||(c.toString(),i.push(c))}}function u(t,e){return t[0]===e[0]&&t[1]===e[1]}const y=(t,e)=>{const{x:i,y:o}=t,{x:n,y:s}=e,r=n-i,a=s-o;return Math.sqrt(a*a+r*r)};class f{constructor({store:t,mode:e,project:i,unproject:o,pointerDistance:n,coordinatePrecision:s}){this.store=void 0,this.mode=void 0,this.project=void 0,this.unproject=void 0,this.pointerDistance=void 0,this.coordinatePrecision=void 0,this.store=t,this.mode=e,this.project=i,this.unproject=o,this.pointerDistance=n,this.coordinatePrecision=s}}class m extends f{constructor(t){super(t)}create(t){const{containerX:e,containerY:i}=t,o=this.pointerDistance/2;return{type:"Feature",properties:{},geometry:{type:"Polygon",coordinates:[[this.unproject(e-o,i-o),this.unproject(e+o,i-o),this.unproject(e+o,i+o),this.unproject(e-o,i+o),this.unproject(e-o,i-o)].map(t=>[t.lng,t.lat])]}}}}class _ extends f{constructor(t){super(t)}measure(t,e){const{x:i,y:o}=this.project(e[0],e[1]);return y({x:i,y:o},{x:t.containerX,y:t.containerY})}}class C extends f{constructor(t,e,i){super(t),this.config=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.getSnappableCoordinate=(t,e)=>this.getSnappable(t,t=>Boolean(t.properties&&t.properties.mode===this.mode&&t.id!==e)),this.config=t,this.pixelDistance=e,this.clickBoundingBox=i}getSnappable(t,e){const i=this.clickBoundingBox.create(t),o=this.store.search(i,e),n={coord:void 0,minDist:Infinity};return o.forEach(e=>{let i;if("Polygon"===e.geometry.type)i=e.geometry.coordinates[0];else{if("LineString"!==e.geometry.type)return;i=e.geometry.coordinates}i.forEach(e=>{const i=this.pixelDistance.measure(t,e);i<n.minDist&&i<this.pointerDistance&&(n.coord=e)})}),n.coord}}class v extends d{constructor(t){super(t),this.mode="linestring",this.currentCoordinate=0,this.currentId=void 0,this.allowSelfIntersections=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.snapping=void 0,this.snappingEnabled=!(!t||void 0===t.snapping)&&t.snapping,this.allowSelfIntersections=!t||void 0===t.allowSelfIntersections||t.allowSelfIntersections,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}registerBehaviors(t){this.snapping=new C(t,new _(t),new m(t))}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onMouseMove(t){if(!this.currentId||0===this.currentCoordinate)return;const e=this.store.getGeometryCopy(this.currentId);e.coordinates.pop();const i=this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId);this.store.updateGeometry([{id:this.currentId,geometry:{type:"LineString",coordinates:[...e.coordinates,i||[t.lng,t.lat]]}}])}onClick(t){const e=this.currentId&&this.snappingEnabled&&this.snapping.getSnappableCoordinate(t,this.currentId)||[t.lng,t.lat];if(0===this.currentCoordinate){const[t]=this.store.create([{geometry:{type:"LineString",coordinates:[e,e]},properties:{mode:this.mode}}]);this.currentId=t,this.currentCoordinate++}else if(1===this.currentCoordinate&&this.currentId){const t=this.store.getGeometryCopy(this.currentId);this.store.updateGeometry([{id:this.currentId,geometry:{type:"LineString",coordinates:[t.coordinates[0],e,e]}}]),this.currentCoordinate++}else if(this.currentId){const i=this.store.getGeometryCopy(this.currentId),[o,n]=i.coordinates[i.coordinates.length-2],{x:s,y:r}=this.project(o,n);if(y({x:s,y:r},{x:t.containerX,y:t.containerY})<this.pointerDistance)i.coordinates.pop(),this.store.updateGeometry([{id:this.currentId,geometry:{type:"LineString",coordinates:[...i.coordinates]}}]),this.currentCoordinate=0,this.currentId=void 0;else{const t={type:"LineString",coordinates:[...i.coordinates,e]};if(!this.allowSelfIntersections&&g({type:"Feature",geometry:t,properties:{}}))return;this.store.updateGeometry([{id:this.currentId,geometry:t}]),this.currentCoordinate++}}}onKeyDown(){}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onDragStart(){}onDrag(){}onDragEnd(){}cleanUp(){try{this.currentId&&this.store.delete([this.currentId])}catch(t){}this.currentId=void 0,this.currentCoordinate=0}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"LineString"===e.geometry.type&&e.properties.mode===this.mode?(this.styles.lineStringColor&&(i.lineStringColor=this.styles.lineStringColor),this.styles.lineStringWidth&&(i.lineStringWidth=this.styles.lineStringWidth),i):i}}class P extends d{constructor(t){super(t),this.mode="point"}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onClick(t){if(!this.store)throw new Error("Mode must be registered first");this.store.create([{geometry:{type:"Point",coordinates:[t.lng,t.lat]},properties:{mode:this.mode}}])}onMouseMove(){}onKeyDown(){}onKeyUp(){}cleanUp(){}onDragStart(){}onDrag(){}onDragEnd(){}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});return"Feature"===e.type&&"Point"===e.geometry.type&&e.properties.mode===this.mode?(this.styles.pointColor&&(i.pointColor=this.styles.pointColor),this.styles.pointOutlineColor&&(i.pointOutlineColor=this.styles.pointOutlineColor),this.styles.pointWidth&&(i.pointWidth=this.styles.pointWidth),i):i}}function M(t,e){return t[0]===e[0]&&t[1]===e[1]}class L extends f{constructor(t,e){super(t),this.config=void 0,this.pixelDistance=void 0,this._startEndPoints=[],this.config=t,this.pixelDistance=e}get ids(){return this._startEndPoints.concat()}set ids(t){}create(t,e){if(this.ids.length)throw new Error("Opening and closing points already creating");if(t.length<=3)throw new Error("Requires at least 4 cooridnates");this._startEndPoints=this.store.create([{geometry:{type:"Point",coordinates:t[0]},properties:{mode:e,closingPoint:!0}},{geometry:{type:"Point",coordinates:t[t.length-2]},properties:{mode:e,closingPoint:!0}}])}delete(){this.ids.length&&(this.store.delete(this.ids),this._startEndPoints=[])}update(t){if(2!==this.ids.length)throw new Error("No closing points to update");this.store.updateGeometry([{id:this.ids[0],geometry:{type:"Point",coordinates:t[0]}},{id:this.ids[1],geometry:{type:"Point",coordinates:t[t.length-3]}}])}isClosingPoint(t){const e=this.store.getGeometryCopy(this.ids[0]),i=this.store.getGeometryCopy(this.ids[1]),o=this.pixelDistance.measure(t,e.coordinates),n=this.pixelDistance.measure(t,i.coordinates);return{isClosing:o<this.pointerDistance,isPreviousClosing:n<this.pointerDistance}}}class x extends d{constructor(t){super(t),this.mode="polygon",this.currentCoordinate=0,this.currentId=void 0,this.allowSelfIntersections=void 0,this.keyEvents=void 0,this.snappingEnabled=void 0,this.isClosed=!1,this.snapping=void 0,this.pixelDistance=void 0,this.closingPoints=void 0,this.snappingEnabled=!(!t||void 0===t.snapping)&&t.snapping,this.allowSelfIntersections=!t||void 0===t.allowSelfIntersections||t.allowSelfIntersections,this.keyEvents=t&&t.keyEvents?t.keyEvents:{cancel:"Escape"}}registerBehaviors(t){this.pixelDistance=new _(t),this.snapping=new C(t,this.pixelDistance,new m(t)),this.closingPoints=new L(t,this.pixelDistance)}start(){this.setStarted(),this.setCursor("crosshair")}stop(){this.setStopped(),this.setCursor("unset"),this.cleanUp()}onMouseMove(t){if(this.setCursor("crosshair"),!this.currentId||0===this.currentCoordinate)return;const e=this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0,i=this.store.getGeometryCopy(this.currentId).coordinates[0];let o;if(e&&(t.lng=e[0],t.lat=e[1]),1===this.currentCoordinate){const e=1/Math.pow(10,this.coordinatePrecision-1),n=Math.max(1e-6,e);o=[i[0],[t.lng,t.lat],[t.lng,t.lat+n],i[0]]}else if(2===this.currentCoordinate)o=[i[0],i[1],[t.lng,t.lat],i[0]];else{const{isClosing:e,isPreviousClosing:n}=this.closingPoints.isClosingPoint(t);n||e?(this.setCursor("pointer"),o=[...i.slice(0,-2),i[0],i[0]],this.isClosed||(this.isClosed=!0)):(this.isClosed&&(this.isClosed=!1),o=[...i.slice(0,-2),[t.lng,t.lat],i[0]])}this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[o]}}]),this.closingPoints.ids.length&&this.closingPoints.update(o)}onClick(t){const e=this.currentId&&this.snappingEnabled?this.snapping.getSnappableCoordinate(t,this.currentId):void 0;if(0===this.currentCoordinate){e&&(t.lng=e[0],t.lat=e[1]);const[i]=this.store.create([{geometry:{type:"Polygon",coordinates:[[[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat],[t.lng,t.lat]]]},properties:{mode:this.mode}}]);this.currentId=i,this.currentCoordinate++}else if(1===this.currentCoordinate&&this.currentId){e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId);if(M([t.lng,t.lat],i.coordinates[0][0]))return;this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[i.coordinates[0][0],[t.lng,t.lat],[t.lng,t.lat],i.coordinates[0][0]]]}}]),this.currentCoordinate++}else if(2===this.currentCoordinate&&this.currentId){e&&(t.lng=e[0],t.lat=e[1]);const i=this.store.getGeometryCopy(this.currentId).coordinates[0];if(M([t.lng,t.lat],i[1]))return;2===this.currentCoordinate&&this.closingPoints.create(i,"polygon"),this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[i[0],i[1],[t.lng,t.lat],[t.lng,t.lat],i[0]]]}}]),this.currentCoordinate++}else if(this.currentId){const i=this.store.getGeometryCopy(this.currentId).coordinates[0],{isClosing:o,isPreviousClosing:n}=this.closingPoints.isClosingPoint(t);if(n||o)this.store.updateGeometry([{id:this.currentId,geometry:{type:"Polygon",coordinates:[[...i.slice(0,-2),i[0]]]}}]),this.currentCoordinate=0,this.currentId=void 0,this.closingPoints.delete();else{if(e&&(t.lng=e[0],t.lat=e[1]),M([t.lng,t.lat],i[this.currentCoordinate-1]))return;const o=function(t=[[[0,0],[0,1],[1,1],[1,0],[0,0]]]){return{type:"Feature",geometry:{type:"Polygon",coordinates:t},properties:{}}}([[...i.slice(0,-1),[t.lng,t.lat],i[0]]]);if(this.currentCoordinate>2&&!this.allowSelfIntersections&&g(o))return;this.store.updateGeometry([{id:this.currentId,geometry:o.geometry}]),this.currentCoordinate++}}}onKeyUp(t){t.key===this.keyEvents.cancel&&this.cleanUp()}onKeyDown(){}onDragStart(){this.setCursor("unset")}onDrag(){}onDragEnd(){this.setCursor("crosshair")}cleanUp(){try{this.currentId&&this.store.delete([this.currentId]),this.closingPoints.ids.length&&this.closingPoints.delete()}catch(t){}this.currentId=void 0,this.currentCoordinate=0}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});if(e.properties.mode===this.mode){if("Polygon"===e.geometry.type)return i.polygonFillColor=this.styles.fillColor||i.polygonFillColor,i.polygonOutlineColor=this.styles.outlineColor||i.polygonOutlineColor,i.polygonOutlineWidth=this.styles.outlineWidth||i.polygonOutlineWidth,i.polygonFillColor=this.styles.fillColor||i.polygonFillColor,i.zIndex=10,i;if("Point"===e.geometry.type)return i.pointWidth=this.styles.closingPointWidth||i.pointWidth,i.pointColor=this.styles.closingPointColor||i.pointColor,i.pointOutlineColor=this.styles.closingPointOutlineColor||"#ffffff",i.pointOutlineWidth=this.styles.closingPointOutlineWidth||2,i.zIndex=30,i}return i}}class E extends d{constructor(t){super({styles:t.styles}),this.mode="render"}registerBehaviors(t){this.mode=t.mode}start(){this.setStarted()}stop(){this.setStopped()}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}styleFeature(){return t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0},this.styles)}}function S(t,i,o){const n=s(t,i),l=function(t,e){const i=r(t[0]),o=r(e[0]),n=r(t[1]),s=r(e[1]),h=Math.sin(o-i)*Math.cos(s),l=Math.cos(n)*Math.sin(s)-Math.sin(n)*Math.cos(s)*Math.cos(o-i);return a(Math.atan2(h,l))}(t,i),d=h(t,n/2,l);return[e(d[0],o),e(d[1],o)]}function k(t,e){const i=[];for(let o=0;o<t.length-1;o++){const n=S(t[o],t[o+1],e);i.push(n)}return i}class w extends f{constructor(t,e){super(t),this.config=void 0,this.selectionPointBehavior=void 0,this._midPoints=[],this.config=t,this.selectionPointBehavior=e}get ids(){return this._midPoints.concat()}set ids(t){}insert(t,e){const i=this.store.getGeometryCopy(t),{midPointFeatureId:o,midPointSegment:n}=this.store.getPropertiesCopy(t),s=this.store.getGeometryCopy(o),r="Polygon"===s.type?s.coordinates[0]:s.coordinates;r.splice(n+1,0,i.coordinates),s.coordinates="Polygon"===s.type?[r]:r,this.store.updateGeometry([{id:o,geometry:s}]),this.store.delete([...this._midPoints,...this.selectionPointBehavior.ids]),this.create(r,o,e),this.selectionPointBehavior.create(r,s.type,o)}create(t,e,i){if(!this.store.has(e))throw new Error("Store does not have feature with this id");this._midPoints=this.store.create(function(t,e,i){return k(t,i).map((t,i)=>({geometry:{type:"Point",coordinates:t},properties:e(i)}))}(t,t=>({mode:this.mode,midPoint:!0,midPointSegment:t,midPointFeatureId:e}),i))}delete(){this._midPoints.length&&(this.store.delete(this._midPoints),this._midPoints=[])}getUpdated(t){if(0!==this._midPoints.length)return k(t,this.coordinatePrecision).map((t,e)=>({id:this._midPoints[e],geometry:{type:"Point",coordinates:t}}))}}class D extends f{constructor(t){super(t),this._selectionPoints=[]}get ids(){return this._selectionPoints.concat()}set ids(t){}create(t,e,i){this._selectionPoints=this.store.create(function(t,e,i){const o=[],n="Polygon"===e?t.length-1:t.length;for(let e=0;e<n;e++)o.push({geometry:{type:"Point",coordinates:t[e]},properties:i(e)});return o}(t,e,t=>({mode:this.mode,selectionPoint:!0,selectionPointFeatureId:i,index:t})))}delete(){this.ids.length&&(this.store.delete(this.ids),this._selectionPoints=[])}getUpdated(t){if(0!==this._selectionPoints.length)return this._selectionPoints.map((e,i)=>({id:e,geometry:{type:"Point",coordinates:t[i]}}))}getOneUpdated(t,e){if(void 0!==this._selectionPoints[t])return{id:this._selectionPoints[t],geometry:{type:"Point",coordinates:e}}}}function I(t,e){let i=!1;for(let r=0,a=e.length;r<a;r++){const a=e[r];for(let e=0,r=a.length,h=r-1;e<r;h=e++)(n=a[e])[1]>(o=t)[1]!=(s=a[h])[1]>o[1]&&o[0]<(s[0]-n[0])*(o[1]-n[1])/(s[1]-n[1])+n[0]&&(i=!i)}var o,n,s;return i}const b=(t,e,i)=>{const o=t=>t*t,n=(t,e)=>o(t.x-e.x)+o(t.y-e.y);return Math.sqrt(((t,e,i)=>{const o=n(e,i);if(0===o)return n(t,e);let s=((t.x-e.x)*(i.x-e.x)+(t.y-e.y)*(i.y-e.y))/o;return s=Math.max(0,Math.min(1,s)),n(t,{x:e.x+s*(i.x-e.x),y:e.y+s*(i.y-e.y)})})(t,e,i))};class O extends f{constructor(t,e,i){super(t),this.config=void 0,this.createClickBoundingBox=void 0,this.pixelDistance=void 0,this.config=t,this.createClickBoundingBox=e,this.pixelDistance=i}find(t,e){let i,o,n=Infinity,s=Infinity;const r=this.createClickBoundingBox.create(t),a=this.store.search(r);for(let r=0;r<a.length;r++){const h=a[r],l=h.geometry;if("Point"===l.type){const r=!e&&h.properties.midPoint;if(h.properties.selectionPoint||r)continue;const a=this.pixelDistance.measure(t,l.coordinates);h.properties.midPoint&&a<this.pointerDistance&&a<s?(s=a,o=h):!h.properties.midPoint&&a<this.pointerDistance&&a<n&&(n=a,i=h)}else if("LineString"===l.type)for(let e=0;e<l.coordinates.length-1;e++){const o=l.coordinates[e],s=l.coordinates[e+1],r=b({x:t.containerX,y:t.containerY},this.project(o[0],o[1]),this.project(s[0],s[1]));r<this.pointerDistance&&r<n&&(n=r,i=h)}else"Polygon"===l.type&&I([t.lng,t.lat],l.coordinates)&&(n=0,i=h)}return{clickedFeature:i,clickedMidPoint:o}}}class F extends f{constructor(t,e,i,o){super(t),this.config=void 0,this.featuresAtMouseEvent=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.dragPosition=void 0,this.config=t,this.featuresAtMouseEvent=e,this.selectionPoints=i,this.midPoints=o}get position(){return this.dragPosition?this.dragPosition.concat():void 0}set position(t){if(void 0!==t){if(!Array.isArray(t)||2!==t.length||"number"!=typeof t[0]||"number"!=typeof t[1])throw new Error("Position must be [number, number] array");this.dragPosition=t.concat()}else this.dragPosition=void 0}drag(t,e){const{clickedFeature:i}=this.featuresAtMouseEvent.find(t,!0);if(!i||i.id!==e)return;const o=this.store.getGeometryCopy(e),n=[t.lng,t.lat];if("Polygon"===o.type||"LineString"===o.type){let t,i;if("Polygon"===o.type?(t=o.coordinates[0],i=t.length-1):"LineString"===o.type&&(t=o.coordinates,i=t.length),void 0===i||!t||!this.dragPosition)return!1;for(let e=0;e<i;e++){const i=t[e],o=[this.dragPosition[0]-n[0],this.dragPosition[1]-n[1]];t[e]=[i[0]-o[0],i[1]-o[1]]}"Polygon"===o.type&&(t[t.length-1]=[t[0][0],t[0][1]]);const s=this.selectionPoints.getUpdated(t)||[],r=this.midPoints.getUpdated(t)||[];this.store.updateGeometry([{id:e,geometry:o},...s,...r])}else"Point"===o.type&&this.store.updateGeometry([{id:e,geometry:{type:"Point",coordinates:n}}])}}class W extends f{constructor(t,e,i,o){super(t),this.config=void 0,this.pixelDistance=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.config=t,this.pixelDistance=e,this.selectionPoints=i,this.midPoints=o}drag(t,e){const i=this.store.getGeometryCopy(e);let o;if("LineString"===i.type)o=i.coordinates;else{if("Polygon"!==i.type)return!1;o=i.coordinates[0]}const n={dist:Infinity,index:-1,isFirstOrLastPolygonCoord:!1};for(let e=0;e<o.length;e++){const s=this.pixelDistance.measure(t,o[e]);if(s<this.pointerDistance&&s<n.dist){const t="Polygon"===i.type&&(e===o.length-1||0===e);n.dist=s,n.index=t?0:e,n.isFirstOrLastPolygonCoord=t}}if(-1===n.index)return!1;const s=[t.lng,t.lat];if(n.isFirstOrLastPolygonCoord){const t=o.length-1;o[0]=s,o[t]=s}else o[n.index]=s;const r=this.selectionPoints.getOneUpdated(n.index,s),a=r?[r]:[],h=this.midPoints.getUpdated(o)||[];return this.store.updateGeometry([{id:e,geometry:i},...a,...h]),!0}}function B(t){let e=0,i=0,o=0;return("Polygon"===t.geometry.type?t.geometry.coordinates[0].slice(0,-1):t.geometry.coordinates).forEach(t=>{e+=t[0],i+=t[1],o++},!0),[e/o,i/o]}function K(t,e){const i=t,o=e,n=r(i[1]),s=r(o[1]);let h=r(o[0]-i[0]);h>Math.PI&&(h-=2*Math.PI),h<-Math.PI&&(h+=2*Math.PI);const l=Math.log(Math.tan(s/2+Math.PI/4)/Math.tan(n/2+Math.PI/4)),d=(a(Math.atan2(h,l))+360)%360;return d>180?-(360-d):d}function X(t,e,i){const o=e/6371008.8,n=t[0]*Math.PI/180,s=r(t[1]),a=r(i),h=o*Math.cos(a);let l=s+h;Math.abs(l)>Math.PI/2&&(l=l>0?Math.PI-l:-Math.PI-l);const d=Math.log(Math.tan(l/2+Math.PI/4)/Math.tan(s/2+Math.PI/4)),c=Math.abs(d)>1e-11?h/d:Math.cos(s),p=[(180*(n+o*Math.sin(a)/c)/Math.PI+540)%360-180,180*l/Math.PI];return p[0]+=p[0]-t[0]>180?-360:t[0]-p[0]>180?360:0,p}function Y(t,e){t[0]+=t[0]-e[0]>180?-360:e[0]-t[0]>180?360:0;const i=e[1]*Math.PI/180,o=t[1]*Math.PI/180,n=o-i;let s=Math.abs(t[0]-e[0])*Math.PI/180;s>Math.PI&&(s-=2*Math.PI);const r=Math.log(Math.tan(o/2+Math.PI/4)/Math.tan(i/2+Math.PI/4)),a=Math.abs(r)>1e-11?n/r:Math.cos(i);return 6371008.8*Math.sqrt(n*n+a*a*s*s)}class j extends f{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastBearing=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastBearing=void 0}rotate(t,i){const o=this.store.getGeometryCopy(i);if("Polygon"!==o.type&&"LineString"!==o.type)return;const n=[t.lng,t.lat],s=K(B({type:"Feature",geometry:o,properties:{}}),n);if(!this.lastBearing)return void(this.lastBearing=s+180);let r;if(function(t,e){if(0===e)return t;const i=B(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const o=K(i,t)+e,n=Y(i,t),s=X(i,n,o);t[0]=s[0],t[1]=s[1]})}({type:"Feature",geometry:o,properties:{}},-(this.lastBearing-(s+180))),"Polygon"===o.type)r=o.coordinates[0];else{if("LineString"!==o.type)return;r=o.coordinates}r.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const a=this.midPoints.getUpdated(r)||[],h=this.selectionPoints.getUpdated(r)||[];this.store.updateGeometry([{id:i,geometry:o},...h,...a]),this.lastBearing=s+180}}class G extends f{constructor(t,e,i){super(t),this.config=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.lastDistance=void 0,this.config=t,this.selectionPoints=e,this.midPoints=i}reset(){this.lastDistance=void 0}scale(t,i){const o=this.store.getGeometryCopy(i);if("Polygon"!==o.type&&"LineString"!==o.type)return;const n=[t.lng,t.lat],r=s(B({type:"Feature",geometry:o,properties:{}}),n);if(!this.lastDistance)return void(this.lastDistance=r);let a;if(function(t,e){if(1===e)return t;const i=B(t);("Polygon"===t.geometry.type?t.geometry.coordinates[0]:t.geometry.coordinates).forEach(t=>{const o=Y(i,t),n=K(i,t),s=X(i,o*e,n);t[0]=s[0],t[1]=s[1]})}({type:"Feature",geometry:o,properties:{}},1-(this.lastDistance-r)/r),"Polygon"===o.type)a=o.coordinates[0];else{if("LineString"!==o.type)return;a=o.coordinates}a.forEach(t=>{t[0]=e(t[0],this.coordinatePrecision),t[1]=e(t[1],this.coordinatePrecision)});const h=this.midPoints.getUpdated(a)||[],l=this.selectionPoints.getUpdated(a)||[];this.store.updateGeometry([{id:i,geometry:o},...l,...h]),this.lastDistance=r}}class U extends d{constructor(t){super(t),this.mode="select",this.dragEventThrottle=5,this.dragEventCount=0,this.selected=[],this.flags=void 0,this.keyEvents=void 0,this.selectionPoints=void 0,this.midPoints=void 0,this.featuresAtMouseEvent=void 0,this.pixelDistance=void 0,this.clickBoundingBox=void 0,this.dragFeature=void 0,this.dragCoordinate=void 0,this.rotateFeature=void 0,this.scaleFeature=void 0,this.flags=t&&t.flags?t.flags:{},this.keyEvents=t&&t.keyEvents?t.keyEvents:{deselect:"Escape",delete:"Delete",rotate:"r",scale:"s"},this.dragEventThrottle=t&&void 0!==t.dragEventThrottle&&t.dragEventThrottle||5}registerBehaviors(t){this.pixelDistance=new _(t),this.clickBoundingBox=new m(t),this.featuresAtMouseEvent=new O(t,this.clickBoundingBox,this.pixelDistance),this.selectionPoints=new D(t),this.midPoints=new w(t,this.selectionPoints),this.rotateFeature=new j(t,this.selectionPoints,this.midPoints),this.scaleFeature=new G(t,this.selectionPoints,this.midPoints),this.dragFeature=new F(t,this.featuresAtMouseEvent,this.selectionPoints,this.midPoints),this.dragCoordinate=new W(t,this.pixelDistance,this.selectionPoints,this.midPoints)}deselect(){this.store.updateProperty(this.selected.map(t=>({id:t,property:"selected",value:!1}))),this.onDeselect(this.selected[0]),this.selected=[],this.selectionPoints.delete(),this.midPoints.delete()}deleteSelected(){this.store.delete(this.selected),this.selected=[]}onRightClick(t){if(!this.selectionPoints.ids.length)return;let e,i=Infinity;if(this.selectionPoints.ids.forEach(o=>{const n=this.store.getGeometryCopy(o),s=this.pixelDistance.measure(t,n.coordinates);s<this.pointerDistance&&s<i&&(i=s,e=this.store.getPropertiesCopy(o))}),!e)return;const o=e.selectionPointFeatureId,n=e.index,s=this.store.getPropertiesCopy(o),r=this.flags[s.mode];if(!(r&&r.feature&&r.feature.coordinates&&r.feature.coordinates.deletable))return;const a=this.store.getGeometryCopy(o);let h;if("Polygon"===a.type){if(h=a.coordinates[0],h.length<=4)return}else if("LineString"===a.type&&(h=a.coordinates,h.length<=3))return;h&&("Polygon"===a.type&&0===n||n===h.length-1?(h.shift(),h.pop(),h.push([h[0][0],h[0][1]])):h.splice(n,1),this.store.delete([...this.midPoints.ids,...this.selectionPoints.ids]),this.store.updateGeometry([{id:o,geometry:a}]),this.selectionPoints.create(h,a.type,o),r&&r.feature&&r.feature.coordinates&&r.feature.coordinates.midpoints&&this.midPoints.create(h,o,this.coordinatePrecision))}onLeftClick(t){const{clickedFeature:e,clickedMidPoint:i}=this.featuresAtMouseEvent.find(t,this.selected.length>0);if(this.selected.length&&i)this.midPoints.insert(i.id,this.coordinatePrecision);else if(e){const{mode:t}=this.store.getPropertiesCopy(e.id),i=this.selected[0];if(i){if(i===e.id)return;this.deselect()}const o=this.flags[t];if(!o||!o.feature)return;this.selected=[e.id],this.store.updateProperty([{id:e.id,property:"selected",value:!0}]),this.onSelect(e.id);const{type:n,coordinates:s}=this.store.getGeometryCopy(e.id);let r;"LineString"===n?r=s:"Polygon"===n&&(r=s[0]),r&&o&&o.feature.coordinates&&(this.selectionPoints.create(r,n,e.id),o.feature.coordinates.midpoints&&this.midPoints.create(r,e.id,this.coordinatePrecision))}else if(this.selected.length)return void this.deselect()}start(){this.setStarted()}stop(){this.setStopped(),this.cleanUp()}onClick(t){"right"!==t.button?"left"===t.button&&this.onLeftClick(t):this.onRightClick(t)}onKeyDown(){}onKeyUp(t){if(t.key===this.keyEvents.delete){if(!this.selected.length)return;this.onDeselect(this.selected[0]),this.deleteSelected(),this.selectionPoints.delete(),this.midPoints.delete()}else t.key===this.keyEvents.deselect&&this.cleanUp()}cleanUp(){this.selected.length&&this.deselect()}onDragStart(t,e){if(!this.selected.length)return;const i=this.store.getPropertiesCopy(this.selected[0]),o=this.flags[i.mode];o&&o.feature&&(o.feature.draggable||o.feature.coordinates&&o.feature.coordinates.draggable)&&(this.dragEventCount=0,this.setCursor("grabbing"),this.dragFeature.position=[t.lng,t.lat],e(!1))}onDrag(t){const e=this.selected[0];if(!e||!this.dragFeature.position)return;const i=this.store.getPropertiesCopy(e),o=this.flags[i.mode];if(this.dragEventCount++,this.dragEventCount%this.dragEventThrottle!=0)if(o&&o.feature&&o.feature.rotateable&&t.heldKeys.includes("r"))this.rotateFeature.rotate(t,e);else if(o&&o.feature&&o.feature.scaleable&&t.heldKeys.includes("s"))this.scaleFeature.scale(t,e);else{if(o&&o.feature&&o.feature.coordinates&&o.feature.coordinates.draggable&&this.dragCoordinate.drag(t,e))return;o&&o.feature&&o.feature.draggable&&(this.dragFeature.drag(t,e),this.dragFeature.position=[t.lng,t.lat])}}onDragEnd(t,e){this.setCursor("grab"),this.dragFeature.position=void 0,this.rotateFeature.reset(),this.scaleFeature.reset(),e(!0)}onMouseMove(t){if(!this.selected.length||this.dragFeature.position)return;let e=!1;this.midPoints.ids.forEach(i=>{if(e)return;const o=this.store.getGeometryCopy(i);this.pixelDistance.measure(t,o.coordinates)<this.pointerDistance&&(e=!0)}),this.selectionPoints.ids.forEach(i=>{const o=this.store.getGeometryCopy(i);this.pixelDistance.measure(t,o.coordinates)<this.pointerDistance&&(e=!1)}),this.setCursor(e?"crosshair":"unset")}styleFeature(e){const i=t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0});if(e.properties.mode===this.mode){if("Polygon"===e.geometry.type)return this.styles.selectedColor&&(i.polygonFillColor=this.styles.selectedColor),this.styles.selectedColor&&(i.polygonOutlineColor=this.styles.selectedColor),i.zIndex=10,i;if("Point"===e.geometry.type){if(e.properties.selectionPoint)return i.pointColor=this.styles.selectionPointColor||i.pointColor,i.pointOutlineColor=this.styles.selectionPointOutlineColor||i.pointOutlineColor,i.pointWidth=this.styles.selectionPointWidth||i.pointWidth,i.pointOutlineWidth=this.styles.midPointOutlineWidth||2,i.zIndex=30,i;if(e.properties.midPoint)return i.pointColor=this.styles.midPointColor||i.pointColor,i.pointOutlineColor=this.styles.midPointOutlineColor||i.pointOutlineColor,i.pointWidth=this.styles.midPointWidth||4,i.pointOutlineWidth=this.styles.midPointOutlineWidth||2,i.zIndex=40,i}}return i}}class T extends d{constructor(...t){super(...t),this.mode="static"}start(){}stop(){}onKeyUp(){}onKeyDown(){}onClick(){}onDragStart(){}onDrag(){}onDragEnd(){}onMouseMove(){}styleFeature(){return t({},{polygonFillColor:"#3f97e0",polygonOutlineColor:"#3f97e0",polygonOutlineWidth:4,polygonFillOpacity:.3,pointColor:"#3f97e0",pointOutlineColor:"#ffffff",pointOutlineWidth:0,pointWidth:6,lineStringColor:"#3f97e0",lineStringWidth:4,zIndex:0})}}const N=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){const e=16*Math.random()|0;return("x"==t?e:3&e|8).toString(16)})};function A(t,e,i,o,n){for(;o>i;){if(o-i>600){const s=o-i+1,r=e-i+1,a=Math.log(s),h=.5*Math.exp(2*a/3),l=.5*Math.sqrt(a*h*(s-h)/s)*(r-s/2<0?-1:1);A(t,e,Math.max(i,Math.floor(e-r*h/s+l)),Math.min(o,Math.floor(e+(s-r)*h/s+l)),n)}const s=t[e];let r=i,a=o;for(z(t,i,e),n(t[o],s)>0&&z(t,i,o);r<a;){for(z(t,r,a),r++,a--;n(t[r],s)<0;)r++;for(;n(t[a],s)>0;)a--}0===n(t[i],s)?z(t,i,a):(a++,z(t,a,o)),a<=e&&(i=a+1),e<=a&&(o=a-1)}}function z(t,e,i){const o=t[e];t[e]=t[i],t[i]=o}function J(t,e){$(t,0,t.children.length,e,t)}function $(t,e,i,o,n){n||(n=it([])),n.minX=Infinity,n.minY=Infinity,n.maxX=-Infinity,n.maxY=-Infinity;for(let s=e;s<i;s++){const e=t.children[s];R(n,t.leaf?o(e):e)}return n}function R(t,e){return t.minX=Math.min(t.minX,e.minX),t.minY=Math.min(t.minY,e.minY),t.maxX=Math.max(t.maxX,e.maxX),t.maxY=Math.max(t.maxY,e.maxY),t}function q(t,e){return t.minX-e.minX}function H(t,e){return t.minY-e.minY}function Z(t){return(t.maxX-t.minX)*(t.maxY-t.minY)}function Q(t){return t.maxX-t.minX+(t.maxY-t.minY)}function V(t,e){const i=Math.max(t.minX,e.minX),o=Math.max(t.minY,e.minY),n=Math.min(t.maxX,e.maxX),s=Math.min(t.maxY,e.maxY);return Math.max(0,n-i)*Math.max(0,s-o)}function tt(t,e){return t.minX<=e.minX&&t.minY<=e.minY&&e.maxX<=t.maxX&&e.maxY<=t.maxY}function et(t,e){return e.minX<=t.maxX&&e.minY<=t.maxY&&e.maxX>=t.minX&&e.maxY>=t.minY}function it(t){return{children:t,height:1,leaf:!0,minX:Infinity,minY:Infinity,maxX:-Infinity,maxY:-Infinity}}function ot(t,e,i,o,n){const s=[e,i];for(;s.length;){if((i=s.pop())-(e=s.pop())<=o)continue;const r=e+Math.ceil((i-e)/o/2)*o;A(t,r,e,i,n),s.push(e,r,r,i)}}class nt{constructor(t){this._maxEntries=void 0,this._minEntries=void 0,this.data=void 0,this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(.4*this._maxEntries)),this.clear()}search(t){let e=this.data;const i=[];if(!et(t,e))return i;const o=this.toBBox,n=[];for(;e;){for(let s=0;s<e.children.length;s++){const r=e.children[s],a=e.leaf?o(r):r;et(t,a)&&(e.leaf?i.push(r):tt(t,a)?this._all(r,i):n.push(r))}e=n.pop()}return i}collides(t){let e=this.data;if(et(t,e)){const i=[];for(;e;){for(let o=0;o<e.children.length;o++){const n=e.children[o],s=e.leaf?this.toBBox(n):n;if(et(t,s)){if(e.leaf||tt(t,s))return!0;i.push(n)}}e=i.pop()}}return!1}load(t){if(t.length<this._minEntries){for(let e=0;e<t.length;e++)this.insert(t[e]);return}let e=this._build(t.slice(),0,t.length-1,0);if(this.data.children.length)if(this.data.height===e.height)this._splitRoot(this.data,e);else{if(this.data.height<e.height){const t=this.data;this.data=e,e=t}this._insert(e,this.data.height-e.height-1,!0)}else this.data=e}insert(t){this._insert(t,this.data.height-1)}clear(){this.data=it([])}remove(t){let e=this.data;const i=this.toBBox(t),o=[],n=[];let s,r,a=!1;for(;e||o.length;){if(e||(e=o.pop(),r=o[o.length-1],s=n.pop(),a=!0),e.leaf){const i=e.children.indexOf(t);-1!==i&&(e.children.splice(i,1),o.push(e),this._condense(o))}a||e.leaf||!tt(e,i)?r?(s++,e=r.children[s],a=!1):e=null:(o.push(e),n.push(s),s=0,r=e,e=e.children[0])}}toBBox(t){return t}compareMinX(t,e){return t.minX-e.minX}compareMinY(t,e){return t.minY-e.minY}_all(t,e){const i=[];for(;t;)t.leaf?e.push(...t.children):i.push(...t.children),t=i.pop();return e}_build(t,e,i,o){const n=i-e+1;let s,r=this._maxEntries;if(n<=r)return s=it(t.slice(e,i+1)),J(s,this.toBBox),s;o||(o=Math.ceil(Math.log(n)/Math.log(r)),r=Math.ceil(n/Math.pow(r,o-1))),s=it([]),s.leaf=!1,s.height=o;const a=Math.ceil(n/r),h=a*Math.ceil(Math.sqrt(r));ot(t,e,i,h,this.compareMinX);for(let n=e;n<=i;n+=h){const e=Math.min(n+h-1,i);ot(t,n,e,a,this.compareMinY);for(let i=n;i<=e;i+=a){const n=Math.min(i+a-1,e);s.children.push(this._build(t,i,n,o-1))}}return J(s,this.toBBox),s}_chooseSubtree(t,e,i,o){for(;o.push(e),!e.leaf&&o.length-1!==i;){let i,o=Infinity,r=Infinity;for(let a=0;a<e.children.length;a++){const h=e.children[a],l=Z(h),d=(n=t,s=h,(Math.max(s.maxX,n.maxX)-Math.min(s.minX,n.minX))*(Math.max(s.maxY,n.maxY)-Math.min(s.minY,n.minY))-l);d<r?(r=d,o=l<o?l:o,i=h):d===r&&l<o&&(o=l,i=h)}e=i||e.children[0]}var n,s;return e}_insert(t,e,i){const o=i?t:this.toBBox(t),n=[],s=this._chooseSubtree(o,this.data,e,n);for(s.children.push(t),R(s,o);e>=0&&n[e].children.length>this._maxEntries;)this._split(n,e),e--;this._adjustParentBBoxes(o,n,e)}_split(t,e){const i=t[e],o=i.children.length,n=this._minEntries;this._chooseSplitAxis(i,n,o);const s=this._chooseSplitIndex(i,n,o),r=it(i.children.splice(s,i.children.length-s));r.height=i.height,r.leaf=i.leaf,J(i,this.toBBox),J(r,this.toBBox),e?t[e-1].children.push(r):this._splitRoot(i,r)}_splitRoot(t,e){this.data=it([t,e]),this.data.height=t.height+1,this.data.leaf=!1,J(this.data,this.toBBox)}_chooseSplitIndex(t,e,i){let o,n=Infinity,s=Infinity;for(let r=e;r<=i-e;r++){const e=$(t,0,r,this.toBBox),a=$(t,r,i,this.toBBox),h=V(e,a),l=Z(e)+Z(a);h<n?(n=h,o=r,s=l<s?l:s):h===n&&l<s&&(s=l,o=r)}return o||i-e}_chooseSplitAxis(t,e,i){const o=t.leaf?this.compareMinX:q,n=t.leaf?this.compareMinY:H;this._allDistMargin(t,e,i,o)<this._allDistMargin(t,e,i,n)&&t.children.sort(o)}_allDistMargin(t,e,i,o){t.children.sort(o);const n=this.toBBox,s=$(t,0,e,n),r=$(t,i-e,i,n);let a=Q(s)+Q(r);for(let o=e;o<i-e;o++){const e=t.children[o];R(s,t.leaf?n(e):e),a+=Q(s)}for(let o=i-e-1;o>=e;o--){const e=t.children[o];R(r,t.leaf?n(e):e),a+=Q(r)}return a}_adjustParentBBoxes(t,e,i){for(let o=i;o>=0;o--)R(e[o],t)}_condense(t){for(let e,i=t.length-1;i>=0;i--)0===t[i].children.length?i>0?(e=t[i-1].children,e.splice(e.indexOf(t[i]),1)):this.clear():J(t[i],this.toBBox)}}class st{constructor(t){this.tree=void 0,this.idToNode=void 0,this.nodeToId=void 0,this.tree=new nt(t&&t.maxEntries?t.maxEntries:9),this.idToNode=new Map,this.nodeToId=new Map}setMaps(t,e){this.idToNode.set(String(t.id),e),this.nodeToId.set(e,String(t.id))}toBBox(t){const e=[],i=[];let o;if("Polygon"===t.geometry.type)o=t.geometry.coordinates[0];else if("LineString"===t.geometry.type)o=t.geometry.coordinates;else{if("Point"!==t.geometry.type)throw new Error("Not a valid feature to turn into a bounding box");o=[t.geometry.coordinates]}for(let t=0;t<o.length;t++)i.push(o[t][1]),e.push(o[t][0]);const n=Math.min(...i),s=Math.max(...i);return{minX:Math.min(...e),minY:n,maxX:Math.max(...e),maxY:s}}insert(t){if(this.idToNode.get(String(t.id)))throw new Error("Feature already exists");const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}load(t){const e=[],i=new Set;t.forEach(t=>{const o=this.toBBox(t);if(this.setMaps(t,o),i.has(String(t.id)))throw new Error(`Duplicate feature ID found ${t.id}`);i.add(String(t.id)),e.push(o)}),this.tree.load(e)}update(t){this.remove(t.id);const e=this.toBBox(t);this.setMaps(t,e),this.tree.insert(e)}remove(t){const e=this.idToNode.get(t);if(!e)throw new Error(`${t} not inserted into the spatial index`);this.tree.remove(e)}clear(){this.tree.clear()}search(t){return this.tree.search(this.toBBox(t)).map(t=>this.nodeToId.get(t))}collides(t){return this.tree.collides(this.toBBox(t))}}class rt{constructor(t){this.tracked=void 0,this.spatialIndex=void 0,this.store=void 0,this._onChange=()=>{},this.store={},this.spatialIndex=new st,this.tracked=!t||!1!==t.tracked,t&&t.data&&this.load(t.data,t.validateFeature)}getId(){return N()}clone(t){return JSON.parse(JSON.stringify(t))}has(t){return Boolean(this.store[t])}load(t,e){if(0===t.length)return;const i=this.clone(t);i.forEach(t=>{t.id||(t.id=N()),this.tracked&&(t.properties.createdAt||(t.properties.createdAt=+new Date),t.properties.updatedAt||(t.properties.updatedAt=+new Date))});const o=[];i.forEach(t=>{e&&e(t),this.store[t.id]=t,o.push(t.id)}),this.spatialIndex.load(i),this._onChange(o,"create")}search(t,e){const i=this.spatialIndex.search(t).map(t=>this.store[t]);return this.clone(e?i.filter(e):i)}registerOnChange(t){this._onChange=(e,i)=>{t(e,i)}}getGeometryCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get geometry copy`);return this.clone(e.geometry)}getPropertiesCopy(t){const e=this.store[t];if(!e)throw new Error(`No feature with this id (${t}), can not get properties copy`);return this.clone(e.properties)}updateProperty(t){const e=[];t.forEach(({id:t,property:i,value:o})=>{const n=this.store[t];if(!n)throw new Error(`No feature with this (${t}), can not update geometry`);e.push(t),n.properties[i]=o,this.tracked&&(n.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}updateGeometry(t){const e=[];t.forEach(({id:t,geometry:i})=>{e.push(t);const o=this.store[t];if(!o)throw new Error(`No feature with this (${t}), can not update geometry`);o.geometry=this.clone(i),this.spatialIndex.update(o),this.tracked&&(o.properties.updatedAt=+new Date)}),this._onChange&&this._onChange(e,"update")}create(e){const i=[];return e.forEach(({geometry:e,properties:o})=>{let n,s=t({},o);this.tracked&&(n=+new Date,o?(s.createdAt="number"==typeof o.createdAt?o.createdAt:n,s.updatedAt="number"==typeof o.updatedAt?o.updatedAt:n):s={createdAt:n,updatedAt:n});const r=this.getId(),a={id:r,type:"Feature",geometry:e,properties:s};this.store[r]=a,this.spatialIndex.insert(a),i.push(r)}),this._onChange&&this._onChange([...i],"create"),i}delete(t){t.forEach(t=>{if(!this.store[t])throw new Error("No feature with this id, can not delete");delete this.store[t],this.spatialIndex.remove(t)}),this._onChange&&this._onChange([...t],"delete")}copyAll(){return this.clone(Object.keys(this.store).map(t=>this.store[t]))}}class at{constructor(e){this._modes=void 0,this._mode=void 0,this._adapter=void 0,this._enabled=!1,this._store=void 0,this._eventListeners=void 0,this._adapter=e.adapter,this._mode=new T,this._modes=t({},e.modes,{static:this._mode}),this._eventListeners={change:[],select:[],deselect:[]},this._store=e.data?new rt({data:e.data}):new rt;const i=t=>{const e=[],i=this._store.copyAll().filter(i=>!t.includes(i.id)||(e.push(i),!1));return{changed:e,unchanged:i}},o=(t,e)=>{this._eventListeners.change.forEach(i=>{i(t,e)});const{changed:o,unchanged:n}=i(t);"create"===e?this._adapter.render({created:o,deletedIds:[],unchanged:n,updated:[]},this.getModeStyles()):"update"===e?this._adapter.render({created:[],deletedIds:[],unchanged:n,updated:o},this.getModeStyles()):"delete"===e?this._adapter.render({created:[],deletedIds:t,unchanged:n,updated:[]},this.getModeStyles()):"styling"===e&&this._adapter.render({created:[],deletedIds:[],unchanged:n,updated:[]},this.getModeStyles())},n=t=>{this._eventListeners.select.forEach(e=>{e(t)});const{changed:e,unchanged:o}=i([t]);this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:e},this.getModeStyles())},s=t=>{this._eventListeners.deselect.forEach(t=>{t()});const{changed:e,unchanged:o}=i([t]);e&&this._adapter.render({created:[],deletedIds:[],unchanged:o,updated:e},this.getModeStyles())};if(Object.keys(this._modes).forEach(t=>{this._modes[t].register({mode:t,store:this._store,setCursor:this._adapter.setCursor,project:this._adapter.project,unproject:this._adapter.unproject,onChange:o,onSelect:n,onDeselect:s})}),e.data){const t=this._store.copyAll().filter(t=>!(t.properties&&!Object.keys(this._modes).includes(t.properties.mode)&&(this._store.delete([t.id]),1)));this._adapter.render({created:t,deletedIds:[],unchanged:[],updated:[]},this.getModeStyles())}}getModeStyles(){const t={};return Object.keys(this._modes).forEach(e=>{t[e]=this._modes[e].styleFeature.bind(this._modes[e])}),t}setModeStyles(t,e){this._modes[t].styles=e}getSnapshot(){return this._store.copyAll()}get enabled(){return this._enabled}set enabled(t){throw new Error("Enabled is read only")}getCurrentMode(){return this._mode.mode}changeMode(t){if(!this._modes[t])throw new Error("No mode with this name present");this._mode.stop(),this._mode=this._modes[t],this._mode.start()}start(){this._enabled=!0,this._adapter.register({onClick:t=>{this._mode.onClick(t)},onMouseMove:t=>{this._mode.onMouseMove(t)},onKeyDown:t=>{this._mode.onKeyDown(t)},onKeyUp:t=>{this._mode.onKeyUp(t)},onDragStart:(t,e)=>{this._mode.onDragStart(t,e)},onDrag:t=>{this._mode.onDrag(t)},onDragEnd:(t,e)=>{this._mode.onDragEnd(t,e)}})}stop(){this._enabled=!1,this._adapter.unregister()}on(t,e){const i=this._eventListeners[t];i.includes(e)||i.push(e)}off(t,e){const i=this._eventListeners[t];i.includes(e)&&i.splice(i.indexOf(e),1)}}export{at as TerraDraw,c as TerraDrawCircleMode,p as TerraDrawFreehandMode,i as TerraDrawGoogleMapsAdapter,o as TerraDrawLeafletAdapter,v as TerraDrawLineStringMode,n as TerraDrawMapboxGLAdapter,P as TerraDrawPointMode,x as TerraDrawPolygonMode,E as TerraDrawRenderMode,U as TerraDrawSelectMode};
|
|
2
2
|
//# sourceMappingURL=terra-draw.modern.js.map
|