-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoneAtlas.py
214 lines (176 loc) · 8.25 KB
/
oneAtlas.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
from qgis.PyQt.QtWidgets import QAction, QMessageBox, QRadioButton, QGridLayout, QGroupBox
from qgis.PyQt.QtGui import QIcon, QColor
from qgis.core import Qgis
from qgis.core import (
QgsVectorLayer,
QgsRasterLayer,
QgsProject,
)
from .mySearch import MySearch
# Access class through python console
# my = qgis.utils.plugins['OneAtlas']
class OneAtlas:
def __init__(self, iface):
self.iface = iface
self.menu = 'OneAtlas'
self.toolbar = self.iface.addToolBar(self.menu)
self.mySearch = None
# Connect function who diable layer button
self.iface.currentLayerChanged.connect(self.disableLayerBtnByLayerSelection)
# Add action to toolbar
def addAction(self, menu, icon, callback):
icon = QIcon(os.path.dirname(__file__) + f'/{icon}.png')
action = QAction(icon, menu, self.iface.mainWindow())
action.triggered.connect(callback)
self.toolbar.addAction(action)
self.iface.addPluginToMenu(self.menu, action)
return action
# Add plugins actions
def initGui(self):
self.searchAction = self.addAction('Search OneAtlas', 'logo', self.showMySearch)
self.layerAction = self.addAction('Display Imagery', 'layer', self.stream)
self.disableLayerBtnByLayerSelection(self.iface.activeLayer())
# self.layerAction.setEnabled(False)
# Remove the toolbar
def unload(self):
for action in self.toolbar.actions():
self.iface.removePluginMenu(self.menu, action)
self.iface.mainWindow().removeToolBar(self.toolbar)
# Init then show the search dialog
def showMySearch(self):
if self.mySearch is None:
self.mySearch = MySearch(self.iface)
self.mySearch.show()
# Disable layer button is active layer isn't valid
def disableLayerBtnByLayerSelection(self, layer):
# Try to disconnect last layer selection detection
# TODO this is really dirty...
try:
self.activeLayer.selectionChanged.disconnect(self.disableLayerBtnByFeatureSelection)
except:
pass
# Disable layer button
self.layerAction.setEnabled(False)
# Check if current selected layer is a VectorLayer
if isinstance(layer, QgsVectorLayer):
self.activeLayer = layer
# Connect the selection changed signal
self.activeLayer.selectionChanged.connect(self.disableLayerBtnByFeatureSelection)
self.disableLayerBtnByFeatureSelection()
# Check if attributes 'service' exist in selected features
def disableLayerBtnByFeatureSelection(self):
self.layerAction.setEnabled(False)
if self.activeLayer.selectedFeatureCount() == 1:
self.selectedFeature = self.activeLayer.selectedFeatures()[0]
self.layerAction.setEnabled(self.selectedFeature.fields().indexFromName('service') != -1)
# Stream image
def stream(self):
goForStream = True
# Avoid auth exception if you reopen the project and don't open the search
if self.mySearch == None:
self.mySearch = MySearch(self.iface)
# Get selected feature of active layer
service = self.selectedFeature['service']
# Setup raster params
isWcs = False
if service == 'BaseMap':
if self.mySearch.bmAuth is None:
try:
self.mySearch.bmSetAuth()
except:
return
username = self.mySearch.bmUsernameInput.text()
password = self.mySearch.bmPasswordInput.text()
layers = self.mySearch.bmGetLayer(self.selectedFeature['wmts'])
styles = 'default'
tileMatrixSet = '4326'
urlAttr = 'wmts'
elif service == 'Data':
if self.mySearch.dtHeaders is None:
try:
self.mySearch.dtSetAuth()
except:
return
username = 'APIKEY'
password = self.mySearch.dtApikeyInput.text()
# Dialog for image choice (panchro/multi & wmts/wcs)
self.msgBox = QMessageBox()
self.msgBox.setWindowTitle(self.menu)
if self.selectedFeature['processingLevel'] == 'ALBUM':
self.iface.messageBar().pushMessage("Warning", "The feature can't be displayed (no WMTS for Archive features)", level=Qgis.Warning)
goForStream = False
else:
protocolGroup = QGroupBox('Protocol')
protocolGrid = QGridLayout()
wmtsRadio = QRadioButton('WMTS')
wcsRadio = QRadioButton('WCS')
protocolGrid.addWidget(wmtsRadio, 0, 0)
protocolGrid.addWidget(wcsRadio, 0, 1)
protocolGroup.setLayout(protocolGrid)
styleGroup = QGroupBox('Style')
styleGrid = QGridLayout()
multispectralRadio = QRadioButton('multispectral')
panchromaticRadio = QRadioButton('panchromatic')
pmsRadio = QRadioButton('pms')
styleGrid.addWidget(multispectralRadio, 0, 0)
styleGrid.addWidget(panchromaticRadio, 0, 1)
styleGrid.addWidget(pmsRadio, 0, 2)
styleGroup.setLayout(styleGrid)
self.msgBox.layout().addWidget(protocolGroup, 0, 0)
self.msgBox.layout().addWidget(styleGroup, 1, 0)
wmtsRadio.setChecked(True)
if type(self.selectedFeature['wcs_multispectral']) != str and type(self.selectedFeature['wcs_panchromatic']) != str and type(self.selectedFeature['wcs_pms']) != str:
protocolGroup.setEnabled(False)
if type(self.selectedFeature['wmts_pms']) != str:
multispectralRadio.setEnabled(True)
panchromaticRadio.setEnabled(True)
pmsRadio.setEnabled(False)
multispectralRadio.setChecked(True)
else:
multispectralRadio.setEnabled(True)
panchromaticRadio.setEnabled(True)
pmsRadio.setEnabled(True)
pmsRadio.setChecked(True)
self.msgBox.setStandardButtons(QMessageBox.Abort | QMessageBox.Ok)
reply = self.msgBox.exec_()
if reply == QMessageBox.Abort:
return
if wmtsRadio.isChecked():
urlAttr = 'wmts_'
layers = 'default'
styles = 'rgb'
tileMatrixSet = 'EPSG4326'
else:
urlAttr = 'wcs_'
isWcs = True
if multispectralRadio.isChecked():
urlAttr += 'multispectral'
elif panchromaticRadio.isChecked():
urlAttr += 'panchromatic'
else:
urlAttr += 'pms'
else:
self.error(f'Service "{service}" of the feature ocg_fid={self.selectedFeature.id()} isn\'t recognized\nIt should be "Basemap" or "Data"')
return
if goForStream:
# Add a WMTS raster layer
# Order of url parameters are important !
# Why layers is required, maybe is an internal id for wmts gesture ?
# What is styles ?
try:
url = self.selectedFeature[urlAttr]
name = f'{service} {self.selectedFeature["id"]}'
if isWcs:
rlayer = QgsRasterLayer(f'dpiMode=7&identifier=default&password={password}&url={url}&username={username}', name, 'wcs')
else:
rlayer = QgsRasterLayer(f'crs=EPSG:4326&dpiMode=7&format=image/png&layers={layers}&password={password}&styles={styles}&tileMatrixSet={tileMatrixSet}&url={url}&username={username}', name, 'wms')
except Exception as e:
self.error(f'Error in protocol connection\n\n{str(e)}')
return
if rlayer.isValid() == False:
self.error(f'Raster layer is invalid\n\n{rlayer.error()}')
return
QgsProject.instance().addMapLayer(rlayer)
def error(self, msg):
QMessageBox.critical(None, self.menu, msg)