利用folium实现地理数据可视化

数据源:https://www.poi86.com/poi/baidu/district/330421/1.html

利用folium实现地理数据可视化:

# coding: utf-8

import pandas as pd
import json
import os
# 导入Geopandas
import geopandas as gp
import pandahouse
import folium.plugins as plugins
import folium
from sqlalchemy import create_engine, Column, MetaData, literal


## 读取配置项
uri_ck = 'xxxxx'
con_ck = {'host': 'xxxx', 'database': 'xxxx', 'user': 'xx', 'password': 'xxxx'}
engine_ck = create_engine(uri_ck)

# pandahouse.read_clickhouse(sql_addrs, connection=con_ck)



with open('/嘉善/baidu_330421.geojson', 'r', encoding='utf-8') as f:
    geo = json.load(f)

coun_code = geo['name']
df = pd.DataFrame(columns = ['coun_code','town_id','town_name','town_type',                             'polygon_geo'])


geo_list = []
for i in range(len(geo['features'])): 
    town_id = geo['features'][i]['properties']['id']
    town_name = geo['features'][i]['properties']['name']
    town_type = geo['features'][i]['geometry']['type']
    coordinates = geo['features'][i]['geometry']['coordinates']
    s = []
    for j in range(len(coordinates)):
        a = []
        mul_polygon = coordinates[j]
        for k in range(len(mul_polygon)):
            polygon = mul_polygon[k]
            geo_list.append(polygon)
            polygon_str = ",".join(map(str,polygon))
            polygon_geo = '[(' + polygon_str[1:-1].replace('[','(').replace(']',')')+')]'
            a.append(polygon_geo)
    s.append(a)
    polygon_geo = str(s).replace('\'','')
    serise = pd.Series([coun_code,town_id,town_name,town_type,polygon_geo], index=df.columns, name = i)
    df = df.append(serise, ignore_index=True)
    df['polygon_geo'] = df['polygon_geo'].astype(str)


df['polygon_geo'] = df['polygon_geo'].astype(str)


# pandahouse.to_clickhouse(df, table='yx_grid_town_bak', connection=con_ck, index=False)

s = []
for i in geo_list:
    a = []
    for j in i:
        a.append(j[::-1])
    s.append(a)
print(s)


len(s[1])
len(geo_list[1])


import folium.plugins as plugins
import folium


tiles= 'https://wprd01.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=7'

m = folium.Map([30.83085,120.926031],
               tiles=tiles ,
               attr='百度地图',
               zoom_start=15,
               control_scale=True,
               width='70%'
              )

for i in range(len(s)):
    folium.Polygon(
        locations=s[i],
        popup=folium.Popup('标记坐标点之间多边形区域', max_width=200),
        color='blue', # 线颜色
        fill=True, # 是否填充
        weight=3, # 边界线宽
    ).add_to(m)

m              

m.save(os.path.join(r'/Users/xiaokkk/Desktop/addrpool/', 'baidu_嘉善.html'))  #将结果以HTML形式保存

实现效果: 

猜你喜欢

转载自blog.csdn.net/qq_41081716/article/details/130070620
今日推荐