-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
128 lines (101 loc) · 4.45 KB
/
utils.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
120
121
122
123
124
125
126
127
128
from qgis.PyQt.QtWidgets import QMessageBox
from qgis.core import Qgis, QgsColorRampShader, QgsNetworkAccessManager, QgsApplication, QgsAuthMethodConfig
from qgis.PyQt.Qt import QColor
from qgis.PyQt.QtNetwork import QNetworkRequest
from qgis.PyQt.QtCore import QUrl
import ssl
import json
from .settings_manager import SettingsManager
ssl._create_default_https_context = ssl._create_unverified_context
def validate_credentials() -> bool:
testurl = 'https://api.maptiler.com/maps/basic/style.json'
smanager = SettingsManager()
auth_cfg_id = smanager.get_setting('auth_cfg_id')
if auth_cfg_id:
am = QgsApplication.authManager()
cfg = QgsAuthMethodConfig()
(res, cfg) = am.loadAuthenticationConfig(auth_cfg_id, cfg, True)
if res:
token = cfg.configMap().get("token")
if token and len(token) > 33 and "_" in token:
request = QNetworkRequest(QUrl(testurl))
reply_content = QgsNetworkAccessManager.instance().blockingGet(request, auth_cfg_id)
if not reply_content.error():
return True
return False
def is_qgs_vectortile_api_enable():
# judge vtile is available or not
# e.g. QGIS3.10.4 -> 31004
qgis_version_str = str(Qgis.QGIS_VERSION_INT)
minor_ver = int(qgis_version_str[1:3])
return minor_ver >= 13
def is_qgs_early_resampling_enabled():
qgis_version_str = str(Qgis.QGIS_VERSION_INT)
minor_ver = int(qgis_version_str[1:3])
return minor_ver >= 25
def is_in_darkmode(threshold=383):
"""detect the Qt in Darkmode or not
This function has a dependancy on PyQt, QMessageBox.
Although Qt has no API to detect running in Darkmode or not,
it is able to get RGB value of widgets, including UI parts of them.
This function detect Darkmode by evaluating a sum of RGB value of the widget with threshold.
Args:
threshold (int, optional): a sum of RGB value (each 0-255, sum 0-765). Default to 383, is just median.
Returns:
bool: True means in Darkmode, False in not.
"""
# generate empty QMessageBox to detect
# generated widgets has default color palette in the OS
empty_mbox = QMessageBox()
# get a background color of the widget
red = empty_mbox.palette().window().color().red()
green = empty_mbox.palette().window().color().green()
blue = empty_mbox.palette().window().color().blue()
sum_rgb_value = red + green + blue
return sum_rgb_value < threshold
def load_color_ramp_from_file(fp: str) -> list:
with open(fp, 'r') as f:
lines = f.readlines()[2:] # get rid of header
ramp_items = [[float(line.rstrip("\n").split(',')[0])] + list(map(int, line.rstrip("\n").split(',')[1:5])) for line in lines]
ramp_lst = [QgsColorRampShader.ColorRampItem(ramp_item[0], QColor(ramp_item[1], ramp_item[2], ramp_item[3], ramp_item[4])) for ramp_item in ramp_items]
min_ramp_value = ramp_items[0][0]
max_ramp_value = ramp_items[-1][0]
return min_ramp_value, max_ramp_value, ramp_lst
class MapTilerApiException(Exception):
def __init__(self, message, content):
self.message = message
self.content = content
super().__init__(self.message)
def _qgis_request(url: str):
smanager = SettingsManager()
auth_cfg_id = smanager.get_setting('auth_cfg_id')
if "https://api.maptiler.com" in url:
if "key=" in url:
url = url.split("key=")[0]
request = QNetworkRequest(QUrl(url))
reply_content = QgsNetworkAccessManager.instance().blockingGet(request, auth_cfg_id)
else:
request = QNetworkRequest(QUrl(url))
reply_content = QgsNetworkAccessManager.instance().blockingGet(request)
if not reply_content.error():
return reply_content
else:
error_msg = ""
error_content = ""
if reply_content.errorString():
error_msg = f"{reply_content.errorString()}"
if reply_content.content():
error_content = f"{str(reply_content.content(), 'utf-8')}"
raise MapTilerApiException(error_msg, error_content)
def qgis_request_json(url: str) -> dict:
reply_content = _qgis_request(url)
if not reply_content.error():
json_data = json.loads(reply_content.content().data().decode())
return json_data
else:
return None
def qgis_request_data(url: str) -> bytes:
reply_content = _qgis_request(url)
return reply_content.content().data()
if __name__ == "__main__":
validate_credentials()