26.使用 Vue 3 + OpenLayers 加载远程 Shapefile 数据并显示图形

在 Web GIS 开发中,我们经常需要将远程的 Shapefile 数据加载到地图上进行展示。本文将介绍如何使用 Vue 3 和 OpenLayers 来实现这一功能,并提供完整的代码示例。

项目依赖和准备工作

1. 引入 OpenLayers

OpenLayers 是一个强大的开源 JavaScript 地图库,可以用于在 Web 应用中加载和显示各种地图数据。

javascript 复制代码
npm install ol

2. 引入 Shapefile 解析工具

我们需要一个工具来解析 Shapefile 数据,这里选择 shapefile 这个库。

javascript 复制代码
npm install shapefile

Vue 3 + OpenLayers 示例代码

以下是完整代码,包括模板、逻辑和样式部分。

App.vue

javascript 复制代码
<!--
 * @Author: 彭麒
 * @Date: 2024/12/13
 * @Email: 1062470959@qq.com
 * @Description: 此源码版权归吉檀迦俐所有,可供学习和借鉴或商用。
 -->
<template>
  <button class="back-button" @click="goBack">返回</button>
  <div class="container">
    <div class="w-full flex justify-center">
      <div class="font-bold text-[24px]">在Vue3中使用OpenLayers加载远程shp数据,map上显示图形</div></div>
    <div id="vue-openlayers"></div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import 'ol/ol.css'
import { Map, View } from 'ol'
import SourceVector from 'ol/source/Vector'
import LayerVector from 'ol/layer/Vector'
import GeoJSON from 'ol/format/GeoJSON'
import { Tile } from 'ol/layer'
import OSM from 'ol/source/OSM'
import * as shapefile from 'shapefile'
import router from "@/router";
const goBack = () => {
  router.push('/OpenLayers');
};
// Initialize map and source
const map = ref<Map | null>(null)
const source = new SourceVector({
  wrapX: false
})
const view = new View({
  projection: "EPSG:4326",
  center: [8.2275, 6.8185],
  zoom: 3
})

// Function to read shapefile and add features to the source
const readshp = () => {
  const shp = "https://cdn.rawgit.com/mbostock/shapefile/master/test/points.shp"
  const dbf = "https://cdn.rawgit.com/mbostock/shapefile/master/test/points.dbf"

  shapefile.open(shp, dbf, { encoding: 'utf-8' })
    .then(source2 => {
      const log = (result: any) => {
        if (result.done) return
        const feature = new GeoJSON().readFeature(result.value)
        source.addFeature(feature)
        return source2.read().then(log)
      }
      return source2.read().then(log)
    })
    .catch(error => console.error(error))
}

// Function to initialize the map
const initMap = () => {
  map.value = new Map({
    target: 'vue-openlayers',
    layers: [
      new Tile({
        source: new OSM()
      }),
      new LayerVector({
        source: source
      }),
    ],
    view: view
  })
}

// Initialize map and read shapefile on component mount
onMounted(() => {
  initMap()
  readshp()
})
</script>

<style scoped>
.container {
  width: 840px;
  height: 550px;
  margin: 50px auto;
  border: 1px solid #42B983;
}

#vue-openlayers {
  width: 800px;
  height: 420px;
  margin: 0 auto;
  border: 1px solid #42B983;
  position: relative;
}
</style>

代码解析

  1. 地图初始化

    • 使用 ol/Mapol/View 创建 OpenLayers 地图实例,并设置地图视图的投影、中心点和缩放级别。
    • 加载 OSM 瓦片图层作为底图。
  2. Shapefile 数据解析

    • 借助 shapefile 库解析远程 Shapefile 数据。Shapefile 通常由 .shp.dbf 文件组成,需要同时加载。
    • 使用 GeoJSON 格式化工具将解析的数据转为 OpenLayers 可用的 Feature,并添加到矢量数据源 SourceVector 中。
  3. 显示数据

    • 将矢量数据源绑定到 LayerVector 图层,并添加到地图实例中。

示例运行效果

  • Shapefile 数据 :文中使用了 mbostock/shapefile 项目的示例数据。
  • 效果截图

地图加载完成后,Shapefile 中的点数据会正确显示在 OpenLayers 地图上。


常见问题

  1. 跨域问题

    • 如果远程服务器未设置 CORS,可以考虑使用代理服务器解决。
  2. 本地文件加载

    • 浏览器无法直接读取本地文件,可以通过后端接口或使用 FileReader 进行加载。

希望本文对您了解如何使用 Vue 3 和 OpenLayers 加载 Shapefile 数据有所帮助。如果您有任何问题或建议,欢迎留言交流!

相关推荐
「、皓子~20 分钟前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
就改了22 分钟前
Ajax——在OA系统提升性能的局部刷新
前端·javascript·ajax
凌冰_24 分钟前
Ajax 入门
前端·javascript·ajax
京东零售技术39 分钟前
京东小程序JS API仓颉改造实践
前端
奋飛1 小时前
TypeScript系列:第六篇 - 编写高质量的TS类型
javascript·typescript·ts·declare·.d.ts
老A技术联盟1 小时前
从小白入门,基于Cursor开发一个前端小程序之Cursor 编程实践与案例分析
前端·小程序
风铃喵游1 小时前
构建引擎: 打造小程序编译器
前端·小程序·架构
sunbyte1 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ThemeClock(主题时钟)
前端·javascript·css·vue.js·前端框架·tailwindcss
小飞悟1 小时前
🎯 什么是模块化?CommonJS 和 ES6 Modules 到底有什么区别?小白也能看懂
前端·javascript·设计
浏览器API调用工程师_Taylor1 小时前
AOP魔法:一招实现登录弹窗的全局拦截与动态处理
前端·javascript·vue.js