Created
June 4, 2021 17:05
-
-
Save plmrry/892d9652d2fdccd367c25a83861d3e27 to your computer and use it in GitHub Desktop.
Fetch shapefile, create GeoJSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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