3-Openlayers加载GeoServer发布的WMS服务

首先根据1-借助Node搭建OpenLayers开发环境来搭建一个项目。

1 加载图层组

  1. 新建一个index.html,代码如下:

    <!DOCTYPE html>
    <html lang="zh">
      <head>
        <meta charset="utf-8">
        <title>Using OpenLayers with Parcel</title>
        <style>
          @import "node_modules/ol/ol.css";
        </style>
        <style>
          html, body {
            margin: 0;
            height: 100%;
          }
          #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
          }
        </style>
      </head>
      <body>
        <div id="map"></div>
        <script src="elm-pep.js"></script>
        <script src="./main.js" type="module"></script>
      </body>
    </html>
    
  2. 如下图所示,点击在【Layer Preview】中查看WMS的url参数,关于如何创建图层组,请参考5-借助GeoServer创建图层组一文。
    在这里插入图片描述

  3. 复制?前面的地址即为url的参数。
    在这里插入图片描述

  4. 新建一个main.js文件,代码如下:

    import 'ol/ol.css';
    import ImageWMS from "ol/source/ImageWMS";
    import Map from 'ol/Map';
    import View from 'ol/View'
    import {Image as ImageLayer} from 'ol/layer';
    
    
    const layers = [
        new ImageLayer({
            extent: [12620511.818300, 4122531.384400, 12658467.178700, 4145118.878400],//数据范围
            source: new ImageWMS({
                url: 'http://localhost:8080/geoserver/zhengzhou/wms',//WMS服务地址
                params: {
                    'LAYERS': 'zhengzhou:zy',//图层参数
                },
                ratio: 1,
                serverType: 'geoserver',//服务类型
                crossOrigin: 'anonymous',//允许跨域
            }),
        }),
    ];
    
    const map = new Map({
        layers: layers,
        target: 'map',
        view: new View({
            center: [12640484, 4134721],
            zoom: 12,
        }),
    });
    
  5. 然后运行npm start命令,访问http://localhost:1234/即可查看发布的图层组,效果图略。


另外,加载多个图层,也可以修改params参数下的LAYERS属性为数组形式,也可以加载多个图层。

params: {
    'LAYERS': [
        'zhengzhou:区县级行政区划',
        'zhengzhou:植被',
        'zhengzhou:建筑物区域',
        'zhengzhou:水系',
        'zhengzhou:商圈',
        'zhengzhou:小区',
        'zhengzhou:地铁站出入口形状',
        'zhengzhou:道路中心线',
        'zhengzhou:城市轨道交通',
        'zhengzhou:铁路',
        'zhengzhou:道路交叉点',
        'zhengzhou:行政地名',
        'zhengzhou:自然地名',
        'zhengzhou:地铁站出入口',
        'zhengzhou:地铁站',
    ],
    'TILED': true
},

2 加载单个图层

修改LAYERS参数为工作区:图层名称即可。

const layers = [
 new TileLayer({
     source: new TileWMS({
         url: 'http://localhost:8080/geoserver/zhengzhou/wms',
         params: {
             'LAYERS': 'zhengzhou:图层名称',//此处替换为单个图层名称即可
             'TILED': true
         },
         //...
     }),
 })
];

3 加载WMS的其他方式

加载WMS总共有3种方式,上面我们采用的是:ol.layer.Image+ol.source.ImageWMS
另外还有另外两种方式:

  1. ol.layer.Tile+ol.source.TileWMS,并且其与第一种方式的关键参数一致。

    import 'ol/ol.css';
    import Map from 'ol/Map';
    import TileLayer from 'ol/layer/Tile';
    import TileWMS from 'ol/source/TileWMS';
    import View from 'ol/View';
    
    const layers = [
        new TileLayer({
            extent: [12620511.818300, 4122531.384400, 12658467.178700, 4145118.878400],//数据范围
            source: new TileWMS({
                url: 'http://localhost:8080/geoserver/zhengzhou/wms',//WMS服务地址
                params: {'LAYERS': 'zhengzhou:zy', 'TILED': true},
                serverType: 'geoserver',
                transition: 0,
                crossOrigin: 'anonymous',//允许跨域
            }),
        }),
    ];
    
  2. 同样采用ol.layer.Tile+ol.source.TileWMS,与上面不同的是,这种方式采用的是512×256的瓦片网格加载,即进一步设置了瓦片网格的信息参数,即实例化的ol.tilegrid.TileGrid网格对象。

    import 'ol/ol.css';
    import Map from 'ol/Map';
    import TileGrid from 'ol/tilegrid/TileGrid';
    import TileLayer from 'ol/layer/Tile';
    import TileWMS from 'ol/source/TileWMS';
    import View from 'ol/View';
    import {get as getProjection} from 'ol/proj';
    import {getWidth} from 'ol/extent';
    
    const projExtent = getProjection('EPSG:3857').getExtent();
    const startResolution = getWidth(projExtent) / 256;
    const resolutions = new Array(22);
    for (let i = 0, ii = resolutions.length; i < ii; ++i) {
        resolutions[i] = startResolution / Math.pow(2, i);
    }
    const tileGrid = new TileGrid({
        extent: [12620511.818300, 4122531.384400, 12658467.178700, 4145118.878400],
        resolutions: resolutions,
        tileSize: [512, 256],
    });
    
    const layers = [
        new TileLayer({
            source: new TileWMS({
                url: 'http://localhost:8080/geoserver/zhengzhou/wms',
                params: {
                    'LAYERS': 'zhengzhou:zy',
                    'TILED': true
                },
                serverType: 'geoserver',
                tileGrid: tileGrid,
                crossOrigin: 'anonymous',//允许跨域
            }),
        })
    ];
    //...
    

总结:Image WMSTiled WMS的区别是,Tiled WMS的Tiles(即瓦片)可以被缓存,因此浏览器将不会重新获取已经查看过的区域的数据,但可能存在未知瓦片重复标签的问题(原文为:But there may be problems with repeated labels for WMS servers that are not aware of tiles),使用Image WMS则可以避免这一问题。

参考资料

  1. https://openlayers.org/en/latest/examples/wms-image.html
  2. https://openlayers.org/en/latest/examples/wms-tiled.html
  3. https://openlayers.org/en/latest/examples/wms-custom-tilegrid-512x256.html
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值