Skip to content

Instantly share code, notes, and snippets.

@plmrry
Created June 4, 2021 17:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plmrry/892d9652d2fdccd367c25a83861d3e27 to your computer and use it in GitHub Desktop.
Save plmrry/892d9652d2fdccd367c25a83861d3e27 to your computer and use it in GitHub Desktop.
Fetch shapefile, create GeoJSON
const fetch = require("node-fetch");
const jszip = require("jszip");
const shapefile = require("shapefile");
async function fetchGeoJSON(endpoint) {
console.log(`🐞 Endpoint: ${endpoint}`);
console.log(`🐞 Fetching..`);
const res = await fetch(endpoint);
const buffer = await res.buffer();
console.log(`🐞 Un-zipping...`);
const unzipped = await jszip.loadAsync(buffer);
console.log(`🐞 Reading files...`);
const { files = {} } = unzipped;
const fileNames = Object.keys(files);
const shpName = fileNames.find((d) => d.match(/\.shp$/));
const dbfName = fileNames.find((d) => d.match(/\.dbf$/));
const shpData = await unzipped.file(shpName).async("arraybuffer");
const dbfData = await unzipped.file(dbfName).async("arraybuffer");
console.log(`🐞 Reading shapefile...`);
const geoJSON = await shapefile.read(shpData, dbfData);
console.log(`πŸ¦€ Done!`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment