python folium 地图热力图,画标记点

import folium

from folium.plugins import HeatMap, MarkerCluster, FastMarkerCluster

import webbrowser


 

def createMap(arrayPoint, msgList, saveFile):

    """msgList :[{"msg":"","type":1},...]"""

    centrPoint = [32.0, 118.845]

    map_osm = getMapBaseSource(centrPoint, 1)

    HeatMap(arrayPoint).add_to(map_osm)  # 将热力图添加到前面建立的map里

    if len(msgList) > 0:

        draw_MarkerCluster(map_osm, arrayPoint, msgList)

    map_osm.save(saveFile)     # 保存为html文件

    return map_osm


 

def getMapBaseSource(centerPoint, baseSource):

    """

    设置地图源

    return  map: 默认是openstreetmap

    baseSource:  1-高德地图

    """

    map = folium.Map(location=centerPoint, zoom_start=10)

    if (baseSource == 1):

        folium.TileLayer(tiles='http://webrd02.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}',

                         attr="© 高德地图",

                         min_zoom=0,

                         max_zoom=18,

                         control=False,

                         zoom_start=10,

                         show=False,

                         overlay=False,

                         ).add_to(map)

    return map


 

def createMapWithMark(punitList, saveFile):

    arrayPoint = []

    msgList = []

    centrPoint = [32.0, 118.845]

    for item in punitList:

        arrayPoint.append([item["lat"], item["lng"]])

        tmp = "{} 经纬度{},{},id={}".format(

            item["name"], item["lng"], item["lat"], item["id"])

        msgList.append({"msg": tmp, "type": item["type"]})

    map_osm = getMapBaseSource(centrPoint, 1)

    HeatMap(arrayPoint).add_to(map_osm)

    if len(msgList) > 0:

        draw_MarkerCluster(map_osm, arrayPoint, msgList)

    map_osm.save(saveFile)


 

def draw_MarkerCluster(map, arrayPoint, msgList):

    """

    绘制可缩放的标记物数字指示

    msgList :[{"msg":"","type":1},...]

    """

    marker_cluster = MarkerCluster().add_to(map)

    for i in range(len(arrayPoint)):

        msg = msgList[i]["msg"]

        uType = msgList[i]["type"]

        if (uType == 1):

            folium.Marker(location=arrayPoint[i], popup=msg,

                          icon=None).add_to(marker_cluster)

        elif (uType == 2):

            folium.Marker(location=arrayPoint[i], popup=msg,

                          icon=folium.Icon(color="red", icon=None)).add_to(marker_cluster)

        else:

            folium.Marker(location=arrayPoint[i], popup=msg,

                          icon=folium.Icon(color="black", icon=None)).add_to(marker_cluster)

    # folium.Marker(

    #     location=loc,

    #     popup=msg,

    #     icon=folium.Icon(color="green", icon="ok-sign"),

    # ).add_to(marker_cluster)

    # folium.Marker(

    #     location=loc,

    #     popup=msg,

    #     icon=folium.Icon(color="red", icon="remove-sign"),

    # ).add_to(marker_cluster)


 

def readFilePointToMap(file, saveFile):

    arrayPoint = []

    msgList = []

    with open(file, 'r', encoding='utf-8') as f:

        lines = f.readlines()

        for line in lines:

            strs = line.split()

            if (len(strs) > 3):

                lng = (float(strs[2]))

                lat = (float(strs[3]))

                arrayPoint.append([lat, lng])

                tmp = "{} 经纬度{},{},id={}".format(

                    strs[1], lng, lat, strs[0])

                msgList.append({"msg": tmp, "type": 1})

    f.close()

    createMap(arrayPoint, msgList, saveFile)


 

def readFilePointToMap2(file1, file2, saveFile):

    arrayPoint = []

    msgList = []

    with open(file1, 'r', encoding='utf-8') as f:

        lines = f.readlines()

        for line in lines:

            strs = line.split()

            if (len(strs) > 3):

                lng = (float(strs[2]))

                lat = (float(strs[3]))

                arrayPoint.append([lat, lng])

                tmp = "{} 经纬度{},{},id={}".format(

                    strs[1], lng, lat, strs[0])

                msgList.append({"msg": tmp, "type": 1})

    f.close()

    with open(file2, 'r', encoding='utf-8') as f:

        lines = f.readlines()

        for line in lines:

            strs = line.split()

            if (len(strs) > 3):

                lng = (float(strs[2]))

                lat = (float(strs[3]))

                arrayPoint.append([lat, lng])

                tmp = "{} 经纬度{},{},id={}".format(

                    strs[1], lng, lat, strs[0])

                msgList.append({"msg": tmp, "type": 2})

    f.close()

    createMap(arrayPoint, msgList, saveFile)


 

if __name__ == "__main__":

    fileJZ = "E:\\1.txt"

    saveFile = "E:\\hotmap\\所有A点.html"

    # readFilePointToMap(fileJZ, saveFile)

    fileXF = "E:\\2.txt"

    saveFile = "E:\\hotmap\\所有B点.html"

    # readFilePointToMap(fileXF, saveFile)

    # webbrowser.open(saveFile)  # 默认浏览器打开

    readFilePointToMap2(fileJZ, fileXF, "E:\\hotmap\\所有点.html")

    webbrowser.open(saveFile)

你可能感兴趣的:(python)