banner
Riceneeder

Riceneeder

白天研究生,晚上研究死
github
email

小工具分享

使用前需要對標本數據做一定的預處理,使標本數據為以下格式的 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 重構並整合到我自己的工具腳本裡吧

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。