无法在Pyqt5应用程序中导出Folium绘制多边形上的坐标

2022-04-18 00:00:00 python pyqt5 draw folium

问题描述

我正在用pyqt5编写应用程序,使用Folium来标记一些区域并获得GPS坐标。应用程序工作,我可以画多边形,但当我点击导出按钮,没有任何反应。也许有人遇到了类似的问题,知道原因吗?

import folium, io, sys
from folium.plugins.draw import Draw
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5 import QtWebEngineWidgets

class Mapy(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.interfejs()

    def interfejs(self):
        vbox = QVBoxLayout(self)
        self.webEngineView = QtWebEngineWidgets.QWebEngineView()
        self.loadPage()
        vbox.addWidget(self.webEngineView)
        self.setLayout(vbox)
        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle('mapy')
        self.show()

    def loadPage(self):
        m = folium.Map(
            location=[51.7687323,19.4569911], zoom_start=5
        )
        Draw(
            export=True,
            filename='my_data.geojson',
            position='topleft',
            draw_options={'polyline': False, 'rectangle': False,'circle':False, 'circlemarker':False},
            edit_options={'poly': {'allowIntersection': False}}
        ).add_to(m)
        data = io.BytesIO()
        m.save(data, close_file=False)
        self.webEngineView.setHtml(data.getvalue().decode())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    okno = Mapy()
    sys.exit(app.exec_())

解决方案

您必须使用页面配置文件中的downloadRequested信号管理下载:

import io
import sys

import folium
from folium.plugins.draw import Draw

from PyQt5.QtWidgets import QApplication, QFileDialog, QVBoxLayout, QWidget
from PyQt5.QtWebEngineWidgets import QWebEngineView


class Mapy(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.interfejs()

    def interfejs(self):
        vbox = QVBoxLayout(self)
        self.webEngineView = QWebEngineView()
        self.webEngineView.page().profile().downloadRequested.connect(
            self.handle_downloadRequested
        )
        self.loadPage()
        vbox.addWidget(self.webEngineView)
        self.setLayout(vbox)
        self.setGeometry(300, 300, 350, 250)
        self.setWindowTitle("mapy")
        self.show()

    def loadPage(self):
        m = folium.Map(location=[51.7687323, 19.4569911], zoom_start=5)
        Draw(
            export=True,
            filename="my_data.geojson",
            position="topleft",
            draw_options={
                "polyline": False,
                "rectangle": False,
                "circle": False,
                "circlemarker": False,
            },
            edit_options={"poly": {"allowIntersection": False}},
        ).add_to(m)
        data = io.BytesIO()
        m.save(data, close_file=False)
        self.webEngineView.setHtml(data.getvalue().decode())

    def handle_downloadRequested(self, item):
        path, _ = QFileDialog.getSaveFileName(
            self, "Save File", item.suggestedFileName()
        )
        if path:
            item.setPath(path)
            item.accept()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    okno = Mapy()
    sys.exit(app.exec_())

相关文章