Logo

绘制 GeoJSON 函数

使用此函数在 SVG 和 HTML 输出中渲染 GeoJSON。

概念

绘制 GeoJSON 函数

使用此函数在 SVG 和 HTML 输出中渲染 GeoJSON。

1from shapely.geometry import shape 2 3def plot_geojson(json: dict, output_format: str='html'): 4 """渲染一个geojson对象,并返回以下输出格式:'html' 或 'svg'""" 5 6 # 将给定的json转换为shapely几何对象 7 geom = shape(json) 8 9 # 获取SVG表示形式 10 svg_text = geom._repr_svg_() 11 12 if output_format == 'svg': 13 print(svg_text) 14 15 else: 16 html_template = ''' 17 <!DOCTYPE html> 18 <html> 19 20 <body> 21 <center> 22 {} 23 </center> 24 </body> 25 </html> 26 ''' 27 28 # 将SVG图形插入HTML模板 29 html = html_template.format(svg_text) 30 31 print(html) 32 33 34my_geojson = { 35 'type': 'Polygon', 36 'coordinates': [[(490, 750), (0, 440), (490, 70), (490, 750)]] 37} 38 39plot_geojson(my_geojson, output_format='html')
1import fiona 2from osgeo import gdal 3 4print(gdal.__version__) 5print(fiona.__version__)

使用 Geopandas 进行地理处理

空间数据可视化