forked from warrieka/geopunt4Qgis
/
geopunt4qgisParcel.py
370 lines (317 loc) · 16.8 KB
/
geopunt4qgisParcel.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
geopunt4qgisdialog
A QGIS plugin
"Tool om geopunt in QGIS te gebruiken"
-------------------
begin : 2014-11-08
copyright : (C) 2014 by Kay Warrie
email : kaywarrie@gmail.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 import QtCore, QtGui
from ui_geopunt4qgisParcel import Ui_geopunt4QgisParcelDlg
from qgis.core import *
from qgis.gui import QgsMessageBar, QgsVertexMarker, QgsRubberBand
import geopunt, os, json, webbrowser
from geometryhelper import geometryHelper
from parcelHelper import parcelHelper
class geopunt4QgisParcelDlg(QtGui.QDialog):
def __init__(self, iface):
QtGui.QDialog.__init__(self, None)
self.setWindowFlags( self.windowFlags() & ~QtCore.Qt.WindowContextHelpButtonHint )
#self.setWindowFlags( self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint)
self.iface = iface
# initialize locale
locale = QtCore.QSettings().value("locale/userLocale", "nl")
if not locale: locale == 'nl'
else: locale = locale[0:2]
localePath = os.path.join(os.path.dirname(__file__), 'i18n', 'geopunt4qgis_{}.qm'.format(locale))
if os.path.exists(localePath):
self.translator = QtCore.QTranslator()
self.translator.load(localePath)
if QtCore.qVersion() > '4.3.3': QtCore.QCoreApplication.installTranslator(self.translator)
self._initGui()
def _initGui(self):
"""setup the user interface"""
self.ui = Ui_geopunt4QgisParcelDlg()
self.ui.setupUi(self)
self.ui.buttonBox.addButton( QtGui.QPushButton("Sluiten"), QtGui.QDialogButtonBox.RejectRole )
for btn in self.ui.buttonBox.buttons():
btn.setAutoDefault(0)
#get settings
self.s = QtCore.QSettings()
self.loadSettings()
#setup geometryHelper object
self.gh = geometryHelper(self.iface)
self.ph = parcelHelper(self.iface)
#variables
self.firstShow = True
self.municipalities = []
self.departments = []
self.sections = []
self.parcels = []
self.graphics = []
#setup a message bar
self.bar = QgsMessageBar()
self.bar.setSizePolicy( QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Fixed )
self.ui.verticalLayout.addWidget(self.bar)
#event handlers
self.ui.municipalityCbx.currentIndexChanged.connect(self.municipalityChanged)
self.ui.departmentCbx.currentIndexChanged.connect( self.departmentChanged )
self.ui.sectionCbx.currentIndexChanged.connect(self.sectionChanged)
self.ui.parcelCbx.currentIndexChanged.connect(self.parcelChanged)
self.ui.ZoomKnop_muni.clicked.connect(self.zoomTo)
self.ui.ZoomKnop_dep.clicked.connect(self.zoomTo)
self.ui.ZoomKnop_sect.clicked.connect(self.zoomTo)
self.ui.ZoomKnop_parcel.clicked.connect(self.zoomTo)
self.ui.buttonBox.helpRequested.connect(self.openHelp)
self.ui.saveBtn.clicked.connect(self.saveParcel)
self.finished.connect( self.clean)
def loadSettings(self):
self.saveToFile = int( self.s.value("geopunt4qgis/parcelSavetoFile" , 1))
layerName = self.s.value("geopunt4qgis/parcelLayerText", "")
if layerName :
self.layerName= layerName
self.timeout = int( self.s.value("geopunt4qgis/timeout" ,15))
if int( self.s.value("geopunt4qgis/useProxy" , 0)):
self.proxy = self.s.value("geopunt4qgis/proxyHost" ,"")
self.port = self.s.value("geopunt4qgis/proxyPort" ,"")
else:
self.proxy = ""
self.port = ""
self.startDir = self.s.value("geopunt4qgis/startDir", os.path.dirname(__file__))
self.parcel = geopunt.parcel(self.timeout, self.proxy, self.port )
def show(self):
QtGui.QDialog.show(self)
if self.firstShow:
inet = geopunt.internet_on( proxyUrl=self.proxy, port=self.port, timeout=self.timeout )
if inet:
self.firstShow = False
self.municipalities = self.parcel.getMunicipalities()
self.ui.municipalityCbx.clear()
muniNames = [n['municipalityName'] for n in self.municipalities]
self.ui.municipalityCbx.addItems( [''] + muniNames )
self.setCompleter( muniNames, self.ui.municipalityCbx )
else:
self.bar.pushMessage(
QtCore.QCoreApplication.translate("geopunt4QgisParcelDlg", "Waarschuwing "),
QtCore.QCoreApplication.translate("geopunt4QgisParcelDlg",
"Kan geen verbing maken met het internet."), level=QgsMessageBar.WARNING, duration=5)
def saveParcel(self):
if not self.layernameValid(): return
municipality= self.ui.municipalityCbx.currentText()
department = self.ui.departmentCbx.currentText()
niscodes = [n['municipalityCode'] for n in self.municipalities if n['municipalityName'] == municipality ]
niscode = (niscodes[0] if len(niscodes) else '')
departmentcodes = [n['departmentCode'] for n in self.departments if n['departmentName'] == department ]
departmentcode = (departmentcodes[0] if len( departmentcodes) else '')
section = self.ui.sectionCbx.currentText()
parcelNr = self.ui.parcelCbx.currentText()
if '' in (niscode, departmentcode, section, parcelNr): return
try:
if municipality != '' or department != '' or section != '' or parcelNr != '':
parcelInfo = self.parcel.getParcel( niscode, departmentcode, section, parcelNr, 31370, 'full')
shape = json.loads( parcelInfo['geometry']['shape'])
pts = [n.asPolygon() for n in self.PolygonsFromJson( shape )]
mPolygon = QgsGeometry.fromMultiPolygon( pts )
self.ph.save_parcel_polygon(mPolygon, parcelInfo, self.layerName, self.saveToFile,
self, os.path.join(self.startDir, self.layerName))
except geopunt.geopuntError as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.WARNING, duration=5)
return
except Exception as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.CRITICAL)
return
self.accept()
def municipalityChanged(self):
municipality= self.ui.municipalityCbx.currentText()
if municipality == '': return
niscodes = [n['municipalityCode'] for n in self.municipalities if n['municipalityName'] == municipality ]
niscode = (niscodes[0] if len(niscodes) else '')
if niscode == '': return
try:
self.departments = self.parcel.getDepartments(niscode)
except geopunt.geopuntError as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.WARNING, duration=5)
return
except Exception as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.CRITICAL)
return
self.ui.departmentCbx.setEnabled(1)
self.ui.departmentCbx.clear()
depNames = [n['departmentName'] for n in self.departments]
self.ui.departmentCbx.addItems([''] + depNames )
self.setCompleter( depNames, self.ui.departmentCbx)
self.ui.sectionCbx.setEnabled(0)
self.ui.parcelCbx.setEnabled(0)
self.ui.saveBtn.setEnabled(0)
def departmentChanged(self):
department = self.ui.departmentCbx.currentText()
municipality= self.ui.municipalityCbx.currentText()
if municipality == '' or department == '': return
niscodes = [n['municipalityCode'] for n in self.municipalities if n['municipalityName'] == municipality ]
niscode = (niscodes[0] if len(niscodes) else '')
departmentcodes = [n['departmentCode'] for n in self.departments if n['departmentName'] == department ]
departmentcode = (departmentcodes[0] if len( departmentcodes) else '')
if niscode == '' or departmentcode == '': return
try:
self.sections = [n['sectionCode'] for n in self.parcel.getSections(niscode, departmentcode)]
except geopunt.geopuntError as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.WARNING, duration=5)
return
except Exception as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.CRITICAL)
return
self.ui.sectionCbx.clear()
self.ui.sectionCbx.setEnabled(1)
self.ui.sectionCbx.addItems([''] + self.sections )
self.setCompleter( self.sections, self.ui.sectionCbx )
self.ui.parcelCbx.setEnabled(0)
self.ui.saveBtn.setEnabled(0)
def sectionChanged(self):
municipality= self.ui.municipalityCbx.currentText()
department = self.ui.departmentCbx.currentText()
section = self.ui.sectionCbx.currentText()
if municipality == '' or department == '' or section == '': return
niscodes = [n['municipalityCode'] for n in self.municipalities if n['municipalityName'] == municipality ]
niscode = (niscodes[0] if len(niscodes) else '')
departmentcodes = [n['departmentCode'] for n in self.departments if n['departmentName'] == department ]
departmentcode = (departmentcodes[0] if len( departmentcodes) else '')
if niscode == '' or departmentcode == '': return
try:
self.parcels = self.parcel.getParcels( niscode, departmentcode, section )
except geopunt.geopuntError as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.WARNING, duration=5)
return
except Exception as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.CRITICAL)
return
self.ui.parcelCbx.clear()
self.ui.parcelCbx.setEnabled(1)
parcelNrs = [n['perceelnummer'] for n in self.parcels]
self.ui.parcelCbx.addItems([''] + parcelNrs)
self.setCompleter( parcelNrs, self.ui.parcelCbx )
self.ui.saveBtn.setEnabled(0)
def parcelChanged(self):
self.ui.saveBtn.setEnabled( self.ui.parcelCbx.currentText() != '' )
def zoomTo(self):
sender = self.sender()
municipality= self.ui.municipalityCbx.currentText()
department = self.ui.departmentCbx.currentText()
niscodes = [n['municipalityCode'] for n in self.municipalities if n['municipalityName'] == municipality ]
niscode = (niscodes[0] if len(niscodes) else '')
departmentcodes = [n['departmentCode'] for n in self.departments if n['departmentName'] == department ]
departmentcode = (departmentcodes[0] if len( departmentcodes) else '')
section = self.ui.sectionCbx.currentText()
parcelNr = self.ui.parcelCbx.currentText()
try:
if sender is self.ui.ZoomKnop_muni and municipality != '':
muniInfo = self.parcel.getMunicipalitieInfo( niscode, 31370, 'full')
if muniInfo == []: return
bbox= json.loads( muniInfo['geometry']['boundingBox'])['coordinates'][0]
self.clearGraphics()
self.gh.zoomtoRec( bbox[0], bbox[2], 31370 )
shape = json.loads( muniInfo['geometry']['shape'])
for n in self.PolygonsFromJson( shape ): self.addGraphic(n)
return
if sender is self.ui.ZoomKnop_dep and municipality != '' and department != '':
depInfo = self.parcel.getDepartmentInfo( niscode, departmentcode, 31370, 'full')
if depInfo == []: return
bbox= json.loads( depInfo['geometry']['boundingBox'])['coordinates'][0]
self.clearGraphics()
self.gh.zoomtoRec( bbox[0], bbox[2], 31370 )
shape = json.loads( depInfo['geometry']['shape'])
for n in self.PolygonsFromJson( shape ): self.addGraphic(n)
return
if sender is self.ui.ZoomKnop_sect and municipality != '' and department != '' and section != '':
sectInfo = self.parcel.getSectionInfo( niscode, departmentcode, section, 31370, 'full')
if sectInfo == []: return
bbox= json.loads( sectInfo['geometry']['boundingBox'])['coordinates'][0]
self.clearGraphics()
self.gh.zoomtoRec( bbox[0], bbox[2], 31370 )
shape = json.loads( sectInfo['geometry']['shape'])
for n in self.PolygonsFromJson( shape ): self.addGraphic(n)
return
if sender is self.ui.ZoomKnop_parcel and municipality != '' and department != '' and section != '' and parcelNr != '':
parcelInfo = self.parcel.getParcel( niscode, departmentcode, section, parcelNr, 31370, 'full')
if parcelInfo == []: return
bbox= json.loads( parcelInfo['geometry']['boundingBox'])['coordinates'][0]
self.clearGraphics()
self.gh.zoomtoRec( bbox[0], bbox[2], 31370 )
shape = json.loads( parcelInfo['geometry']['shape'])
for n in self.PolygonsFromJson( shape ): self.addGraphic(n)
return
except geopunt.geopuntError as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.WARNING, duration=5)
return
except Exception as e:
self.bar.pushMessage("Error", str( e.message) , level=QgsMessageBar.CRITICAL)
return
def openHelp(self):
webbrowser.open_new_tab("http://www.geopunt.be/voor-experts/geopunt-plug-ins/functionaliteiten/zoek-een-perceel")
def layernameValid(self):
if not hasattr(self, 'layerName'):
layerName, accept = QtGui.QInputDialog.getText(None,
QtCore.QCoreApplication.translate("geopunt4Qgis", 'Laag toevoegen'),
QtCore.QCoreApplication.translate("geopunt4Qgis", 'Geef een naam voor de laag op:') )
if accept == False:
return False
else:
self.layerName = layerName
return True
def PolygonsFromJson(self, geojson ):
geoType= geojson['type']
Polygons = []
if geoType == "Polygon":
rings = geojson['coordinates']
mPolygon = [ rings ]
if geoType == "MultiPolygon":
mPolygon = geojson['coordinates']
print len( mPolygon)
for rings in mPolygon:
prjPolygon = []
for ring in rings:
prjRing = self.gh.prjLineToMapCrs( ring, 31370 )
prjPolygon.append( prjRing.asPolyline() )
gPolygon = QgsGeometry.fromPolygon( prjPolygon )
Polygons.append( gPolygon )
return Polygons
def addGraphic(self, geom ):
canvas = self.iface.mapCanvas()
rBand = QgsRubberBand(canvas, True)
self.graphics.append( rBand )
rBand.setToGeometry( geom, None )
rBand.setColor(QtGui.QColor(0,0,255, 70))
if QGis.QGIS_VERSION_INT >= 20600:
rBand.setBorderColor( QtGui.QColor(0,0,250, 220) )
rBand.setWidth(3)
def clearGraphics(self):
canvas = self.iface.mapCanvas()
for g in self.graphics:
canvas.scene().removeItem(g)
def setCompleter(self, values, wgt):
completer = QtGui.QCompleter( self )
completerModel = QtGui.QStringListModel( self )
wgt.setCompleter( completer )
completer.setModel( completerModel )
completer.setCaseSensitivity(False)
completerModel.setStringList( values )
def clean(self):
self.clearGraphics()
self.ui.municipalityCbx.setCurrentIndex(0)
self.ui.departmentCbx.clear()
self.ui.sectionCbx.clear()
self.ui.parcelCbx.clear()
self.ui.departmentCbx.setDisabled(1)
self.ui.sectionCbx.setDisabled(1)
self.ui.parcelCbx.setDisabled(1)