diff --git a/src/Button/CopyButton/CopyButton.tsx b/src/Button/CopyButton/CopyButton.tsx index 53b088b4a4..9660b95584 100644 --- a/src/Button/CopyButton/CopyButton.tsx +++ b/src/Button/CopyButton/CopyButton.tsx @@ -70,7 +70,7 @@ const CopyButton: React.FC = ({ const copy = feat.clone(); - layers[0].getSource().addFeature(copy); + layers[0].getSource()?.addFeature(copy); AnimateUtil.moveFeature( map, diff --git a/src/Button/DeleteButton/DeleteButton.tsx b/src/Button/DeleteButton/DeleteButton.tsx index 6e6f01cab9..69d0db22b8 100644 --- a/src/Button/DeleteButton/DeleteButton.tsx +++ b/src/Button/DeleteButton/DeleteButton.tsx @@ -63,7 +63,7 @@ export const DeleteButton: React.FC = ({ const onFeatureSelect = (event: OlSelectEvent) => { onFeatureRemove?.(event); const feat = event.selected[0]; - layers[0].getSource().removeFeature(feat); + layers[0].getSource()?.removeFeature(feat); }; const finalClassName = className diff --git a/src/Button/DigitizeButton/DigitizeButton.tsx b/src/Button/DigitizeButton/DigitizeButton.tsx index 538a806a22..9483d665ba 100644 --- a/src/Button/DigitizeButton/DigitizeButton.tsx +++ b/src/Button/DigitizeButton/DigitizeButton.tsx @@ -21,6 +21,7 @@ import * as OlEventConditions from 'ol/events/condition'; import OlGeometry from 'ol/geom/Geometry'; import _isFunction from 'lodash/isFunction'; +import _isArray from 'lodash/isArray'; import ToggleButton, { ToggleButtonProps } from '../ToggleButton/ToggleButton'; import MapUtil from '@terrestris/ol-util/dist/MapUtil/MapUtil'; @@ -901,7 +902,7 @@ class DigitizeButton extends React.Component = ({ } const newInteraction = new OlInteractionDraw({ - source: layer.getSource(), + source: layer.getSource() || undefined, type: type, geometryFunction: geometryFunction, style: drawStyle ?? DigitizeUtil.defaultDigitizeStyleFunction, @@ -227,7 +227,7 @@ const DrawButton: React.FC = ({ const onModalLabelCancelInternal = () => { onModalLabelCancel?.(); - layer.getSource().removeFeature(digitizeTextFeature); + layer.getSource()?.removeFeature(digitizeTextFeature); setDigitizeTextFeature(null); }; diff --git a/src/Button/GeoLocationButton/GeoLocationButton.tsx b/src/Button/GeoLocationButton/GeoLocationButton.tsx index a23a06b01d..119f0fc6ea 100644 --- a/src/Button/GeoLocationButton/GeoLocationButton.tsx +++ b/src/Button/GeoLocationButton/GeoLocationButton.tsx @@ -136,7 +136,7 @@ class GeoLocationButton extends React.Component { if (showMarker) { this._markerFeature = new OlFeature(); - this._geoLocationLayer.getSource().addFeature(this._markerFeature); + this._geoLocationLayer.getSource()?.addFeature(this._markerFeature); } } @@ -151,7 +151,7 @@ class GeoLocationButton extends React.Component { if (showMarker && !this._markerFeature) { this._markerFeature = new OlFeature(); - this._geoLocationLayer.getSource().addFeature(this._markerFeature); + this._geoLocationLayer.getSource()?.addFeature(this._markerFeature); } } @@ -241,7 +241,7 @@ class GeoLocationButton extends React.Component { } if (this._markerFeature) { this._markerFeature = null; - this._geoLocationLayer.getSource().clear(); + this._geoLocationLayer.getSource()?.clear(); } return; } @@ -257,8 +257,8 @@ class GeoLocationButton extends React.Component { if (!this._markerFeature) { this._markerFeature = new OlFeature(); } - if (!this._geoLocationLayer.getSource().getFeatures().includes(this._markerFeature)) { - this._geoLocationLayer.getSource().addFeature(this._markerFeature); + if (!this._geoLocationLayer.getSource()?.getFeatures().includes(this._markerFeature)) { + this._geoLocationLayer.getSource()?.addFeature(this._markerFeature); } const heading = this._geoLocation.getHeading() || 0; const speed = this._geoLocation.getSpeed() || 0; diff --git a/src/Button/MeasureButton/MeasureButton.tsx b/src/Button/MeasureButton/MeasureButton.tsx index ed14d196d7..6b1d235431 100644 --- a/src/Button/MeasureButton/MeasureButton.tsx +++ b/src/Button/MeasureButton/MeasureButton.tsx @@ -394,7 +394,7 @@ class MeasureButton extends React.Component { const drawType = measureType === 'polygon' ? OlGeometryType.MULTI_POLYGON : OlGeometryType.MULTI_LINE_STRING; const drawInteraction = new OlInteractionDraw({ - source: this._measureLayer.getSource(), + source: this._measureLayer.getSource() || undefined, type: drawType, maxPoints: maxPoints, style: new OlStyleStyle({ @@ -513,7 +513,9 @@ class MeasureButton extends React.Component { this._eventKeys.click = map.on('click', (e: OlMapBrowserEvent) => this.onMapClick(e)); - if (!multipleDrawing && source.getFeatures().length > 0) { + const features = source?.getFeatures(); + + if (!multipleDrawing && features && features.length > 0) { this.cleanupTooltips(); this.createMeasureTooltip(); @@ -521,7 +523,7 @@ class MeasureButton extends React.Component { this.createHelpTooltip(); } - source.clear(); + source?.clear(); } } @@ -755,7 +757,7 @@ class MeasureButton extends React.Component { this.cleanupTooltips(); if (this._measureLayer) { - this._measureLayer.getSource().clear(); + this._measureLayer.getSource()?.clear(); } } diff --git a/src/Button/ModifyButton/ModifyButton.tsx b/src/Button/ModifyButton/ModifyButton.tsx index 41204d9b6b..972d755d32 100644 --- a/src/Button/ModifyButton/ModifyButton.tsx +++ b/src/Button/ModifyButton/ModifyButton.tsx @@ -238,7 +238,7 @@ export const ModifyButton: React.FC = ({ const onFeatureSelect = (event: OlSelectEvent) => { if (editLabel) { const labeled = event.selected.find(f => f.get('isLabel')); - setEditLabelFeature(labeled); + setEditLabelFeature(labeled || null); } }; diff --git a/src/Container/AddWmsPanel/AddWmsLayerEntry/AddWmsLayerEntry.tsx b/src/Container/AddWmsPanel/AddWmsLayerEntry/AddWmsLayerEntry.tsx index ec0687dab4..1a33a42cb8 100644 --- a/src/Container/AddWmsPanel/AddWmsLayerEntry/AddWmsLayerEntry.tsx +++ b/src/Container/AddWmsPanel/AddWmsLayerEntry/AddWmsLayerEntry.tsx @@ -65,7 +65,7 @@ export class AddWmsLayerEntry extends React.Component(WrappedComponent: React.ComponentType

, laye timeChanged = newValues => { layers.forEach(config => { if (config.isWmsTime) { - const parms = config.layer.getSource().getParams(); + const parms = config.layer.getSource()?.getParams(); const timeParam = findTimeParam(parms); if (_isArray(newValues)) { parms[timeParam] = `${newValues[0]}/${newValues[1]}`; } else { parms[timeParam] = `${newValues}`; } - config.layer.getSource().updateParams(parms); + config.layer.getSource()?.updateParams(parms); } if (config.customHandler) { config.customHandler(newValues); diff --git a/src/LayerSwitcher/LayerSwitcher.tsx b/src/LayerSwitcher/LayerSwitcher.tsx index 6cba0a3601..0b62846469 100644 --- a/src/LayerSwitcher/LayerSwitcher.tsx +++ b/src/LayerSwitcher/LayerSwitcher.tsx @@ -130,7 +130,7 @@ export class LayerSwitcher extends React.Component { this._wmsTimeLayers.forEach(config => { if (config.layer && config.layer.get('type') === 'WMSTime') { - const params = config.layer.getSource().getParams(); + const params = config.layer.getSource()?.getParams(); let time; if (Array.isArray(value)) { time = value[0]; @@ -278,7 +278,7 @@ export class TimeLayerSliderPanel extends React.Component