calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
715 stars 228 forks source link

Get EPSG of the shapefile or reproject automatically in 4326 #153

Closed ArthurGenet closed 2 years ago

ArthurGenet commented 3 years ago

Hello,

I am using shapefile-js right now to load a shapefile, convert it to a geojson and display it on a map.

I am doing this right now:

reader.readAsArrayBuffer(this.inputFile); reader.onload = function() { let shapefile = require("shapefile"); shapefile .read(this.result) .then((geojson) => { // do things here }); }

The problem is that the Geojson does not have the CRS of the layer. So I either want to get the EPSG (so I will be able to reproject my layer later with gdal) or to be able to reproject automatically when loading always in the same projection (4326 for example)

Do you know if it is possible to do this?

calvinmetcalf commented 3 years ago

that is the default behavior of this library, to reproject it to 4326, but you are using some other library because this one is called shpjs so what you should be doing is

let shapefile = require("shpjs");
shapefile(this.result)
.then((geojson) => {
// do things here
});
ArthurGenet commented 3 years ago

Ok thank you for your reply @calvinmetcalf, I don't really know what should I put in shapefile(xxxxx) because the user choose a file in his computer but I don't have access to its path, only to the chosen shapefile. And doing something like shapefile(inputFile) doesn't seem to work. And I did this and it doesn't work either:

reader.readAsArrayBuffer(this.inputFile);
reader.onload = function(){
let shapefile = require("shpjs");
shapefile(this.result) //doesn't work
.then((geojson) => {
// do things here
}

Do you know if there is a way to transform a shapefile to a geojson with shpjs without using the path file?

calvinmetcalf commented 3 years ago

put a .catch(e=>console.log(e)) at the end so we can see what sort of error we're getting, this library supports either the file path or the zipped shapefile

ArthurGenet commented 3 years ago

ok thanks a lot, it is working perfectly with zip file, I was not 100% sure of what I could do (sorry for late answer) Have a nice day :)