-
Notifications
You must be signed in to change notification settings - Fork 0
/
arcGIS_Viewer_POSTGRES.py
119 lines (101 loc) · 3.51 KB
/
arcGIS_Viewer_POSTGRES.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import folium, logging, sys, threading
import psycopg2
import psycopg2.extras
from psycopg2 import pool
from flask import Flask, send_file, request
from io import BytesIO
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
from PyQt6.QtWebEngineWidgets import *
from xyzservices import TileProvider
from functools import lru_cache
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Flask(__name__)
# Database connection parameters
DB_PARAMS = {
"host": "localhost",
"database": "xxxxxxxxxx",
"user": "xxxxxxxxxx",
"password": "xxxxxxxxx"
}
# Initialize connection pool
conn_pool = pool.ThreadedConnectionPool(1, 20, **DB_PARAMS)
class CustomTileProvider(TileProvider):
def __init__(self, name, attribution, url_template):
super().__init__({
"name": name,
"url": url_template,
"attribution": attribution
})
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.browser = QWebEngineView()
self.setCentralWidget(self.browser)
self.showMaximized()
self.load_map()
def load_map(self):
m = folium.Map(location=[50.0000, -5.0000], zoom_start=6, tiles=None)
zoom_levels = self.get_zoom_levels()
for zoom in zoom_levels:
tile_provider = CustomTileProvider(
name=f'xxxxxxxxxxxxx - Zoom {zoom}',
attribution="xxxxxxxxxxxxxxxxxxxxxxxxx",
url_template=f"http://localhost:5000/get_tile/{{z}}/{{x}}/{{y}}?zoom={zoom}"
)
folium.TileLayer(
tiles=tile_provider.url,
attr=tile_provider.attribution,
name=tile_provider.name,
overlay=True,
control=True
).add_to(m)
# Add a LayerControl to toggle between zoom levels
folium.LayerControl().add_to(m)
html = m.get_root().render()
self.browser.setHtml(html)
@lru_cache(maxsize=1)
def get_zoom_levels(self):
zoom_levels = []
conn = conn_pool.getconn()
try:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute("SELECT DISTINCT z FROM images ORDER BY z")
zoom_levels = [row[0] for row in cur.fetchall()]
except Exception as e:
logger.error(f"Error fetching zoom levels: {e}")
finally:
conn_pool.putconn(conn)
return zoom_levels
@app.route('/get_tile/<int:z>/<int:x>/<int:y>')
def get_tile(z, x, y):
image_data = get_image_from_db(z, x, y)
if image_data:
return send_file(BytesIO(image_data), mimetype='image/png')
else:
return '', 404
@lru_cache(maxsize=1000)
def get_image_from_db(z, x, y):
image_data = None
conn = conn_pool.getconn()
try:
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute("SELECT image FROM images WHERE z = %s AND x = %s AND y = %s", (z, x, y))
result = cur.fetchone()
if result:
image_data = result[0]
except Exception as e:
logger.error(f"Error fetching image from database: {e}")
finally:
conn_pool.putconn(conn)
return image_data
def run_flask():
app.run(port=5000, threaded=True)
if __name__ == "__main__":
flask_thread = threading.Thread(target=run_flask)
flask_thread.start()
qt_app = QApplication(sys.argv)
window = MainWindow()
sys.exit(qt_app.exec())