Skip to content

Commit 76f410d

Browse files
committed
refactor(ol-source-image-wms): pass-through all props and events from ol/source/ImageWMS
1 parent 3621c25 commit 76f410d

File tree

2 files changed

+66
-83
lines changed

2 files changed

+66
-83
lines changed

docs/componentsguide/sources/imagewms/index.md

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -54,74 +54,67 @@ const center = ref([-10997148, 4569099]);
5454

5555
## Properties
5656

57-
### attributions
57+
### Props from OpenLayers
5858

59-
- **Type**: `string`
60-
61-
Attributions
62-
63-
### crossOrigin
64-
65-
- **Type**: `string`
66-
- **Default**: `ol-layer`
67-
68-
The crossOrigin attribute for loaded images. Note that you must provide a crossOrigin value if you want to access pixel data with the Canvas renderer.
69-
70-
### hidpi
71-
72-
- **Type**: `Boolean`
73-
- **Default**: `true`
74-
75-
### serverType
76-
77-
- **Type**: `string`
59+
Properties are passed-trough from OpenLayers directly.
60+
Their types and default values can be checked-out [in the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_source_ImageWMS-ImageWMS.html).
61+
Only some properties deviate caused by reserved keywords from Vue / HTML.
62+
This deviating props are described in the section below.
7863

79-
The type of the remote WMS server: mapserver, geoserver or qgis. Only needed if hidpi is true.
64+
### Deviating Properties
8065

81-
### imageLoadFunction
66+
The following additional properties are available for setting specific `params`.
8267

83-
- **Type**: `Function`
68+
#### layers
8469

85-
Optional function to load an image given a URL
70+
Sets / overrides the `params.LAYERS` property.
8671

87-
### imageSmoothing
72+
- **Type**: `string` | `unknown[]`
8873

89-
- **Type**: `Boolean`
90-
- **Default**: `true`
74+
#### styles
9175

92-
Enable image smoothing.
76+
Sets / overrides the `params.STYLES` property.
9377

94-
### layers
78+
- **Type**: `string` | `unknown[]`
9579

96-
- **Type**: `[String,Array]`
80+
#### time
9781

98-
### styles
82+
Sets / overrides the `params.TIME` property.
9983

100-
- **Type**: `[String,Array]`
101-
102-
### ratio
103-
104-
- **Type**: `number`
105-
- **Default**: `1.5`
84+
- **Type**: `string`
10685

107-
Ratio. 1 means image requests are the size of the map viewport, 2 means twice the width and height of the map viewport, and so on. Must be 1 or higher.
86+
## Events
10887

109-
### resolutions
88+
You have access to all Events from the underlying source.
89+
Check out [the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_source_ImageWMS-ImageWMS.html) to see the available events tht will be fired.
11090

111-
- **Type**: `Array<number>`
91+
```html
92+
<ol-source-image-wms :url="imgUrl" @error="handleEvent" />
93+
```
11294

113-
Resolutions. If specified, requests will be made for these resolutions only.
95+
## Methods
11496

115-
### url
97+
You have access to all Methods from the underlying source.
98+
Check out [the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_source_ImageWMS-ImageWMS.html) to see the available methods.
11699

117-
- **Type**: `string`
100+
To access the source, you can use a `ref()` as shown below:
118101

119-
WMS service URL.
102+
```vue
103+
<template>
104+
<!-- ... -->
105+
<ol-source-image-wms :url="url" ref="sourceRef" />
106+
<!-- ... -->
107+
</template>
120108
121-
### params
109+
<script setup lang="ts">
110+
import { ref, onMounted } from "vue";
111+
import type ImageWMS from "ol/source/ImageWMS";
122112
123-
WMS request parameters.
124-
`VERSION` is `1.3.0` by default.
125-
`WIDTH`, `HEIGHT`, `BBOX` and `CRS` (`SRS` for WMS version < `1.3.0`) will be set dynamically.
113+
const sourceRef = ref<{ source: ImageWMS }>(null);
126114
127-
- **Type**: `Object.<string, *>`
115+
onMounted(() => {
116+
const source: ImageWMS = sourceRef.value?.source;
117+
// call your method on `source`
118+
});
119+
</script>
120+
```

src/components/sources/OlSourceImageWMS.vue

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,22 @@
22
<div v-if="false"></div>
33
</template>
44
<script setup lang="ts">
5-
import ImageWMS from "ol/source/ImageWMS";
6-
import type { Options as ProjectionOptions } from "ol/proj/Projection";
7-
import Projection from "ol/proj/Projection";
5+
import ImageWMS, { type Options } from "ol/source/ImageWMS";
86
import { inject, onMounted, onUnmounted, watch } from "vue";
9-
import type { Extent } from "ol/extent";
107
import type ImageLayer from "ol/layer/Image";
118
import type ImageSource from "ol/source/Image";
12-
import type { ServerType } from "ol/source/wms";
139
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
14-
import type { ProjectionLike } from "ol/proj";
10+
import projectionFromProperties from "@/helpers/projection";
11+
import eventGateway from "@/helpers/eventGateway";
1512
1613
const props = withDefaults(
17-
defineProps<{
18-
attributions?: string;
19-
crossOrigin?: string;
20-
imageExtent?: Extent;
21-
projection?: ProjectionLike;
22-
reprojectionErrorThreshold?: number;
23-
format?: string;
24-
version?: string;
25-
matrixSet?: string;
26-
serverType?: ServerType;
27-
imageSmoothing?: boolean;
28-
layers: string | unknown[];
29-
styles?: string | unknown[];
30-
time?: string;
31-
ratio?: number;
32-
imageSize?: number[];
33-
url?: string;
34-
params?: Record<string, unknown>;
35-
imageLoadFunction?: (...args: unknown[]) => unknown;
36-
}>(),
14+
defineProps<
15+
Options & {
16+
layers: string | unknown[];
17+
styles?: string | unknown[];
18+
time?: string;
19+
}
20+
>(),
3721
{
3822
projection: "EPSG:3857",
3923
reprojectionErrorThreshold: 0.5,
@@ -45,27 +29,33 @@ const props = withDefaults(
4529
ratio: 1,
4630
}
4731
);
32+
const emit = defineEmits([]);
4833
4934
const layer = inject<ImageLayer<ImageSource> | null>("imageLayer");
5035
const { properties } = usePropsAsObjectProperties(props);
5136
5237
const createSource = () => {
53-
return new ImageWMS({
38+
const i = new ImageWMS({
5439
...properties,
5540
params: {
5641
...props.params,
5742
LAYERS: props.layers,
5843
STYLES: props.styles,
5944
TIME: props.time,
6045
},
61-
projection:
62-
typeof properties.projection === "string"
63-
? properties.projection
64-
: // @ts-ignore
65-
new Projection({
66-
...properties.projection,
67-
}),
46+
projection: projectionFromProperties(properties.projection),
6847
});
48+
49+
eventGateway(emit, i, [
50+
"change",
51+
"error",
52+
"imageloadend",
53+
"imageloaderror",
54+
"imageloadstart",
55+
"propertychange",
56+
]);
57+
58+
return i;
6959
};
7060
7161
let source = createSource();

0 commit comments

Comments
 (0)