美文网首页GIS加油站
openlayers中区域掩膜的实现

openlayers中区域掩膜的实现

作者: 牛老师讲GIS | 来源:发表于2024-07-03 22:52 被阅读0次

概述

在前文完成了mapboxGL中区域掩膜的实现。近日有人问到说在openlayers中如何实现,本文就带大家看看如何在openlayers中实现区域掩膜。

实现效果

map1.gif

实现

1. 实现思路

  1. 在地图容器中添加一个canvas,设置其在map之上;
  2. 监听map的postrender事件,每次事件触发重新绘制掩膜;
  3. 通过map.getPixelFromCoordinate实现地理坐标到屏幕坐标的转换;
  4. 通过globalCompositeOperation = 'source-out'设置反向裁剪;

2. 实现代码

1. 添加canvas

const { offsetWidth, offsetHeight } = map.getViewport()
canvas = document.createElement('canvas');
canvas.width = offsetWidth;
canvas.height = offsetHeight;
canvas.style.position = 'absolute';
canvas.style.top = '0px';
canvas.style.left = '0px';
canvas.style.zIndex = '1';
ctx = canvas.getContext('2d');
map.getViewport().appendChild(canvas);

2. 注册map事件

map.on('postrender', e => {
  addMapModal()
})

3. 事件实现

function addMapModal(params) {
const { fillStyle, strokeStyle, lineWidth } = {
  fillStyle: 'rgba(255,255,255,0.8)',
  strokeStyle: '#f00',
  lineWidth: 3,
  ...params
}
ctx.fillStyle = fillStyle;
ctx.strokeStyle = strokeStyle;
ctx.lineWidth = lineWidth;
ctx.clearRect(0, 0, canvas.width, canvas.height)
const coords = modalData.map(coord => {
  return map.getPixelFromCoordinate(ol.proj.fromLonLat(coord))
})

ctx.beginPath();
coords.forEach((coord, index) => {
  index === 0 ? ctx.moveTo(coord[0], coord[1]) : ctx.lineTo(coord[0], coord[1])
})
ctx.closePath();
ctx.fill();

ctx.globalCompositeOperation = 'source-out';
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.fill();
ctx.globalCompositeOperation = 'source-over';
ctx.beginPath();
coords.forEach((coord, index) => {
  index === 0 ? ctx.moveTo(coord[0], coord[1]) : ctx.lineTo(coord[0], coord[1])
})
ctx.closePath();
ctx.stroke()
}

相关文章

  • OpenCV:四、图像的基础操作

    前言 在上一章中描述了如何使用自己实现掩膜运算以及调用cv :: filter2D函数对图像进行掩膜运算矩阵的掩膜...

  • Openlayers3中实现地图的切割

    概述: 本文讲述如何在Openlayers3中结合canvas实现对地图的切割。 效果: 全图 切割北京区域 切割...

  • GEEMODIS数据质量波段应用

    MODIS质量控制数据应用 主要功能 使用MODIS的QA波段来进行云区掩膜,深海区域掩膜 代码 步骤分析 定义函...

  • 掩膜

    通过使用黑白二值图像将对应于黑色部分的原始图像的像素改变为黑色的操作被称为掩膜。 举个例子,比如说要提取蓝色部分,...

  • Ol4中晕圈点效果的实现

    概述 本文讲述如何在Openlayers4中实现晕圈的点效果。 思路 结合Openlayers4中的overlay...

  • QR码设计(5)之mask

    转载请注明出处 1.1掩膜版的种类 QR码的掩膜版一共8种。他是在数据字和纠错字都在矩阵中填充完后,选择掩膜版类型...

  • Opencv中的掩膜mask

    搞了很久一会儿,终于初步的明白了mask是个什么东东,简单的说,就是一个控制原图哪里需要进行对应改变的标志图,这么...

  • Python中ArcPy读取Excel表格长时间序列数据、批量反

      本文介绍基于Python中ArcPy模块,实现Excel数据读取并导入图层,同时进行IDW插值与批量掩膜的方法...

  • 什么是掩膜(又叫掩码),它的作用是什么

    什么是掩膜(mask) 数字图像处理中的掩膜的概念是借鉴于PCB制版的过程,在半导体制造中,许多芯片工艺步骤采用光...

  • openlayers入门开发系列之地图模态层篇

    本篇的重点内容是为了到达自己想要的区域高亮效果,利用openlayers结合turf.js实现地图模态层功能,效果...

网友评论

    本文标题:openlayers中区域掩膜的实现

    本文链接:https://www.haomeiwen.com/subject/qslicjtx.html