Vue+OpenLayers学习系列(七)OpenLayers 加载GeoServer发布的矢量切片地图

本文详细介绍了如何使用GeoServer进行矢量数据的切片处理,并通过OpenLayers实现切片数据的地图展示。包括矢量切片插件的安装、配置,以及OpenLayers中图层的加载、地图的初始化等关键步骤。

一、首先要切片

注意首先要下载 GeoServer 的矢量切片插件(参见:https://zhuanlan.zhihu.com/p/62751184

安装完矢量切片插件后,发布图层时,需要勾选如下:

然后找到发布的矢量切片图层

打开预览,并且查看浏览器中的源码,查看它的调用方式(在后面OpenLayers调用它时可以用同样的方式去调用):

二、OpenLayers 调用

<template>
  <div>
    <div id="map" ref="rootmap">
    </div>
  </div>
</template>

<script>
  import "ol/ol.css";
  import { Map, View } from "ol";
  import {VectorTile as VectorLayerTile,Tile} from 'ol/layer'
  import {Style,Fill,Stroke} from 'ol/style';
  import {VectorTile as VectorSourceTile,OSM,WMTS} from 'ol/source'
  import {createXYZ} from 'ol/tilegrid';
  import WMTSTileGrid from 'ol/tilegrid/WMTS'
  import MVT from 'ol/format/MVT'
  import {Projection} from 'ol/proj'
  import {GeoJSON} from 'ol/format'

  export default {
    name: 'OlVectorTiles',
    data() {
      return {
        map: null,
        baseUrl: 'http://localhost:8080/geoserver/gwc/service/wmts',
        params: null,
        gridsetName: null
      };
    },
    mounted() {     //可以出来结果
      this.gridsetName = 'EPSG:4326',
      this.params = {
        'REQUEST': 'GetTile',
        'SERVICE': 'WMTS',
        'VERSION': '1.0.0',
        'LAYER': 'test-world:opengeo_countries',
        'STYLE': '',
        'TILEMATRIX': this.gridsetName + ':{z}',
        'TILEMATRIXSET': this.gridsetName,
        'FORMAT': 'application/json;type=geojson',
        'TILECOL': '{x}',
        'TILEROW': '{y}'
      };

      //切片名
      let matrixIds = ['EPSG:4326:0', 'EPSG:4326:1', 'EPSG:4326:2', 'EPSG:4326:3',
        'EPSG:4326:4', 'EPSG:4326:5', 'EPSG:4326:6', 'EPSG:4326:7', 'EPSG:4326:8',
        'EPSG:4326:9', 'EPSG:4326:10', 'EPSG:4326:11', 'EPSG:4326:12', 'EPSG:4326:13',
        'EPSG:4326:14', 'EPSG:4326:15', 'EPSG:4326:16', 'EPSG:4326:17', 'EPSG:4326:18',
        'EPSG:4326:19', 'EPSG:4326:20', 'EPSG:4326:21'];

      //分辨率
      let resolutions = [0.703125, 0.3515625, 0.17578125, 0.087890625, 0.0439453125,
        0.02197265625, 0.010986328125, 0.0054931640625, 0.00274658203125, 0.001373291015625,
        6.866455078125E-4, 3.4332275390625E-4, 1.71661376953125E-4, 8.58306884765625E-5,
        4.291534423828125E-5, 2.1457672119140625E-5, 1.0728836059570312E-5, 5.364418029785156E-6,
        2.682209014892578E-6, 1.341104507446289E-6, 6.705522537231445E-7, 3.3527612686157227E-7];

      //底图
      let tileOSM = new Tile({
        source: new OSM()
      });

      //切片策略
      let tileGrid = new WMTSTileGrid({
        tileSize: [256,256],
        extent: [-180.0,-90.0,180.0,90.0],  //范围
        origin: [-180.0, 90.0],   //切片原点
        resolutions: resolutions,   //分辨率
        matrixIds: matrixIds    //层级标识列表,与地图级数保持一致
      });

      //设置地图投影
      let projection = new Projection({
        code: 'EPSG:4326',
        units: 'degrees',
        axisOrientation: 'neu'
      });

      let vectorSource = new VectorSourceTile({
        url: this.urlConstruct(),
        format: new GeoJSON({}),    //切片格式
        projection: projection,
        tileGrid: tileGrid
      });
      let vectorLayer = new VectorLayerTile({
        source: vectorSource,
        wrapX:false,
      });
      let views = new View({
        center: [0, 0],
        projection: projection,
        zoom: 2,
        resolutions: resolutions,
        extent: [-180.0,-90.0,180.0,90.0]
      });
      this.map = new Map({
        layers: [tileOSM, vectorLayer],
        view: views,
        target: 'map',
      });

    },
    methods:{
      urlConstruct(){
        let url = this.baseUrl + '?';
        for (let param in this.params){
           url = url + param + '=' + this.params[param] + '&';
        }
        url = url.slice(0, -1);
        return url;
      }
    }
  };
</script>

<style>
  #map{
    height:800px;
    width: 1400px;
  }
  /*隐藏ol的一些自带元素*/
  .ol-attribution,.ol-zoom { display: none;}

