Skip to content

Commit 6f2815d

Browse files
committed
refactor(ol-source-wmts): migrate to script setup
1 parent e1b63ba commit 6f2815d

File tree

5 files changed

+171
-206
lines changed

5 files changed

+171
-206
lines changed

docs/componentsguide/sources/wmts/index.md

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Example below shows how to use ol-layer-tile component together with ol-source-w
1919
<ol-map
2020
:loadTilesWhileAnimating="true"
2121
:loadTilesWhileInteracting="true"
22-
style="height:400px"
22+
style="height: 400px"
2323
>
2424
<ol-view ref="view" :center="center" :rotation="rotation" :zoom="zoom" />
2525
@@ -40,35 +40,20 @@ Example below shows how to use ol-layer-tile component together with ol-source-w
4040
</ol-map>
4141
</template>
4242
43-
<script>
43+
<script setup>
4444
import { ref } from "vue";
45-
export default {
46-
setup() {
47-
const center = ref([-11158582, 4813697]);
48-
const zoom = ref(4);
49-
const rotation = ref(0);
50-
const url = ref("https://mrdata.usgs.gov/mapcache/wmts");
51-
const layerName = ref("sgmc2");
52-
const matrixSet = ref("GoogleMapsCompatible");
53-
const format = ref("image/png");
54-
const styleName = ref("default");
55-
const attribution = ref(
56-
'Tiles © <a href="https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/">ArcGIS</a>'
57-
);
58-
59-
return {
60-
center,
61-
zoom,
62-
rotation,
63-
url,
64-
layerName,
65-
matrixSet,
66-
format,
67-
styleName,
68-
attribution,
69-
};
70-
},
71-
};
45+
46+
const center = ref([-11158582, 4813697]);
47+
const zoom = ref(4);
48+
const rotation = ref(0);
49+
const url = ref("https://mrdata.usgs.gov/mapcache/wmts");
50+
const layerName = ref("sgmc2");
51+
const matrixSet = ref("GoogleMapsCompatible");
52+
const format = ref("image/png");
53+
const styleName = ref("default");
54+
const attribution = ref(
55+
'Tiles © <a href="https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Population_Density/MapServer/">ArcGIS</a>'
56+
);
7257
</script>
7358
```
7459

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<template>
2+
<div v-if="false"></div>
3+
</template>
4+
5+
<script setup>
6+
import WMTS from "ol/source/WMTS";
7+
import Projection from "ol/proj/Projection";
8+
import WMTSTileGrid from "ol/tilegrid/WMTS";
9+
import { get as getProjection } from "ol/proj";
10+
import { getTopLeft, getWidth } from "ol/extent";
11+
import { inject, onMounted, onUnmounted, watch, computed } from "vue";
12+
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
13+
14+
const props = defineProps({
15+
tileZoomLevel: {
16+
type: Number,
17+
default: 30,
18+
},
19+
attributions: {
20+
type: String,
21+
},
22+
cacheSize: {
23+
type: Number,
24+
},
25+
crossOrigin: {
26+
type: String,
27+
},
28+
projection: {
29+
type: [String, Object],
30+
default: "EPSG:3857",
31+
},
32+
reprojectionErrorThreshold: {
33+
type: Number,
34+
default: 0.5,
35+
},
36+
tilePixelRatio: {
37+
type: Number,
38+
default: 1,
39+
},
40+
format: {
41+
type: String,
42+
default: "image/jpeg",
43+
},
44+
version: {
45+
type: String,
46+
default: "1.0.0",
47+
},
48+
matrixSet: {
49+
type: String,
50+
},
51+
dimensions: {
52+
type: Object,
53+
},
54+
requestEncoding: {
55+
type: String,
56+
default: "KVP",
57+
},
58+
url: {
59+
type: String,
60+
},
61+
urls: {
62+
type: Array,
63+
},
64+
wrapX: {
65+
type: Boolean,
66+
default: false,
67+
},
68+
transition: {
69+
type: Number,
70+
},
71+
layer: {
72+
type: String,
73+
},
74+
tileMatrixPrefix: {
75+
type: String,
76+
default: "",
77+
},
78+
styles: {
79+
type: [String, Array, Function],
80+
},
81+
});
82+
83+
const tileLayer = inject("tileLayer");
84+
const { properties } = usePropsAsObjectProperties(props);
85+
86+
const extent = computed(() => getProjection(properties.projection).getExtent());
87+
const origin = computed(() => getTopLeft(extent.value));
88+
const size = computed(() => getWidth(extent.value) / 256);
89+
90+
const getTileGrid = computed(() => {
91+
const resolutions = new Array(properties.tileZoomLevel);
92+
const matrixIds = new Array(properties.tileZoomLevel);
93+
94+
for (let z = 0; z < properties.tileZoomLevel; ++z) {
95+
resolutions[z] = size.value / Math.pow(2, z);
96+
matrixIds[z] = props.tileMatrixPrefix + z;
97+
}
98+
99+
return new WMTSTileGrid({
100+
origin: origin.value,
101+
resolutions,
102+
matrixIds,
103+
});
104+
});
105+
106+
const source = computed(
107+
() =>
108+
new WMTS({
109+
...properties,
110+
projection:
111+
typeof properties.projection == "string"
112+
? properties.projection
113+
: new Projection({
114+
...properties.projection,
115+
}),
116+
tileGrid: getTileGrid.value,
117+
})
118+
);
119+
120+
watch(source, () => {
121+
tileLayer.value.setSource(source.value);
122+
});
123+
124+
watch(tileLayer, () => {
125+
tileLayer.value.setSource(source.value);
126+
});
127+
128+
onMounted(() => {
129+
tileLayer.value.setSource(source.value);
130+
});
131+
132+
onUnmounted(() => {
133+
tileLayer.value.setSource(null);
134+
});
135+
136+
defineExpose({
137+
tileLayer,
138+
source,
139+
});
140+
</script>
141+
142+
<style lang=""></style>

src/components/sources/SourceWMTS.vue

Lines changed: 0 additions & 147 deletions
This file was deleted.

src/components/sources/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import OlSourceXYZ from "./OlSourceXYZ.vue";
2+
import OlSourceWMTS from "./OlSourceWMTS.vue";
23
import SourceOSM from "./SourceOSM.vue";
34
import SourceImageStatic from "./SourceImageStatic.vue";
4-
import SourceWMTS from "./SourceWMTS.vue";
55
import SourceVector from "./SourceVector.vue";
66
import SourceCluster from "./SourceCluster.vue";
77
import SourceBingMaps from "./SourceBingMaps.vue";
@@ -19,9 +19,9 @@ function install(app) {
1919
install.installed = true;
2020

2121
app.component("ol-source-xyz", OlSourceXYZ);
22+
app.component("ol-source-wmts", OlSourceWMTS);
2223
app.component(SourceOSM.name, SourceOSM);
2324
app.component(SourceImageStatic.name, SourceImageStatic);
24-
app.component(SourceWMTS.name, SourceWMTS);
2525
app.component(SourceVector.name, SourceVector);
2626
app.component(SourceCluster.name, SourceCluster);
2727
app.component(SourceBingMaps.name, SourceBingMaps);
@@ -37,9 +37,9 @@ export default install;
3737
export {
3838
install,
3939
OlSourceXYZ,
40+
OlSourceWMTS,
4041
SourceOSM,
4142
SourceImageStatic,
42-
SourceWMTS,
4343
SourceVector,
4444
SourceCluster,
4545
SourceTianDiTu,

0 commit comments

Comments
 (0)