vue3成功引入shpjs的方法

安装:

npm install shpjs --save

引入:

参考了很多篇文章,博主们使用的import引入或者require,我试过了不好使,

若使用 import shp from 'shpjs'  报错:

Uncaught ReferenceError: global is not defined
at node_modules/immediate/lib/browser.js (browser.js:2:16)
at __require2 (chunk-GFT2G5UO.js?v=3a3694f4:18:50)
at node_modules/lie/lib/browser.js (browser.js:2:17)
at __require2 (chunk-GFT2G5UO.js?v=3a3694f4:18:50)
at node_modules/shpjs/lib/binaryajax-browser.js (binaryajax-browser.js:2:17)
at __require2 (chunk-GFT2G5UO.js?v=3a3694f4:18:50)
at node_modules/shpjs/lib/binaryajax-fetch.js (binaryajax-fetch.js:2:18)
at __require2 (chunk-GFT2G5UO.js?v=3a3694f4:18:50)
at node_modules/shpjs/lib/index.js (index.js:7:20)
at __require2 (chunk-GFT2G5UO.js?v=3a3694f4:18:50)

若使用 let shapefile = require('shpjs/dist/shp')  报错:

Uncaught ReferenceError: require is not defined
    at fileReader.onload

于是安装 require 然后就各种报错了,dev都进展不下去,又把require 给卸了

然后看了GitHub的shpjs  GitHub - oxy86/react-leaflet-maps-shapefile: React app with a full-screen interactive map and a simple button to upload geospatial data files in shapefile format.icon-default.png?t=N7T8https://github.com/oxy86/react-leaflet-maps-shapefile

点击这个 shpjs链接,跳转到npm shpjs - npm (npmjs.com)icon-default.png?t=N7T8https://www.npmjs.com/package/shpjs?activeTab=readme,里面有解释怎么使用shpjs,介绍用的是

<script src='dist/shp.js'> </script> 引入,

注意:要看自己安装好了shpjs之后,dist/shp.js在哪里,我安装好后是在node_modules/shpjs/dist/shp.js,于是在index.html加上这一行:

 <script src='/node_modules/shpjs/dist/shp.js'> </script>

 试一下:

const loadShpZip = (e) => {
  let file = e.target.files[0];
  let [name, format] = file.name.split(".");
  // if (file?.size / 1024 / 1024 > 10) {
  //     alert("请选择内容小于10MB的文件!")
  // }
  if (".zip".includes(format)) {
    let fileReader = new FileReader();
    fileReader.readAsArrayBuffer(file);
    fileReader.onload = function () {
      shp.parseZip(this.result).then((geojson) => {
        console.log("zip加载", geojson);
        // addGeoJsonToMap(geojson, name)
      });
    };
  } else {
    alert("暂不支持该类型数据文件");
  }
};

成功了

参考:

shpjs - npm (npmjs.com)icon-default.png?t=N7T8https://www.npmjs.com/package/shpjs?activeTab=readme前端加载shapefile数据_shpjs-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/yememory/article/details/127810563?spm=1001.2014.3001.5506shp文件纯前端的上传、解析、编辑、下载_shpjs-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/weixin_43081649/article/details/128713219?spm=1001.2014.3001.5506

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值