</style>

 

 

 

 

 

 

Vue项目中使用OpenLayers调用由GeoServer发布矢量数据,主要依赖于GeoServer提供的WFS(Web Feature Service)服务。OpenLayers可以通过`ol.source.Vector`结合`ol.format.GeoJSON`来加载GeoServer返回的GeoJSON格式的矢量数据。以下是一个完整的实现流程: ### 1. GeoServer 配置与发布矢量图层 在GeoServer发布矢量图层时,需确保图层支持WFS服务,并设置输出格式为GeoJSON。具体步骤如下: - 登录GeoServer管理界面,进入目标图层的“发布”设置。 - 在“WFS”选项卡中,确保该图层已启用WFS服务。 - 设置支持的输出格式为`application/json`或`GeoJSON`。 - 保存设置后,可通过访问WFS服务接口测试数据输出,例如: ``` http://geoserver:8080/geoserver/wfs?service=WFS&version=2.0.0&request=GetFeature&typeNames=workspace:layername&outputFormat=application/json ``` ### 2. Vue 项目中引入 OpenLayersVue项目中安装并引入OpenLayers库。可以通过npm安装: ```bash npm install ol ``` 在需要使用地图的组件中引入必要的模块: ```javascript import Map from 'ol/Map'; import View from 'ol/View'; import {bbox as bboxStrategy} from 'ol/loadingstrategy'; import GeoJSON from 'ol/format/GeoJSON'; import VectorSource from 'ol/source/Vector'; import {tile as tileStrategy} from 'ol/loadingstrategy'; import XYZ from 'ol/source/XYZ'; import {click as clickStrategy} from 'ol/interaction/Select'; import {bbox as bboxStrategy} from 'ol/loadingstrategy'; ``` ### 3. 创建地图加载矢量数据 在Vue组件中创建地图实例,并配置矢量图层以从GeoServer加载数据: ```javascript export default { mounted() { const vectorSource = new VectorSource({ format: new GeoJSON(), url: function(extent) { return 'http://geoserver:8080/geoserver/wfs?service=WFS&' + 'version=2.0.0&request=GetFeature&typeNames=workspace:layername&' + 'outputFormat=application/json&srsname=EPSG:3857&' + 'bbox=' + extent.join(',') + ',EPSG:3857'; }, strategy: bboxStrategy }); const vectorLayer = new VectorLayer({ source: vectorSource }); const map = new Map({ target: 'map', view: new View({ center: [0, 0], zoom: 2 }), layers: [ new TileLayer({ source: new XYZ({ url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png' }) }), vectorLayer ] }); } } ``` ### 4. 地图交互与样式配置 OpenLayers支持丰富的交互方式和样式配置。可以通过`ol.interaction.Select`和`ol.events.condition`实现点击选择矢量要素的功能。样式方面,可以为矢量图层设置自定义的`style`函数或对象,以控制不同要素的显示效果。 ```javascript import Select from 'ol/interaction/Select'; import {click} from 'ol/events/condition'; const select = new Select({ condition: click }); map.addInteraction(select); ``` ### 5. 优化与性能考虑 - **分页加载**:对于大规模矢量数据,建议启用GeoServer的分页支持,避免一次性加载过多数据。 - **简化几何**:在GeoServer端启用几何简化,减少传输数据量。 - **缓存机制**:合理配置浏览器和GeoServer端的缓存策略,提高加载效率。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值