簡體   English   中英

在 folium Draw 插件中自動導出 Geojson 文件?

[英]Export Geojson file automatically in folium Draw plugin?

我最近被介紹到folium ,我需要訪問導出的 Geojson 文件。 我為我的項目找到了一個簡短的代碼。

import folium
from folium.plugins import Draw


m = folium.Map()

draw = Draw(
            export=True,
            filename='data.geojson',
            position='topleft',
            draw_options={'polyline': False,
                          'circlemarker': False,
                          'polygon': False,
                          'marker': False},
            edit_options={'poly': {'allowIntersection': False}})

draw.add_to(m)
m

這很簡單。

但我想知道是否有人知道是否有任何方法可以在繪制每個多邊形后自動保存 Geojson 文件而無需單擊導出按鈕; 因為我需要實時訪問屬性。

提前致謝。

您有 2 個選項可以自動保存 geojson。

  1. 更改 folium Draw 插件的模板以保存,或者
  2. 包括我在下面演示的自定義 js 片段

..

import folium
from folium.plugins import Draw
m = folium.Map()

draw = Draw(
            export=True,
            filename='data.geojson',
            position='topleft',
            draw_options={'polyline': False,
                          'circlemarker': False,
                          'polygon': False,
                          'marker': False},
            edit_options={'poly': {'allowIntersection': False}})

draw.add_to(m)
#Refresh the map html
m.get_root().render()

#modify script tag contents to include layer download on 'layeradd' event
script =f"""
       {m.get_name()}.on('layeradd', function(e) {{
       layer = e.layer;
       if (layer.toGeoJSON()) {{
       document.getElementById('export').click()}} }})"""
e = folium.Element(script)
html = m.get_root()
html.script.add_child(e)
m

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM