T85_code/.ipynb_checkpoints/get_altitude-checkpoint.ipynb

138 lines
4.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "888d089c-a9c8-4d2d-af74-dff1a8ccfefd",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import json\n",
"import time\n",
"from typing import List\n",
"import requests\n",
"import pandas as pd\n",
"\n",
"\n",
"class GetElevation:\n",
"\n",
" @classmethod\n",
" def __SendQuery(cls, latLngString: str) -> json:\n",
" query = ('https://api.opentopodata.org/v1/mapzen?locations={}&interpolation=bilinear'.format(latLngString))\n",
" res = requests.get(query).json()\n",
" if res[\"status\"] != \"OK\":\n",
" raise Exception(res[\"error\"])\n",
" return res\n",
"\n",
" def GetSingleElevation(self, latitude: float, longitude: float) -> float:\n",
" \"\"\"\n",
" 获取单个高程输入经纬度格式为数值类型返回值为高程float类型\n",
" :param latitude: 纬度\n",
" :param longitude: 经度\n",
" :return: 高程\n",
" \"\"\"\n",
" if latitude < -90 or latitude > 90:\n",
" raise Exception(\"纬度的范围应在-90-90之间请检查数据源\")\n",
" latLngString = str(latitude) + \",\" + str(longitude)\n",
" res = self.__SendQuery(latLngString)\n",
" elevation = res[\"results\"][0][\"elevation\"]\n",
" return elevation\n",
"\n",
" def GetMultiElevation(self, latitude: List[float], longitude: List[float]) -> List[float]:\n",
" \"\"\"\n",
" 获取数组类型的高程,输入经纬度格式为经度数组和纬度数组,返回值为高程数组\n",
" :param latitude:纬度数组\n",
" :param longitude:经度数组\n",
" :return:高程数组\n",
" \"\"\"\n",
" if len(latitude) != len(longitude):\n",
" raise Exception(\"纬度数组和经度数组长度不一致!请检查数据源!\")\n",
" for lat in latitude:\n",
" if lat < -90 or lat > 90:\n",
" raise Exception(\"纬度的范围应在-90-90之间请检查数据源\")\n",
" elevationList = []\n",
" hundredNums = len(latitude) // 100\n",
" # 查询整百的高程\n",
" for i in range(hundredNums):\n",
" latLngString = \"\"\n",
" for idx in range(100 * i, 100 * (i + 1)):\n",
" latLngString += (str(latitude[idx]) + \",\" + str(longitude[idx]) + \"|\")\n",
" res = self.__SendQuery(latLngString)\n",
" for idx in range(100):\n",
" elevationList.append(res[\"results\"][idx][\"elevation\"])\n",
" time.sleep(1)\n",
" # 查询剩余的不到100的高程\n",
" latLngString = \"\"\n",
" for i in range(hundredNums * 100, len(latitude)):\n",
" latLngString += (str(latitude[i]) + \",\" + str(longitude[i]) + \"|\")\n",
" res = self.__SendQuery(latLngString)\n",
" for i in range(len(latitude) - hundredNums * 100):\n",
" elevationList.append(res[\"results\"][i][\"elevation\"])\n",
" return elevationList\n",
"\n",
" def ExportToXlsx(self, latLongDf: pd.DataFrame, elevationList: List[float], outputPath: str) -> None:\n",
" \"\"\"\n",
" 如果用户可以传入一个DataFrame数据可以将返回得到的高程拼接并输出\n",
" :param latLongDf: DataFrame数据\n",
" :param elevationList: 高程数组\n",
" :param outputPath: 输出路径\n",
" :return: 无返回值\n",
" \"\"\"\n",
" latLongDf[\"elevation\"] = elevationList\n",
" latLongDf.to_excel(outputPath, index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2a226b08-0c92-483e-b590-29a39dce6298",
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"data = pd.read_excel('./lat_lon.xlsx')\n",
"data.columns = ['plant', 'longitude', 'latitude']"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91afb581-1994-47c4-85ca-d3ea3e13e95d",
"metadata": {},
"outputs": [],
"source": [
"multiEle = ele.GetMultiElevation(data[\"latitude\"], data[\"longitude\"])\n",
"print(multiEle)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}