之前簡單介紹了使用Folium包實現(xiàn)python的地圖可視化【可視化】python地圖可視化_Folium
本文介紹如何使用該方法希太,對wkt格式數(shù)據(jù)和geojson格式數(shù)據(jù)做可視化
- 導入需要用的包几缭,包括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格式的數(shù)據(jù)可視化
# 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