浅谈前端自定义VectorGrid矢量瓦片样式

目录

前言

一、VectorGrid相关API介绍

1、VectorGrid

2、 LayerStyles样式详解

二、样式自动配置

1、页面定义

2、地图及PBF瓦片引入

3、矢量瓦片样式定义

 4、鼠标事件交互

三、最终效果

1、自定义样式展示

2、鼠标交互 

 总结


前言

        在上一篇博客中,详细讲述了在LeafLet.js中集成VectorGrid插件进行矢量瓦片渲染的案例,原文连接基于VectorGrid加载GeoServer发布的矢量瓦片实例,感兴趣的朋友可以直接点击链接进行查阅之前的博文。在上面的博文中,针对GeoServer发布的矢量瓦片,其内网样式和交互效果是固定的。

        试想如果遇到以下场景,应该怎么满足。1、用户需要在前端自定义矢量瓦片的样式风格,比如自己调节填充颜色,填充线宽度等。2、需要根据不同的属性值进行个性化设置。在传统的预切型瓦片处理过程中,这种动态渲染的需求是很难满足的,因此需要一种支持客户端自定义的样式控制技术。下面是之前的效果:

        本文将以Leaflet为例,深入讲解在VectorGrid控件中,如何在前端进行矢量瓦片的个性化定制,同时给出完整的示例代码,帮助各位用户掌握和使用源码更快速的掌握如何在前端实现矢量瓦片的动态自定义控制。

一、VectorGrid相关API介绍

        要想使用VectorGrid进行矢量瓦片的渲染,样式必不可少。因此在熟悉讲解样式动态控制之前,必须要先将其底层的API进行相关的介绍,防止用户一下子不明就里。

1、VectorGrid

        A VectorGrid is a generic, abstract class for displaying tiled vector data. it provides facilities for symbolizing and rendering the data in the vector tiles, but lacks the functionality to fetch the vector tiles from wherever they are. Extends Leaflet's L.GridLayer.

        这里比较重要配置属性就是vectorTileLayerStyles,这个就是用来控制样式的属性。后面将重点围绕这个属性进行展开。

2、 LayerStyles样式详解

        下面来重点介绍一下LayerStyles样式,先来看一下官网的说明,在矢量瓦片中,也是有图层的概念,跟leaflet中的图层概念有一点不一样,需要注意一下。

Vector tiles have a concept of "layer" different from the Leaflet concept of "layer".
In Leaflet, a "layer" is something that can be atomically added or removed from the map. In vector tiles, a "layer" is a named set of features (points, lines or polygons) which share a common theme.
A vector tile layer¹ can have several layers². In the mapbox-streets-v6 vector tiles layer¹ above, there are named layers² like admin, water or roads.
(¹ In Leaflet)
(² Groups of themed features)
Styling is done via per-layer² sets of L.Path options in the vectorTileLayerStyles layer¹ option:

        官网给出了一个样式的样例,我们来看一下,需要注意的是,下面引用的landuse、admin、water都是发布的矢量数据中的一个图层,在Geoserver中可以找到(后面会详细讲解):

vectorTileLayerStyles: {
        // A plain set of L.Path options.
        landuse: {
            weight: 0,
            fillColor: '#9bc2c4',
            fillOpacity: 1,
            fill: true
        },
        // A function for styling features dynamically, depending on their
        // properties and the map's zoom level
        admin: function(properties, zoom) {
            var level = properties.admin_level;
            var weight = 1;
            if (level == 2) {weight = 4;}
            return {
                weight: weight,
                color: '#cf52d3',
                dashArray: '2, 6',
                fillOpacity: 0
            }
        },
        // A function for styling features dynamically, depending on their
        // properties, the map's zoom level, and the layer's geometry
        // dimension (point, line, polygon)
        water: function(properties, zoom, geometryDimension) {
	    if (geometryDimension === 1) {   // point
	        return ({
                    radius: 5,
                    color: '#cf52d3',
                });
	    }
	    
	    if (geometryDimension === 2) {   // line
                 return ({
                    weight: 1,
                    color: '#cf52d3',
                    dashArray: '2, 6',
                    fillOpacity: 0
                });
	    }
	    
	    if (geometryDimension === 3) {   // polygon
	         return ({
                    weight: 1,
                    fillColor: '#9bc2c4',
                    fillOpacity: 1,
                    fill: true
                });
	    }
        },
        // An 'icon' option means that a L.Icon will be used
        place: {
            icon: new L.Icon.Default()
        },
        road: []
    }

二、样式自动配置

        在了解了以上的API后,我们开始进行相关代码的开发。闲言少叙,直入正题。

1、页面定义

        同样的,我们首先进行html页面的定义,在页面中定义了基本的网页骨架。用来展示具体的底图以及叠加矢量瓦片。

<!DOCTYPE html>
<html>
<head>
	<title>VectorGrid矢量瓦片样式自定义</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="shortcut icon" type="image/x-icon" href="docs/images/favicon.ico" />
	<link rel="stylesheet" href="/2d/leaflet/leaflet.css" />
    <script src="/2d/leaflet/leaflet.js?v=1.0.0"></script>
	<script src="https://unpkg.com/leaflet.vectorgrid@latest/dist/Leaflet.VectorGrid.bundled.js"></script>
    <script src="https://unpkg.com/leaflet.vectorgrid@latest/dist/Leaflet.VectorGrid.js"></script>
	<style>
        #map {
            margin: 0;
            padding: 0;
            position: absolute;
            width: 99%;
            height: 98%;
        }
    </style>
</head>
<body>
<div id="map"></div>
</body>
</html>

