首页 > 其他分享 >openlayers WebGL裁剪图层,双图层拼接显示

openlayers WebGL裁剪图层,双图层拼接显示

时间:2024-07-11 18:28:51浏览次数:10  
标签:const WebGL height width openlayers new 图层 gl

本篇介绍一下使用openlayers WebGL裁剪图层,双图层拼接显示

1 需求

  • WebGL裁剪图层,双图层拼接显示

2 分析

  • 图层prerender和postrender事件的使用

  • WebGL scissor方法的使用

scissor方法指定了一个裁剪区域,用来将绘图区域限制在其限定的盒形区域内。

gl.scissor(x, y, width, height);

参数:

x,指定盒形裁剪区域左下角所在的横坐标,默认为 0。

y,指定盒形裁剪区域左下角的纵坐标,默认为 0。

width,用来确定盒型裁剪区域宽度的非负数,默认为 canvas 的宽度。

height,用以指定盒形裁剪区域高度的非负数,默认为 canvas 的高度。

4 实现

在这里插入图片描述

<template>
  <div id="map" class="map"></div>
  <div class="toolbar">
    <el-slider v-model="rateH" :min="1" :max="100" :step="1" @input="handleInput"></el-slider>
    <el-slider v-model="rateV" :min="1" :max="100" :step="1" @input="handleInput"></el-slider>
  </div>
</template>

<script setup lang="ts">
import { Map, View } from 'ol';
import { WebGLTile as WebGLTileLayer } from 'ol/layer';
import { fromLonLat, get } from 'ol/proj';
import { XYZ } from 'ol/source';
import { getRenderPixel } from 'ol/render.js';

const projection = get('EPSG:3857');
const key = '替换为天地图key';
const layerTypeMap = {
  vector: ['vec', 'cva'], // [矢量底图, 矢量注记]
  image: ['img', 'cia'], // [影像底图, 影像注记]
  terrain: ['ter', 'cta'] // [地形晕渲, 地形注记]
};
let map = null;
const rateH = ref(50);
const rateV = ref(50);

const imageLayer = new WebGLTileLayer({
  source: new XYZ({
    url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap['image'][0]}_w&tk=${key}&x={x}&y={y}&l={z}`,
    projection
  })
});

const terrainLayer = new WebGLTileLayer({
  source: new XYZ({
    url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap['terrain'][0]}_w&tk=${key}&x={x}&y={y}&l={z}`,
    projection
  })
});

onMounted(() => {
  initMap('image');
});

const initMap = (layerType = 'image') => {
  // c: 经纬度 w: 墨卡托
  const matrixSet = 'w';
  map = new Map({
    target: 'map',
    layers: [
      // 底图
      terrainLayer,
      imageLayer,
      // 注记
      new WebGLTileLayer({
        source: new XYZ({
          url: `https://t{0-7}.tianditu.gov.cn/DataServer?T=${layerTypeMap[layerType][1]}_${matrixSet}&tk=${key}&x={x}&y={y}&l={z}`,
          projection
        })
      })
    ],
    view: new View({
      center: fromLonLat([116.406393, 39.909006]),
      projection: projection,
      zoom: 5,
      maxZoom: 17,
      minZoom: 1
    })
  });

  imageLayer.on('prerender', function (event) {
    const gl = event.context;
    gl.enable(gl.SCISSOR_TEST);
    //获取地图[宽,高]像素(数组)
    const mapSize = map.getSize();
    // getRenderPixel从地图视口的CSS像素获取事件的画布上下文的像素。
    // 获取canvas坐标的左下和右上点坐标
    const bottomLeft = getRenderPixel(event, [0, mapSize[1]]);
    const topRight = getRenderPixel(event, [mapSize[0], 0]);

    const width = Math.round((topRight[0] - bottomLeft[0]) * (rateH.value / 100));
    const height = Math.round((topRight[1] - bottomLeft[1] )* (rateV.value / 100));;
    gl.scissor(bottomLeft[0], bottomLeft[1], width, height);
  });

  imageLayer.on('postrender', function (event) {
    const gl = event.context;
    gl.disable(gl.SCISSOR_TEST);
  });
};

const handleInput = val => {
  map.render();
};
</script>
<style scoped lang="scss">
.map {
  width: 100%;
  height: 100%;
}

