#!/usr/bin/env python# -*- coding: utf-8 -*-# @File : map03.py# @Author: huifer# @Date : 2018/6/28import pandas as pdimport mathimport foliumdef degree_conversion_decimal(x): """ 度分转换成十进制 :param x: float :return: integer float """ integer = int(x) integer = integer + (x - integer) * 1.66666667 return integerdef distance(origin, destination): """ 经纬度计算两点距离 :param origin: :param destination: :return: """ lat1, lon1 = origin lat2, lon2 = destination radius = 6371 # km dlat = math.radians(lat2 - lat1) dlon = math.radians(lon2 - lon1) a = math.sin(dlat / 2) * math.sin(dlat / 2) + math.cos(math.radians(lat1)) / * math.cos(math.radians(lat2)) * math.sin(dlon / 2) * math.sin(dlon / 2) c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a)) d = radius * c return d# 数据准备data = pd.read_excel('SURF_CHN_MUL_HOR_STATION.xlsx')# 修改成十进制 以及保留1一位小数data['经度'] = data['经度'].apply(degree_conversion_decimal)data['纬度'] = data['纬度'].apply(degree_conversion_decimal)data['观测场拔海高度(米)'] = data['观测场拔海高度(米)'].apply(lambda x: round(x, 1))data['气压传感器拔海高度(米)'] = data['气压传感器拔海高度(米)'].apply(lambda x: round(x, 1))# 保存新的文件# data.to_csv('气象站信息十进制.csv')data["距离杭州(km)"] = data.apply(lambda r: distance((r['纬度'], r['经度']), (30.14, 120.1)), axis=1)# print(data[data['距离杭州(km)']<100].sort_values('距离杭州(km)'))# 选择除了杭州以外的内容selected_st = data[data['距离杭州(km)'] < 100].sort_values('距离杭州(km)').iloc[1::]# 展示地图# 提取数据hzdata = data.ix[data['站名'] == '杭州', ['站名', '纬度', '经度']]myMap = folium.Map(location=[hzdata.iloc[0]['纬度'], hzdata.iloc[0]['经度']])icon_hz = dict( prefix='fa', color='red', icon_color='darkred', icon='cny')icon = folium.Icon(**icon_hz)folium.Marker( location=[hzdata.iloc[0]['纬度'], hzdata.iloc[0]['经度']], popup="杭州", icon=icon).add_to(myMap)for i in range(len(selected_st)): name = selected_st.iloc[i]['站名'] x = selected_st.iloc[i]['纬度'] y = selected_st.iloc[i]['经度'] test = folium.Html( '<b>name:{}</b></br> <b>x:{}</b></br> <b>y:{}</b></br>'.format(name, x, y), script=True) popup = folium.Popup(test, max_width=2650) folium.Marker( location=[x, y], popup=popup, ).add_to(myMap)myMap.save("test.html")