openlayer 加载4547坐标系 以及 wfs服务数据(或其他坐标系)

1.首先查看坐标系基础信息如范围等:如下图:
在这里插入图片描述
2.若将地图设置成4547坐标系:
核心代码:

    proj4.defs(
      "EPSG:4547",
      "+proj=tmerc +lat_0=0 +lon_0=114 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs +type=crs"
    );
    register(proj4);
    const projection = new Projection({
      code: "EPSG:4547",
      units: "metre",
      extent: [344577.88, 2380651.49, 655422.12, 5036050.38],
    });
    var map = new Map({
      target: "map",
      view: new View({
        center: [495186.517, 2496151.273600001],
        zoom: 2,
        projection: projection,
      }),
      layers: [
        new TileLayer({
          className: "baseLayerClass",
          source: new Tianditu({
            key: "1d109683f4d84198e37a38c442d68311",
            projection: projection
          }),
        }),
      ],
    });

最后你将得到一个如下图所示的地图:
在这里插入图片描述
这样的地图就是4547坐标系

3.若要加载4547坐标系的wfs数据:
如下图所示,wfs查询后返回的数据:为4547的数据,这点也可以在wfs服务的xml文件中查看的到:
wfs服务返回的数据如下图所示:
在这里插入图片描述

在这里插入图片描述

wfs服务的xml如下图所示:可以看到数据是4547坐标系(这里是超图的wfs服务)
在这里插入图片描述

4.核心加载代码:

    const vectorSource = new VectorSource({
      format: new GeoJSON(),
      loader:function(extent, resolution, projection, success, failure) {
     const proj = projection.getCode();
     const url = wfsUrl;
     const xhr = new XMLHttpRequest();
     xhr.open('GET', url);
     const onError = function() {
       vectorSource.removeLoadedExtent(extent);
       failure();
     }
     xhr.onerror = onError;
     xhr.onload = function() {
       if (xhr.status == 200) {
         const features = vectorSource.getFormat().readFeatures(xhr.responseText,{
          dataProjection:"EPSG:4547",
          // featureProjection:"EPSG:4547",
         });
        //  const features = vectorSource.getFormat()
        //  console.log('这是features',features);
         vectorSource.addFeatures(features);
         success(features);
       } else {
         onError();
       }
     }
     xhr.send();
   },
    });

    const vector = new VectorLayer({
      source: vectorSource,
      style: new Style({
        stroke: new Stroke({
          color: "rgba(0, 0, 255, 1.0)",
          width: 10,
        }),
      }),
    });

5.注意:readFeatures 的 dataProjection和featureProjection属性,若你的地图设置的是4326坐标系则需要设置为:即源数据为4547,转换数据为4326,则可在4326的地图上绘制出4547的数据。

{
          dataProjection:"EPSG:4547",
          featureProjection:"EPSG:4326",
}

6.这里我放置一下wfs服务链接,仅供参考使用超图wfs的小伙伴,(其中token为用户自己的超图服务token)

-------------这里是服务地址-----------/wfs200?token=token&version=2.0.0&count=10000&service=WFS&request=GetFeature&outputFormat=json&typenames=

其余server服务小伙伴参考3的数据返回格式,是一样的返回geojson格式的数据即可。
7.openlayer 注册坐标系:注册了之后可使用 “EPSG:4547”,例如4代码中的dataProjection:“EPSG:4547”

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

8.openlayer 生成projection,例如2代码中的projection: projection,

    const projection = new Projection({
      code: "EPSG:4547",
      units: "metre",
      extent: [344577.88, 2380651.49, 655422.12, 5036050.38],
    });

9.proj4
http://proj4js.org/
npm 安装后 import proj4 from “proj4” 即可

npm install proj4

在这里插入图片描述
10.坐标系proj4可在EPSG官网查看复制:
https://epsg.io/
在这里插入图片描述
11.具体坐标系相关可跳转至另外一篇博文:
点击跳转

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值