Skip to content

Commit

Permalink
Merge pull request #476 from terrestris/add-vector-tile-support
Browse files Browse the repository at this point in the history
feat: add support vor mapbox vector tiles
  • Loading branch information
hwbllmnn authored Sep 14, 2023
2 parents ba36161 + 52a6982 commit 6f425fb
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 11 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.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
},
"homepage": "https://github.com/terrestris/react-util#readme",
"peerDependencies": {
"ol": ">=7",
"react": ">=17",
"react-dom": ">=17",
"ol": ">=7"
"react-dom": ">=17"
},
"dependencies": {
"@terrestris/base-util": "^1.0.1",
Expand All @@ -48,7 +48,6 @@
"@babel/plugin-proposal-optional-chaining": "^7.13.12",
"@babel/preset-env": "^7.14.1",
"@babel/preset-react": "^7.13.13",
"eslint-plugin-simple-import-sort": "^10.0.0",
"@babel/preset-typescript": "^7.13.0",
"@cfaester/enzyme-adapter-react-18": "^0.7.1",
"@terrestris/eslint-config-typescript": "^3.0.0",
Expand All @@ -66,6 +65,7 @@
"core-js": "^3.12.0",
"enzyme": "^3.11.0",
"eslint": "^8.14.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"jest": "^29.6.4",
"jest-canvas-mock": "^2.5.2",
"jest-environment-jsdom": "^29.6.4",
Expand All @@ -75,6 +75,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

0 comments on commit 6f425fb

Please sign in to comment.