forked from 3liz/QgisCadastrePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cadastre_menu.py
467 lines (395 loc) · 18.8 KB
/
cadastre_menu.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Cadastre - QGIS plugin menu class
A QGIS plugin
This plugins helps users to import the french land registry ('cadastre')
into a database. It is meant to ease the use of the data in QGIs
by providing search tools and appropriate layer symbology.
-------------------
begin : 2013-06-11
copyright : (C) 2013 by 3liz
email : info@3liz.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtXml import QDomDocument
from qgis.core import *
from cadastre_identify_parcelle import IdentifyParcelle
from cadastre_dialogs import cadastre_common, cadastre_search_dialog, cadastre_import_dialog, cadastre_load_dialog, cadastre_option_dialog, cadastre_about_dialog, cadastre_parcelle_dialog, cadastre_message_dialog
import ConfigParser
import os.path
import tempfile
from time import time
# ---------------------------------------------
class cadastre_menu:
def __init__(self, iface):
self.iface = iface
self.mapCanvas = iface.mapCanvas()
self.cadastre_search_dialog = None
def cadastre_add_submenu(self, submenu):
self.iface.addPluginToMenu("&Cadastre", submenu.menuAction())
def initGui(self):
# Import Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/database.png")
self.import_action = QAction(icon, u"Importer des données", self.iface.mainWindow())
QObject.connect(self.import_action, SIGNAL("triggered()"), self.open_import_dialog)
# Search Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/search.png")
self.search_action = QAction(icon, u"Outils de recherche", self.iface.mainWindow())
QObject.connect(self.search_action, SIGNAL("triggered()"), self.toggle_search_dialog)
if not self.cadastre_search_dialog:
dialog = cadastre_search_dialog(self.iface)
self.cadastre_search_dialog = dialog
# Load Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/output.png")
self.load_action = QAction(icon, u"Charger des données", self.iface.mainWindow())
QObject.connect(self.load_action, SIGNAL("triggered()"), self.open_load_dialog)
# Composer Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/mActionSaveAsPDF.png")
self.export_action = QAction(icon, u"Exporter la vue", self.iface.mainWindow())
QObject.connect(self.export_action, SIGNAL("triggered()"), self.export_view)
# Options Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/config.png")
self.option_action = QAction(icon, u"Configurer le plugin", self.iface.mainWindow())
QObject.connect(self.option_action, SIGNAL("triggered()"), self.open_option_dialog)
# About Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/about.png")
self.about_action = QAction(icon, u"À propos", self.iface.mainWindow())
QObject.connect(self.about_action, SIGNAL("triggered()"), self.open_about_dialog)
# Help Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/about.png")
self.help_action = QAction(icon, u"Aide", self.iface.mainWindow())
QObject.connect(self.help_action, SIGNAL("triggered()"), self.open_help)
# version Submenu
icon = QIcon(os.path.dirname(__file__) + "/icons/about.png")
self.version_action = QAction(icon, u"Notes de version", self.iface.mainWindow())
QObject.connect(self.version_action, SIGNAL("triggered()"), self.open_message_dialog)
# Add Cadastre to Extension menu
self.iface.addPluginToMenu(u"&Cadastre", self.import_action)
self.iface.addPluginToMenu(u"&Cadastre", self.load_action)
self.iface.addPluginToMenu(u"&Cadastre", self.search_action)
self.iface.addPluginToMenu(u"&Cadastre", self.export_action)
self.iface.addPluginToMenu(u"&Cadastre", self.option_action)
self.iface.addPluginToMenu(u"&Cadastre", self.about_action)
self.iface.addPluginToMenu(u"&Cadastre", self.version_action)
self.iface.addPluginToMenu(u"&Cadastre", self.help_action)
# Add cadastre toolbar
self.toolbar = self.iface.addToolBar(u'&Cadastre');
# open import dialog
self.openImportAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/database.png"),
u"Importer des données",
self.iface.mainWindow()
)
self.openImportAction.triggered.connect(self.open_import_dialog)
self.toolbar.addAction(self.openImportAction)
self.toolbar.setObjectName("cadastreToolbar");
# open load dialog
self.openLoadAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/output.png"),
u"Charger des données",
self.iface.mainWindow()
)
self.openLoadAction.triggered.connect(self.open_load_dialog)
self.toolbar.addAction(self.openLoadAction)
# open search dialog
self.openSearchAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/search.png"),
u"Outils de recherche",
self.iface.mainWindow()
)
self.openSearchAction.triggered.connect(self.toggle_search_dialog)
#~ self.openSearchAction.setCheckable(True)
self.toolbar.addAction(self.openSearchAction)
# export composer
self.runExportAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/mActionSaveAsPDF.png"),
u"Exporter la vue",
self.iface.mainWindow()
)
self.runExportAction.triggered.connect(self.export_view)
self.toolbar.addAction(self.runExportAction)
# open Option dialog
self.openOptionAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/config.png"),
u"Configurer le plugin",
self.iface.mainWindow()
)
self.openOptionAction.triggered.connect(self.open_option_dialog)
self.toolbar.addAction(self.openOptionAction)
# open About dialog
self.openAboutAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/about.png"),
u"À propos",
self.iface.mainWindow()
)
self.openAboutAction.triggered.connect(self.open_about_dialog)
self.toolbar.addAction(self.openAboutAction)
# Create action for "Parcelle information"
self.identifyParcelleAction = QAction(
QIcon(os.path.dirname(__file__) +"/icons/toolbar/get-parcelle-info.png"),
"Infos parcelle",
self.iface.mainWindow()
)
self.identifyParcelleAction.setCheckable(True)
self.identifyParcelleAction.triggered.connect( self.setIndentifyParcelleTool )
self.toolbar.addAction( self.identifyParcelleAction )
self.setActionsExclusive()
# Display About window on first use
s = QSettings()
firstUse = s.value("cadastre/isFirstUse" , 1, type=int)
if firstUse == 1:
s.setValue("cadastre/isFirstUse", 0)
self.open_about_dialog()
# Display some messages depending on version number
mConfig = ConfigParser.ConfigParser()
metadataFile = os.path.dirname(__file__) + "/metadata.txt"
mConfig.read( metadataFile )
self.mConfig = mConfig
myVersion = mConfig.get('general', 'version').replace('.', '_')
myVersionMsg = s.value("cadastre/version_%s" % myVersion , 1, type=int)
if myVersionMsg == 1:
s.setValue("cadastre/version_%s" % myVersion , 0)
self.open_message_dialog()
# Project load or create : refresh search and identify tool
self.iface.projectRead.connect(self.onProjectRead)
self.iface.newProjectCreated.connect(self.onNewProjectCreated)
# Delete layers from table when deleted from registry
lr = QgsMapLayerRegistry.instance()
lr.layersRemoved.connect( self.checkIdentifyParcelleTool )
def open_import_dialog(self):
'''
Import dialog
'''
dialog = cadastre_import_dialog(self.iface)
dialog.exec_()
def open_load_dialog(self):
'''
Load dialog
'''
dialog = cadastre_load_dialog(
self.iface,
self.cadastre_search_dialog
)
dialog.exec_()
def toggle_search_dialog(self):
'''
Search dock widget
'''
if self.cadastre_search_dialog.isVisible():
self.cadastre_search_dialog.hide()
else:
self.cadastre_search_dialog.show()
def export_view(self):
'''
Export current view to PDF
'''
# Load template from file
s = QSettings()
f = s.value("cadastre/composerTemplateFile", '', type=str)
if not os.path.exists(f):
f = '%s/composers/paysage_a4.qpt' % os.path.dirname(__file__)
s.setValue("cadastre/composerTemplateFile", f)
QApplication.setOverrideCursor(Qt.WaitCursor)
tf = file(f, 'rt')
tc= tf.read()
tf.close()
d= QDomDocument()
d.setContent(tc)
# Create composition
ms = self.iface.mapCanvas().mapSettings()
c = QgsComposition(ms)
c.loadFromTemplate(d)
c.setPlotStyle(QgsComposition.Print)
#c.setPrintResolution(300)
# Set map
canvas = self.iface.mapCanvas()
cm = c.getComposerMapById(0)
extent = canvas.extent()
scale = canvas.scale()
cm.setMapCanvas(canvas)
if extent:
cm.zoomToExtent(extent)
if scale:
cm.setNewScale(scale)
# Export
tempDir = s.value("cadastre/tempDir", '%s' % tempfile.gettempdir(), type=str)
self.targetDir = tempfile.mkdtemp('', 'cad_export_', tempDir)
temp = int(time()*100)
temppath = os.path.join(tempDir, 'export_cadastre_%s.pdf' % temp)
c.exportAsPDF(temppath)
QApplication.restoreOverrideCursor()
if os.path.exists(temppath):
cadastre_common.openFile(temppath)
def open_option_dialog(self):
'''
Config dialog
'''
dialog = cadastre_option_dialog(self.iface)
dialog.exec_()
def open_about_dialog(self):
'''
About dialog
'''
dialog = cadastre_about_dialog(self.iface)
dialog.exec_()
def setActionsExclusive(self):
# Build an action list from QGIS navigation toolbar
actionList = self.iface.mapNavToolToolBar().actions()
# Add actions from QGIS attributes toolbar (handling QWidgetActions)
tmpActionList = self.iface.attributesToolBar().actions()
for action in tmpActionList:
if isinstance(action, QWidgetAction):
actionList.extend( action.defaultWidget().actions() )
else:
actionList.append( action )
# ... add other toolbars' action lists...
# Build a group with actions from actionList and add your own action
group = QActionGroup( self.iface.mainWindow() )
group.setExclusive(True)
for action in actionList:
group.addAction( action )
group.addAction( self.identifyParcelleAction )
def checkIdentifyParcelleTool(self, layerIds=None):
'''
When layers are removed from the project
Check if the layer Parcelle remains
If not deactivate Identify parcelle tool
'''
# Find parcelle layer
parcelleLayer = None
try:
from cadastre_dialogs import cadastre_common
parcelleLayer = cadastre_common.getLayerFromLegendByTableProps('parcelle_info')
except:
parcelleLayer = None
if not parcelleLayer:
self.identifyParcelleAction.setChecked(False)
self.iface.actionPan().trigger()
return
def setIndentifyParcelleTool(self):
'''
Activite the identify tool
for the layer geo_parcelle
'''
# Find parcelle layer
parcelleLayer = cadastre_common.getLayerFromLegendByTableProps('parcelle_info')
if not parcelleLayer:
QMessageBox.warning(
self.cadastre_search_dialog,
u"Cadastre",
u"La couche de parcelles n'a pas été trouvée dans le projet"
)
self.identifyParcelleAction.setChecked(False)
self.iface.actionPan().trigger()
return
self.identyParcelleTool = IdentifyParcelle( self.mapCanvas, parcelleLayer )
self.identyParcelleTool.cadastreGeomIdentified.connect(self.getParcelleInfo)
# The activate identify tool
self.mapCanvas.setMapTool(self.identyParcelleTool)
def getParcelleInfo(self, layer, feature):
'''
Return information of the identified
parcelle
'''
parcelleDialog = cadastre_parcelle_dialog(
self.iface,
layer,
feature,
self.cadastre_search_dialog
)
parcelleDialog.show()
def onProjectRead(self):
'''
Refresh search dialog when new data has been loaded
'''
if self.cadastre_search_dialog:
self.cadastre_search_dialog.checkMajicContent()
self.cadastre_search_dialog.clearComboboxes()
self.cadastre_search_dialog.setupSearchCombobox('commune', None, 'sql')
self.cadastre_search_dialog.setupSearchCombobox('section', None, 'sql')
self.checkIdentifyParcelleTool()
def onNewProjectCreated(self):
'''
Refresh search dialog when new data has been loaded
'''
self.checkIdentifyParcelleTool()
if self.cadastre_search_dialog:
self.cadastre_search_dialog.checkMajicContent()
self.cadastre_search_dialog.clearComboboxes()
def open_help(self):
'''Opens the html help file content with default browser'''
#~ localHelpUrl = "https://github.com/3liz/QgisCadastrePlugin/blob/master/doc/index.rst"
localHelpUrl = os.path.dirname(__file__) + "/doc/index.html"
QDesktopServices.openUrl( QUrl(localHelpUrl) )
def open_message_dialog(self):
'''
Display a message to the user
'''
versionMessages = {
'1.1.0': [
[
u'Compatibilité avec QGIS 2.6',
u'La compatibilité n\'est pas assurée à 100 % avec la dernière version 2.6 de QGIS, notamment pour la création d\'une base Spatialite vide. Vous pouvez utiliser les outils de QGIS pour le faire.'
] ,
[
u'Lien entre les parcelles EDIGEO et MAJIC',
u'Pour cette nouvelle version du plugin, la structure de la base de données a été légèrement modifiée. Pour pouvoir utiliser les fonctions du plugin Cadastre, vous devez donc impérativement <b>réimporter les données dans une base vide</b>'
] ,
[
u'Validation des géométries',
u'Certaines données EDIGEO contiennent des géométries invalides (polygones croisés dit "papillons", polygones non fermés, etc.). Cette version utilise une fonction de PostGIS qui tente de corriger ces invalidités. Il faut impérativement <b>utiliser une version récente de PostGIS</b> : 2.0.4 minimum pour la version 2, ou les version ultérieures (2.1 par exemple)'
]
]
,
'1.4.0': [
[
u'Modification de la structure',
u'Pour cette nouvelle version 1.4.0 du plugin, la structure de la base de données a été légèrement modifiée par rapport à la 1.4.0. Pour pouvoir utiliser les fonctions du plugin Cadastre, vous devez donc impérativement <b>réimporter les données dans une base vide</b>. Les changements concernent les identifiants des tables parcelle, geo_parcelle, commune, local00, local10, pev, pevexoneration, pevtaxation, pevprincipale, pevprofessionnelle, pevdependances, ainsi que la création d\'une table parcelle_info pour consolider EDIGEO et MAJIC.'
]
]
}
mConfig = self.mConfig
version = mConfig.get('general', 'version')
changelog = mConfig.get('general', 'changelog')
message = '<h2>Version %s - notes concernant cette version</h2>' % version
if version in versionMessages:
message+='<ul>'
for item in versionMessages[version]:
message+='<li><b>%s</b> - %s</li>' % (item[0], item[1])
message+='</ul>'
message+= '<h3>Changelog</h3>'
message+= '<p>'
i = 0
for item in changelog.split('*'):
if i == 0:
message+= '<b>%s</b><ul>' % item.decode('utf-8')
else:
message+= '<li>%s</li>' % item.decode('utf-8')
i+=1
message+='</ul>'
message+= '</p>'
dialog = cadastre_message_dialog(self.iface, message)
dialog.exec_()
def unload(self):
self.iface.removePluginMenu(u"&Cadastre", self.import_action)
self.iface.removePluginMenu(u"&Cadastre", self.load_action)
self.iface.removePluginMenu(u"&Cadastre", self.search_action)
self.iface.removePluginMenu(u"&Cadastre", self.export_action)
self.iface.removePluginMenu(u"&Cadastre", self.option_action)
self.iface.removePluginMenu(u"&Cadastre", self.about_action)
self.iface.removePluginMenu(u"&Cadastre", self.version_action)
self.iface.removePluginMenu(u"&Cadastre", self.help_action)
self.iface.mainWindow().removeToolBar(self.toolbar)
if self.cadastre_search_dialog:
self.iface.removeDockWidget(self.cadastre_search_dialog)