2、地图及PBF瓦片引入

        要实现矢量瓦片的接入,本文以PBF瓦片为演示类型。需要在Map页面定义后,将影像底图和矢量瓦片实现动态叠加。

var mymap = L.map('map').setView([29.052934, 104.0625], 3);

	var tileLayer = L.tileLayer('http://localhost:8086/data/xxgc/q0403/{z}/{x}/{y}.png', {
		maxZoom: 7,
		minZoom:0
	});
	
    tileLayer.addTo(mymap);
	
	const pbfUrl = "http://localhost:8083/geoserver/gwc/service/tms/1.0.0/gisdev%3A2019_province_4326@EPSG%3A900913@pbf/{z}/{x}/{y}.pbf";

3、矢量瓦片样式定义

        按照第一章节的样式详解介绍,这里将完成矢量瓦片的样式具体定义。这里的演示数据是全国各省份、自治区、直辖市的矢量数据。空间信息如下:

        可以看到下面是矢量瓦片的属性信息,包括fid、省代码、省、类型等等。在GeoServer自带的预览控件中可以看到以上的信息。

        本文的需求就是要根据不同的行政区划类型进行控制:如果是类型是省则矢量瓦片展示为黄色、自治区瓦片使用绿色展示、直辖市则采用红色展示。同时还有另外一个需求,点击矢量瓦片的,可以悬浮展示不同省份的名称信息。

 var vectorTileStyling = { 
		'2019_province_4326': function (properties, zoom) { // tianhe 为切片图层名称          
            //var name = properties.Name; // 获取图层的 Name 属性
			if(properties.类型 ==  '省'){
				return { 
					//color: '#ffd700', 
					color: 'yellow', 
					//fillColor: '#e6d933', 
					fillColor: 'yellow', 
					fillOpacity: 0.5, 
					fill: true, 
					opacity: 1, 
					weight: 3, 
					dashArray: '5', 
				}
			}else if(properties.类型 ==  '自治区'){
				return { 
					//color: '#ffd700', 
					color: 'green', 
					//fillColor: '#e6d933', 
					fillColor: 'green', 
					fillOpacity: 0.5, 
					fill: true, 
					opacity: 1, 
					weight: 3, 
					dashArray: '5', 
				}
			} else if(properties.类型 ==  '直辖市' || properties.类型 ==  '特别行政区'){
				return { 
					//color: '#ffd700', 
					color: 'red', 
					//fillColor: '#e6d933', 
					fillColor: 'red', 
					fillOpacity: 0.5, 
					fill: true, 
					opacity: 1, 
					weight: 3, 
					dashArray: '5', 
				}
			}
        }

    }

请注意,2019_province_4326表示在Geoserver发布的图层的名字。在GeoServer中可以查看到

 4、鼠标事件交互

需求场景:这里需要定义地图交互事件,将鼠标悬浮到图层上展示省份的名称、编码以及类型。

 

var vectorTileOptions = {
		 layerURL: pbfUrl,
		 rendererFactory: L.canvas.tile,           
		 tms: true, 
		 interactive: true,	//开启VectorGrid触发mouse/pointer事件
		 vectorTileLayerStyles: vectorTileStyling
	 };      
	 var vectorTile = new L.vectorGrid.protobuf(pbfUrl, vectorTileOptions).addTo(mymap)

	vectorTile.on('mouseover', function (e) {    
		var properties = e.layer.properties;    
		L.popup()
		 .setContent(properties.省+";" + properties.省代码+";" +properties.类型 )
		 .setLatLng(e.latlng)
		 .openOn(mymap);  
	});

三、最终效果

1、自定义样式展示

2、鼠标交互 

 总结

        以上就是本文的主要内容,本文将以Leaflet为例,深入讲解在VectorGrid控件中,如何在前端进行矢量瓦片的个性化定制,同时给出完整的示例代码,帮助各位用户掌握和使用源码更快速的掌握如何在前端实现矢量瓦片的动态自定义控制。

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 42
    评论
Cesium矢量瓦片是一种地理数据存储和渲染格式,用于呈现三维地理信息。矢量瓦片采用了类似于栅格瓦片的切片技术,但不同的是它存储的是矢量数据而不是图像数据。 Cesium矢量瓦片使用地理坐标系将地理信息分割成小块,每个小块称为一个矢量瓦片。这些矢量瓦片包含了地理要素的几何形状、属性数据以及可视化样式。与传统的矢量数据不同,矢量瓦片使用了基于Web的标准技术来压缩和存储数据,使得数据加载和渲染更加高效。 使用Cesium矢量瓦片可以实现实时的、交互式的三维地理可视化。由于矢量数据相对于栅格数据更为灵活,可以根据用户需求进行动态渲染和分析,因此矢量瓦片在GIS(地理信息系统)、地图服务和虚拟现实等领域有着广泛的应用。它能够支持多种地理数据类型,包括地形地貌、建筑物、航道、路网等,使得用户能够以更直观、更精细的方式来探索地理数据。 Cesium矢量瓦片的优势在于其高效性和可定制性。它能够在网络上快速加载和传输数据,减少了数据传输和渲染的时间成本。同时,用户可以根据自己的需求对数据进行自定义样式和符号设置,灵活地呈现地理信息。此外,Cesium矢量瓦片还支持多种数据格式的转换和集成,使得用户可以方便地与其他地理数据格式进行互操作。 总之,Cesium矢量瓦片是一种创新的地理数据存储和渲染格式,它将高效性、可定制性和多样化的地理数据类型相结合,为用户提供了实时、交互式的地理信息可视化体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜郎king

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值