【cesium】cesium和openlayer加载非3857和4326的坐标系WMS图层方法

背景:得到一个地图服务,wms,坐标系EPSG是4509,如何加载到cesium和openlayer上呢?
先上图
在这里插入图片描述

在这里插入图片描述
实现思路:
openlayer中利用proj4对坐标系进行设置,配置整个map的投影

proj4.defs(
        'EPSG:4509',
        '+proj=tmerc +lat_0=0 +lon_0=117 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs'
      );
      register(proj4);

      const projection = new Projection({
        code: 'EPSG:4509',
        extent: [108195, 1048459, 908195, 9048459],
      });
layers.push(
          //绿色地形
        new TileLayer({
          source: new TileWMS({
            url: 'http://*******/geoserver/Tiff/wms',
            params: {
              SERVICE: 'WMS',
              VERSION: '1.1.0',
              REQUEST: 'GetMap',
              FORMAT: 'image/png',
              TRANSPARENT: true,
              LAYERS: 'Tiff:'+terrainArray[i],
              WIDTH: 256,
              HEIGHT: 256,
              SRS: 'EPSG:4509',
            },
            ratio: 1,
            serverType: 'geoserver',
          }),
        })
        );
const map = new Map({
        controls: defaultControls().extend([new ScaleLine()]),
        layers: layers,
        target: 'map',
        view: new View({
          projection: projection,
          center: fromLonLat([117.0148998624, 36.6688142975], projection),
          zoom: 10,
        }),
      });

cesium中比较特别,虽然本质也是用proj4进行了转换,但是cesium自带的wmsprivder是无法对bbox的每次请求做出修改的,都会发送经纬度。因此采用UrlTemplateImageryProvider自定义了根据xyzoom进行转换成4509坐标的数值。

var imageryProvider = new Cesium.UrlTemplateImageryProvider({
    url: '****/geoserver/Tiff/wms?service=WMS&version=1.1.0&request=GetMap&format=image%2Fpng&transparent=true&' +
      'layers=Tiff%3A'+terrainArray[i]+'&width=256&height=256&' +
      'srs=EPSG%3A4509&bbox={wsen}&styles=',
    customTags: {
      wsen: function (imageryProvider, x, y, zoom) {
        
        var lon = (x/Math.pow(2,zoom)*360-180);
        var n=Math.PI-2*Math.PI*y/Math.pow(2,zoom);
        var lat = (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
        
  
        var lonMax = ((x+1)/Math.pow(2,zoom)*360-180);
        var nMax=Math.PI-2*Math.PI*(y+1)/Math.pow(2,zoom);
        var latMax = (180/Math.PI*Math.atan(0.5*(Math.exp(nMax)-Math.exp(-nMax))));
  
  
        //console.log({ lon: lon, lat: lat, lonMax: lonMax, latMax: latMax });
        var epsg4509WSCoords = proj4('WGS84', epsg4509, [lon, lat]);
        var epsg4509ENCoords = proj4('WGS84', epsg4509, [lonMax, latMax]);
        console.log("" + epsg4509WSCoords[0] + "," + epsg4509WSCoords[1] + "," + epsg4509ENCoords[0] + "," + epsg4509ENCoords[1]);
        return "" + epsg4509WSCoords[0] + "," + epsg4509ENCoords[1]+ "," + epsg4509ENCoords[0] + "," +epsg4509WSCoords[1] ;
      }
    }
  });
  
  var imageryLayer = new Cesium.ImageryLayer(imageryProvider);
  viewer.imageryLayers.add(imageryLayer);

ps:terrainArray是个图层的数组 不用介意。
cesium中有个问题待解决,就是缩放到全国范围瓦片会有一些偏差,和计算出来的bbox有关,可以尝试设置最小level

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

gis分享

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

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

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

打赏作者

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

抵扣说明:

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

余额充值