花式GIS

【可视化】python地图可视化_wkt_geojson

2019-05-03  本文已影响5人  账号已删除

之前简单介绍了使用Folium包实现python的地图可视化【可视化】python地图可视化_Folium
本文介绍如何使用该方法,对wkt格式数据和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 = 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格式
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
上一篇下一篇

猜你喜欢

热点阅读