calvinmetcalf / shapefile-js

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

Maybe this is good for the code.. Function parseZip with the name of the element isolated from the another names like .. '.mshp' or something with 4 or more chars #156

Closed wellingtonsys closed 2 years ago

wellingtonsys commented 3 years ago

shp.parseZip = function(buffer, whiteList) { var key; buffer = toBuffer(buffer); var zip = unzip(buffer); var names = []; whiteList = whiteList || []; for (key in zip) { if (key.indexOf('__MACOSX') !== -1) { continue; } if (key.slice(-4).toLowerCase() === '.shp') { names.push(key.slice(0, -4)); zip[key.slice(0, -3) + key.slice(-3).toLowerCase()] = zip[key]; } else if (key.slice(-4).toLowerCase() === '.prj') { zip[key.slice(0, -3) + key.slice(-3).toLowerCase()] = proj4(zip[key]); } else if (key.slice(-5).toLowerCase() === '.json' || whiteList.indexOf(key.split('.').pop()) > -1) { names.push(key.slice(0, -3) + key.slice(-3).toLowerCase()); } else if (key.slice(-4).toLowerCase() === '.dbf' || key.slice(-4).toLowerCase() === '.cpg') { zip[key.slice(0, -3) + key.slice(-3).toLowerCase()] = zip[key]; } } if (!names.length) { throw new Error('no layers founds'); } var geojson = names.map(function(name) { var parsed, dbf; var lastDotIdx = name.lastIndexOf('.'); if (lastDotIdx > -1 && name.slice(lastDotIdx).indexOf('json') > -1) { parsed = JSON.parse(zip[name]); parsed.fileName = name.slice(0, lastDotIdx); } else if (whiteList.indexOf(name.slice(lastDotIdx + 1)) > -1) { parsed = zip[name]; parsed.fileName = name; } else { if (zip[name + '.dbf']) { dbf = parseDbf(zip[name + '.dbf'], zip[name + '.cpg']); } parsed = shp.combine([parseShp(zip[name + '.shp'], zip[name + '.prj']), dbf]); parsed.fileName = name; } return parsed; }); if (geojson.length === 1) { return geojson[0]; } else { return geojson; } };

calvinmetcalf commented 3 years ago

um I don't know what you're suggesting here, if you are suggesting a code change, you can can just edit it on github and open a pull which will give me a better idea of what you actually changed