ol问题总结二

news/2024/7/10 2:18:57 标签: openlayer, vue

一、加载坐标系是4326格式的,使用wfsServer发布的服务,图层加载失败;坐标系是3857格式的。图层加载正常

原因:4326格式的,发布出来的,经纬度是颠倒的

解决方案一:将经纬度进行反转

<template>
  <div class="hello">
    <div id="mapId" class="mapContainerClass"></div>
  </div>
</template>

<script>
import axios from 'axios'
import OlMap from 'ol/Map'
import View from 'ol/View'
import { defaults as defaultControls} from 'ol/control'
import { get as getProj } from 'ol/proj'
import { getWidth, getTopLeft } from 'ol/extent'
import GML2 from 'ol/format/GML2'

import { Tile as TileLayer, Vector as VectorLayer} from 'ol/layer'
import { WMTS, Vector as VectorSource } from 'ol/source'
import WMTSTileGrid from 'ol/tilegrid/WMTS'
import { Fill, Style, Stroke,Circle } from 'ol/style'

export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  data() {
    return {
      tiandituKey: '',
      map: null,
      fillUrl:'',
      GML2Layer: null,
    }
  },
  mounted() {
    this.initMap()
    this.map.on(
      'moveend',
      debounce((evt) => {
        this.mapMoveEnd(evt)
      }, 600),
      this
    )
  },
  methods: {
    initMap() {
      
      this.map = new OlMap({
        target: 'mapId', //地图容器div的ID
        view: new View({
          projection: 'EPSG:4326', // 投影方式
          center: [116.70392481556995, 36.37675167502263], //地图初始中心点
          extent: [-180, -90, 180, 90],
          // center: [12991421.48404573, 4352586.42500966],
          // extent: [-20037508.342789244, -238107693.26496765, 20037508.342789244, 238107693.26496765],
          zoom: 4.5, //地图初始显示级数
          minZoom: 1, //最大瓦片显示级数
          maxZoom: 18 //最小瓦片显示级数
        }),
        controls: defaultControls({
          // 默认控件
          zoom: false,
          rotate: false
        })
      })

      let baseMapLayer = new TileLayer({
        zIndex:0,
        source: this.getSource({
          title: '矢量', // 底图title
          source: 'TDTTile',
          visible: true,
          crossOrigin: 'anonymous',
          projection: 'EPSG:4326',
          url: `http://t0.tianditu.com/vec_c/wmts`,
          zIndex: 0
        })
      })
      let baseMapLayer2 = new TileLayer({
        zIndex:0,
        source: this.getSource({
          title: '矢量注记',
          source: 'TDTTile',
          visible: true,
          crossOrigin: 'anonymous',
          projection: 'EPSG:4326',
          url: `http://t0.tianditu.com/cva_c/wmts`,
          zIndex: 1,
          type: 'text' // type: 注记图层标识
        })
      })
      this.map.addLayer(baseMapLayer)
      this.map.addLayer(baseMapLayer2)

      this.GML2Layer = new VectorLayer({
          style: new Style({
            stroke: new Stroke({
              color: '#2697FF',
              width: 0,
              lineDash: [0]
            }),
            fill: new Fill({
              color: 'rgba(38,151,255,0.3)'
            }),
            image: new Circle({
              radius: 4,
              offset: [0, 0],
              fill: new Fill({
                color: 'red'
              }),
              stroke: new Stroke({
                color: 'gray',
                width: 0
              }),
              rotation:  0
          })
        }),
        // source: new VectorSource({
        //   projection: 'EPSG: 4326',
        //   url: this.pointUrl + 'EPSG:4326',
        //   format: new GML2(),
        // })
      })
      
      // this.map.addLayer(this.GML2Layer)
      this.initGML2Data()
     },
     mapMoveEnd() {
      this.initGML2Data()
     },
    initGML2Data() {
      let extent = this.map.getView().calculateExtent()
      let bbox = extent[1] + ',' + extent[0] + ',' + extent[3] + ',' + extent[2]
      this.fillUrl = 'http://xxx/arcMapServer/WFSServer?request=GetFeature&service=WFS&version=1.1.0&typename=&propertyName=&outputFormat=gml2&srsname=EPSG:4326&bbox=' + bbox + ',EPSG:4326where'
      axios.get(`${this.fillUrl}`).then(res=>{
        console.log('res:===',res)
        let features = new GML2().readFeatures(res.data, {
          featureProjection: this.map.getView().getProjection(),
          dataProjection: this.map.getView().getProjection(),
        })
        if(features.length) {
          features.forEach(feature => {
            for (var i=0; i<feature.values_.Shape.flatCoordinates.length; i=i+3) {
              var a = feature.values_.Shape.flatCoordinates[i]
              feature.values_.Shape.flatCoordinates[i] = feature.values_.Shape.flatCoordinates[i+1]
              feature.values_.Shape.flatCoordinates[i+1]=a
            }
          })
          GML2Source = new VectorSource({
            features
          })
          this.GML2Layer.setSource(GML2Source)
          this.map.addLayer(GML2Layer)
        }
      })
    },
    getSource(options={}) {
      let Source = null
      if(options.source === 'TDTTile') {
        const projection = getProj(options.projection) // url地址中有vec_c 时使用4326经纬度投影;有vec_w时使用球面墨卡托投影
        const projectionExtent = projection.getExtent()
        const size = getWidth(projectionExtent) / 256
        const resolutions = new Array(18)
        const matrixIds = new Array(18)
        for (let z = 1; z < 19; z++) {
          // generate resolutions and matrixIds arrays for this WMTS
          resolutions[z] = size / Math.pow(2, z)
          matrixIds[z] = options?.layer ? options.projection + ':' + z : z
        }
        const types = options.url.split('/')[3].split('_')

        let tiandituUrl = options.url + (options.url.indexOf('?') >= 0 ? '&tk=' : '?tk=') + this.tiandituKey
        // 在线天地图
        Source = new WMTS({
          ...options,
          crossOrigin: 'anonymous',
          // url: 'http://t0.tianditu.com/img_w/wmts', http://t0.tianditu.gov.cn/vec_c/wmts
          url: options.layer ? options.url : tiandituUrl,
          layer: options.layer || types[0], // 'img' || 'vec'|| ......
          matrixSet: options.matrixSet || types[1], // 'w' || 'c' || ......
          format: options.layer ? 'image/png' : 'tiles',
          style: '', // default
          projection: projection,
          tileGrid: new WMTSTileGrid({
            origin: getTopLeft(projectionExtent),
            resolutions: resolutions,
            matrixIds: matrixIds
          })
        })
      }
      return Source
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.mapContainerClass {
  width: 100vw;
  height: 100vh;
}
h3 {
  margin: 40px 0 0;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

二、图片图层加载时,设置imageExtent的规则

将地图的zoom级别调整到合适的位置,通过以下获取当前的地图的extent,根据这个值进行对图片的imageExtent的设置

// 地图移动/zoom变化结束  evt
    mapMoveEnd() {
      if (this.map) {
        const curZoom = this.map.getView().getZoom()
        const extent = this.map.getView().calculateExtent()
        console.log('mapInfo.vue---地图移动/zoom变化结束', curZoom, extent)
      }
    },


http://www.niftyadmin.cn/n/4928522.html

相关文章

linux文件I/O之 open() 函数用法

#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> typedef unsigned int mode_t ; int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); 函数功能 打开或创建一个文件 返回值 成功…

手撕数据结构之栈+例题

目录 一、栈的概念及结构 二、栈的头文件及基本框架 三、接口实现 1、对栈的初始化 2、栈的销毁 3、入栈操作 4、出栈操作 5、判断栈是否为空 6、返回栈顶元素 7、遍历栈 四、有效的括号 - 力扣&#xff08;LeetCode&#xff09; 题目描述&#xff1a; 思路&#xff…

基于Python爬虫+词云图+情感分析对某东上完美日记的用户评论分析

&#x1f935;‍♂️ 个人主页&#xff1a;艾派森的个人主页 ✍&#x1f3fb;作者简介&#xff1a;Python学习者 &#x1f40b; 希望大家多多支持&#xff0c;我们一起进步&#xff01;&#x1f604; 如果文章对你有帮助的话&#xff0c; 欢迎评论 &#x1f4ac;点赞&#x1f4…

多个配置WebMvcConfigurationSupport失效问题

最近在项目中用类继承WebMvcConfigurationSupport实现拦截器 Configuration RequiredArgsConstructor public class SpringWebSupport extends WebMvcConfigurationSupport {private final ProjectInterceptor projectInterceptor;// 拦截器 //设置拦截器对象和拦截请求Ove…

【图像恢复】基于交替乘子方法(ADMM)图像恢复算法研究[固定点收敛和应用](Matlab代码实现)

&#x1f4a5;&#x1f4a5;&#x1f49e;&#x1f49e;欢迎来到本博客❤️❤️&#x1f4a5;&#x1f4a5; &#x1f3c6;博主优势&#xff1a;&#x1f31e;&#x1f31e;&#x1f31e;博客内容尽量做到思维缜密&#xff0c;逻辑清晰&#xff0c;为了方便读者。 ⛳️座右铭&a…

oracle怎样给某个普通用户授予杀自己用户会话的权限

一 问题描述 想给某个普通用户授予杀掉自己会话的权限 二 解决办法 2.1 用sys用户创建杀会话的存储过程 create or replace procedure scott_p_kill_session( v_sid number, v_serial number )asv_varchar2 varchar2(100);beginif v_sid is not null and v_serial is not n…

PHP面向对象面试题

1、简述面对对象六大设计原则 &#xff1f; 面向对象六大设计原则是一组指导软件设计的原则&#xff0c;它们有助于提高代码的可维护性、可扩展性和可重用性。这些原则是&#xff1a; 单一职责原则&#xff08;Single Responsibility Principle&#xff0c;SRP&#xff09;&a…

IC岗位详解| 高薪模拟版图工程师需要掌握哪些技能?

IC模拟版图设计在IC行业中是门槛相对较低的一个岗位&#xff0c;其他岗位大都要求是科班毕业&#xff0c;或者是硕士以上学历&#xff0c;IC模拟版图设计本科生也很好入门&#xff0c;对于基础差的同学这是非常好的一个入门机会。 模拟版图工程师介绍 模拟版图设计工程师为专…