Thursday, April 28, 2022

Display Map with Markers and Polygons with Folium and PyQt6

This demo program show how to create a folium map and attach it to qwbengineview to place is anywhere on a window and update the map with a press of a button.


The Output:



The code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import sys
import io
import folium 
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QApplication, QPushButton
from PyQt5.QtWebEngineWidgets import QWebEngineView # pip install PyQtWebEngine

class Window(QMainWindow):

    def __init__(self):
        super(Window, self).__init__()      
         
        self.initUI()

    def initUI(self):
         
        self.pb1 = QPushButton('Update', self)
        self.pb1.setGeometry(340, 330, 100, 30)       
        self.pb1.clicked.connect(self.onClick_pb2)        
        
        coordinate = (15.872172863923, 120.39238696289)
        m = folium.Map(
        	#tiles='Stamen Terrain',
        	#tiles='cartodbpositron',
            tiles='Stamen Toner',
            zoom_start=13,
        	location=coordinate
        )
        folium.Rectangle([(15.882172863923, 120.41238696289), (15.862172863923, 120.38238696289)],
                    color="blue",
                    weight=1,
                    fill=True,
                    fill_color="blue",
                    fill_opacity=0.1).add_to(m)
        folium.Marker(location=[15.872172863923, 120.39238696289],popup='Basista',tooltip='Click here to see Popup',icon=folium.Icon(color='purple',prefix='fa',icon='anchor')).add_to(m)
        # save map data to data object
        data = io.BytesIO()
        m.save(data, close_file=False)

        self.webView = QWebEngineView(self)
        self.webView.setHtml(data.getvalue().decode())
        self.webView.setGeometry(20,20, 420,280)
        self.setGeometry(25, 45, 450, 370)
        self.setWindowTitle('Post 28')
        self.show()
        
    def onClick_pb2(self):
        coordinate1 = (14.5409, 121.0503)
        m = folium.Map(
        	#tiles='Stamen Terrain',
        	tiles='cartodbpositron',
            zoom_start=18,
        	location=coordinate1
        )
        folium.Circle(location=[14.5409, 121.0503], popup='BGC', fill_color='green', fill_opacity=0.1, radius=50, weight=0, color="green").add_to(m)
        folium.Marker(location=[14.5409, 121.0503],popup='BGC',tooltip='Click here to see Popup').add_to(m)
        data = io.BytesIO()
        m.save(data, close_file=False)
        
        self.webView.setHtml(data.getvalue().decode())                    

def main():

    app = QApplication(sys.argv)
    ex = Window()
    ex.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()

No comments:

Post a Comment