-
Notifications
You must be signed in to change notification settings - Fork 168
/
Copy pathcswlib.py
1471 lines (1266 loc) · 52.9 KB
/
cswlib.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
@module library for g.gui.cswbrowser
@brief GUI csw browser
(C) 2015 by the GRASS Development Team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
@author Matej Krejci <matejkrejci gmail.com> (GSoC 2015)
"""
import json
import os
import sys
import tempfile
import webbrowser
import xml.etree.ElementTree as ET
from subprocess import PIPE
from threading import Thread
import grass.script as gs
from grass.pygrass.modules import Module
from grass.script import parse_key_val
from grass.script.setup import set_gui_path
set_gui_path()
import wx
import wx.html as html
from wx import SplitterWindow
from wx.html import EVT_HTML_LINK_CLICKED, HW_DEFAULT_STYLE, HW_SCROLLBAR_AUTO
from wx.lib.mixins.listctrl import ListCtrlAutoWidthMixin
from .cswutil import (
get_connections_from_file,
normalize_text,
open_url,
renderXML,
render_template,
)
from . import globalvar
from .mdeditorfactory import ADD_RM_BUTTON_SIZE
from .mdutil import StaticContext, yesNo
class ConstraintsBuilder(wx.Panel):
def __init__(self, parent, settings=""):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.label = wx.StaticText(
self,
-1,
label="Manual for creating constraints:\n\
*Constraints must be in brackets([]) \n\
*Constraints must be in quotation marks(') \n\
Examples\n\
OR expression\n\
a || b || c\n\
['a','b','c']\n\
\n\
AND expression \n\
a && b && c\n\
[['a','b','c']]\n\
\n\
More expressions \n\
(a && b) || c || d || e\n\
[['a','b'],['c'],['d'],['e']] or [['a','b'],'c','d','e']",
)
self.constrCtrl = wx.TextCtrl(self, -1)
self.applyBtt = wx.Button(self, -1, label="Apply")
self.cancelBtt = wx.Button(self, -1, label="Cancel")
self.constrCtrl.SetValue(settings)
self._layout()
def _layout(self):
panelSizer = wx.BoxSizer(wx.VERTICAL)
sb = wx.StaticBox(self, label="Constraints")
sbs = wx.StaticBoxSizer(sb, orient=wx.VERTICAL)
sbs.Add(self.label, flag=wx.TOP | wx.LEFT | wx.RIGHT, border=10)
sbs.Add(10, 10, 1, wx.EXPAND)
sbs.Add(
self.constrCtrl,
1,
flag=wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,
border=10,
)
panelSizer.Add(sbs, flag=wx.ALL, border=10)
horSizer = wx.BoxSizer(wx.HORIZONTAL)
horSizer.Add(self.applyBtt)
horSizer.Add(self.cancelBtt, flag=wx.LEFT, border=5)
panelSizer.Add(10, 10, 1, wx.EXPAND)
panelSizer.Add(horSizer, flag=wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, border=10)
self.SetSizerAndFit(panelSizer)
class CSWBrowserPanel(wx.Panel):
def __init__(self, parent, main, giface=None):
wx.Panel.__init__(self, parent)
try:
global \
BBox, \
CatalogueServiceWeb, \
Environment, \
ExceptionReport, \
FileSystemLoader, \
GError, \
GMessage, \
GUI, \
GWarning, \
HtmlFormatter, \
PropertyIsLike, \
XmlLexer, \
highlight
from jinja2 import Environment, FileSystemLoader
from owslib.csw import CatalogueServiceWeb
from owslib.fes import BBox, PropertyIsLike
from owslib.ows import ExceptionReport
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import XmlLexer
from core.gcmd import GError, GMessage, GWarning
from gui_core.forms import GUI
except ModuleNotFoundError as e:
msg = e.msg
sys.exit(
globalvar.MODULE_NOT_FOUND.format(
lib=msg.split("'")[-2], url=globalvar.MODULE_URL
)
)
self.context = StaticContext()
self.giface = giface
self.config = main.config
self.parent = main
self.constraintsWidgets = []
self.constraintsWidgets1 = []
self.constraintsAdvanced = False
self.idResults = []
self.constString = ""
self.outpoutschema = "dc"
self.warns = True
sizeConst = 55
self.splitterBrowser = SplitterWindow(
self, style=wx.SP_3D | wx.SP_LIVE_UPDATE | wx.SP_BORDER
)
self.context = StaticContext()
self.connectionFilePath = os.path.join(
self.context.lib_path, "connections_resources.xml"
)
self.pnlLeft = wx.Panel(self.splitterBrowser, id=wx.ID_ANY)
self.pnlRight = wx.Panel(self.splitterBrowser, -1)
self.keywordLbl = wx.StaticText(self.pnlLeft, -1, "Keywords")
self.numResultsLbl = wx.StaticText(self.pnlLeft, -1, "Max")
self.catalogLbl = wx.StaticText(self.pnlLeft, -1, "Catalog")
self.findResNumLbl = wx.StaticText(self.pnlLeft, -1, "")
self.advanceChck = wx.CheckBox(self.pnlLeft, label="Advanced")
self.advanceChck.Bind(wx.EVT_CHECKBOX, self.OnKeywordDialog)
self.stbFind = wx.StaticBox(self.pnlLeft, -1, "Filter")
self.stbSearch = wx.StaticBox(self.pnlLeft, -1, "Search settings")
self.catalogCmb = wx.ComboBox(self.pnlLeft, id=-1, pos=wx.DefaultPosition)
self.keywordCtr = wx.TextCtrl(self.pnlLeft)
self.catalogCmb.Bind(wx.EVT_COMBOBOX, self.OnSetCatalog)
w, self.h = self.keywordCtr.GetSize()
self.numResultsSpin = wx.SpinCtrl(
self.pnlLeft,
min=1,
max=100,
initial=20,
size=(sizeConst, self.h),
style=wx.ALIGN_RIGHT | wx.SP_ARROW_KEYS,
)
self.addKeywordCtr = wx.Button(self.pnlLeft, -1, "+", size=ADD_RM_BUTTON_SIZE)
self.addKeywordCtr.Bind(wx.EVT_BUTTON, self.addKeyWidget)
self.findBtt = wx.Button(self.pnlLeft, size=(sizeConst, self.h), label="Search")
self.findBtt.SetBackgroundColour((255, 127, 80))
qtyp = [
"All",
"Collection",
"Dataset",
"Event",
"Image",
"InteractiveResource",
"MovingImage",
"PhysicalObject",
"Service",
"Software",
"Sound",
"StillImage",
"Text",
]
self.qtypeCb = wx.ComboBox(
self.pnlLeft, id=-1, pos=wx.DefaultPosition, choices=qtyp
)
self.qtypeCb.SetValue("All")
self.qtypeCb.Disable()
# -----Results---
self.resultList = AutoWidthListCtrl(self.pnlLeft)
self.resultList.Bind(wx.EVT_LIST_ITEM_FOCUSED, self.setTooltip)
self.resultList.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnRecord)
self.bttNextItem1 = wx.Button(self.pnlLeft, label=">")
self.bttNextItem2 = wx.Button(self.pnlLeft, label=">>")
self.bttBackItem1 = wx.Button(self.pnlLeft, label="<")
self.bttBackItem2 = wx.Button(self.pnlLeft, label="<<")
self.bttNextItem1.Bind(wx.EVT_BUTTON, self.OnNavigate, self.bttNextItem1)
self.bttNextItem2.Bind(wx.EVT_BUTTON, self.OnNavigate, self.bttNextItem2)
self.bttBackItem1.Bind(wx.EVT_BUTTON, self.OnNavigate, self.bttBackItem1)
self.bttBackItem2.Bind(wx.EVT_BUTTON, self.OnNavigate, self.bttBackItem2)
self.findBtt.Bind(wx.EVT_BUTTON, self.OnSearch)
self.refreshResultList()
# -----BB---------
self.stbBB = wx.StaticBox(self.pnlLeft, -1, "Extent")
self.bbWestLb = wx.StaticText(self.pnlLeft, -1, "Xmin")
self.bbSouthLb = wx.StaticText(self.pnlLeft, -1, "Ymin")
self.bbEastLb = wx.StaticText(self.pnlLeft, -1, "Xmax")
self.bbNorthLb = wx.StaticText(self.pnlLeft, -1, "Ymax")
self.bbWest = wx.TextCtrl(self.pnlLeft)
self.bbSouth = wx.TextCtrl(self.pnlLeft)
self.bbEast = wx.TextCtrl(self.pnlLeft)
self.bbNorth = wx.TextCtrl(self.pnlLeft)
"""
self.bbWest.Bind(wx.EVT_TEXT_ENTER, self.OnTypeBB)
self.bbSouth.Bind(wx.EVT_TEXT_ENTER, self.OnTypeBB)
self.bbEast.Bind(wx.EVT_TEXT_ENTER, self.OnTypeBB)
self.bbNorth.Bind(wx.EVT_TEXT_ENTER, self.OnTypeBB)
"""
self.bbSetGlobalBtt1 = wx.Button(self.pnlLeft, label="Global")
self.bbSetMapExtendBtt1 = wx.Button(self.pnlLeft, label="Map extent")
self.bbSetGlobalBtt1.Bind(wx.EVT_BUTTON, self.setBBoxGlobal)
self.bbSetMapExtendBtt1.Bind(wx.EVT_BUTTON, self.setBBoxGRASS)
self.setBBoxGlobal()
# -----right panel
self.bttAddWms = wx.Button(self.pnlRight, label="Add WMS")
self.bttAddWfs = wx.Button(self.pnlRight, label="Add WFS")
self.bttAddWcs = wx.Button(self.pnlRight, label="Add WCS")
self.bttAddWms.Bind(wx.EVT_BUTTON, self.OnService, self.bttAddWms)
self.bttAddWfs.Bind(wx.EVT_BUTTON, self.OnService, self.bttAddWfs)
self.bttAddWcs.Bind(wx.EVT_BUTTON, self.OnService, self.bttAddWcs)
self.bttViewRequestXml = wx.Button(self.pnlRight, label="Request XML")
self.bttViewResponseXml = wx.Button(self.pnlRight, label="Response XML")
self.bttViewRequestXml.Bind(wx.EVT_BUTTON, self.OnShowReguest)
self.bttViewResponseXml.Bind(wx.EVT_BUTTON, self.OnShowResponse)
self.htmlView = html.HtmlWindow(
self.pnlRight,
id=-1,
pos=wx.DefaultPosition,
size=wx.DefaultSize,
style=HW_DEFAULT_STYLE | HW_SCROLLBAR_AUTO,
name="metadata",
)
self.htmlView.SetBorders(5)
self.htmlView.Bind(EVT_HTML_LINK_CLICKED, self.onHtmlLinkClicked)
# self.htmlView=wx.html2.WebView.New(self.pnlRight, not supported in wx 2.8.12.1
self.refreshNavigationButt()
self._layout()
def OnKeywordDialog(self, evt):
if self.advanceChck.GetValue():
self.geDialog = wx.Dialog(
self,
id=wx.ID_ANY,
title="constraints builder",
style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER,
size=wx.DefaultSize,
pos=wx.DefaultPosition,
)
self.geDialog.SetSize((500, 500))
self.constrPnl = ConstraintsBuilder(self.geDialog, self.constString)
self.constrPnl.applyBtt.Bind(wx.EVT_BUTTON, self.OnSetAdvancedConstraints)
self.constrPnl.cancelBtt.Bind(wx.EVT_BUTTON, self._destroyDialog)
dbSizer = wx.BoxSizer(wx.VERTICAL)
dbSizer.Add(self.constrPnl, flag=wx.EXPAND)
self.geDialog.SetSizerAndFit(dbSizer)
self.geDialog.ShowModal()
def _destroyDialog(self, evt):
self.advanceChck.SetValue(False)
self.geDialog.Destroy()
def check_char_pair(self, s, start_char="[", end_char="]", same_char=False):
"""Check char pair count in the string
param s: string
start_char: start char
param end_char: end char
param same_char: bool
:return: bool (count of start char is equal or not equal to
the count of end char)
"""
a = 0
for i in range(len(s)):
if s[i] == start_char:
a += 1
elif s[i] == end_char:
a -= 1
if not same_char:
return a == 0
else:
return True if a % 2 == 0 else False
def OnSetAdvancedConstraints(self, evt):
error_message = "Constraints syntax error"
self.constString = self.constrPnl.constrCtrl.GetValue()
if self.constString == "" or not (
self.constString.startswith("[") and self.constString.endswith("]")
):
GMessage(error_message)
return
if not self.check_char_pair(self.constString):
GMessage(error_message)
return
if not self.check_char_pair(
self.constString, start_char="'", end_char="'", same_char=True
):
GMessage(error_message)
return
self.constraintsAdvanced = True
self.geDialog.Destroy()
def addKeyWidget(self, evt):
name = evt.GetEventObject().GetLabel()
if name == "+":
kw = wx.TextCtrl(self.pnlLeft)
addKeywordCtr = wx.Button(self.pnlLeft, -1, "-", size=ADD_RM_BUTTON_SIZE)
addKeywordCtr.Bind(wx.EVT_BUTTON, self.addKeyWidget)
self.constraintsWidgets.append(kw)
self.constraintsWidgets1.append(addKeywordCtr)
self.leftSearchSizer.Add(kw, 0, wx.EXPAND)
self.rightSearchSizer.Add(addKeywordCtr, 0)
else:
sizeLeft = len(self.leftSearchSizer.GetChildren())
sizeRight = len(self.rightSearchSizer.GetChildren())
self.constraintsWidgets.pop().Destroy()
self.constraintsWidgets1.pop().Destroy()
self.leftSearchSizer.Remove(sizeLeft)
self.rightSearchSizer.Remove(sizeRight)
self.Fit()
def OnShowReguest(self, evt):
# request_html = encodeString(highlight_xml(self.context, self.catalog.request,False))
path = os.path.join(tempfile.gettempdir(), "htmlRequest.xml")
request = gs.decode(self.catalog.request)
if os.path.exists(path):
os.remove(path)
f = open(path, "w")
f.writelines(request)
f.close()
if yesNo(self, "Do you want to open <request> in default browser", "Open file"):
open_url(path)
else:
self.htmlView.SetPage((renderXML(self.context, request)))
def OnShowResponse(self, evt):
# response_html = encodeString(highlight_xml(self.context, self.catalog.response,False))
path = os.path.join(tempfile.gettempdir(), "htmlResponse.xml")
response = gs.decode(self.catalog.response)
if os.path.exists(path):
os.remove(path)
f = open(path, "w")
f.write(response)
f.close()
if yesNo(
self, "Do you want to open <response> in default browser", "Open file"
):
open_url(path)
else:
self.htmlView.SetPage((renderXML(self.context, response)))
def getTmpConnection(self, name):
key = "/connections/%s/url" % name
return self.config.Read(key)
def OnRecord(self, evt):
"""show record metadata"""
self.refreshServiceButt()
curr = 0
idx = self.resultList.GetNextItem(
curr, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED
)
if not idx:
return
if self.outpoutschema == "gmd":
startfr = self.startfrom + idx + 1
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout)
try:
cat.getrecords2(
constraints=self.constraints,
maxrecords=1,
startposition=startfr,
outputschema="http://www.isotc211.org/2005/gmd",
)
except ExceptionReport as err:
GWarning("Error getting response: %s" % err)
return
except KeyError as err:
GWarning("Record parsing error, unable to locate record identifier")
return
if self.catalog:
md = list(cat.records.values())[0]
path = "record_metadata_gmd.html"
metadata = render_template("en", self.context, md, path)
self.htmlView.SetPage(metadata)
else:
identifier = self.get_item_data(idx, "identifier")
cat = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout)
try:
cat.getrecordbyid([self.catalog.records[identifier].identifier])
except ExceptionReport as err:
GWarning("Error getting response: %s" % err)
return
except KeyError as err:
GWarning("Record parsing error, unable to locate record identifier")
return
record = cat.records[identifier]
record.xml_url = cat.request
if self.catalog:
path = "record_metadata_dc.html"
metadata = render_template("en", self.context, record, path)
try:
record = self.catalog.records[identifier]
except KeyError as err:
GWarning("@!Record parsing error, unable to locate record identifier")
return
self.htmlView.SetPage(metadata)
self.findServices(record, idx)
def findServices(self, record, item):
"""scan record for WMS/WMTS|WFS|WCS endpoints"""
links = record.uris + record.references
WMSWMST_LINK_TYPES = [
"OGC:WMS",
"OGC:WMTS",
"OGC:WMS-1.1.1-http-get-map",
"OGC:WMS-1.1.1-http-get-capabilities",
"OGC:WMS-1.3.0-http-get-map",
"OGC:WMS-1.3.0-http-get-capabilities",
"urn:x-esri:specification:ServiceType:wms:url",
"urn:x-esri:specification:ServiceType:Gmd:URL.wms",
]
WFS_LINK_TYPES = [
"OGC:WFS",
"OGC:WFS-1.0.0-http-get-capabilities",
"OGC:WFS-1.1.0-http-get-capabilities",
"urn:x-esri:specification:ServiceType:wfs:url",
"urn:x-esri:specification:ServiceType:Gmd:URL.wfs",
]
WCS_LINK_TYPES = [
"OGC:WCS",
"OGC:WCS-1.1.0-http-get-capabilities",
"urn:x-esri:specification:ServiceType:wcs:url",
"urn:x-esri:specification:ServiceType:Gmd:URL.wcs",
]
services = {}
for link in links:
if "scheme" in link:
link_type = link["scheme"]
elif "protocol" in link:
link_type = link["protocol"]
else:
link_type = None
if link_type is not None:
link_type = link_type.upper()
wmswmst_link_types = list(map(str.upper, WMSWMST_LINK_TYPES))
wfs_link_types = list(map(str.upper, WFS_LINK_TYPES))
wcs_link_types = list(map(str.upper, WCS_LINK_TYPES))
# if the link type exists, and it is one of the acceptable
# interactive link types, then set
if all(
[
link_type is not None,
link_type in wmswmst_link_types + wfs_link_types + wcs_link_types,
]
):
if link_type in wmswmst_link_types:
services["wms"] = link["url"]
self.bttAddWms.Enable()
if link_type in wfs_link_types:
services["wfs"] = link["url"]
self.bttAddWfs.Enable()
if link_type in wcs_link_types:
services["wcs"] = link["url"]
self.bttAddWcs.Enable()
self.set_item_data(item, "link", json.dumps(services))
def refreshNavigationButt(self, stat=False):
if stat:
self.bttNextItem1.Enable()
self.bttNextItem2.Enable()
self.bttBackItem1.Enable()
self.bttBackItem2.Enable()
self.bttViewRequestXml.Enable()
self.bttViewResponseXml.Enable()
else:
self.bttNextItem1.Disable()
self.bttNextItem2.Disable()
self.bttBackItem1.Disable()
self.bttBackItem2.Disable()
self.bttAddWms.Disable()
self.bttAddWfs.Disable()
self.bttAddWcs.Disable()
self.bttViewRequestXml.Disable()
self.bttViewResponseXml.Disable()
def refreshServiceButt(self, stat=False):
if not stat:
self.bttAddWms.Disable()
self.bttAddWfs.Disable()
self.bttAddWcs.Disable()
else:
self.bttAddWms.Enable()
self.bttAddWfs.Enable()
self.bttAddWcs.Enable()
def OnNavigate(self, evt):
name = evt.GetEventObject().GetLabel()
if name == "<<":
self.startfrom = 0
elif name == ">>":
self.startfrom = self.catalog.results["matches"] - self.maxrecords
elif name == ">":
self.startfrom += self.maxrecords
if self.startfrom >= self.catalog.results["matches"]:
msg = "End of results. Go to start?"
if yesNo(self, msg, "End of results"):
self.startfrom = 0
else:
return
elif name == "<":
self.startfrom -= self.maxrecords
if self.startfrom < 0:
msg = "Start of results. Go to end?"
if yesNo(self, msg, "Navigation"):
self.startfrom = self.catalog.results["matches"] - self.maxrecords
else:
return
self.loadConstraints()
try:
if self.outpoutschema == "gmd":
self.catalog.getrecords2(
constraints=self.constraints,
maxrecords=self.maxrecords,
esn="full",
startposition=self.startfrom,
outputschema="http://www.isotc211.org/2005/gmd",
)
else:
self.catalog.getrecords2(
constraints=self.constraints,
maxrecords=self.maxrecords,
startposition=self.startfrom,
esn="full",
)
except ExceptionReport as err:
GWarning("Search error: %s" % err)
return
except Exception as err:
GWarning("Connection error: %s" % err)
return
if self.outpoutschema == "gmd":
self.displyResultsGMD()
return
self.displyResults()
def OnSetCatalog(self, evt):
val = evt.GetSelection()
self.config.WriteInt("/guiSearch/catalog", val)
def loadSettings(self):
n = self.config.ReadInt("/guiSearch/catalog", 0)
self.catalogCmb.SetSelection(n)
def setTooltip(self, evt):
index = evt.GetIndex()
if index > -1:
text = self.resultList.GetItem(index, 1)
text = text.GetText()
self.resultList.SetToolTip(wx.ToolTip(text))
def setBBoxGRASS(self, evt=None):
vdb = Module("g.region", flags="bg", stdout_=PIPE)
vdb = parse_key_val(vdb.outputs.stdout)
self.bbNorth.SetValue(vdb["ll_n"])
self.bbSouth.SetValue(vdb["ll_s"])
self.bbWest.SetValue(vdb["ll_w"])
self.bbEast.SetValue(vdb["ll_e"])
def setBBoxGlobal(self, evt=None):
self.bbNorth.SetValue("90")
self.bbSouth.SetValue("-90")
self.bbWest.SetValue("-180")
self.bbEast.SetValue("180")
def _get_csw(self):
"""function to init owslib.csw.CatalogueServiceWeb"""
# connect to the server
try:
self.catalog = CatalogueServiceWeb(self.catalog_url, timeout=self.timeout)
return True
except ExceptionReport as err:
msg = "Error connecting to service: %s" % err
except ValueError as err:
msg = "Value Error: %s" % err
except Exception as err:
msg = "Unknown Error: %s" % err
GMessage("CSW Connection error: %s" % msg)
return False
def refreshResultList(self):
self.resultList.ClearAll()
self.resultList.InsertColumn(0, "Type", width=wx.LIST_AUTOSIZE)
self.resultList.InsertColumn(1, "Service", width=300)
def loadConstraints(self):
if not self.advanceChck.GetValue():
self.constraints = []
kw = self.keywordCtr.GetValue()
if kw:
self.constraints.append(PropertyIsLike("csw:AnyText", kw))
for constraint in self.constraintsWidgets:
kw = constraint.GetValue()
if kw != "":
self.constraints.append(PropertyIsLike("csw:AnyText", kw))
if len(self.constraints) > 1: # exclusive search (a && b)
self.constraints = [self.constraints]
else:
# convert constreints to owslib object fes
tmpConstraints = self.constraints
for x, constraint in enumerate(tmpConstraints):
if isinstance(constraint, list):
for y, const in enumerate(constraint):
self.constraints[x][y] = PropertyIsLike("csw:AnyText", const)
continue
self.constraints[x] = PropertyIsLike("csw:AnyText", constraint)
def _openWebServiceDiag(self, data_url):
from web_services.dialogs import AddWSDialog
if self.giface:
self.WSDialog = AddWSDialog(self.parent, giface=self.giface)
self.WSDialog.OnSettingsChanged([data_url, "", ""])
self.WSDialog.Show()
else:
GMessage(_("No access to layer tree. Run g.gui.cswbroswer from g.gui"))
def OnService(self, evt):
def strip_str(s):
return s.strip().replace('"', "").replace("'", "")
name = evt.GetEventObject().GetLabel()
idx = self.resultList.GetNextItem(0, wx.LIST_NEXT_ALL, wx.LIST_STATE_SELECTED)
if not idx:
return
cmd = []
# Strip { }
data = self.get_item_data(idx, "link")[1:-1]
item_data = dict(
(strip_str(k), strip_str(v))
for k, v in (item.split(":", 1) for item in data.split(", "))
)
if name == "Add WMS":
data_url = item_data["wms"]
service = ["r.in.wms", "Add web service layer"]
dlg = wx.SingleChoiceDialog(
self,
"Choice of module for WMS ",
"Web Service selection",
service,
wx.CHOICEDLG_STYLE,
)
if dlg.ShowModal() == wx.ID_OK:
selected_module = dlg.GetStringSelection()
if selected_module == service[0]:
cmd.append("r.in.wms")
cmd.append("url=%s" % data_url)
GUI(parent=self, show=True, modal=True).ParseCommand(cmd=cmd)
elif selected_module == service[1]:
self._openWebServiceDiag(data_url)
dlg.Destroy()
elif name == "Add WFS":
data_url = item_data["wfs"]
service = ["v.in.wfs", "Add web service layer"]
dlg = wx.SingleChoiceDialog(
self,
"Choice of module for WFS ",
"Web Service selection",
service,
wx.CHOICEDLG_STYLE,
)
if dlg.ShowModal() == wx.ID_OK:
selected_module = dlg.GetStringSelection()
if selected_module == service[0]:
cmd.append("v.in.wfs")
cmd.append("url=%s" % data_url)
GUI(parent=self, show=True, modal=True).ParseCommand(cmd=cmd)
elif selected_module == service[1]:
self._openWebServiceDiag(data_url)
dlg.Destroy()
elif name == "Add WCS":
GMessage("Not implemented")
def GetQtype(self): # TODO need to implement
val = self.qtypeCb.GetValue()
if val == "All":
return None
else:
return val
def OnSearch(self, evt):
"""execute search"""
wx.BeginBusyCursor()
self.refreshResultList()
self.catalog = None
self.loadConstraints()
self.catalog_url = None
# clear all fields and disable buttons
self.findResNumLbl.SetLabel("")
current_text = self.catalogCmb.GetValue()
if current_text == "":
GMessage("Please set catalog")
wx.EndBusyCursor()
return
self.catalog_url = self.getTmpConnection(current_text)
# start position and number of records to return
self.startfrom = 0
self.maxrecords = self.numResultsSpin.GetValue()
# set timeout
self.timeout = self.parent.connectionPanel.timeoutSpin.GetValue()
# bbox
minx = self.bbWest.GetValue() # TODO add grass number validator
miny = self.bbSouth.GetValue()
maxx = self.bbEast.GetValue()
maxy = self.bbNorth.GetValue()
bbox = [minx, miny, maxx, maxy]
if bbox != ["-180", "-90", "180", "90"]:
self.constraints.append(BBox(bbox))
if not self._get_csw():
wx.EndBusyCursor()
return
# TODO: allow users to select resources types
# to find ('service', 'dataset', etc.)
try:
self.catalog.getrecords2(
constraints=self.constraints, maxrecords=self.maxrecords, esn="full"
)
self.outpoutschema = "dc"
except ExceptionReport as err:
GError("Search error: %s" % err)
wx.EndBusyCursor()
return
except Exception as err:
GError("Connection error: %s" % err)
wx.EndBusyCursor()
return
###work around for GMD records
if len(self.catalog.records) == 0 and self.catalog.results["matches"] != 0:
try:
self.catalog.getrecords2(
constraints=self.constraints,
maxrecords=self.maxrecords,
esn="full",
outputschema="http://www.isotc211.org/2005/gmd",
)
self.outpoutschema = "gmd"
if self.warns:
GWarning(
"Endopoint of service is not setup properly. Server returns ISO metadata(https://www.isotc211.org/2005/gmd) instead of CSW records(https://schemas.opengis.net/csw/2.0.2/record.xsd). CSW browser may work incorrectly."
)
self.warns = False
except ExceptionReport as err:
GError("Search error: %s" % err)
wx.EndBusyCursor()
return
except Exception as err:
GError("Connection error: %s" % err)
wx.EndBusyCursor()
return
###work around for GMD records- END
if self.catalog.results["matches"] == 0:
self.findResNumLbl.SetLabel("0 results")
self.refreshNavigationButt(True)
return
self.refreshNavigationButt(True)
# parsing for another html template(GMD)
if self.outpoutschema == "gmd":
self.displyResultsGMD()
return
self.displyResults()
wx.EndBusyCursor()
def get_item_data(self, index, field):
if field == "identifier":
return self.idResults[index]["identifier"]
else:
return self.idResults[index]["link"]
def set_item_data(self, index, field, value):
"""set identifier"""
try:
self.idResults[index]
idx = True
except IndexError:
idx = False
if idx:
if field == "identifier":
self.idResults[index]["identifier"] = value
if field == "link":
self.idResults[index]["link"] = value
else:
d = {}
if field == "identifier":
d["identifier"] = value
if field == "link":
d[index]["link"] = value
self.idResults.insert(index, d)
def displyResultsGMD(self):
self.refreshResultList()
position = self.catalog.results["returned"] + self.startfrom
msg = "Showing %s - %s of %s result(s)" % (
self.startfrom + 1,
position,
self.catalog.results["matches"],
)
self.findResNumLbl.SetLabel(msg)
index = 0
for rec in self.catalog.records:
if self.catalog.records[rec].identification.identtype:
item = wx.ListItem()
self.resultList.InsertStringItem(
index,
normalize_text(self.catalog.records[rec].identification.identtype),
)
else:
self.resultList.SetItem(index, 0, "unknown")
if self.catalog.records[rec].identification.title:
self.resultList.SetItem(
index,
1,
normalize_text(self.catalog.records[rec].identification.title),
)
if self.catalog.records[rec].identification.title:
self.set_item_data(
index, "identifier", self.catalog.records[rec].identification.title
)
if index % 2:
self.resultList.SetItemBackgroundColour(index, "LIGHT GREY")
index += 1
self.Fit()
def displyResults(self):
"""display search results"""
self.refreshResultList()
position = self.catalog.results["returned"] + self.startfrom
msg = "Showing %s - %s of %s result(s)" % (
self.startfrom + 1,
position,
self.catalog.results["matches"],
)
self.findResNumLbl.SetLabel(msg)
index = 0
for rec in self.catalog.records:
if self.catalog.records[rec].type:
item = wx.ListItem()
self.resultList.InsertItem(
index, normalize_text(self.catalog.records[rec].type)
)
else:
self.resultList.InsertItem(index, "unknown")
if self.catalog.records[rec].title:
self.resultList.SetItem(
index, 1, normalize_text(self.catalog.records[rec].title)
)
if self.catalog.records[rec].identifier:
self.set_item_data(
index, "identifier", self.catalog.records[rec].identifier
)
if index % 2:
self.resultList.SetItemBackgroundColour(index, "LIGHT GREY")
index += 1
self.Fit()
def onHtmlLinkClicked(self, event):
Thread(target=webbrowser.open, args=(event.GetLinkInfo().GetHref(), 0)).start()
def updateCatalogBox(self, evt=None):
if self.catalogCmb.GetCount() == 0:
msg = "No services/connections defined."
self.htmlView.SetPage("<p><h3>%s</h3></p>" % msg)
def _layout(self):
self.mainsizer = wx.BoxSizer(wx.HORIZONTAL)
rightPanelSizer = wx.BoxSizer(wx.VERTICAL)
leftPanelSizer = wx.BoxSizer(wx.VERTICAL)
# search static box sizers---start
mainSearchSizer = wx.BoxSizer(wx.HORIZONTAL)
self.rightSearchSizer = wx.BoxSizer(wx.VERTICAL)
self.leftSearchSizer = wx.BoxSizer(wx.VERTICAL)
upSearchSizer = wx.BoxSizer(wx.HORIZONTAL)
upSearchSizer.Add(self.qtypeCb, 1, wx.EXPAND)
self.leftSearchSizer.Add(upSearchSizer, 1, wx.EXPAND)
self.rightSearchSizer.Add(wx.StaticText(self.pnlLeft), 0)
mainSearchSizer.Add(self.leftSearchSizer, wx.EXPAND)
mainSearchSizer.Add(self.rightSearchSizer)
self.leftSearchSizer.Add(self.keywordLbl, 0)
self.rightSearchSizer.Add(4, 0, 1, wx.EXPAND)
self.rightSearchSizer.Add(self.advanceChck, 0)
self.leftSearchSizer.Add(self.keywordCtr, 0, wx.EXPAND)
self.rightSearchSizer.Add(self.addKeywordCtr, 0)
stbBox0Sizer = wx.StaticBoxSizer(self.stbSearch, wx.VERTICAL)
self.stBox1Sizer = wx.StaticBoxSizer(self.stbFind, wx.VERTICAL)
self.stBox1Sizer.Add(mainSearchSizer, 0, wx.EXPAND)
# bounding box sizer---------------
bb1sizer = wx.BoxSizer(wx.HORIZONTAL)
bb2sizer = wx.BoxSizer(wx.HORIZONTAL)
bb3sizer = wx.BoxSizer(wx.HORIZONTAL)