Skip to content

Commit

Permalink
Merge pull request #39 from geolonia/fix#37
Browse files Browse the repository at this point in the history
緯度経度ヘッダーに対応、Shift-JIS の CSV を UTF-8に変換する
  • Loading branch information
naogify authored Feb 13, 2024
2 parents 1c20998 + 75db797 commit 2056a8f
Show file tree
Hide file tree
Showing 10 changed files with 670 additions and 718 deletions.
21 changes: 21 additions & 0 deletions bin/convertToUtf8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const iconv = require('iconv-lite');
const jschardet = require('jschardet');

const convertToUtf8 = (content) => {
const detectedEncoding = jschardet.detect(content).encoding;

console.log(`Detected encoding: ${detectedEncoding}`);

if (detectedEncoding === 'SHIFT_JIS') {
return iconv.decode(content, 'Shift_JIS');
}

// UTF-8 と判定された場合はそのまま返す
if (detectedEncoding === 'UTF-8') {
return content;
}

return false;
}

module.exports = convertToUtf8;
6 changes: 6 additions & 0 deletions bin/createCatalogJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ function createCatalogJson(dirPath) {
}

} else {

// 緯度経度がない CSV/Excel は、GeoJSON に変換しないので、catalog.json に含めない
if (!file.name.endsWith(".geojson")) {
return;
}

// ファイルの場合
const fileNameWithoutExt = getFileNameWithoutExtension(file.name);
const dataItem = {
Expand Down
73 changes: 73 additions & 0 deletions bin/csv-to-geojson.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const Papa = require('papaparse');

const csvToGeoJSON = async (csvString) => {
return new Promise((resolve, reject) => {
Papa.parse(csvString, {
header: true,
skipEmptyLines: true,
complete: (results) => {
const latHeaders = ['緯度', 'lat', 'latitude', 'Lat', 'Latitude', 'LAT', 'LATITUDE'];
const lonHeaders = ['経度', 'lon', 'lng', 'longitude', 'Lon', 'Lng', 'Longitude', 'LON', 'LNG', 'LONGITUDE'];

let latField, lonField;

const headers = results.meta.fields;

// 緯度・経度のヘッダー名を判定
for (const field of headers) {

if (typeof latField === 'undefined' && latHeaders.includes(field)) {
latField = field;
}
if (typeof lonField === 'undefined' && lonHeaders.includes(field)) {
lonField = field;
}
}

if (!latField || !lonField) {
console.log("緯度または経度の列が見つかりません。");
resolve(false);
}

// GeoJSONオブジェクトを作成
const geoJson = {
type: "FeatureCollection",
features: results.data.map(record => {
const latValue = parseFloat(record[latField]);
const lonValue = parseFloat(record[lonField]);

if (isNaN(latValue) || isNaN(lonValue)) {
return null;
}

// recordから緯度・経度のフィールドを削除
delete record[latField];
delete record[lonField];

return {
type: "Feature",
geometry: {
type: "Point",
coordinates: [lonValue, latValue]
},
properties: record
};
}).filter(feature => feature !== null)
};

//geojson の features が空の場合はエラー
if (geoJson.features.length === 0) {
console.log("緯度・経度の列に数値以外の値が含まれているか、データが空です。");
resolve(false);
}

resolve(geoJson);
},
error: (err) => {
reject(err);
}
});
});
}

module.exports = csvToGeoJSON;
28 changes: 20 additions & 8 deletions bin/excel2geojson.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const { excel2csv } = require('./excel2csv');
const { writeFile, readFile } = require('fs/promises');
const klaw = require('klaw');
const csv2geojson = require('csv2geojson');
const ConversionError = require('./error');
const path = require('path');
const csvToGeoJSON = require('./csv-to-geojson');
const convertToUtf8 = require('./convertToUtf8');

const inputDir = process.argv[2];

const excelToGeoJson = async (inputDir) => {
Expand All @@ -27,19 +28,30 @@ const excelToGeoJson = async (inputDir) => {
throw new ConversionError("excelToGeoJson", excelPath);
}
} else if (file.path.endsWith(".csv")) {
csvData = await readFile(file.path, 'utf-8');
csvData = await readFile(file.path);
csvData = convertToUtf8(csvData);

if (!csvData) {
// 警告を出力
console.log(`Error: CSV データ ${file.path} を UTF-8 に変換できませんでした。`);
continue;
}

csvData = csvData.toString('utf-8');
}

if (csvData) {
const geoJsonPath = file.path.replace(/.csv$|.xlsx$/, '.geojson');

try {

csv2geojson.csv2geojson(
csvData,
async (err, geojson) => {
await writeFile(geoJsonPath, JSON.stringify(geojson));
});
const geojson = await csvToGeoJSON(csvData);
if (!geojson) {
console.log(`Error: CSV データ ${file.path} を GeoJSON に変換できませんでした。`);
continue;
}

await writeFile(geoJsonPath, JSON.stringify(geojson));

} catch (err) {
throw new ConversionError("csvToGeoJson", file.path);
Expand Down
6 changes: 5 additions & 1 deletion bin/geojson2mbtiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const klaw = require('klaw');
const path = require('path');
const exec = promisify(_exec);
const uuid = require('uuid');

const inputDir = process.argv[2];

Expand All @@ -17,10 +18,13 @@ const geojsonToMbtiles = async (inputDir) => {

if (file.path.endsWith(".geojson")) {
const fileName = path.basename(file.path, '.geojson');

// 同名ファイルがある場合にエラーが出るので、uuidをつける
const tileFileName = fileName + "-" + uuid.v4();
console.log(`Processing ${fileName}...`);

const geojsonPath = file.path;
const mbtilesPath = path.join(tmpdir, `${fileName}.mbtiles`);
const mbtilesPath = path.join(tmpdir, `${tileFileName}.mbtiles`);

await exec([
'tippecanoe',
Expand Down
Loading

0 comments on commit 2056a8f

Please sign in to comment.