Closed gausie closed 7 years ago
can you link to where in the documentation it implies this (couldn't find it)
Erm the readme uses non-URL filenames
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 .
@calvinmetcalf can u please do an example, I tried local file protocol . outcome: [Error: Protocol:file: not supported.] }
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.
yeah you probably can't use it with a file url, if you need to test it locally use something like serve
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();
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);
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
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?
Also, perhaps related, would it be possible to allow a callback strategy in addition to promise?
I figured it out. Sorry for the newb question!
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
Despite the documentation, you cannot reference local files - only file URLs to be accessed via binaryAjax.