之前简单介绍了使用Folium包实现python的地图可视化【可视化】python地图可视化_Folium
本文介绍如何使用该方法,对wkt格式数据和geojson格式数据做可视化
导入需要用的包,包括shapely(几何处理),folium(可视化),json(获得geojson字符串)
from shapely.geometry import shape,mapping
from shapely.geometry import Polygon
from shapely.wkt import dumps, loads
import folium
import json
#wkt格式
wkt_string="POLYGON ((0 0, 10 10, 10 0, 0 0))"
使用shapely的polygon直接进行可视化绘制
# shapely格式
# polygon = Polygon([(0, 0), (10, 10), (10, 0)])
polygon=loads(wkt_string)
print('area',polygon.area,'length',polygon.length)
print('wkt',polygon.wkt)
polygon
# 用Polygon图层显示
m=folium.Map(location=(5,5),zoom_start=6)
# Polygon图层
# folium.Polygon([
# [0,0],[10,0],[3,3],[0,5]
# ],fill_color='red').add_to(m)
xlst,ylst=polygon.exterior.xy
lst=[[y,x] for x,y in zip(xlst,ylst)]
folium.Polygon(lst,fill_color='red').add_to(m)
m
使用geojson格式的数据可视化
# geojson格式
json_poly=mapping(polygon)
print('shape json',json_poly,'\n')
geojson_dic={
"type": "Feature",
"geometry": mapping(polygon),
}
geojson_poly=json.dumps(geojson_dic)
print('geojson_poly',geojson_poly)
shape json {'type': 'Polygon', 'coordinates': (((0.0, 0.0), (10.0, 10.0), (10.0, 0.0), (0.0, 0.0)),)}
geojson_poly {"type": "Feature", "geometry": {"type": "Polygon", "coordinates": [[[0.0, 0.0], [10.0, 10.0], [10.0, 0.0], [0.0, 0.0]]]}}
# 用GeoJson格式显示
m=folium.Map(location=(5,5),zoom_start=6)
# GeoJson图层
folium.GeoJson(
geojson_poly,
name='geojson'
).add_to(m)
m