-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathByggnad2Fastighet_v3_1.py
371 lines (314 loc) · 17.7 KB
/
Byggnad2Fastighet_v3_1.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
"""
Name : Byggnad2Fastighet_v3_1
With QGIS : 33602
Andrew Mercer, 10.06.2024
This model is designed for a specific use case and not for general usage.
Usage is free but almost certainly of limited use.
It requires vector layers (1 point and 2 polygon).
The model takes points representing builings, which are not alwyas located
within the actual building's geometry, and tries to match these to polygons
representing the same building and then transfers attributes from the points to
cadastral parcels under the assumption that the building polygon
is correctly placed.
"""
from qgis.core import QgsProcessing
from qgis.core import QgsProcessingAlgorithm
from qgis.core import QgsProcessingMultiStepFeedback
from qgis.core import QgsProcessingParameterVectorLayer
from qgis.core import QgsProcessingParameterFeatureSink
from qgis.core import QgsProcessingParameterField
from qgis.core import QgsProcessingUtils
import processing
from qgis.core import (
QgsFields,
QgsField,
QgsFeature,
QgsFeatureSink,
QgsProject,
NULL,
QgsPalLayerSettings,
QgsTextFormat,
QgsTextBackgroundSettings,
QgsVectorLayerSimpleLabeling
)
from qgis.PyQt.QtCore import (
QVariant
)
from PyQt5.QtGui import QFont, QColor
from qgis.utils import iface
class Byggfast(QgsProcessingAlgorithm):
def initAlgorithm(self, config=None):
layers_names = [layer.name() for layer in QgsProject.instance().mapLayers().values()]
byggPointLayer = None
byggFieldsDefault = None
byggPolygonLayer = None
fastPolygonLayer = None
fastFieldsDefault = None
for i in range(len(layers_names)):
if 'byggnader_sverige_point' in layers_names[i]:
byggPointLayer = layers_names[i]
byggFieldsDefault = ['id', 'anlaggning_id', 'fastighetsnyckel', 'fast_byg_uuid', 'byggnadsbeteckning', 'visningsurl']
if 'by_' in layers_names[i]:
byggPolygonLayer =layers_names[i]
if 'ay_' in layers_names[i]:
fastPolygonLayer =layers_names[i]
fastFieldsDefault = ['FNR_FDS', 'OBJEKT_ID', 'KOMMUNNAMN', 'FASTIGHET']
self.addParameter(QgsProcessingParameterVectorLayer('byggnaderpoints', 'ByggnaderPoints', types=[QgsProcessing.TypeVectorPoint], defaultValue=byggPointLayer))
self.addParameter(QgsProcessingParameterField('byggnadfields', 'ByggnadFields', type=QgsProcessingParameterField.Any, parentLayerParameterName='byggnadpoints', allowMultiple=True, defaultValue=byggFieldsDefault))
self.addParameter(QgsProcessingParameterVectorLayer('byggnaderpolygons', 'ByggnaderPolygons', types=[QgsProcessing.TypeVectorPolygon], defaultValue=byggPolygonLayer))
self.addParameter(QgsProcessingParameterVectorLayer('fastigheterpolygons', 'FastigheterPolygons', types=[QgsProcessing.TypeVectorPolygon], defaultValue=fastPolygonLayer))
self.addParameter(QgsProcessingParameterField('fastighetfields', 'FastighetFields', type=QgsProcessingParameterField.Any, parentLayerParameterName='fastigheterpolygon', allowMultiple=True, defaultValue=fastFieldsDefault))
#self.addParameter(QgsProcessingParameterFeatureSink('SnapGeometriesToLayer', 'SnapGeometriesToLayer', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
#self.addParameter(QgsProcessingParameterFeatureSink('Intersection', 'Intersection', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
#self.addParameter(QgsProcessingParameterFeatureSink('Centroids', 'Centroids', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
#self.addParameter(QgsProcessingParameterFeatureSink('Extractbylocation', 'ExtractByLocation', type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
self.addParameter(QgsProcessingParameterFeatureSink('BMFastighet', 'BM Fastigheter', optional=True, type=QgsProcessing.TypeVectorAnyGeometry, createByDefault=True, defaultValue=None))
def processAlgorithm(self, parameters, context, model_feedback):
# Use a multi-step feedback, so that individual child algorithm progress reports are adjusted for the
# overall progress through the model
feedback = QgsProcessingMultiStepFeedback(14, model_feedback)
results = {}
outputs = {}
###############################################################################################################
# Create spatial index
alg_params = {
'INPUT': parameters['byggnaderpoints']
}
outputs['CreateSpatialIndex'] = processing.run('native:createspatialindex', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(1)
if feedback.isCanceled():
return {}
# Create spatial index
alg_params = {
'INPUT': parameters['byggnaderpolygons']
}
outputs['CreateSpatialIndex'] = processing.run('native:createspatialindex', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(2)
if feedback.isCanceled():
return {}
# Create spatial index
alg_params = {
'INPUT': parameters['fastigheterpolygons']
}
outputs['CreateSpatialIndex'] = processing.run('native:createspatialindex', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(3)
if feedback.isCanceled():
return {}
###############################################################################################################
# Intersect - Which points are inside building polygons. Disjoint and Intersect return only trues so each must be run individually
alg_params = {
'INPUT': parameters['byggnaderpoints'],
'INTERSECT': parameters['byggnaderpolygons'],
'PREDICATE': [0], # intersect
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Intersect'] = processing.run('native:extractbylocation', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(4)
if feedback.isCanceled():
return {}
###################################################
# Disjoint - Which points are outside building polygons
alg_params = {
'INPUT': parameters['byggnaderpoints'],
'INTERSECT': parameters['byggnaderpolygons'],
'PREDICATE': [2], # disjoint
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Disjoint'] = processing.run('native:extractbylocation', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(5)
if feedback.isCanceled():
return {}
# Snap geometries to layer - points outside building polygons snap to nearest building polygon
alg_params = {
'BEHAVIOR': 3, # Prefer closest point, don't insert new vertices
'INPUT': outputs['Disjoint']['OUTPUT'], #parameters['byggnadpoints'],
'REFERENCE_LAYER': parameters['byggnaderpolygons'],
'TOLERANCE': 50,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT #parameters['SnapGeometriesToLayer']
}
outputs['SnapGeometriesToLayer'] = processing.run('native:snapgeometries', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
#results['SnapGeometriesToLayer'] = outputs['SnapGeometriesToLayer']['OUTPUT']
feedback.setCurrentStep(6)
if feedback.isCanceled():
return {}
# Buffer - Create buffer around point on edge of building, buffer half inside and half outside
alg_params = {
'DISSOLVE': False,
'DISTANCE': 0.1,
'END_CAP_STYLE': 0, # Round
'INPUT': outputs['SnapGeometriesToLayer']['OUTPUT'],
'JOIN_STYLE': 0, # Round
'MITER_LIMIT': 2,
'SEGMENTS': 5,
'SEPARATE_DISJOINT': False,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['Buffer'] = processing.run('native:buffer', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(7)
if feedback.isCanceled():
return {}
# Intersection - Get the part of the buffer that is within the building
alg_params = {
'GRID_SIZE': None,
'INPUT': parameters['byggnaderpolygons'],
'INPUT_FIELDS': ['fid'],
'OVERLAY': outputs['Buffer']['OUTPUT'],
'OVERLAY_FIELDS': parameters['byggnadfields'],#['id','anlaggning_id','fastighetsnyckel','fast_byg_uuid','byggnadsbeteckning','visningsurl'],
'OVERLAY_FIELDS_PREFIX': None,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT #parameters['Intersection']
}
outputs['Intersection'] = processing.run('native:intersection', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
#results['Intersection'] = outputs['Intersection']['OUTPUT']
feedback.setCurrentStep(8)
if feedback.isCanceled():
return {}
# Centroids - Set a point that should now be within the building
alg_params = {
'ALL_PARTS': False,
'INPUT': outputs['Intersection']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT #parameters['Centroids']
}
outputs['Centroids'] = processing.run('native:centroids', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
#results['Centroids'] = outputs['Centroids']['OUTPUT']
feedback.setCurrentStep(9)
if feedback.isCanceled():
return {}
# Create spatial index
alg_params = {
'INPUT': outputs['Centroids']['OUTPUT']
}
outputs['CreateSpatialIndex'] = processing.run('native:createspatialindex', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(10)
if feedback.isCanceled():
return {}
###################################################
# Merge vector layers
alg_params = {
'CRS': 'ProjectCrs',
'LAYERS': [outputs['Centroids']['OUTPUT'],outputs['Intersect']['OUTPUT']],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['remergePoints'] = processing.run('native:mergevectorlayers', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(11)
if feedback.isCanceled():
return {}
# Extract by location
alg_params = {
'INPUT': parameters['fastigheterpolygons'],
'INTERSECT': outputs['remergePoints']['OUTPUT'],
'PREDICATE': [0], # intersect
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT #parameters['Extractbylocation']
}
outputs['ExtractByLocation'] = processing.run('native:extractbylocation', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
#results['Extractbylocation'] = outputs['ExtractByLocation']['OUTPUT']
feedback.setCurrentStep(12)
if feedback.isCanceled():
return {}
# Create spatial index
alg_params = {
'INPUT': outputs['ExtractByLocation']['OUTPUT']
}
outputs['CreateSpatialIndex'] = processing.run('native:createspatialindex', alg_params, context=context, feedback=feedback, is_child_algorithm=True)
feedback.setCurrentStep(13)
if feedback.isCanceled():
return {}
###############################################################################################################
# Fetch the feature "layers" for fastigheter and byggnader. These are so called iterators and must be converted to lists of features otherwise Python shit happens.
features_fastigheter = list(context.temporaryLayerStore().mapLayers()[outputs['ExtractByLocation']['OUTPUT']].getFeatures())
features_byggnader_del1 = list(context.temporaryLayerStore().mapLayers()[outputs['Centroids']['OUTPUT']].getFeatures())
features_byggnader_del2 = list(context.temporaryLayerStore().mapLayers()[outputs['Intersect']['OUTPUT']].getFeatures())
features_byggnader = features_byggnader_del1 + features_byggnader_del2
source = self.parameterAsSource(
parameters,
'fastigheterpolygons',
context
)
# outputs['Intersect']['OUTPUT']
# Create attribute fields for the new layer
newFields = QgsFields()
#fastighetAttributeNameList = ['FNR_FDS', 'OBJEKT_ID', 'KOMMUNKOD', 'FASTIGHET']
fastighetAttributeNameList = parameters['fastighetfields']
for i in range(len(fastighetAttributeNameList)):
newFields.append(QgsField(fastighetAttributeNameList[i], QVariant.String))
newFields.append(QgsField('AntalByggnader', QVariant.Int))
#byggnadAttributeNameList = ['id', 'anlaggning_id', 'fastighetsnyckel', 'fast_byg_uuid', 'byggnadsbeteckning', 'visningsurl']
byggnadAttributeNameList = parameters['byggnadfields']
for j in range(len(byggnadAttributeNameList)):
newFields.append(QgsField(byggnadAttributeNameList[j], QVariant.String))
# Loop through "fastigheter" features
spacer ='; '
fastighetCount = 0
for feature_fastighet in features_fastigheter:
fastighet_geometry = feature_fastighet.geometry()
# Loop through "byggnader" features. has_building keeps track of i) if a fastighet is associated with a byggnad and how many
has_building = 0
for feature_byggnad in features_byggnader:
# Is the building within the property boundary? If "yes" check building count, if this is first building for this property cretae a new feature from the property
if fastighet_geometry.contains(feature_byggnad.geometry()):
has_building += 1
if has_building == 1:
fastighetCount += 1
newFeature = QgsFeature()
newFeature.setGeometry(fastighet_geometry)
fastighetAttributList = []
fastighet_attributes = feature_fastighet.attributes()
for name in fastighetAttributeNameList:
if fastighet_attributes[feature_fastighet.fields().indexFromName(name)] == NULL:
fastighetAttributList.append(None)
else:
fastighetAttributList.append(fastighet_attributes[feature_fastighet.fields().indexFromName(name)])
byggnadAttributList = ['']*len(byggnadAttributeNameList)
byggnadAttributeDict = dict(zip(byggnadAttributeNameList,byggnadAttributList))
byggnad_attributes = feature_byggnad.attributes()
for name in byggnadAttributeNameList:
if byggnad_attributes[feature_byggnad.fields().indexFromName(name)] == NULL:
attribute = 'Saknas'
else:
attribute = byggnad_attributes[feature_byggnad.fields().indexFromName(name)]
if has_building == 1:
byggnadAttributeDict[name]+=(attribute)
else:
byggnadAttributeDict[name]+=(spacer + attribute)
if has_building > 0:
fastighetAttributList.append(has_building)
attributeList = fastighetAttributList
for name in byggnadAttributeNameList:
attributeList.append(byggnadAttributeDict[name])
newFeature.setAttributes(attributeList)
# Define feature layer to receive new features. This is weird in models and completely inconsistent with both plugins and console scripting
if fastighetCount == 1:
(sink, dest_id) = self.parameterAsSink(
parameters,
'BMFastighet',
context, newFields, source.wkbType(), source.sourceCrs()
)
sink.addFeature(newFeature, QgsFeatureSink.FastInsert)
results['BMFastighet'] = dest_id
vlayer = QgsProcessingUtils.mapLayerFromString(results['BMFastighet'], context)
vlayer.renderer().symbol().setColor(QColor(150,150,250))
vlayer.triggerRepaint()
label_settings = QgsPalLayerSettings()
#label_settings.drawBackground = True
label_settings.fieldName = 'AntalByggnader'
text_format = QgsTextFormat()
background_color = QgsTextBackgroundSettings()
background_color.setFillColor(QColor(200,200,255))
background_color.setEnabled(True)
text_format.setBackground(background_color )
label_settings.setFormat(text_format)
vlayer.setLabeling(QgsVectorLayerSimpleLabeling(label_settings))
vlayer.setLabelsEnabled(True)
vlayer.triggerRepaint()
iface.layerTreeView().refreshLayerSymbology(vlayer.id())
########################################################################################################
return results
def name(self):
return 'Byggnad2Fastighet_v3_1'
def displayName(self):
return 'Byggnad2Fastighet_v3_1'
def group(self):
return ''
def groupId(self):
return ''
def createInstance(self):
return Byggfast()