mapbox / shp-write

create and write to shapefiles in pure javascript
BSD 3-Clause "New" or "Revised" License
290 stars 186 forks source link

Prj in options does not work #115

Open adikte opened 7 months ago

adikte commented 7 months ago

Hello i am trying to make a shp file with a custom PROJCS in the file.prj but nothing seem to work. I am using shp-write@0.4.3. I tried using both shpwrite.zip and shpwrite.download with no results. Whatever i put in the options object i get the standard prj (WGS84), everything else work fine thought.

`function table_to_shapefile(source, geomType, file_name) {

const options = {
    folder: file_name,
    filename:  file_name,
    outputType: "blob",
    compression: "DEFLATE",
    prj: 'PROJCS["RGF93_v1_Lambert-93",GEOGCS["GCS_RGF93_v1",DATUM["D_Reseau_Geodesique_Francais_1993_v1",SPHEROID["GRS_1980",6378137.0,298.257222101]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",700000.0],PARAMETER["False_Northing",6600000.0],PARAMETER["Central_Meridian",3.0],PARAMETER["Standard_Parallel_1",49.0],PARAMETER["Standard_Parallel_2",44.0],PARAMETER["Latitude_Of_Origin",46.5],UNIT["Meter",1.0]]',
    types: {
        point: file_name,
        polygon: file_name,
        polyline: file_name,
      }
};

// transform data to geojson
const geojson = transform_geom_geojson(source, geomType);
//shpwrite.download(geojson, options)

shpwrite.zip(geojson, options)
    .then((res_zip) => {
        const anchor = document.createElement('a');
        anchor.href = URL.createObjectURL(res_zip);
        anchor.download = file_name + '.zip';
        document.body.appendChild(anchor);
        anchor.click();
        document.body.removeChild(anchor);
    })
    .catch((error) => {
        console.error("Erreur lors de la création du fichier ZIP", error);
    });

}`

johan-cho commented 3 weeks ago

Hi @adikte,

Having this issue as well, have you (or anyone else) found a solution? I remember this worked on an older version.

Thanks all

edit:

it seems like this line (taken from shpwrite.js in the npm package) was removed

var prj = (options && options.prj) ? options.prj : defaultPrj;

another edit:

building from source w/ npm run make results in the line (2909) appearing there; don't know why that's happening

another edit:

this solution is ugly, but I went into node_modules/@mapbox/shp-write/shpwrite.js and changed line 4188 from

zipTarget.file(fileName + ".prj", prj);

to:

zipTarget.file(fileName + ".prj", options?.prj || prj);
sheindel commented 3 weeks ago

@adikte or @johan-cho

Can you confirm in your node_modules which version you have installed? Did you note the change from the original package shp-write to the new package @mapbox/shp-write?

sheindel commented 3 weeks ago

Here is what I have in my node_modules folder after installing @mapbox/shp-write in a fresh project image

sheindel commented 3 weeks ago

I also looked at a recent project I set up that uses @mapbox/shp-write with vite and pnpm as my primary build tooling and digging into the output and minified JS, I can still see this line image

johan-cho commented 3 weeks ago

this is what i see setup when I download from npm: image

edit: similar to pnpm

sheindel commented 3 weeks ago

Oh interesting!

I'm not sure why that is happening. I do see that in my node_modules as well. However, when I debug through my use of shpwrite.zip(...) inside the packaged module with my current application, I can confirm that it does have this line in it. This should be because when you import it is actually looking at the dist/index.js file in the package repo, which simply looks at various files in src/*. The shpwrite.js is for browser specific use when including it as a traditional JS script (e.g. <script src='shpwrite.js'></script>) and actually is not needed but I believe it gets automatically built when the library is installed because the make command is called.

image

johan-cho commented 3 weeks ago

I think both @adikte were using the shpwrite.js file as a traditional browser script. yeah, it's interesting that I see the line with npm run make locally, but it doesn't exist in the unpkg or npm links.

I'm sure whenever you update the package it'll probably fix itself, but for now we have a solution and I'm satisfied.

Thanks for all your work on this package! It's really made my life easier.