Skip to content

Commit

Permalink
Merge pull request #86 from 35C4n0r/feat-gdb-geojson
Browse files Browse the repository at this point in the history
feat: add script to parse gdb to geoJSONs
  • Loading branch information
dhruv-1001 committed Feb 19, 2024
2 parents 5b82dd7 + a811c89 commit 14984af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@types/swagger-ui-express": "^4.1.4",
"bun": "^1.0.7",
"express": "^4.18.2",
"fgdb": "^1.0.0",
"geojson-rbush": "^3.2.0",
"maxmind": "^4.3.16",
"maxmind-db-reader": "^0.2.1",
Expand Down
49 changes: 49 additions & 0 deletions server/scripts/gdb_to_geojson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import fgdb from 'fgdb';
import * as fs from 'fs/promises';
import * as path from "path";

const inputDir = `${import.meta.dir}/../geojson-data/indian_villages_boundaries.zip`;
const outputDir = `${import.meta.dir}/../geojson-data/indian_village_boundaries`;

function isZipFile(filename) {
return filename.endsWith('.zip');
}

async function createIfAbsent(filePath) {
if (!await fs.exists(filePath)) {
await fs.mkdir(filePath, {recursive: true});
}
}

const convertGDBtoGeoJSON = async (gdbFileBuffer, outputFilePath) => {
try {
const geoJSON = await fgdb(gdbFileBuffer);
const contentToWrite = JSON.stringify(geoJSON);
await fs.writeFile(outputFilePath, contentToWrite, 'utf8');
console.log("Converted and saved:", outputFilePath);

} catch (error) {
console.error("Error converting GDB to GeoJSON:", error);
}
};

const processFiles = async () => {
await createIfAbsent(outputDir);
const items = await fs.readdir(inputDir, {withFileTypes: true, recursive: true});

for (const item of items) {
const itemPath = path.join(inputDir, item.name);

if (!isZipFile(item.name)) {
await createIfAbsent(path.join(outputDir, item.name));
} else if (isZipFile(item.name)) {
const fileBuffer = await fs.readFile(itemPath);
await convertGDBtoGeoJSON(fileBuffer, path.join(outputDir, item.name.replace(/\.zip$/i, '.geoJSON')));
}
}
};


processFiles().then(r => {
console.log("Converted all gdbs to geJSON");
});

0 comments on commit 14984af

Please sign in to comment.