-
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.
Merge pull request #39 from geolonia/fix#37
緯度経度ヘッダーに対応、Shift-JIS の CSV を UTF-8に変換する
- Loading branch information
Showing
10 changed files
with
670 additions
and
718 deletions.
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
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; |
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,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; |
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
Oops, something went wrong.