leaflet矢量瓦片vetorgrid显示聚合和图标裁剪显示不全的问题

1、问题现象

使用leaflet显示矢量瓦片会出现图片挤压的问题和图片裁剪显示不全的问题

2、解决办法和思路

1)数据抽稀

方法一:在createTile方法通过控制feature在单张瓦片里面显示的数量,在小层级的时候进行筛选过滤,对点数据类型,代码如下:

//判断当期瓦片里面的features,然后对数据进行抽稀,显示的数量不超过5个
            //小于14层的瓦片,抽稀,显示的数量不超过5个,大于14级全部显示
            if (
              (coords.z <= 14 && layer.features.length > 5 && feat.id % Math.ceil(layer.features.length / 5) == 0) ||
              layer.features.length <= 5 ||
              coords.z > 14 ||
              feat.type != 1
            ) {
              var featureLayer = this._createLayer(feat, pxPerExtent)

              for (var j = 0; j < styleOptions.length; j++) {
                var style = L.extend({}, L.Path.prototype.options, styleOptions[j])
                featureLayer.render(renderer, style)
                renderer._addPath(featureLayer)
              }

              if (this.options.interactive) {
                featureLayer.makeInteractive()
              }

              if (storeFeatures) {
                renderer._features[id] = {
                  layerName: layerName,
                  feature: featureLayer
                }
              }
            }

方法二:通过设置矢量瓦片vectorTileLayerStyles的样式,可以获取到当前的层级和数据信息,设置一个空的占位图标解决抽稀问题:

vectorTileLayerStyles[layerStyle.layername] = function (properties: any, zoom: number) {
      if (options && options.length > 0) {
        let style = {} //返回的数据格式
        options.forEach((item: any) => {
          //如何当前的层级落在option的zoom数组范围内
          if (zoom >= item.zoom[0] && zoom <= item.zoom[1]) {
            let icon = L.icon({
              iconUrl: `/static/images/Iconpoint/${item.iconName}.svg`,
              iconSize: item.iconSize
            })
            //点的数据抽稀设置
            if (item.sliceCount && item.fieldname) {
              let data = properties[item.fieldname]
              //测试用
              // data = data.split('.')[1]
              //取余数
              if (data && Number(data) % Number(item.sliceCount) == 0) {
                style = {
                  icon: icon
                }
              } else {
                style = {
                  icon: defaultIcon
                }
              }
            }
          }
        })
        return style

options配置如下:

         options: [
            {
              zoom: [1, 10], //不同层级下的显示最小值和最大值
              sliceCount: 70, //抽稀的数量
              iconSize: [23, 30],
              iconName: 'monitorvideo',
              fieldname: 'gbindexcode'//抽稀属性字段
            },
            {
              zoom: [11, 12], //不同层级下的显示
              sliceCount: 20, //抽稀的数量
              iconSize: [23, 30],
              iconName: 'monitorvideo',
              fieldname: 'gbindexcode'
            },
            {
              zoom: [13, 15], //不同层级下的显示
              sliceCount: 10, //抽稀的数量
              iconSize: [23, 30],
              iconName: 'monitorvideo',
              fieldname: 'gbindexcode'
            },
            {
              zoom: [16, 20], //不同层级下的显示
              sliceCount: 1, //抽稀的数量
              iconSize: [23, 30],
              iconName: 'monitorvideo',
              fieldname: 'gbindexcode'
            }
          ]

2)图片边缘裁切的问题处理

在gridlayer绘制的时候判断当前的数据是否会超出绘制边界,超出绘制边界后不予显示(可能会存在数据丢失的问题),也可以移动绘制的位置让其显示完全

_updateIcon: function (layer) {
    if (!this._drawing) {
      return
    }

    var icon = layer.options.icon,
      options = icon.options,
      size = L.point(options.iconSize),
      anchor = options.iconAnchor || (size && size.divideBy(2, true)),
      p = layer._point.subtract(anchor),
      ctx = this._ctx,
      img = layer._getImage()
    if (img.complete) {
      if (p.x + size.x > 256 || p.x < 0 || p.y + size.y > 256 || p.y < 0) {
        console.log('图像超出画板边界!')
      } else {
        ctx.drawImage(img, p.x, p.y, size.x, size.y)
      }
    } else {
      L.DomEvent.on(img, 'load', function () {
        if (p.x + size.x > 256 || p.x < 0 || p.y + size.y > 256 || p.y < 0) {
          console.log('图像超出画板边界!')
        } else {
          ctx.drawImage(img, p.x, p.y, size.x, size.y)
        }
      })
    }

    this._drawnLayers[layer._leaflet_id] = layer
  }
})

3、解决结果

4、参考资料

Leaflet.VectorGrid API reference

https://gis.stackexchange.com/questions/448812/leaflet-vectorgrid-circle-markers-cut-off-at-border-in-custom-implementation

Rendering errors (symbol clipping) at vector tile edges · Issue #149 · Leaflet/Leaflet.VectorGrid · GitHub

canvas 绘制图片 - ctx.drawImage()-CSDN博客

leaflet瓦片图层缩放级别外不隐藏_leafeft marker按照zomm显示隐藏-CSDN博客

相关推荐
还是鼠鼠1 小时前
图书管理系统 Axios 源码__编辑图书
前端·javascript·vscode·ajax·前端框架
北极象1 小时前
vue3中el-input无法获得焦点的问题
前端·javascript·vue.js
GISer_Jing2 小时前
react redux监测值的变化
前端·javascript·react.js
m0_528723812 小时前
react中useEffect的使用
前端·javascript·react.js
Real_man3 小时前
noVNC 技术解析与最佳实践
javascript
大模型铲屎官3 小时前
HTML从入门到精通:链接与图像标签全解析
开发语言·前端·javascript·html·编程·链接标签·图像标签
大模型铲屎官3 小时前
HTML 列表标签全解析:无序与有序列表的深度应用
前端·javascript·html·有序列表·列表标签·无序列表
霸王蟹5 小时前
文本复制兼容方案最佳实现落地。
前端·javascript·vue.js·笔记·学习
扎量丙不要犟6 小时前
跨平台的客户端gui到底是选“原生”还是web
前端·javascript·c++·qt·rust·electron·tauri
字节全栈_rJF6 小时前
Flutter Candies 一桶天下
前端·javascript·flutter