前端 解析压缩包,并且读取Shp生成GeoJson在MapBox上渲染

  • 这里需要先安装shapefile;jszip;turf

npm install shapefile

npm install jszip

npm install @turf/turf

javascript 复制代码
<ElUpload
              ref="upload"
              v-model:file-list="fileListShp"
              :limit="1"
              accept=".zip"
              :show-file-list="true"
              :auto-upload="true"
              :on-exceed="() => handleExceed()"
              :before-remove="handleRemoveFireShp"
              :action="apiPath_fireUpload"
              :headers="getFileHeaders()"
              name="files"
              :before-upload="(file) => beforeAvatarUploadShp('fj', file)"
            >
              <div class="btn_shp">上传文件</div>
</ElUpload>

<script setup>
import * as turf from '@turf/turf';
import JSZip from 'jszip';
import * as shapefile from 'shapefile';

const handleExceed = () => {
  ElMessage.warning('最多上传1份压缩包');
};

// 删除文档
const handleRemoveFireShp = async (uploadFile) => {
  if ('response' in uploadFile) {
    removeDocListShp.value.push(uploadFile?.response?.[0]);
  } else {
    removeDocListShp.value.push(uploadFile);
  }
};


// 上传前验证
const beforeAvatarUploadShp = (key, rawFile) => {
  if (!UPLOAD_FILE_TYPE_ZIP(rawFile)) {
    ElMessage.error('仅支持zip格式!');

    return false;
  }

  fileChange(rawFile);  //核心功能代码
  handleCheckedIcon('删除');
};
</script>

核心代码

javascript 复制代码
//解析文件
const fileChange = (param) => {
  // upload.value.clearFiles();

  const fileData = param;

  shpName.value = param.name;

  const zip = new JSZip();

  zip.loadAsync(fileData).then((res) => {
    const fileList = Object.keys(res.files);
    const pattern = new RegExp(/\S\.shp$/);
    const shpFile = fileList.find((i) => pattern.test(i));
    let geojsonArr = [];

    if (!shpFile) {
      ElMessage.warning('压缩包里面没有Shp文件');
      REMOVE_LAYER(`${layerGroupId.High_Light_RESULT_LAYER_SHP}`);
      fileListShp.value = [];

      return;
    }

    zip
      .file(shpFile)
      .async('arraybuffer') // 此处是压缩包中的shp文件,arraybuffer(此时在回调的参数中已经可以获取到上传的zip压缩包下的所有文件)
      .then(function (content) {
        // 这个就是文件中的内容
        shapefile
          .open(content)
          .then((source) => {
            source.read().then(function log(result) {
              if (result.done) {
                //解析完成

                var collection = turf.featureCollection(geojsonArr);

                //REMOVE_LAYER(`${layerGroupId.High_Light_RESULT_LAYER_SHP}`);

                //ADD_MAP_ZDFX_LAYER(layerGroupId.High_Light_RESULT_LAYER_SHP, collection);

                //可以在地图展示图层
                return;
              }

              const json = result.value;

              geojsonArr.push(json);

              return source.read().then(log);
            });
          })
          .catch((error) => {
            console.error(error.stack);
            ElMessage.error('读取shp文件失败');
          });
      });
  });
};
相关推荐
谭光志4 小时前
Vibe Coding 时代,程序员该何去何从
前端·后端·ai编程
仿生狮子6 小时前
别再说“全栈”了,AI 时代团队只认这 5 种人
前端·人工智能·后端
kyriewen6 小时前
AI 写的 React 组件,合并前必查的 6 个坏味道——每个都有代码对比
前端·javascript·react.js
Highcharts.js6 小时前
软件开发公司为什么选择 Highcharts?
前端·前端框架·echarts·数据可视化·技术选型·highcharts·图表工具
阿祖zu7 小时前
企业 AI 转型之痛,个人提效十倍不止,组织效率仍止步不前?
前端·aigc·agent
摇滚侠8 小时前
SpringBoot3+Vue3 全套视频教程 45-51
前端·javascript·vue.js
酸梅果茶8 小时前
【6】lightning_lm项目-LIO 前端 -点云预处理模块
前端·slam
Hilaku8 小时前
前端大裁员背后的恐慌:我们还剩什么底牌?
前端·javascript·程序员
咖啡无伴侣9 小时前
unplugin-auto-import 使用详解+面试高频考点
前端·架构
搬砖记录员9 小时前
录屏文件打不开?H.265兼容性踩坑与4种实战方案
前端