forked from domlysz/BlenderGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefs.py
executable file
·852 lines (693 loc) · 27.4 KB
/
prefs.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
import json
import logging
log = logging.getLogger(__name__)
import sys, os
import bpy
from bpy.props import StringProperty, IntProperty, FloatProperty, BoolProperty, EnumProperty, FloatVectorProperty
from bpy.types import Operator, Panel, AddonPreferences
import addon_utils
from . import bl_info
from .core.proj.reproj import EPSGIO
from .core.proj.srs import SRS
from .core.checkdeps import HAS_GDAL, HAS_PYPROJ, HAS_PIL, HAS_IMGIO
from .core import settings
PKG = __package__
def getAppData():
home = os.path.expanduser('~')
loc = os.path.join(home, '.bgis')
if not os.path.exists(loc):
os.mkdir(loc)
return loc
APP_DATA = getAppData()
'''
Default Enum properties contents (list of tuple (value, label, tootip))
Theses properties are automatically filled from a serialized json string stored in a StringProperty
This is workaround to have an editable EnumProperty (ie the user can add, remove or edit an entry)
because the Blender Python API does not provides built in functions for these tasks.
To edit the content of these enum, we just need to write new operators which will simply update the json string
As the json backend is stored in addon preferences, the property will be saved and restored for the next blender session
'''
DEFAULT_CRS = [
('EPSG:3857', 'Web Mercator', 'Worldwide projection, high distortions, not suitable for precision modelling'),
('EPSG:4326', 'WGS84 latlon', 'Longitude and latitude in degrees, DO NOT USE AS SCENE CRS (this system is defined only for reprojection tasks')
]
DEFAULT_DEM_SERVER = [
("https://portal.opentopography.org/API/globaldem?demtype=SRTMGL1&west={W}&east={E}&south={S}&north={N}&outputFormat=GTiff", 'OpenTopography SRTM 30m', 'OpenTopography.org web service for SRTM 30m global DEM'),
("https://portal.opentopography.org/API/globaldem?demtype=SRTMGL3&west={W}&east={E}&south={S}&north={N}&outputFormat=GTiff", 'OpenTopography SRTM 90m', 'OpenTopography.org web service for SRTM 90m global DEM'),
("http://www.gmrt.org/services/GridServer?west={W}&east={E}&south={S}&north={N}&layer=topo&format=geotiff&resolution=high", 'Marine-geo.org GMRT', 'Marine-geo.org web service for GMRT global DEM (terrestrial (ASTER) and bathymetry)')
]
DEFAULT_OVERPASS_SERVER = [
("https://lz4.overpass-api.de/api/interpreter", 'overpass-api.de', 'Main Overpass API instance'),
("http://overpass.openstreetmap.fr/api/interpreter", 'overpass.openstreetmap.fr', 'French Overpass API instance'),
("https://overpass.kumi.systems/api/interpreter", 'overpass.kumi.systems', 'Kumi Systems Overpass Instance')
]
#default filter tags for OSM import
DEFAULT_OSM_TAGS = [
'building',
'highway',
'landuse',
'leisure',
'natural',
'railway',
'waterway'
]
class BGIS_OT_pref_show(Operator):
bl_idname = "bgis.pref_show"
bl_description = 'Display BlenderGIS addons preferences'
bl_label = "Preferences"
bl_options = {'INTERNAL'}
def execute(self, context):
addon_utils.modules_refresh()
context.preferences.active_section = 'ADDONS'
bpy.data.window_managers["WinMan"].addon_search = bl_info['name']
#bpy.ops.wm.addon_expand(module=PKG)
mod = addon_utils.addons_fake_modules.get(PKG)
mod.bl_info['show_expanded'] = True
bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
return {'FINISHED'}
class BGIS_PREFS(AddonPreferences):
bl_idname = PKG
################
#Predefined Spatial Ref. Systems
def listPredefCRS(self, context):
return [tuple(elem) for elem in json.loads(self.predefCrsJson)]
#store crs preset as json string into addon preferences
predefCrsJson: StringProperty(default=json.dumps(DEFAULT_CRS))
predefCrs: EnumProperty(
name = "Predefinate CRS",
description = "Choose predefinite Coordinate Reference System",
#default = 1, #possible only since Blender 2.90
items = listPredefCRS
)
################
#proj engine
def getProjEngineItems(self, context):
items = [ ('AUTO', 'Auto detect', 'Auto select the best library for reprojection tasks') ]
if HAS_GDAL:
items.append( ('GDAL', 'GDAL', 'Force GDAL as reprojection engine') )
if HAS_PYPROJ:
items.append( ('PYPROJ', 'pyProj', 'Force pyProj as reprojection engine') )
#if EPSGIO.ping(): #too slow
# items.append( ('EPSGIO', 'epsg.io', '') )
items.append( ('EPSGIO', 'epsg.io', 'Force epsg.io as reprojection engine') )
items.append( ('BUILTIN', 'Built in', 'Force reprojection through built in Python functions') )
return items
def updateProjEngine(self, context):
settings.proj_engine = self.projEngine
projEngine: EnumProperty(
name = "Projection engine",
description = "Select projection engine",
items = getProjEngineItems,
update = updateProjEngine
)
################
#img engine
def getImgEngineItems(self, context):
items = [ ('AUTO', 'Auto detect', 'Auto select the best imaging library') ]
if HAS_GDAL:
items.append( ('GDAL', 'GDAL', 'Force GDAL as image processing engine') )
if HAS_IMGIO:
items.append( ('IMGIO', 'ImageIO', 'Force ImageIO as image processing engine') )
if HAS_PIL:
items.append( ('PIL', 'PIL', 'Force PIL as image processing engine') )
return items
def updateImgEngine(self, context):
settings.img_engine = self.imgEngine
imgEngine: EnumProperty(
name = "Image processing engine",
description = "Select image processing engine",
items = getImgEngineItems,
update = updateImgEngine
)
################
#OSM
osmTagsJson: StringProperty(default=json.dumps(DEFAULT_OSM_TAGS)) #just a serialized list of tags
def listOsmTags(self, context):
prefs = context.preferences.addons[PKG].preferences
tags = json.loads(prefs.osmTagsJson)
#put each item in a tuple (key, label, tooltip)
return [ (tag, tag, tag) for tag in tags]
osmTags: EnumProperty(
name = "OSM tags",
description = "List of registered OSM tags",
items = listOsmTags
)
################
#Basemaps
def getCacheFolder(self):
return bpy.path.abspath(self.get("cacheFolder", ''))
def setCacheFolder(self, value):
if os.access(value, os.X_OK | os.W_OK):
self["cacheFolder"] = value
else:
log.error("The selected cache folder has no write access")
self["cacheFolder"] = "The selected folder has no write access"
cacheFolder: StringProperty(
name = "Cache folder",
default = APP_DATA, #Does not works !?
description = "Define a folder where to store Geopackage SQlite db",
subtype = 'DIR_PATH',
get = getCacheFolder,
set = setCacheFolder
)
synchOrj: BoolProperty(
name="Synch. lat/long",
description='Keep geo origin synchronized with crs origin. Can be slow with remote reprojection services',
default=True)
zoomToMouse: BoolProperty(name="Zoom to mouse", description='Zoom towards the mouse pointer position', default=True)
lockOrigin: BoolProperty(name="Lock origin", description='Do not move scene origin when panning map', default=False)
lockObj: BoolProperty(name="Lock objects", description='Retain objects geolocation when moving map origin', default=True)
resamplAlg: EnumProperty(
name = "Resampling method",
description = "Choose GDAL's resampling method used for reprojection",
items = [ ('NN', 'Nearest Neighboor', ''), ('BL', 'Bilinear', ''), ('CB', 'Cubic', ''), ('CBS', 'Cubic Spline', ''), ('LCZ', 'Lanczos', '') ]
)
################
#Network
def listOverpassServer(self, context):
return [tuple(entry) for entry in json.loads(self.overpassServerJson)]
#store crs preset as json string into addon preferences
overpassServerJson: StringProperty(default=json.dumps(DEFAULT_OVERPASS_SERVER))
overpassServer: EnumProperty(
name = "Overpass server",
description = "Select an overpass server",
#default = 0,
items = listOverpassServer
)
def listDemServer(self, context):
return [tuple(entry) for entry in json.loads(self.demServerJson)]
#store crs preset as json string into addon preferences
demServerJson: StringProperty(default=json.dumps(DEFAULT_DEM_SERVER))
demServer: EnumProperty(
name = "Elevation server",
description = "Select a server that provides Digital Elevation Model datasource",
#default = 0,
items = listDemServer
)
################
#IO options
mergeDoubles: BoolProperty(
name = "Merge duplicate vertices",
description = 'Merge shared vertices between features when importing vector data',
default = False)
adjust3Dview: BoolProperty(
name = "Adjust 3D view",
description = "Update 3d view grid size and clip distances according to the new imported object's size",
default = True)
forceTexturedSolid: BoolProperty(
name = "Force textured solid shading",
description = "Update shading mode to display raster's texture",
default = True)
################
#System
def updateLogLevel(self, context):
logger = logging.getLogger(PKG)
logger.setLevel(logging.getLevelName(self.logLevel))
logLevel: EnumProperty(
name = "Logging level",
description = "Select the logging level",
items = [('DEBUG', 'Debug', ''), ('INFO', 'Info', ''), ('WARNING', 'Warning', ''), ('ERROR', 'Error', ''), ('CRITICAL', 'Critical', '')],
update = updateLogLevel,
default = 'DEBUG'
)
################
def draw(self, context):
layout = self.layout
#SRS
box = layout.box()
box.label(text='Spatial Reference Systems')
row = box.row().split(factor=0.5)
row.prop(self, "predefCrs", text='')
row.operator("bgis.add_predef_crs", icon='ADD')
row.operator("bgis.edit_predef_crs", icon='PREFERENCES')
row.operator("bgis.rmv_predef_crs", icon='REMOVE')
row.operator("bgis.reset_predef_crs", icon='PLAY_REVERSE')
#Basemaps
box = layout.box()
box.label(text='Basemaps')
box.prop(self, "cacheFolder")
row = box.row()
row.prop(self, "zoomToMouse")
row.prop(self, "lockObj")
row.prop(self, "lockOrigin")
row.prop(self, "synchOrj")
row = box.row()
row.prop(self, "resamplAlg")
#IO
box = layout.box()
box.label(text='Import/Export')
row = box.row().split(factor=0.5)
split = row.split(factor=0.9, align=True)
split.prop(self, "osmTags")
split.operator("wm.url_open", icon='INFO').url = "http://wiki.openstreetmap.org/wiki/Map_Features"
row.operator("bgis.add_osm_tag", icon='ADD')
row.operator("bgis.edit_osm_tag", icon='PREFERENCES')
row.operator("bgis.rmv_osm_tag", icon='REMOVE')
row.operator("bgis.reset_osm_tags", icon='PLAY_REVERSE')
row = box.row()
row.prop(self, "mergeDoubles")
row.prop(self, "adjust3Dview")
row.prop(self, "forceTexturedSolid")
#Network
box = layout.box()
box.label(text='Remote datasource')
row = box.row().split(factor=0.5)
row.prop(self, "overpassServer")
row.operator("bgis.add_overpass_server", icon='ADD')
row.operator("bgis.edit_overpass_server", icon='PREFERENCES')
row.operator("bgis.rmv_overpass_server", icon='REMOVE')
row.operator("bgis.reset_overpass_server", icon='PLAY_REVERSE')
row = box.row().split(factor=0.5)
row.prop(self, "demServer")
row.operator("bgis.add_dem_server", icon='ADD')
row.operator("bgis.edit_dem_server", icon='PREFERENCES')
row.operator("bgis.rmv_dem_server", icon='REMOVE')
row.operator("bgis.reset_dem_server", icon='PLAY_REVERSE')
#System
box = layout.box()
box.label(text='System')
box.prop(self, "projEngine")
box.prop(self, "imgEngine")
box.prop(self, "logLevel")
#######################
class PredefCRS():
'''
Collection of utility methods (callable at class level) to deal with predefined CRS dictionary
Can be used by others operators that need to fill their own crs enum
'''
@staticmethod
def getData():
'''Load the json string'''
prefs = bpy.context.preferences.addons[PKG].preferences
return json.loads(prefs.predefCrsJson)
@classmethod
def getName(cls, key):
'''Return the convenient name of a given srid or None if this crs does not exist in the list'''
data = cls.getData()
try:
return [entry[1] for entry in data if entry[0] == key][0]
except IndexError:
return None
@classmethod
def getEnumItems(cls):
'''Return a list of predefined crs usable to fill a bpy EnumProperty'''
return [tuple(entry) for entry in cls.getData()]
#################
# Collection of operators to manage predefined CRS
class BGIS_OT_add_predef_crs(Operator):
bl_idname = "bgis.add_predef_crs"
bl_description = 'Add predefinate CRS'
bl_label = "Add"
bl_options = {'INTERNAL'}
crs: StringProperty(name = "Definition", description = "Specify EPSG code or Proj4 string definition for this CRS")
name: StringProperty(name = "Description", description = "Choose a convenient name for this CRS")
desc: StringProperty(name = "Description", description = "Add a description or comment about this CRS")
def check(self, context):
return True
def search(self, context):
if not EPSGIO.ping():
self.report({'ERROR'}, "Cannot request epsg.io website")
else:
results = EPSGIO.search(self.query)
self.results = json.dumps(results)
if results:
self.crs = 'EPSG:' + results[0]['code']
self.name = results[0]['name']
def updEnum(self, context):
crsItems = []
if self.results != '':
for result in json.loads(self.results):
srid = 'EPSG:' + result['code']
crsItems.append( (result['code'], result['name'], srid) )
return crsItems
def fill(self, context):
if self.results != '':
crs = [crs for crs in json.loads(self.results) if crs['code'] == self.crsEnum][0]
self.crs = 'EPSG:' + crs['code']
self.desc = crs['name']
query: StringProperty(name='Query', description='Hit enter to process the search', update=search)
results: StringProperty()
crsEnum: EnumProperty(name='Results', description='Select the desired CRS', items=updEnum, update=fill)
search: BoolProperty(name='Search', description='Search for coordinate system into EPSG database', default=False)
save: BoolProperty(name='Save to addon preferences', description='Save Blender user settings after the addition', default=False)
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)#, width=300)
def draw(self, context):
layout = self.layout
layout.prop(self, 'search')
if self.search:
layout.prop(self, 'query')
layout.prop(self, 'crsEnum')
layout.separator()
layout.prop(self, 'crs')
layout.prop(self, 'name')
layout.prop(self, 'desc')
#layout.prop(self, 'save') #sincce Blender2.8 prefs are autosaved
def execute(self, context):
if not SRS.validate(self.crs):
self.report({'ERROR'}, 'Invalid CRS')
if self.crs.isdigit():
self.crs = 'EPSG:' + self.crs
#append the new crs def to json string
prefs = context.preferences.addons[PKG].preferences
data = json.loads(prefs.predefCrsJson)
data.append((self.crs, self.name, self.desc))
prefs.predefCrsJson = json.dumps(data)
#change enum index to new added crs and redraw
#prefs.predefCrs = self.crs
context.area.tag_redraw()
#end
if self.save:
bpy.ops.wm.save_userpref()
return {'FINISHED'}
class BGIS_OT_rmv_predef_crs(Operator):
bl_idname = "bgis.rmv_predef_crs"
bl_description = 'Remove predefinate CRS'
bl_label = "Remove"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
key = prefs.predefCrs
if key != '':
data = json.loads(prefs.predefCrsJson)
data = [e for e in data if e[0] != key]
prefs.predefCrsJson = json.dumps(data)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_reset_predef_crs(Operator):
bl_idname = "bgis.reset_predef_crs"
bl_description = 'Reset predefinate CRS'
bl_label = "Reset"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
prefs.predefCrsJson = json.dumps(DEFAULT_CRS)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_edit_predef_crs(Operator):
bl_idname = "bgis.edit_predef_crs"
bl_description = 'Edit predefinate CRS'
bl_label = "Edit"
bl_options = {'INTERNAL'}
crs: StringProperty(name = "EPSG code or Proj4 string", description = "Specify EPSG code or Proj4 string definition for this CRS")
name: StringProperty(name = "Description", description = "Choose a convenient name for this CRS")
desc: StringProperty(name = "Name", description = "Add a description or comment about this CRS")
def invoke(self, context, event):
prefs = context.preferences.addons[PKG].preferences
key = prefs.predefCrs
if key == '':
return {'CANCELLED'}
data = json.loads(prefs.predefCrsJson)
entry = [entry for entry in data if entry[0] == key][0]
self.crs, self.name, self.desc = entry
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
key = prefs.predefCrs
data = json.loads(prefs.predefCrsJson)
if SRS.validate(self.crs):
data = [entry for entry in data if entry[0] != key] #deleting
data.append((self.crs, self.name, self.desc))
prefs.predefCrsJson = json.dumps(data)
context.area.tag_redraw()
else:
self.report({'ERROR'}, 'Invalid CRS')
return {'FINISHED'}
#################
# Collection of operators to manage predefinates OSM Tags
class BGIS_OT_add_osm_tag(Operator):
bl_idname = "bgis.add_osm_tag"
bl_description = 'Add new predefinate OSM filter tag'
bl_label = "Add"
bl_options = {'INTERNAL'}
tag: StringProperty(name = "Tag", description = "Specify the tag (examples : 'building', 'landuse=forest' ...)")
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)#, width=300)
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
tags = json.loads(prefs.osmTagsJson)
tags.append(self.tag)
prefs.osmTagsJson = json.dumps(tags)
prefs.osmTags = self.tag #update current idx
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_rmv_osm_tag(Operator):
bl_idname = "bgis.rmv_osm_tag"
bl_description = 'Remove predefinate OSM filter tag'
bl_label = "Remove"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
tag = prefs.osmTags
if tag != '':
tags = json.loads(prefs.osmTagsJson)
del tags[tags.index(tag)]
prefs.osmTagsJson = json.dumps(tags)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_reset_osm_tags(Operator):
bl_idname = "bgis.reset_osm_tags"
bl_description = 'Reset predefinate OSM filter tag'
bl_label = "Reset"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
prefs.osmTagsJson = json.dumps(DEFAULT_OSM_TAGS)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_edit_osm_tag(Operator):
bl_idname = "bgis.edit_osm_tag"
bl_description = 'Edit predefinate OSM filter tag'
bl_label = "Edit"
bl_options = {'INTERNAL'}
tag: StringProperty(name = "Tag", description = "Specify the tag (examples : 'building', 'landuse=forest' ...)")
def invoke(self, context, event):
prefs = context.preferences.addons[PKG].preferences
self.tag = prefs.osmTags
if self.tag == '':
return {'CANCELLED'}
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
tag = prefs.osmTags
tags = json.loads(prefs.osmTagsJson)
del tags[tags.index(tag)]
tags.append(self.tag)
prefs.osmTagsJson = json.dumps(tags)
prefs.osmTags = self.tag #update current idx
context.area.tag_redraw()
return {'FINISHED'}
#################
# Collection of operators to manage DEM server urls
class BGIS_OT_add_dem_server(Operator):
bl_idname = "bgis.add_dem_server"
bl_description = 'Add new topography web service'
bl_label = "Add"
bl_options = {'INTERNAL'}
url: StringProperty(name = "Url template", description = "Define url template string. Bounding box varaibles are {W}, {E}, {S} and {N}")
name: StringProperty(name = "Description", description = "Choose a convenient name for this server")
desc: StringProperty(name = "Description", description = "Add a description or comment about this remote datasource")
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)#, width=300)
def execute(self, context):
templates = ['{W}', '{E}', '{S}', '{N}']
if all([t in self.url for t in templates]):
prefs = context.preferences.addons[PKG].preferences
data = json.loads(prefs.demServerJson)
data.append( (self.url, self.name, self.desc) )
prefs.demServerJson = json.dumps(data)
context.area.tag_redraw()
else:
self.report({'ERROR'}, 'Invalid URL')
return {'FINISHED'}
class BGIS_OT_rmv_dem_server(Operator):
bl_idname = "bgis.rmv_dem_server"
bl_description = 'Remove a given topography web service'
bl_label = "Remove"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
key = prefs.demServer
if key != '':
data = json.loads(prefs.demServerJson)
data = [e for e in data if e[0] != key]
prefs.demServerJson = json.dumps(data)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_reset_dem_server(Operator):
bl_idname = "bgis.reset_dem_server"
bl_description = 'Reset default topographic web server'
bl_label = "Reset"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
prefs.demServerJson = json.dumps(DEFAULT_DEM_SERVER)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_edit_dem_server(Operator):
bl_idname = "bgis.edit_dem_server"
bl_description = 'Edit a topographic web server'
bl_label = "Edit"
bl_options = {'INTERNAL'}
url: StringProperty(name = "Url template", description = "Define url template string. Bounding box varaibles are {W}, {E}, {S} and {N}")
name: StringProperty(name = "Description", description = "Choose a convenient name for this server")
desc: StringProperty(name = "Description", description = "Add a description or comment about this remote datasource")
def invoke(self, context, event):
prefs = context.preferences.addons[PKG].preferences
key = prefs.demServer
if key == '':
return {'CANCELLED'}
data = json.loads(prefs.demServerJson)
entry = [entry for entry in data if entry[0] == key][0]
self.url, self.name, self.desc = entry
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
key = prefs.demServer
data = json.loads(prefs.demServerJson)
templates = ['{W}', '{E}', '{S}', '{N}']
if all([t in self.url for t in templates]):
data = [entry for entry in data if entry[0] != key] #deleting
data.append((self.url, self.name, self.desc))
prefs.demServerJson = json.dumps(data)
context.area.tag_redraw()
else:
self.report({'ERROR'}, 'Invalid URL')
return {'FINISHED'}
#################
class EditEnum():
'''
Helper to deal with an enum property that use a serialized json backend
Can be used by others operators to edit and EnumProperty
WORK IN PROGRESS
'''
def __init__(self, enumName):
self.prefs = bpy.context.preferences.addons[PKG].preferences
self.enumName = enumName
self.jsonName = enumName + 'Json'
def getData(self):
'''Load the json string'''
data = json.loads(getattr(self.prefs, self.jsonName))
return [tuple(entry) for entry in data]
def append(self, value, label, tooltip, check=lambda x: True):
if not check(value):
return
data = self.getData()
data.append((value, label, tooltip))
setattr(self.prefs, self.jsonName, json.dumps(data))
def remove(self, key):
if key != '':
data = self.getData()
data = [e for e in data if e[0] != key]
setattr(self.prefs, self.jsonName, json.dumps(data))
def edit(self, key, value, label, tooltip):
self.remove(key)
self.append(value, label, tooltip)
def reset(self):
setattr(self.prefs, self.jsonName, json.dumps(DEFAULT_OVERPASS_SERVER))
#################
# Collection of operators to manage Overpass server urls
class BGIS_OT_add_overpass_server(Operator):
bl_idname = "bgis.add_overpass_server"
bl_description = 'Add new OSM overpass server url'
bl_label = "Add"
bl_options = {'INTERNAL'}
url: StringProperty(name = "Url template", description = "Define the url end point of the overpass server")
name: StringProperty(name = "Description", description = "Choose a convenient name for this server")
desc: StringProperty(name = "Description", description = "Add a description or comment about this remote server")
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)#, width=300)
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
data = json.loads(prefs.overpassServerJson)
data.append( (self.url, self.name, self.desc) )
prefs.overpassServerJson = json.dumps(data)
#EditEnum('overpassServer').append(self.url, self.name, self.desc, check=lambda url: url.startswith('http'))
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_rmv_overpass_server(Operator):
bl_idname = "bgis.rmv_overpass_server"
bl_description = 'Remove a given overpass server'
bl_label = "Remove"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
key = prefs.overpassServer
if key != '':
data = json.loads(prefs.overpassServerJson)
data = [e for e in data if e[0] != key]
prefs.overpassServerJson = json.dumps(data)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_reset_overpass_server(Operator):
bl_idname = "bgis.reset_overpass_server"
bl_description = 'Reset default overpass server'
bl_label = "Rest"
bl_options = {'INTERNAL'}
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
prefs.overpassServerJson = json.dumps(DEFAULT_OVERPASS_SERVER)
context.area.tag_redraw()
return {'FINISHED'}
class BGIS_OT_edit_overpass_server(Operator):
bl_idname = "bgis.edit_overpass_server"
bl_description = 'Edit an overpass server url'
bl_label = "Edit"
bl_options = {'INTERNAL'}
url: StringProperty(name = "Url template", description = "Define the url end point of the overpass server")
name: StringProperty(name = "Description", description = "Choose a convenient name for this server")
desc: StringProperty(name = "Description", description = "Add a description or comment about this remote server")
def invoke(self, context, event):
prefs = context.preferences.addons[PKG].preferences
key = prefs.overpassServer
if key == '':
return {'CANCELLED'}
data = json.loads(prefs.overpassServerJson)
entry = [entry for entry in data if entry[0] == key][0]
self.url, self.name, self.desc = entry
return context.window_manager.invoke_props_dialog(self)
def execute(self, context):
prefs = context.preferences.addons[PKG].preferences
key = prefs.overpassServer
data = json.loads(prefs.overpassServerJson)
data = [entry for entry in data if entry[0] != key] #deleting
data.append((self.url, self.name, self.desc))
prefs.overpassServerJson = json.dumps(data)
context.area.tag_redraw()
return {'FINISHED'}
classes = [
BGIS_OT_pref_show,
BGIS_PREFS,
BGIS_OT_add_predef_crs,
BGIS_OT_rmv_predef_crs,
BGIS_OT_reset_predef_crs,
BGIS_OT_edit_predef_crs,
BGIS_OT_add_osm_tag,
BGIS_OT_rmv_osm_tag,
BGIS_OT_reset_osm_tags,
BGIS_OT_edit_osm_tag,
BGIS_OT_add_dem_server,
BGIS_OT_rmv_dem_server,
BGIS_OT_reset_dem_server,
BGIS_OT_edit_dem_server,
BGIS_OT_add_overpass_server,
BGIS_OT_rmv_overpass_server,
BGIS_OT_reset_overpass_server,
BGIS_OT_edit_overpass_server
]
def register():
for cls in classes:
try:
bpy.utils.register_class(cls)
except ValueError as e:
#log.error('Cannot register {}'.format(cls), exc_info=True)
log.warning('{} is already registered, now unregister and retry... '.format(cls))
bpy.utils.unregister_class(cls)
bpy.utils.register_class(cls)
# set default cache folder
prefs = bpy.context.preferences.addons[PKG].preferences
if prefs.cacheFolder == '':
prefs.cacheFolder = APP_DATA
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)