1

I am working with vue3 to load a shp file.

the shp library used is https://github.com/calvinmetcalf/shapefile-js. shpjs 's version is "^5.0.1".

here is the code

   <el-upload
            ref="uploadRef"
            action="/"
            v-model:file-list="state.uploadList" 
            :on-change="uploadFile"
            >
                <template #trigger>
                    <el-button type="primary">select file</el-button>
                </template>
    
    </el-upload>



import shp from 'shpjs';
import { Feature, GeoJSON } from 'geojson';
const uploadFile = async (file) => {
    const format = file.name.split('.').at(-1).toLowerCase();
    let geojson: FeatureCollection<any>;
    switch (format) {
      case 'shp': {
        console.log( await file.raw.arrayBuffer()  );
        const parsed = await shp(await file.raw.arrayBuffer());
        geojson = parsed;
        break;
      }
  }
}

await file.raw.arrayBuffer() outputs

ArrayBuffer(59664156) byteLength: 59664156

but parsing shp from buffer - shp(await file.raw.arrayBuffer()) throws an error

Uncaught (in promise) Error: Can't find end of central directory : is this a zip file ? If it is, see https://stuk.github.io/jszip/documentation/howto/read_zip.html
    at h.readEndOfCentral (shpjs.js?v=9c11b5bb:1045:63)
    at h.load (shpjs.js?v=9c11b5bb:1065:40)
    at shpjs.js?v=9c11b5bb:290:23
    at async unzip_default (shpjs.js?v=9c11b5bb:6488:3)
    at async parseZip (shpjs.js?v=9c11b5bb:6970:15)
    at async Module.loadGeojson (shp.ts:61:24)

1 Answer 1

0

if you're file that was uploaded end in .shp it's not a zip file it's a shape so you want to use the parseShp method import {parseShp} from 'shpjs' or it may be available as shp.parseShp and pass the array buffer to that i.e. await parseShp(await file.raw.arrayBuffer());

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.