可以在Python中保存一个folium对象吗?

jjhzyzn0  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(204)

我试图写一个脚本,使用folium来显示两个gpx轨迹。一个已经计算(预测轨迹),另一个我每天更新(实际轨迹)。
然而,从GPX跟踪跟踪生成folium.PolyLine对象的脚本运行起来相当长,因此,我想保存包含此跟踪的folium.PolyLine对象。然而,我没有找到通过folium'文档保存此对象的方法。
如何保存这种对象?
我已经尝试使用pickle包来保存folium.PolyLine对象:

#[...]

line = folium.PolyLine(points)

path = os.path(...)

with open(path, "w")as f:

    pickle.dump(line, path, -1)

但我得到了这个错误:

Traceback (most recent call last):

    File "/storage/emulated/0/Documents/Pydroid3/script.py", line 372, in <module>

    pickle.dump(m, outp, -1)

_pickle.PicklingError: Can't pickle <function root at 0x75a65af0d0>: attribute lookup root on __main__ failed

/storage/emulated/0 $
68bkxrlz

68bkxrlz1#

我们可以将foliumMap图像保存为html文件:

import folium
m = folium.Map(location=[26, -80], height=500, width=750, zoom_start=8)
file_name = '/content/drive/My Drive/Colab Notebooks/DATA_FOLDERS/IMAGES/my_folium_map'
m.save(file_name + '.html')

然后将其转换为pdf文件,然后将pdf文件转换为png文件:

!pip install pdfkit
!sudo apt-get install wkhtmltopdf
import pdfkit
!pip install PyMuPDF
import fitz
options = {'javascript-delay': 500, 'page-size': 'Letter', 'margin-top': '0.0in', 'margin-right': '0.0in', 'margin-bottom': '0.0in', 'margin-left': '0.0in', 'encoding': "UTF-8", 'custom-header': [('Accept-Encoding', 'gzip')]}
pdfkit.from_file(file_name + '.html',  (file_name + '.pdf'), options=options)
pdf_file = fitz.open(file_name + '.pdf')
page = pdf_file.load_page(0)
pixels = page.get_pixmap()
pixels.save(file_name + '.png')
pdf_file.close()

保存的png文件:

from PIL import Image
folium_png = Image.open(file_name + '.png')
display(folium_png)

注意:foliumheightwidth参数决定保存的图片大小:

folium.Map(..., height=500, width=750, ...)

现在,如果我们想要 actualfolium.Map对象,我们可以将其保存为html文件:

import folium
m = folium.Map(location=[26, -80])
m.save('/content/drive/My Drive/Colab Notebooks/DATA_FOLDERS/IMAGES/some_folium_map.html')

然后要检索保存的folium.Map对象,我们readhtml文件:

file_name = '/content/drive/My Drive/Colab Notebooks/DATA_FOLDERS/IMAGES/some_folium_map.html'
a = open(file_name, 'r', encoding='UTF-8')
a = a.read()

解析str ing以找到attributiondict ionary:

b = a[a.find('attribution') - 1:a.find('attribution') + (a[a.find('attribution') - 1:]).find('}') - 1]

然后pythonicizestr ing并将其作为dict ionary传递给folium.Map函数:

import ast
def pythonicize(some_string):
    some_new_string = some_string
    while some_string == some_new_string:
        if some_string.find('false') >= 0:
            some_new_string = some_string[:some_string.find('false')] + 'F' + some_string[some_string.find('false') + 1:]
        if some_new_string.find('true') >= 0:
            some_new_string = some_new_string[:some_new_string.find('true')] + 'T' + some_new_string[some_new_string.find('true') + 1:]
        if some_new_string.find('false') >= 0 or some_new_string.find('true') >= 0:
            some_string = some_new_string
    return ast.literal_eval('{' + some_new_string + '}')

m = folium.Map(**pythonicize(b))
m

其中m(再次)是folium.Map对象:

相关问题