Skip to content

Commit

Permalink
chore: update to ol 8 api
Browse files Browse the repository at this point in the history
BREAKING CHANGE: the AddWmsLayerEntry accepts a map prop now (required for attributions)
  • Loading branch information
dnlkoch authored and simonseyock committed May 6, 2024
1 parent ddc8e7d commit fc1ff8c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
10 changes: 0 additions & 10 deletions src/Button/PrintButton/PrintButton.example.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import OlMap from 'ol/Map';
import {fromLonLat, get as getProjection} from 'ol/proj';
import {OSM, Vector as VectorSource} from 'ol/source';
import OlSourceOsm from 'ol/source/OSM';
import OlSourceStamen from 'ol/source/Stamen';
import OlSourceTileWMS from 'ol/source/TileWMS';
import WMTS from 'ol/source/WMTS';
import {Circle as CircleStyle, Fill, Stroke, Style} from 'ol/style';
Expand Down Expand Up @@ -133,18 +132,9 @@ const PrintButtonExample = () => {
osm.set('name', 'OpenStreetMap');
osm.set('legendUrl', 'https://terrestris.github.io/react-geo/assets/legend1.png');

const stamen = new OlLayerTile({
source: new OlSourceStamen({
layer: 'watercolor'
})
});
stamen.set('name', 'Stamen');
stamen.set('legendUrl', 'https://terrestris.github.io/react-geo/assets/legend2.png');

const newMap = new OlMap({
layers: [
osm,
stamen,
new OlLayerTile({
name: 'True Color Composite',
opacity: 0.5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import OlLayerTile from 'ol/layer/Tile';
import OlSourceTileWMS from 'ol/source/TileWMS';
import * as React from 'react';

import TestUtil from '../../../Util/TestUtil';
import AddWmsLayerEntry from './AddWmsLayerEntry';

describe('<AddWmsLayerEntry />', () => {
Expand Down Expand Up @@ -37,7 +38,9 @@ describe('<AddWmsLayerEntry />', () => {
it('adds queryable icon if prop wmsLayer has queryable set to true', () => {
testLayer.set('queryable', true);

render(<AddWmsLayerEntry wmsLayer={testLayer} />);
const map = TestUtil.createMap();

render(<AddWmsLayerEntry map={map} wmsLayer={testLayer} />);
let icon;
expect(() => {
icon = screen.getByLabelText(labelIconQueryable);
Expand All @@ -58,7 +61,8 @@ describe('<AddWmsLayerEntry />', () => {
const wmsAttribution = 'Test - attribution';
testLayer.getSource()?.setAttributions(wmsAttribution);

render(<AddWmsLayerEntry wmsLayer={testLayer} />);
const map = TestUtil.createMap();
render(<AddWmsLayerEntry map={map} wmsLayer={testLayer} />);
let icon;
expect(() => {
icon = screen.getByLabelText(labelIconAttribution);
Expand Down
43 changes: 29 additions & 14 deletions src/Container/AddWmsPanel/AddWmsLayerEntry/AddWmsLayerEntry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import { faCopyright, faInfo } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { WmsLayer } from '@terrestris/react-util/dist/Util/typeUtils';
import { Checkbox, Tooltip } from 'antd';
import { FrameState } from 'ol/Map';
import { Attribution as OlAttribution } from 'ol/source/Source';
import OlMap from 'ol/Map';
import React, { useEffect, useState } from 'react';

export type AddWmsLayerEntryProps = {
/**
* The map.
*/
map?: OlMap;
/**
* Object containing layer information
*/
Expand Down Expand Up @@ -44,6 +47,7 @@ export type AddWmsLayerEntryProps = {
*
*/
export const AddWmsLayerEntry: React.FC<AddWmsLayerEntryProps> = ({
map,
wmsLayer,
layerQueryableText = 'Layer is queryable',
ariaLabelCopyright = 'Icon indicating that attribution information for layer is available',
Expand All @@ -61,19 +65,30 @@ export const AddWmsLayerEntry: React.FC<AddWmsLayerEntryProps> = ({
const [queryable, setQueryable] = useState<boolean>();

useEffect(() => {
if (wmsLayer) {
if (wmsLayer.getSource()?.getAttributions()) {
const attributionsFn = wmsLayer.getSource()?.getAttributions() as OlAttribution;
const attributions = (attributionsFn({} as FrameState));
if (attributions?.length > 0) {
if (Array.isArray(attributions)) {
setCopyright(attributions.join(', '));
} else {
setCopyright(attributions);
}
}
if (!map || !wmsLayer) {
return;
}

setQueryable(!!wmsLayer.get('queryable'));

const attributionsFn = wmsLayer.getSource()?.getAttributions();

if (!attributionsFn) {
return;
}

const mapView = map.getView();
const attributions = attributionsFn({
extent: mapView.calculateExtent(),
viewState: mapView.getState()
});

if (attributions?.length > 0) {
if (Array.isArray(attributions)) {
setCopyright(attributions.join(', '));
} else {
setCopyright(attributions);
}
setQueryable(!!wmsLayer.get('queryable'));
}
}, [wmsLayer]);

Expand Down
1 change: 1 addition & 0 deletions src/Container/AddWmsPanel/AddWmsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export const AddWmsPanel: React.FC<AddWmsPanelProps> = ({
<div role="listitem" key={idx}>
<AddWmsLayerEntry
wmsLayer={layer}
map={map}
/>
</div>
)
Expand Down
9 changes: 4 additions & 5 deletions src/LayerSwitcher/LayerSwitcher.example.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import LayerSwitcher from '@terrestris/react-geo/dist/LayerSwitcher/LayerSwitche
import OlLayerTile from 'ol/layer/Tile';
import OlMap from 'ol/Map';
import OlSourceOsm from 'ol/source/OSM';
import OlSourceStamen from 'ol/source/Stamen';
import OlSourceTileWMS from 'ol/source/TileWMS';
import OlView from 'ol/View';
import * as React from 'react';

Expand All @@ -25,11 +25,10 @@ class LayerSwitcherExample extends React.Component {
source: new OlSourceOsm()
}),
new OlLayerTile({
name: 'Stamen',
source: new OlSourceStamen({
layer: 'watercolor'
source: new OlSourceTileWMS({
url: 'https://ows.terrestris.de/osm/service?'
})
}),
})
];

this.map = new OlMap({
Expand Down
7 changes: 2 additions & 5 deletions src/LayerSwitcher/LayerSwitcher.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { render, screen,within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import OlLayerTile from 'ol/layer/Tile';
import OlSourceOsm from 'ol/source/OSM';
import OlSourceStamen from 'ol/source/Stamen';
import React from 'react';

import TestUtil from '../Util/TestUtil';
Expand All @@ -18,10 +17,8 @@ describe('<LayerSwitcher />', () => {
source: new OlSourceOsm()
}),
new OlLayerTile({
source: new OlSourceStamen({
layer: 'watercolor'
})
}),
source: new OlSourceOsm()
})
];
map = TestUtil.createMap();
map.addLayer(layers[0]);
Expand Down

0 comments on commit fc1ff8c

Please sign in to comment.