-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class ConversionError extends Error { | ||
constructor(type, filePath) { | ||
super(); | ||
this.conversionType = type; | ||
this.filePath = filePath; | ||
} | ||
} | ||
|
||
module.exports = ConversionError; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const XLSX = require('xlsx'); | ||
|
||
const excel2csv = async (excelPath) => { | ||
|
||
const workbook = XLSX.readFile(excelPath, {cellNF: true, cellText: true, cellDates: true}); | ||
const sheetName = workbook.SheetNames[0]; | ||
const sheet = workbook.Sheets[sheetName]; | ||
|
||
const csv = XLSX.utils.sheet_to_csv(sheet, { FS: ',', RS: '\r\n', blankrows: false, forceQuotes: true}); | ||
|
||
return csv.endsWith("\r\n") || csv.endsWith("\n") ? csv : csv + "\r\n"; | ||
}; | ||
|
||
module.exports = { excel2csv }; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const { excel2csv } = require('./excel2csv'); | ||
const { writeFile, readFile } = require('fs/promises'); | ||
const klaw = require('klaw'); | ||
const csv2geojson = require('csv2geojson'); | ||
const ConversionError = require('./error'); | ||
const inputDir = process.argv[2]; | ||
|
||
const excelToGeoJson = async (inputDir) => { | ||
const promises = []; | ||
|
||
for await (const file of klaw(inputDir, { depthLimit: -1 })) { | ||
let csvData; | ||
|
||
if (file.path.endsWith(".xlsx")) { | ||
const excelPath = file.path; | ||
try { | ||
csvData = await excel2csv(excelPath); | ||
} catch (err) { | ||
|
||
if (err.message === "FILE_ENDED") { | ||
throw new ConversionError("fileEnded", excelPath); | ||
} | ||
throw new ConversionError("excelToGeoJson", excelPath); | ||
} | ||
} else if (file.path.endsWith(".csv")) { | ||
csvData = await readFile(file.path, 'utf-8'); | ||
} | ||
|
||
if (csvData) { | ||
const geoJsonPath = file.path.replace(/.csv$|.xlsx$/, '.json'); | ||
|
||
try { | ||
|
||
csv2geojson.csv2geojson( | ||
csvData, | ||
{ | ||
latfield: 'lat', | ||
lonfield: 'lng', | ||
delimiter: ',' | ||
}, | ||
async (err, geojson) => { | ||
await writeFile(geoJsonPath, JSON.stringify(geojson)); | ||
}); | ||
|
||
} catch (err) { | ||
throw new ConversionError("csvToGeoJson", file.path); | ||
} | ||
} | ||
} | ||
|
||
await Promise.all(promises); | ||
} | ||
|
||
const main = async (inputDir) => { | ||
try { | ||
excelToGeoJson(inputDir); | ||
} catch (err) { | ||
if (err instanceof ConversionError) { | ||
switch (err.conversionType) { | ||
case "excelToGeoJson": | ||
throw new Error(`Error: Excel ファイルを ${err.filePath} GeoJSON に変換できませんでした。`); | ||
break; | ||
case "fileEnded": | ||
throw new Error(`Error: データが空になっているか、Excel ファイルが破損している可能性があります。`); | ||
break; | ||
case "csvToGeoJson": | ||
throw new Error(`Error: CSV データ ${err.filePath} を GeoJSON に変換できませんでした。`); | ||
break; | ||
default: | ||
throw new Error(err.message); | ||
} | ||
} else { | ||
throw new Error(err.message); | ||
} | ||
} | ||
} | ||
|
||
main(inputDir); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "smartcity-data-upload-action", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"bin": { | ||
"smartcity-data-upload-action": "bin/excel2csv.js" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"klaw": "^4.1.0", | ||
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.0/xlsx-0.20.0.tgz" | ||
} | ||
} |