Skip to content

Commit

Permalink
Tippecannoe の依存関係を install
Browse files Browse the repository at this point in the history
  • Loading branch information
naogify committed Dec 13, 2023
1 parent 837562a commit 1776f92
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ RUN apt-get update && apt-get install -y \
jq \
gdal-bin \
git \
build-essential
build-essential \
nodejs \
npm \
libsqlite3-dev \
zlib1g-dev

# tippecanoe のインストール(felt リポジトリから)
RUN git clone https://github.com/felt/tippecanoe.git \
Expand All @@ -21,6 +25,7 @@ RUN curl -LO https://golang.org/dl/go1.18.linux-amd64.tar.gz \
&& export PATH=$PATH:/usr/local/go/bin \
&& go install github.com/protomaps/go-pmtiles@latest

COPY bin/ /bin/
COPY entrypoint.sh /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]
9 changes: 9 additions & 0 deletions bin/error.js
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;
14 changes: 14 additions & 0 deletions bin/excel2csv.js
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 };
78 changes: 78 additions & 0 deletions bin/excel2geojson.js
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);
4 changes: 4 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@ AWS_SECRET_ACCESS_KEY=$4

echo "INPUT_DIR: $INPUT_DIR"
echo "ACCESS_KEY: $ACCESS_KEY"

# node /bin/excel2geojson.js $INPUT_DIR
# ls -l $INPUT_DIR

27 changes: 27 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions package.json
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"
}
}

0 comments on commit 1776f92

Please sign in to comment.