使用前需要对标本数据做一定的预处理,使标本数据为以下格式的json文件:
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,相关知识网上查询,具体代码如下:
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文件,内容格式如下:
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重构并整合到我自己的工具脚本里吧