使用前に標本データを一定の前処理を行い、標本データを以下の形式の json ファイルにします:
[
{
"NO": "50",
"CNname": "白芷",
"LDname": "Angelica dahurica",
"Province": "北京市",
"Local": "百花山"
},
{
"NO": "51",
"CNname": "白芷",
"LDname": "Angelica dahurica",
"Province": "北京市",
"Local": "百花山"
},
{
"NO": "52",
"CNname": "白芷",
"LDname": "Angelica dahurica",
"Province": "北京市",
"Local": "百花山"
},
……
]
ツールは typescript で書かれているため、nodejs 環境が必要で typescript をインストールする必要があります。関連知識はウェブで調べてください。具体的なコードは以下の通りです:
import axios from 'axios'
import fs from 'fs'
interface eachData {
NO: string
CNname: string
LDname: string
Province: string
Local: string
}
type AllJsonData = eachData[]
interface BaiduResult {
status: number
result: {
location: {
lng: number
lat: number
}
precise: number
confidence: number
comprehension: number
level: string
analys_level: string
}
}
interface AllResult {
NO: string
lng: number
lat: number
comprehension: number
}
const Sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms))
}
const jsonData: AllJsonData = require('./白芷样点数据-NSII.json') //ここにあなたのデータjsonのパスを記入してください
class LocationRequest {
constructor(AllJsonData: AllJsonData) {
this.DataSet = AllJsonData
}
private static BaiduAPIKey = 'baiduapikey' //ここに自分が申請した百度地図apikeyを記入してください。申請方法はウェブで調べてください
private static Url = 'https://api.map.baidu.com/geocoding/v3/'
DataSet: AllJsonData
async GetLocationOnce(Local: string, Province: string): Promise<BaiduResult> {
const { data } = await axios.get(
LocationRequest.Url +
'?output=json&ak=' +
LocationRequest.BaiduAPIKey +
'&address=' +
Local +
'&city=' +
Province +
'&extension_analys_level=1',
)
return data as BaiduResult
}
async GetAllLocation(): Promise<AllResult[]> {
let allResult: AllResult[] = []
for (let i = 0; i < this.DataSet.length; i++) {
if (this.DataSet[i].Province != '0未记录' && this.DataSet[i].Local != '0未记录') {
try {
const data = await this.GetLocationOnce(this.DataSet[i].Local, this.DataSet[i].Province)
console.log(
`${this.DataSet[i].NO}:経度と緯度は:${data.result.location.lat},${data.result.location.lng},可解析度は:${data.result.comprehension}`,
)
allResult.push({
NO: this.DataSet[i].NO,
lng: data.result.location.lng,
lat: data.result.location.lat,
comprehension: data.result.comprehension,
})
} catch (error) {
console.log(`${this.DataSet[i].NO}の経度と緯度の取得に失敗しました!\n${error}`)
allResult.push({
NO: this.DataSet[i].NO,
lng: 0,
lat: 0,
comprehension: 0,
})
}
} else {
console.log(`${this.DataSet[i].NO}は省市または区が記録されていません!`)
allResult.push({
NO: this.DataSet[i].NO,
lng: 0,
lat: 0,
comprehension: 0,
})
}
await Sleep(2000)
}
return allResult
}
}
async function main() {
const data = await new LocationRequest(jsonData).GetAllLocation()
try {
fs.writeFileSync('./白芷样点经纬度.json', JSON.stringify(data)) //ここに結果を出力したい場所のパスを記入してください
console.log('書き込み成功!')
} catch (error) {
console.log(error)
}
}
main()
成功した場合、以下の形式の json ファイルが出力されます:
[
{
"NO": "29",
"lng": 115.7844632112745, //経度
"lat": 33.850642695788835, //緯度
"comprehension": 100 //信頼度
},
{
"NO": "30",
"lng": 115.7844632112745,
"lat": 33.850642695788835,
"comprehension": 100
},
{
"NO": "31",
"lng": 115.7844632112745,
"lat": 33.850642695788835,
"comprehension": 100
},
……
]
この課題が終わった後、Python でこのツールを再構築し、自分のツールスクリプトに統合するかもしれません。