.toolbar {
  position: absolute;
  top: 20px;
  left: 100px;
  width: 500px;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #fff;
  .el-slider {
    margin-right: 50px;
  }
  div {
    width: 100px;
    height: 30px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>

如果想拼接图层放在左上角,只需要修改这句

 gl.scissor(bottomLeft[0],topRight[1]-height , width, height);

在这里插入图片描述

标签:const,WebGL,height,width,openlayers,new,图层,gl
From: https://blog.csdn.net/weixin_56624286/article/details/140359251

相关文章

  • C#+OpenCV基础(九)_拆分合并图层
    1、图片拆分通道图层///<summary>///图片拆分通道图层///</summary>///<paramname="mat">图片</param>///<returns></returns>publicstaticMat[]SplitChannel(Matmat){//拆分通道Cv2.Split(mat,outMat[]mats);ret......
  • 275:vue+openlayers 点图标的大小随着分辨率而变化
    作者:还是大剑师兰特,曾为美国某知名大学计算机专业研究生,现为国内GIS领域高级前端工程师,CSDN知名博主,深耕openlayers、leaflet、mapbox、cesium,canvas,echarts等技术开发,欢迎加微信(gis-dajianshi),一起交流。查看本专栏目录-本文是第275个示例文章目录一......
  • 可视化学习:如何用WebGL绘制3D物体
    在之前的文章中,我们使用WebGL绘制了很多二维的图形和图像,在学习2D绘图的时候,我们提过很多次关于GPU的高效渲染,但是2D图形的绘制只展示了WebGL部分的能力,WebGL更强大的地方在于,它可以绘制各种3D图形,而3D图形能够极大地增强可视化的表现能力。相信很多小伙伴都对此有所耳闻,也有不少......
  • mapboxgl加载geoserver发布的tms矢量图层服务
    mapboxgl加载geoserver发布的tms矢量图层服务//添加源g.map.addSource('tms-source',{type:'vector',scheme:'tms',tiles:['http://localhost:9090/geoserver/gwc/service/tms/1.0.0/route:global_port@EPSG:900913@pbf/{z}/{......
  • 274: vue+openlayers 点图标的大小随着zoom的放大缩小而变化
    作者:还是大剑师兰特,曾为美国某知名大学计算机专业研究生,现为国内GIS领域高级前端工程师,CSDN知名博主,深耕openlayers、leaflet、mapbox、cesium,canvas,echarts等技术开发,欢迎加微信(gis-dajianshi),一起交流。查看本专栏目录-本文是第274个示例文章目录一......
  • 273:vue+openlayers 显示流动轨迹并计算航向
    作者:还是大剑师兰特,曾为美国某知名大学计算机专业研究生,现为国内GIS领域高级前端工程师,CSDN知名博主,深耕openlayers、leaflet、mapbox、cesium,canvas,echarts等技术开发,欢迎加微信(gis-dajianshi),一起交流。查看本专栏目录-本文是第273个示例文章目录一......
  • vue+openlayers之几何图形交互绘制基础与实践
    文章目录1.实现效果2.实现步骤3.示例页面代码3.基本几何图形绘制的关键代码1.实现效果绘制点、线、多边形、圆、正方形、长方形2.实现步骤引用openlayers开发库。加载天地图wmts瓦片地图。在页面上添加几何图形绘制的功能按钮,使用下拉列表(select)设置几何图形绘制......
  • cesium 添加 Echarts图层(航线图)
    cesium添加Echarts航线图(下面附有源码)1、实现思路1、在scene上面新增一个canvas画布2、通坐标转换,将经纬度坐标转为屏幕坐标来实现3、将ecarts中每个series数组中元素都加coordinateSystem:‘cesiumEcharts’2、示例代码<!DOCTYPEhtml><htmllang=......
  • cesium 添加 Echarts 图层(空气质量点图)
    cesium添加Echarts图层(下面附有源码)1、实现思路1、在scene上面新增一个canvas画布2、通坐标转换,将经纬度坐标转为屏幕坐标来实现3、将ecarts中每个series数组中元素都加coordinateSystem:‘cesiumEcharts’2、示例代码<!DOCTYPEhtml><htmllang="en"><hea......
  • 271:vue+openlayers的Collection的应用方法演示
    作者:还是大剑师兰特,曾为美国某知名大学计算机专业研究生,现为国内GIS领域高级前端工程师,CSDN知名博主,深耕openlayers、leaflet、mapbox、cesium,canvas,echarts等技术开发,欢迎加微信(gis-dajianshi),一起交流。查看本专栏目录-本文是第271个示例文章目录一......