官网demo地址:
这篇讲的是加载blob格式的GeoTIFF数据源。
我们找一个tif文件下载下来
https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/36/Q/WD/2020/7/S2A_36QWD_20200701_0_L2A/TCI.tif
放到项目里
然后使用网络请求获取文件并且转为blob格式就可以加载到地图上啦。
fetch("data/tcl.tif")
.then((response) => response.blob())
.then((blob) => {
const source = new GeoTIFF({
sources: [
{
blob: blob,
},
],
});
})
})
小细节:
source.getView().then的回调里还可以获取到View对象,还可以更改属性。
view: source.getView().then((viewConfig) => {
console.log('viewConfig',viewConfig);
viewConfig.showFullExtent = true;
return viewConfig;
}),
完整代码:
<template>
<div class="box">
<h1>Cloud Optimized GeoTIFF (COG) from a Blob</h1>
<div id="map"></div>
<div class="btn"></div>
</div>
</template>
<script>
import GeoTIFF from "ol/source/GeoTIFF.js";
import Map from "ol/Map.js";
import TileLayer from "ol/layer/WebGLTile.js";
export default {
name: "",
components: {},
data() {
return {
map: null,
};
},
computed: {},
created() {},
mounted() {
fetch("data/tcl.tif")
.then((response) => response.blob())
.then((blob) => {
const source = new GeoTIFF({
sources: [
{
blob: blob,
},
],
});
const map = new Map({
target: "map",
layers: [
new TileLayer({
source: source,
}),
],
view: source.getView().then((viewConfig) => {
console.log('viewConfig',viewConfig);
viewConfig.showFullExtent = true;
return viewConfig;
}),
});
});
},
methods: {},
};
</script>
<style lang="scss" scoped>
#map {
width: 100%;
height: 500px;
}
.box {
height: 100%;
}
</style>