Skip to content

Commit 9a36975

Browse files
committed
✨ Add ImageLayer component and related stories for OpenLayers integration
1 parent 13a8b1d commit 9a36975

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

src/lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export { addMarker, removeMarker, getMarkerImage } from './marker-func';
88

99
export { LayerGroup } from './layers/LayerGroup';
1010
export { TileLayer } from './layers/TileLayer';
11+
export { ImageLayer } from './layers/ImageLayer';
1112
export { VectorLayer } from './layers/VectorLayer';
1213
export { GraticuleLayer } from './layers/GraticuleLayer';
1314
export { HeatmapLayer } from './layers/HeatmapLayer';

src/lib/layers/ImageLayer.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Map as OlMap} from 'ol';
2+
import { useEffect, useRef } from 'react';
3+
import OlImageLayer from 'ol/layer/Image';
4+
import { useMap } from '../Map';
5+
import { useGroup } from './LayerGroup';
6+
7+
export function ImageLayer(props: any) {
8+
const map = useMap();
9+
const group = useGroup();
10+
const layerRef = useRef(new OlImageLayer(props)); // single instance
11+
12+
useEffect(() => {
13+
if (!map && !group) return;
14+
15+
const layer = layerRef.current; // same instance every time
16+
props.name && layer.set('name', props.name);
17+
const target = group || map;
18+
19+
if (target) {
20+
if (target instanceof OlMap) {
21+
target.addLayer(layer);
22+
} else {
23+
target.getLayers().push(layer);
24+
}
25+
}
26+
27+
return () => {
28+
if (target) {
29+
if (target instanceof OlMap) {
30+
target.removeLayer(layer);
31+
} else {
32+
target.getLayers().remove(layer);
33+
}
34+
}
35+
};
36+
}, [map, group]);
37+
38+
return null;
39+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Story, Meta } from '@storybook/blocks';
2+
import * as ImageStories from './ImageLayer.stories';
3+
4+
<Meta of={ImageStories} />
5+
6+
# {'<ImageLayer />'}
7+
8+
A react representative of `new ImageLayer(...)`
9+
https://openlayers.org/en/latest/apidoc/module-ol_layer_Image-ImageLayer.html
10+
<Story of={ImageStories.Primary} />
11+
12+
### Usage
13+
```typescript
14+
import {useRef} from 'react';
15+
import ImageWMS from 'ol/source/ImageWMS';
16+
import OSM from 'ol/source/OSM';
17+
import {Map, ImageLayer, View} from 'react-openlayers';
18+
19+
export default function(props) {
20+
const mapRef = useRef();
21+
const source = new ImageWMS({
22+
url: 'https://ahocevar.com/geoserver/wms',
23+
params: {'LAYERS': 'topp:states'},
24+
ratio: 1,
25+
serverType: 'geoserver',
26+
})
27+
return (
28+
<Map ref={mapRef}>
29+
<ImageLayer source={source} extent={[-13884991, 2870341, -7455066, 6338219]} properties={{key: 'myLayer'}}/>
30+
<View center={[-10997148, 4569099]} zoom={4}/>
31+
</Map>
32+
);
33+
}
34+
```
35+
36+
### props
37+
38+
| Prop | type | description |
39+
| ------ | ----------- | ------------|
40+
| source | ImageSource | Source for this layer. |
41+
| extent | Extent | The bounding extent for layer rendering. The layer will not be rendered outside of this extent. |
42+
| zIndex | number | The z-index for layer rendering. At rendering time, the layers will be ordered, first by Z-index and then by position. |
43+
| properties | Object | Arbitrary observable properties. Can be accessed with `#get()` and `#set()` |
44+
| ... | ... | For more, refer to Openlayers documentation |
45+
46+
### props
47+
48+
You can pass any props defined in the
49+
[Openlayers documentation](https://openlayers.org/en/latest/apidoc/module-ol_layer_Image-ImageLayer.html)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import ImageWMS from 'ol/source/ImageWMS';
2+
import { Map, View, ImageLayer } from '../../../lib';
3+
4+
export default {
5+
title: 'Layers/ImageLayer',
6+
}
7+
8+
export const Primary = {
9+
render: (props) => {
10+
const source = new ImageWMS({
11+
url: 'https://ahocevar.com/geoserver/wms',
12+
params: {'LAYERS': 'topp:states'},
13+
ratio: 1,
14+
serverType: 'geoserver',
15+
})
16+
return <Map>
17+
<ImageLayer source={source}
18+
extent={[-13884991, 2870341, -7455066, 6338219]}
19+
properties={{key: 'myLayer'}} />
20+
<View center={[-10997148, 4569099]} zoom={4}/>
21+
</Map>
22+
}
23+
}

0 commit comments

Comments
 (0)