前端 解析压缩包,并且读取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文件失败');
          });
      });
  });
};
相关推荐
程序猿追12 分钟前
HarmonyOS 6 上做个极简浏览器:Web 组件 + 前进后退 + 加载进度
前端·华为·harmonyos
沐沐师14 分钟前
Mybatis 框架教程
前端
三天不学习26 分钟前
Tailwind CSS 快速入门(2026 版):从 v4 零配置上手,到「该不该用、怎么用好」的选型实战
前端·css·tailwind
明月_清风32 分钟前
面对陌生的 GitHub 项目无从下手?这 4 个网站帮你快速读懂源码
前端·后端·github
htzyl2061 小时前
前端阶梯——第九章、颜色、文本与背景
前端·css
zhangzeyuaaa1 小时前
Ruby `require` 完全指南:从 `$LOAD_PATH` 到 `require_relative`
服务器·前端·ruby
htzyl2061 小时前
前端阶梯——第八章、CSS入门与选择器
前端·css
猪猪拆迁队1 小时前
跨电脑复制共享-WebRTC 打洞踩坑
前端·后端·go
Amos_Web2 小时前
Rspack 源码解析(十八):Tree Shaking 如何用 SideEffects 重写模块连接
前端·rust·前端框架
百度一下吧2 小时前
前端包管理工具和使用手册
前端