Vue+OpenLayers学习系列(三)OpenLayers 读取 GeoServer 发布的地图服务

本文详细介绍了如何使用GeoServer发布Shapefile和PostGIS数据为地图服务,并通过OpenLayers在Web应用中读取这些服务。文章提供了具体的代码示例,展示了如何在OpenLayers中配置并显示GeoServer的地图层。

一、首先用 GeoServer 发布地图服务

可以用 GeoServer 发布 Shapefile 数据(参见https://zhuanlan.zhihu.com/p/73644445

也可以用 GeoServer 发布 PostGIS 数据(参见https://zhuanlan.zhihu.com/p/73870146

注意:GeoServer 可能会产生跨域问题,网上很多办法都不靠谱,解决办法在这里:https://www.jianshu.com/p/9773ec789ba4?tdsourcetag=s_pctim_aiomsg

二、OpenLayers 读取 GeoServer 发布的地图服务

<template>
  <div id="map" ref="rootmap">
  </div>
</template>
<script>
  import 'ol/ol.css';
  import Map from 'ol/Map';
  import View from 'ol/View';
  import {Tile as TileLayer, Image as ImageLayer} from 'ol/layer';
  import {OSM, ImageArcGISRest,TileWMS,ImageWMS} from 'ol/source';
  export default{
    name: 'OlGeoserve',
    data(){
      return{
          map: null
      };
    },
    mounted(){
      let url = 'http://localhost:8080/geoserver/ocean/wms';
      this.map = new Map({
        target: "map",
        layers: [
          new TileLayer({
            source: new OSM()   //这个会出现底图
          }),
          new ImageLayer({     //TileLayer、ImageLayer
            source: new ImageWMS({    //TileWMS、ImageWMS
              ratio: 1,
              params: {
                //'FORMAT': 'image/jpeg',   //如果加了这行,地图底图将会被遮住
                'VERSION': '1.1.0',
                'LAYERS': 'ocean:GDSJ',
                'STYLES': '',
              },
              url: url
            })
          })
        ],
        view: new View({
          projection: 'EPSG:4326',
          center: [-10997148, 4569099],
          zoom: 4
        })
      });

    }
  };

</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端的缓存策略,提高加载效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值