Before use, certain preprocessing of the specimen data is required to format the specimen data into a JSON file as follows:
[
{
"NO": "50",
"CNname": "Angelica dahurica",
"LDname": "Angelica dahurica",
"Province": "Beijing",
"Local": "Baihua Mountain"
},
{
"NO": "51",
"CNname": "Angelica dahurica",
"LDname": "Angelica dahurica",
"Province": "Beijing",
"Local": "Baihua Mountain"
},
{
"NO": "52",
"CNname": "Angelica dahurica",
"LDname": "Angelica dahurica",
"Province": "Beijing",
"Local": "Baihua Mountain"
},
……
]
The tool is written in TypeScript, so a Node.js environment is required along with TypeScript installed. Relevant knowledge can be found online, and the specific code is as follows:
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('./Angelica_dahurica_sample_data-NSII.json') // Fill in your data JSON path here
class LocationRequest {
constructor(AllJsonData: AllJsonData) {
this.DataSet = AllJsonData
}
private static BaiduAPIKey = 'baiduapikey' // Fill in your own applied Baidu Map API key here, the application method can be found online
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}:Latitude and Longitude are:${data.result.location.lat},${data.result.location.lng}, Comprehension is:${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} failed to obtain latitude and longitude!\n${error}`)
allResult.push({
NO: this.DataSet[i].NO,
lng: 0,
lat: 0,
comprehension: 0,
})
}
} else {
console.log(`${this.DataSet[i].NO} Province or District not recorded!`)
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('./Angelica_dahurica_latitude_longitude.json', JSON.stringify(data)) // Fill in the path where you want the output results
console.log('Write successful!')
} catch (error) {
console.log(error)
}
}
main()
After successful execution, a JSON file will be output with the following content format:
[
{
"NO": "29",
"lng": 115.7844632112745, // Longitude
"lat": 33.850642695788835, // Latitude
"comprehension": 100 // Confidence
},
{
"NO": "30",
"lng": 115.7844632112745,
"lat": 33.850642695788835,
"comprehension": 100
},
{
"NO": "31",
"lng": 115.7844632112745,
"lat": 33.850642695788835,
"comprehension": 100
},
……
]
After this project, I may refactor this tool in Python and integrate it into my own tool script.