UNPKG

terra-draw-mapbox-gl-adapter

Version:
3 lines (2 loc) 8.3 kB
import{TerraDrawExtend as e}from"terra-draw";function t(){return t=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var i=arguments[t];for(var r in i)({}).hasOwnProperty.call(i,r)&&(e[r]=i[r])}return e},t.apply(null,arguments)}function i(e,t){return i=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},i(e,t)}var r=/*#__PURE__*/function(e){function r(t){var i;return(i=e.call(this,t)||this)._renderBeforeLayerId=void 0,i._prefixId=void 0,i._initialDragPan=void 0,i._initialDragRotate=void 0,i._nextRender=void 0,i._map=void 0,i._container=void 0,i.markerCounter=0,i.markerMap=new Map,i.changedIds={deletion:!1,points:!1,linestrings:!1,polygons:!1,styling:!1},i._map=t.map,i._container=i._map.getContainer(),i._initialDragRotate=i._map.dragRotate.isEnabled(),i._initialDragPan=i._map.dragPan.isEnabled(),i._renderBeforeLayerId=t.renderBelowLayerId,i._prefixId=t.prefixId||"td",i}var n,a;a=e,(n=r).prototype=Object.create(a.prototype),n.prototype.constructor=n,i(n,a);var o=r.prototype;return o.toGlDashArrayFromPixels=function(e,t){if(!e)return null;var i=e[0],r=e[1];if(!Number.isFinite(i)||!Number.isFinite(r)||i<0||r<0)return null;var n=Math.max(1e-4,t);return[i/n,r/n]},o.resizeImage=function(e,t,i,r){var n=new Image;n.crossOrigin="anonymous",n.onload=function(){var e=document.createElement("canvas");e.width=t,e.height=i;var a=e.getContext("2d");if(!a)throw new Error("Could not get canvas context");a.drawImage(n,0,0,t,i);var o=e.toDataURL();r(o)},n.src=e},o._addGeoJSONSource=function(e,t){this._map.addSource(e,{type:"geojson",data:{type:"FeatureCollection",features:t},tolerance:0})},o._addFillLayer=function(e){return this._map.addLayer({id:e,source:e,type:"fill",layout:{"fill-sort-key":["get","zIndex"]},paint:{"fill-color":["get","polygonFillColor"],"fill-opacity":["get","polygonFillOpacity"]}})},o._addFillOutlineLayer=function(e){return this._map.addLayer({id:e+"-outline",source:e,type:"line",layout:{"line-sort-key":["get","zIndex"]},paint:{"line-width":["get","polygonOutlineWidth"],"line-color":["get","polygonOutlineColor"],"line-opacity":["get","polygonOutlineOpacity"]}})},o._addLineLayer=function(e){return this._map.addLayer({id:e,source:e,type:"line",layout:{"line-sort-key":["get","zIndex"]},paint:t({},{"line-dasharray":["coalesce",["get","lineStringDash"],["literal",[]]]},{"line-width":["get","lineStringWidth"],"line-color":["get","lineStringColor"],"line-opacity":["get","lineStringOpacity"]})})},o._addPointLayer=function(e){return this._map.addLayer({id:e,source:e,type:"circle",layout:{"circle-sort-key":["get","zIndex"]},paint:{"circle-stroke-color":["get","pointOutlineColor"],"circle-stroke-width":["get","pointOutlineWidth"],"circle-stroke-opacity":["get","pointOutlineOpacity"],"circle-radius":["get","pointWidth"],"circle-color":["get","pointColor"],"circle-opacity":["get","pointOpacity"]}})},o._addMarkerLayer=function(e){return this._map.addLayer({id:e+"-marker",source:e,type:"symbol",filter:["has","markerId"],layout:{"icon-image":["get","markerId"],"icon-anchor":"bottom","icon-allow-overlap":!0}})},o._addLayer=function(e,t){"Point"===t&&(this._addPointLayer(e),this._addMarkerLayer(e)),"LineString"===t&&this._addLineLayer(e),"Polygon"===t&&(this._addFillLayer(e),this._addFillOutlineLayer(e))},o._addGeoJSONLayer=function(e,t){var i=this._prefixId+"-"+e.toLowerCase();return this._addGeoJSONSource(i,t),this._addLayer(i,e),i},o._setGeoJSONLayerData=function(e,t){var i=this._prefixId+"-"+e.toLowerCase();return this._map.getSource(i).setData({type:"FeatureCollection",features:t}),i},o.updateChangedIds=function(e){var t=this;[].concat(e.updated,e.created).forEach(function(e){"Point"===e.geometry.type?t.changedIds.points=!0:"LineString"===e.geometry.type?t.changedIds.linestrings=!0:"Polygon"===e.geometry.type&&(t.changedIds.polygons=!0)}),e.deletedIds.length>0&&(this.changedIds.deletion=!0),0===e.created.length&&0===e.updated.length&&0===e.deletedIds.length&&(this.changedIds.styling=!0)},o.getLngLatFromEvent=function(e){var t=this._container.getBoundingClientRect();return this.unproject(e.clientX-t.left,e.clientY-t.top)},o.getMapEventElement=function(){return this._map.getCanvas()},o.setDraggability=function(e){e?(this._initialDragRotate&&this._map.dragRotate.enable(),this._initialDragPan&&this._map.dragPan.enable()):(this._initialDragRotate&&this._map.dragRotate.disable(),this._initialDragPan&&this._map.dragPan.disable())},o.project=function(e,t){var i=this._map.project({lng:e,lat:t});return{x:i.x,y:i.y}},o.unproject=function(e,t){var i=this._map.unproject({x:e,y:t});return{lng:i.lng,lat:i.lat}},o.setCursor=function(e){var t=this._map.getCanvas();"unset"===e?t.style.removeProperty("cursor"):t.style.cursor=e},o.setDoubleClickToZoom=function(e){e?this._map.doubleClickZoom.enable():this._map.doubleClickZoom.disable()},o.render=function(e,t){var i=this;this.updateChangedIds(e),this._nextRender&&cancelAnimationFrame(this._nextRender),this._nextRender=requestAnimationFrame(function(){for(var r=[].concat(e.created,e.updated,e.unchanged),n=[],a=[],o=[],l=function(){var e=r[d],l=e.properties,s=t[l.mode](e);if(l.zIndex=s.zIndex,"Point"===e.geometry.type){var p=s.pointOpacity,c=s.pointOutlineOpacity;if(l.pointColor=s.pointColor,l.pointOutlineColor=s.pointOutlineColor,l.pointOutlineWidth=s.pointOutlineWidth,l.pointOutlineOpacity=void 0===c?1:c,l.pointWidth=s.pointWidth,l.pointOpacity=void 0===p?1:p,s.markerUrl&&s.markerWidth&&s.markerHeight&&!i.markerMap.has(s.markerUrl)){var g="marker-"+i.markerCounter++;i.resizeImage(s.markerUrl,s.markerWidth,s.markerHeight,function(e){i._map.loadImage(e,function(e,t){t&&!e?(i._map.addImage(g,t),i._map.setLayoutProperty(i._prefixId+"-point-marker","icon-image",g)):console.error("Error loading marker image: "+s.markerUrl,e)})}),i.markerMap.set(s.markerUrl,g),l.markerId=g,l.pointWidth=0}else s.markerUrl&&i.markerMap.has(s.markerUrl)&&(l.markerId=i.markerMap.get(s.markerUrl),l.pointWidth=0);n.push(e)}else if("LineString"===e.geometry.type){l.lineStringDash=i.toGlDashArrayFromPixels(s.lineStringDash,s.lineStringWidth),l.lineStringColor=s.lineStringColor,l.lineStringWidth=s.lineStringWidth;var y=s.lineStringOpacity;l.lineStringOpacity=void 0===y?1:y,a.push(e)}else if("Polygon"===e.geometry.type){var h=s.polygonOutlineOpacity;l.polygonFillColor=s.polygonFillColor,l.polygonFillOpacity=s.polygonFillOpacity,l.polygonOutlineOpacity=void 0===h?1:h,l.polygonOutlineColor=s.polygonOutlineColor,l.polygonOutlineWidth=s.polygonOutlineWidth,o.push(e)}},d=0;d<r.length;d++)l();var s=i.changedIds.deletion||i.changedIds.styling,p=s||i.changedIds.linestrings,c=s||i.changedIds.polygons;(s||i.changedIds.points)&&i._setGeoJSONLayerData("Point",n),p&&i._setGeoJSONLayerData("LineString",a),c&&i._setGeoJSONLayerData("Polygon",o),i.changedIds={points:!1,linestrings:!1,polygons:!1,deletion:!1,styling:!1}})},o.clear=function(){this._currentModeCallbacks&&(this._currentModeCallbacks.onClear(),this._nextRender&&(cancelAnimationFrame(this._nextRender),this._nextRender=void 0),this._setGeoJSONLayerData("Point",[]),this._setGeoJSONLayerData("LineString",[]),this._setGeoJSONLayerData("Polygon",[]))},o.getCoordinatePrecision=function(){return e.prototype.getCoordinatePrecision.call(this)},o.unregister=function(){e.prototype.unregister.call(this),this._map.removeLayer(this._prefixId+"-point"),this._map.removeLayer(this._prefixId+"-point-marker"),this._map.removeSource(this._prefixId+"-point"),this._map.removeLayer(this._prefixId+"-linestring"),this._map.removeSource(this._prefixId+"-linestring"),this._map.removeLayer(this._prefixId+"-polygon"),this._map.removeLayer(this._prefixId+"-polygon-outline"),this._map.removeSource(this._prefixId+"-polygon")},o.register=function(t){var i;e.prototype.register.call(this,t);var r=this._addGeoJSONLayer("Polygon",[]),n=this._addGeoJSONLayer("LineString",[]),a=this._addGeoJSONLayer("Point",[]);this._renderBeforeLayerId&&(this._map.moveLayer(a,this._renderBeforeLayerId),this._map.moveLayer(n,a),this._map.moveLayer(r+"-outline",n),this._map.moveLayer(r,n)),null!=(i=this._currentModeCallbacks)&&i.onReady&&this._currentModeCallbacks.onReady()},r}(e.TerraDrawBaseAdapter);export{r as TerraDrawMapboxGLAdapter}; //# sourceMappingURL=terra-draw-mapbox-gl-adapter.module.js.map