calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
MIT License
735 stars 230 forks source link

Cannot reference local files #35

Closed gausie closed 7 years ago

gausie commented 9 years ago

Despite the documentation, you cannot reference local files - only file URLs to be accessed via binaryAjax.

calvinmetcalf commented 9 years ago

can you link to where in the documentation it implies this (couldn't find it)

gausie commented 9 years ago

Erm the readme uses non-URL filenames

calvinmetcalf commented 9 years ago

They can be relative urls...

On Sat, Dec 13, 2014, 11:26 AM Samuel Gaus notifications@github.com wrote:

Erm the readme uses non-URL filenames

— Reply to this email directly or view it on GitHub https://github.com/calvinmetcalf/shapefile-js/issues/35#issuecomment-66881734 .

nmccready commented 9 years ago

@calvinmetcalf can u please do an example, I tried local file protocol . outcome: [Error: Protocol:file: not supported.] }

nmccready commented 9 years ago

Your readme kinda implies that it supports local files by stating

shp("files/pandr.zip").then(function(geojson){
        //see bellow for whats here this internally call shp.parseZip()
    });

When in reality I think your referring to your hosted projects path. Which is looking for the file on port 80. It would be nice to pass straight buffers or streams as well.

calvinmetcalf commented 9 years ago

yeah you probably can't use it with a file url, if you need to test it locally use something like serve

Monk-Data commented 8 years ago

You can easily use local files, no need to serve them up over http:

var shp = require('shpjs');
var fs = require('fs');

var fnZip = function() {
    shp(fs.readFileSync('N:\\TIGER2015\\COUNTY\\tl_2015_us_county.zip')).then(function(geo) {
        console.log('success');
        console.log(geo);
    }).catch(function() {
        console.log('error')
        console.log(arguments);
    });
};

var fnParts = function() {
    var obj = shp.combine([
        shp.parseShp(fs.readFileSync('N:\\TIGER2015\\COUNTY\\tl_2015_us_county.shp'), fs.readFileSync('N:\\TIGER2015\\COUNTY\\tl_2015_us_county.prj', 'utf8')),
        shp.parseDbf(fs.readFileSync('N:\\TIGER2015\\COUNTY\\tl_2015_us_county.dbf'))]);
    console.log(obj);
};

fnZip();
fnParts();
craiggoldstone commented 8 years ago

You can use local files, without serving them over HTTP, by using the a file browser.

<input type="file" id="input" onchange="handleFiles(this.files)">
var fr = new FileReader();
fr.onload = function () {
  shp(this.result).then(function(geojson) {
    console.log('loaded geojson:', geojson);
  })
};
fr.readAsArrayBuffer(file);
calvinmetcalf commented 7 years ago

closing this, the tl;dr is that if you use relative file paths but then open the html as a file, things will break, so don't do that

loganpowell commented 6 years ago

Sorry for asking on a closed issue, but - though the readFileSync solution posed by @Monk-Data works - I'm trying to use the async readFile function without any luck. I'm about to convert over a thousand Census shapefiles to geojson, so I can't tie up the thread to use the synchronous version. Any pointers?

loganpowell commented 6 years ago

Also, perhaps related, would it be possible to allow a callback strategy in addition to promise?

loganpowell commented 6 years ago

I figured it out. Sorry for the newb question!

hoogw commented 4 years ago

Your readme kinda implies that it supports local files by stating

shp("files/pandr.zip").then(function(geojson){
        //see bellow for whats here this internally call shp.parseZip()
    });

When in reality I think your referring to your hosted projects path. Which is looking for the file on port 80. It would be nice to pass straight buffers or streams as well.

I initially think the other way, I think it means local file path, in fact, it means url file path. Can you put a note in readme, explain it is a URL resource file path, NOT a local file path.

when read local file, you have to read it into arraybuffer at first place, then, use shp(arraybuffer) to import in