1 回答
TA贡献1847条经验 获得超11个赞
您必须创建一个方法来更新将显示的 shapefile,并在每次选择新选项生成新路径时调用该方法:
import folium
from PyQt5 import QtCore, QtWebEngineWidgets
from PyQt5.QtWidgets import *
import geopandas as gpd
class LeafWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.view = QtWebEngineWidgets.QWebEngineView()
lay = QVBoxLayout(self)
lay.addWidget(self.view)
self.tmp_file = QtCore.QTemporaryFile("XXXXXX.html")
def set_shapefile(self, filename):
shp_file = gpd.read_file(filename)
shp_file_json_str = shp_file.to_json()
m = folium.Map(location=[39.764075, -86.159019], zoom_start=10)
folium.GeoJson(shp_file_json_str).add_to(m)
if self.tmp_file.open():
m.save(self.tmp_file.fileName())
url = QtCore.QUrl.fromLocalFile(self.tmp_file.fileName())
self.view.load(url)
import sys
import os.path
from os import environ
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.uic import loadUiType
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
ui, _ = loadUiType(os.path.join(CURRENT_DIR, "library.ui"))
def suppress_qt_warnings():
environ["QT_DEVICE_PIXEL_RATIO"] = "0"
environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
environ["QT_SCREEN_SCALE_FACTORS"] = "1"
environ["QT_SCALE_FACTOR"] = "1"
class MainApp(QMainWindow, ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.comboBox.currentTextChanged.connect(self.handle_currentTextChanged)
self.handle_currentTextChanged(self.comboBox.currentText())
def handle_currentTextChanged(self, text):
filename = shp_filename = os.path.join(
CURRENT_DIR, "input", "{}_loaded_NoCC.shp".format(text)
)
self.LeafWidget.set_shapefile(filename)
def main():
suppress_qt_warnings()
app = QApplication(sys.argv)
window = MainApp()
window.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
添加回答
举报