Skip to content

Commit

Permalink
Merge branch 'main' into geolocation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonseyock authored Sep 19, 2023
2 parents 76d1b46 + 6f425fb commit 39afcdf
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
${{ runner.OS }}-
- name: Install dependencies ⏬
run: npm ci
run: npm ci --force

- name: Lint code 💄
run: npm run lint
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/on-push-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
${{ runner.OS }}-
- name: Install dependencies ⏬
run: npm ci
run: npm ci --force

- name: Lint code 💄
run: npm run lint
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"moment": "^2.29.4",
"np": "^7.5.0",
"ol": "^7.5.2",
"ol-mapbox-style": "^11.0.3",
"prop-types": "^15.8.1",
"react": "^18.0.0",
"react-dom": "^18.1.0",
Expand Down
11 changes: 11 additions & 0 deletions src/BackgroundLayerChooser/BackgroundLayerChooser.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import OlOverviewMap from 'ol/control/OverviewMap';
import OlLayerBase from 'ol/layer/Base';
import LayerGroup from 'ol/layer/Group';
import OlLayerImage from 'ol/layer/Image';
import OlLayer from 'ol/layer/Layer';
import OlLayerTile from 'ol/layer/Tile';
import { ObjectEvent } from 'ol/Object';
import { getUid } from 'ol/util';
import OlView from 'ol/View';
import { apply as applyMapboxStyle } from 'ol-mapbox-style';
import React, {
useEffect,
useRef,
Expand Down Expand Up @@ -88,6 +90,15 @@ export const BackgroundLayerChooser: React.FC<BackgroundLayerChooserProps> = ({
ovLayer = new OlLayerImage({
source: selectedLayer.getSource()
});
} else if (selectedLayer instanceof LayerGroup) {
if (selectedLayer.get('isVectorTile')) {
ovLayer = new LayerGroup();
applyMapboxStyle(ovLayer, selectedLayer.get('url'));
} else {
ovLayer = new LayerGroup({
layers: selectedLayer.getLayers()
});
}
}
if (ovLayer && mapTarget.current) {
const overViewControl = new OlOverviewMap({
Expand Down
27 changes: 21 additions & 6 deletions src/BackgroundLayerPreview/BackgroundLayerPreview.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import MapUtil from '@terrestris/ol-util/dist/MapUtil/MapUtil';
import { Coordinate } from 'ol/coordinate';
import OlLayerBase from 'ol/layer/Base';
import LayerGroup from 'ol/layer/Group';
import OlLayerImage from 'ol/layer/Image';
import OlLayer from 'ol/layer/Layer';
import OlLayerTile from 'ol/layer/Tile';
import OlLayerVector from 'ol/layer/Vector';
import OlMap from 'ol/Map';
import { getUid } from 'ol/util';
import OlView from 'ol/View';
import { apply as applyMapboxStyle } from 'ol-mapbox-style';
import React, { useEffect,useState } from 'react';

import useMap from '../hooks/useMap';
Expand Down Expand Up @@ -51,10 +55,23 @@ export const BackgroundLayerPreview: React.FC<BackgroundLayerPreviewProps> = ({
previewLayer = new OlLayerTile({
source: layer.getSource()
});
} else if (layer instanceof OlLayerImage){
} else if (layer instanceof OlLayerImage) {
previewLayer = new OlLayerImage({
source: layer.getSource()
});
} else if (layer instanceof OlLayerVector) {
previewLayer = new OlLayerVector({
source: layer.getSource()
});
} else if (layer instanceof LayerGroup) {
if (layer.get('isVectorTile')) {
previewLayer = new LayerGroup();
applyMapboxStyle(previewLayer, layer.get('url'));
} else {
previewLayer = new LayerGroup({
layers: layer.getLayers()
});
}
}

setPreviewMap(new OlMap({
Expand Down Expand Up @@ -98,8 +115,8 @@ export const BackgroundLayerPreview: React.FC<BackgroundLayerPreviewProps> = ({
}, [zoom, center]);

const getBgLayersFromMap = (): OlLayer[] => {
return mainMap?.getLayerGroup().getLayers()
.getArray().filter(backgroundLayerFilter) as OlLayer[] || [];
return MapUtil.getAllLayers(mainMap)
.filter(backgroundLayerFilter) as OlLayer[] || [];
};

const updateBgLayerVisibility = (evt: React.MouseEvent<HTMLDivElement>) => {
Expand All @@ -110,7 +127,7 @@ export const BackgroundLayerPreview: React.FC<BackgroundLayerPreviewProps> = ({
return;
}

const newBgLayer = mainMap?.getLayerGroup().getLayers().getArray()
const newBgLayer = MapUtil.getAllLayers(mainMap)
.find(l => getUid(l) === layerId);

if (!newBgLayer) {
Expand All @@ -123,7 +140,6 @@ export const BackgroundLayerPreview: React.FC<BackgroundLayerPreviewProps> = ({
if (evt.type === 'click') {
onClick(newBgLayer as OlLayer);
}

};

const restoreBgLayerVisibility = () => {
Expand All @@ -134,7 +150,6 @@ export const BackgroundLayerPreview: React.FC<BackgroundLayerPreviewProps> = ({
const uid = getUid(layer);
const activeUid = getUid(activeLayer);
const isActive = uid === activeUid;

if (!previewMap) {
return <></>;
}
Expand Down
182 changes: 182 additions & 0 deletions src/hooks/useModify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
import OlCollection from 'ol/Collection';
import { singleClick } from 'ol/events/condition';
import OlFeature from 'ol/Feature';
import OlGeometry from 'ol/geom/Geometry';
import Modify, { ModifyEvent, Options as ModifyOptions } from 'ol/interaction/Modify';
import Translate, { Options as TranslateOptions, TranslateEvent } from 'ol/interaction/Translate';
import OlVectorLayer from 'ol/layer/Vector';
import OlVectorSource from 'ol/source/Vector';
import {useEffect, useMemo, useRef} from 'react';

import { DigitizeUtil } from '../Util/DigitizeUtil';
import useMap from './useMap';
import {useOlInteraction} from './useOlInteraction';
import {useOlListener} from './useOlListener';
import {usePropOrDefault} from './usePropOrDefault';
import {useSelectFeatures, UseSelectFeaturesProps} from './useSelectFeatures';

interface OwnProps {
/**
* Active state of interactions
*/
active: boolean;
/**
* Additional configuration object to apply to the ol.interaction.Modify.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-Modify.html
* for more information
*
* Note: The keys features, deleteCondition and style are handled internally
* and shouldn't be overwritten without any specific cause.
*/
modifyInteractionConfig?: Omit<ModifyOptions, 'features'|'source'|'deleteCondition'|'style'>;
/**
* Additional configuration object to apply to the ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-Translate.html
* for more information
*
* Note: The key feature is handled internally and shouldn't be overwritten
* without any specific cause.
*/
translateInteractionConfig?: Omit<TranslateOptions, 'features'|'layers'>;
/**
* The vector layer which will be used for digitize features.
* The standard digitizeLayer can be retrieved via `DigitizeUtil.getDigitizeLayer(map)`.
*/
digitizeLayer?: OlVectorLayer<OlVectorSource<OlGeometry>>;
/**
* Listener function for the 'modifystart' event of an ol.interaction.Modify.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-ModifyEvent.html
* for more information.
*/
onModifyStart?: (event: ModifyEvent) => void;
/**
* Listener function for the 'modifyend' event of an ol.interaction.Modify.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Modify-ModifyEvent.html
* for more information.
*/
onModifyEnd?: (event: ModifyEvent) => void;
/**
* Listener function for the 'translatestart' event of an ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-TranslateEvent.html
* for more information.
*/
onTranslateStart?: (event: TranslateEvent) => void;
/**
* Listener function for the 'translateend' event of an ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-TranslateEvent.html
* for more information.
*/
onTranslateEnd?: (event: TranslateEvent) => void;
/**
* Listener function for the 'translating' event of an ol.interaction.Translate.
* See https://openlayers.org/en/latest/apidoc/module-ol_interaction_Translate-TranslateEvent.html
* for more information.
*/
onTranslating?: (event: TranslateEvent) => void;
}

export type UseModifyProps = OwnProps & Omit<UseSelectFeaturesProps,
'layers'|'featuresCollection'|'clearAfterSelect'>;

export const useModify = ({
active,
onFeatureSelect,
onModifyStart,
onModifyEnd,
onTranslateStart,
onTranslateEnd,
onTranslating,
digitizeLayer,
selectStyle,
selectInteractionConfig,
hitTolerance,
modifyInteractionConfig,
translateInteractionConfig
}: UseModifyProps) => {
const map = useMap();

const layer = usePropOrDefault(
digitizeLayer,
() => DigitizeUtil.getDigitizeLayer(map),
[map]
);

const featuresRef = useRef(new OlCollection<OlFeature>());

const layers = useMemo(() => layer ? [layer] : [], [layer]);

useEffect(() => {
if (!active) {
featuresRef.current.clear();
}
}, [active]);

useSelectFeatures({
clearAfterSelect: false,
onFeatureSelect,
active,
layers,
selectStyle,
selectInteractionConfig,
hitTolerance,
featuresCollection: featuresRef.current
});

const translateInteraction = useOlInteraction(
() => {
const newTranslateInteraction = new Translate({
features: featuresRef.current,
...translateInteractionConfig
});
newTranslateInteraction.set('name', 'react-geo-translate-interaction');
return newTranslateInteraction;
},
[translateInteractionConfig],
active
);

const modifyInteraction = useOlInteraction(
() => {
const newModifyInteraction = new Modify({
features: featuresRef.current,
deleteCondition: singleClick,
style: selectStyle ?? DigitizeUtil.DEFAULT_SELECT_STYLE,
...modifyInteractionConfig
});
newModifyInteraction.set('name', 'react-geo-modify-interaction');
return newModifyInteraction;
},
[modifyInteractionConfig],
active
);

useOlListener(
modifyInteraction,
i => i.on('modifystart', e => onModifyStart?.(e)),
[onModifyStart]
);

useOlListener(
modifyInteraction,
i => i.on('modifyend', e => onModifyEnd?.(e)),
[onModifyEnd]
);

useOlListener(
translateInteraction,
i => i.on('translatestart', e => onTranslateStart?.(e)),
[onTranslateStart]
);

useOlListener(
translateInteraction,
i => i.on('translateend', e => onTranslateEnd?.(e)),
[onTranslateEnd]
);

useOlListener(
translateInteraction,
i => i.on('translating', e => onTranslating?.(e)),
[onTranslating]
);
};

0 comments on commit 39afcdf

Please sign in to comment.