{ "cells": [ { "cell_type": "code", "execution_count": 1, "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": 8, "id": "2a226b08-0c92-483e-b590-29a39dce6298", "metadata": { "pycharm": { "name": "#%%\n" } }, "outputs": [ { "data": { "text/plain": " plant longitude latitude\n0 万方发电厂(焦作爱依斯万方电力有限公司) 113.381649 35.255622\n1 三河发电有限责任公司 116.860260 39.953617\n2 上海上电漕泾发电有限公司 121.407593 30.765242\n3 上海吴泾发电有限责任公司 121.471140 31.065113\n4 上海吴泾第二发电有限责任公司 121.471340 31.062532", "text/html": "
\n | plant | \nlongitude | \nlatitude | \n
---|---|---|---|
0 | \n万方发电厂(焦作爱依斯万方电力有限公司) | \n113.381649 | \n35.255622 | \n
1 | \n三河发电有限责任公司 | \n116.860260 | \n39.953617 | \n
2 | \n上海上电漕泾发电有限公司 | \n121.407593 | \n30.765242 | \n
3 | \n上海吴泾发电有限责任公司 | \n121.471140 | \n31.065113 | \n
4 | \n上海吴泾第二发电有限责任公司 | \n121.471340 | \n31.062532 | \n
\n | plant | \nlongitude | \nlatitude | \naltitude | \n
---|---|---|---|---|
0 | \n万方发电厂(焦作爱依斯万方电力有限公司) | \n113.381649 | \n35.255622 | \n88.0 | \n
1 | \n三河发电有限责任公司 | \n116.860260 | \n39.953617 | \n27.0 | \n
2 | \n上海上电漕泾发电有限公司 | \n121.407593 | \n30.765242 | \n4.0 | \n
3 | \n上海吴泾发电有限责任公司 | \n121.471140 | \n31.065113 | \n3.0 | \n
4 | \n上海吴泾第二发电有限责任公司 | \n121.471340 | \n31.062532 | \n4.0 | \n
... | \n... | \n... | \n... | \n... | \n
664 | \n鹤壁丰鹤发电有限责任公司 | \n114.192184 | \n35.850766 | \n182.0 | \n
665 | \n鹤壁同力发电有限责任公司 | \n114.191246 | \n35.860822 | \n145.0 | \n
666 | \n黄冈大别山发电有限责任公司 | \n114.915181 | \n31.144568 | \n53.0 | \n
667 | \n黑龙江华电齐齐哈尔热电有限公司 | \n124.063322 | \n47.387983 | \n147.0 | \n
668 | \n黔桂发电有限责任公司 | \n106.630029 | \n26.607537 | \n1265.0 | \n
669 rows × 4 columns
\n