-
Notifications
You must be signed in to change notification settings - Fork 0
/
layers.py
472 lines (361 loc) · 18.2 KB
/
layers.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
from qgis.core import QgsProject, QgsRasterLayer, QgsVectorLayer, QgsDataSourceUri, QgsFeatureRequest, QgsWkbTypes, QgsVectorFileWriter, QgsFeature, QgsGeometry, QgsRectangle, QgsCoordinateReferenceSystem, QgsPointXY, QgsField
from PyQt5.QtCore import QFileInfo #, QVariant
import shared
from shared import OUTPUT_TYPE, OUTPUT_FIELD_CODE, OUTPUT_ELEVATION
from utils import ToSentenceCase, DisplayOS
#======================================================================================================================
#
# Reads a raster layer from a file
#
#======================================================================================================================
def readRasterLayer(i, allBandData):
# pylint: disable=too-many-locals
fileInfo = QFileInfo(shared.rasterFileName[i])
fileBaseName = fileInfo.baseName()
layer = QgsRasterLayer(shared.rasterFileName[i], fileBaseName)
if not layer.isValid():
shared.fpOut.write("Raster layer '" + shared.rasterFileName[i] + "'failed to load")
return -1
# Store the title as the first list item
allBandData.append(shared.rasterFileTitle[i])
# Get more info
xSize = layer.width()
ySize = layer.height()
cellWidth = layer.rasterUnitsPerPixelX()
cellHeight = layer.rasterUnitsPerPixelY()
provider = layer.dataProvider()
extnt = provider.extent()
dpi = provider.dpi()
shared.fpOut.write("Raster layer '" + shared.rasterFileTitle[i] + "' loaded with X, Y resolution = " + str(cellWidth) + ", " + str(cellHeight) + " m\n")
#shared.fpOut.write(layer.metadata())
#shared.fpOut.write(layer.rasterType())
# Store the above as the second list item
allBandData.append([provider, xSize, ySize, cellWidth, cellHeight, extnt, dpi])
# Now store the data for each band as a QgsRasterBlock
nBands = layer.bandCount()
for band in range(nBands):
#shared.fpOut.write(layer.bandName(i))
bandData = provider.block(band, extnt, xSize, ySize)
# Store as a further list item
allBandData.append(bandData)
# Sort out style
#shared.fpOut.write("Style file " + str(i) + " is " + shared.rasterFileStyle[i])
if not shared.rasterFileStyle[i]:
# No style file specified, so try the default style for this layer
shared.rasterFileStyle[i] = layer.styleURI()
#shared.fpOut.write("Trying default style file " + shared.rasterFileStyle[i])
if not layer.loadDefaultStyle():
shared.fpOut.write("Could not load default style '" + shared.rasterFileStyle[i] + "' for raster layer '" + shared.rasterFileTitle[i] + "'")
else:
# A style file was specified, so try to load it
#shared.fpOut.write("Trying style file " + shared.rasterFileStyle[i])
if not layer.loadNamedStyle(shared.rasterFileStyle[i]):
shared.fpOut.write("Could not load style '" + shared.rasterFileStyle[i] + "' for raster layer '" + shared.rasterFileTitle[i] + "'")
# Set opacity
layer.renderer().setOpacity(shared.rasterFileOpacity[i])
# Add this layer to the app's registry
QgsProject.instance().addMapLayer(layer)
return layer
#======================================================================================================================
#======================================================================================================================
#
# Reads a vector layer from a file
#
#======================================================================================================================
def ReadVectorLayer(i):
if shared.vectorFileType[i] == "ogr":
layer = QgsVectorLayer(shared.vectorFileName[i], shared.vectorFileTitle[i], "ogr")
elif shared.vectorFileType[i] == "spatialite":
uri = QgsDataSourceUri()
uri.setDatabase(shared.vectorFileName[i])
schema = ''
geom_column = 'Geometry'
uri.setDataSource(schema, shared.vectorFileTable[i], geom_column)
layer = QgsVectorLayer(uri.uri(), shared.vectorFileTitle[i], shared.vectorFileType[i])
elif shared.vectorFileType[i] == "xyz":
uri = shared.vectorFileName[i] + "?type=csv&useHeader=No&xField=field_1&yField=field_2&spatialIndex=no&subsetIndex=no&watchFile=no"
layer = QgsVectorLayer(uri, shared.vectorFileTitle[i], "delimitedtext")
else:
shared.fpOut.write("Cannot load vector file of type '" + shared.vectorFileType[i] + "'")
return -1
if not layer.isValid():
shared.fpOut.write("Vector layer '" + shared.vectorFileTitle[i] + "' failed to load")
return -1
shared.fpOut.write("Vector layer '" + shared.vectorFileTitle[i] + "' loaded\n")
# Sort out style
if not shared.vectorFileStyle[i]:
shared.vectorFileStyle[i] = layer.styleURI()
#shared.fpOut.write(shared.vectorFileStyle[i])
if not layer.loadDefaultStyle():
shared.fpOut.write("Could not load default style '" + shared.vectorFileStyle[i] + "' for vector layer '" + shared.vectorFileTitle[i] + "'")
else:
if not layer.loadNamedStyle(shared.vectorFileStyle[i]):
shared.fpOut.write("Could not load style '" + shared.vectorFileStyle[i] + "' for vector layer '" + shared.vectorFileTitle[i] + "'")
# Set opacity
layer.setOpacity(shared.vectorFileOpacity[i])
# Add this layer to the app's registry
QgsProject.instance().addMapLayer(layer)
return layer
#======================================================================================================================
#======================================================================================================================
#
# Reads multiple vector layers from files and merges them
#
#======================================================================================================================
def ReadAndMergeVectorLayers(vectorLayers):
# pylint: disable=too-many-locals
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
category = ""
feats = []
for i in vectorLayers:
if shared.vectorFileType[i] == "ogr":
thisLayer = QgsVectorLayer(shared.vectorFileName[i], shared.vectorFileTitle[i], "ogr")
elif shared.vectorFileType[i] == "spatialite":
uri = QgsDataSourceUri()
uri.setDatabase(shared.vectorFileName[i])
schema = ''
geom_column = 'Geometry'
uri.setDataSource(schema, shared.vectorFileTable[i], geom_column)
thisLayer = QgsVectorLayer(uri.uri(), shared.vectorFileTitle[i], shared.vectorFileType[i])
elif shared.vectorFileType[i] == "xyz":
uri = shared.vectorFileName[i] + "?type=csv&useHeader=No&xField=field_1&yField=field_2&spatialIndex=no&subsetIndex=no&watchFile=no"
thisLayer = QgsVectorLayer(uri, shared.vectorFileTitle[i], "delimitedtext")
else:
shared.fpOut.write("Cannot load vector file of type '" + shared.vectorFileType[i] + "'")
return -1, -1
if not thisLayer.isValid():
shared.fpOut.write("Vector layer '" + shared.vectorFileTitle[i] + "' failed to load")
return -1, -1
shared.fpOut.write("Vector layer '" + shared.vectorFileTitle[i] + "' loaded\n")
# Copy the features from this layer that are within the displayed extent, to a new list
extentRectWithBorder = QgsRectangle()
borderSize = 100
extentRectWithBorder.setXMinimum(shared.extentRect.xMinimum() - borderSize)
extentRectWithBorder.setYMinimum(shared.extentRect.yMinimum() - borderSize)
extentRectWithBorder.setXMaximum(shared.extentRect.xMaximum() + borderSize)
extentRectWithBorder.setYMaximum(shared.extentRect.yMaximum() + borderSize)
request = QgsFeatureRequest(extentRectWithBorder)
features = thisLayer.getFeatures(request)
#features = thisLayer.getFeatures()
print("Copying features from vector layer '" + shared.vectorFileTitle[i] + "'")
feats += features
#print("N features =", len(feats))
#for feat in features:
#newFeature = QgsFeature()
#newFeature.setGeometry(feat.geometry())
#fields = feat.fields()
#newFeature.setFields(fields)
#attrs = feat.attributes()
#newFeature.setAttributes(attrs)
#feats.append(newFeature)
##print(feat)
##feats.append(feat)
flds = thisLayer.fields()
#print(shared.vectorFileTitle[i] + " FIELD NAMES " + str(flds.names()))
#print(shared.vectorFileTitle[i] + " number of attribute fields = " + str(len(flds.names())))
category = shared.vectorFileCategory[i]
# Get the Coordinate Reference System and the list of fields from the last input file
thisLayerCRS = thisLayer.crs().toWkt()
thisLayerFieldList = thisLayer.dataProvider().fields().toList()
# Create the merged layer by checking the geometry type of the input files
layerGeom = thisLayer.geometryType()
layerWkb = thisLayer.wkbType()
isMulti = QgsWkbTypes.isMultiType(layerWkb)
if layerGeom == QgsWkbTypes.PointGeometry:
if isMulti:
mergedLayer = QgsVectorLayer('MultiPoint?crs=' + thisLayerCRS, 'merged', "memory")
else:
mergedLayer = QgsVectorLayer('Point?crs=' + thisLayerCRS, 'merged', "memory")
elif layerGeom == QgsWkbTypes.LineGeometry:
if isMulti:
mergedLayer = QgsVectorLayer('MultiLineString?crs=' + thisLayerCRS, 'merged', "memory")
else:
mergedLayer = QgsVectorLayer('LineString?crs=' + thisLayerCRS, 'merged', "memory")
elif layerGeom == QgsWkbTypes.PolygonGeometry:
if isMulti:
mergedLayer = QgsVectorLayer('MultiPolygon?crs=' + thisLayerCRS, 'merged', "memory")
else:
mergedLayer = QgsVectorLayer('Polygon?crs=' + thisLayerCRS, 'merged', "memory")
else:
geomTypeString = QgsWkbTypes.displayString(int(layerWkb))
errStr = "ERROR: layer type " + geomTypeString + " could not be merged"
print(errStr)
shared.fpOut.write(errStr + "\n")
return -1, -1
# Is the new (but still empty) merged layer OK?
if not mergedLayer.isValid():
titleStr = ""
for i in vectorLayers:
titleStr += shared.vectorFileTitle[i]
if i < len(vectorLayers):
titleStr += "' '"
errStr = "ERROR: could not create new vector layer for merge of '" + titleStr + "'"
print(errStr)
shared.fpOut.write(errStr + "\n")
return -1, -1
# Sort the list of features by identifier
feats.sort(key = lambda feat : feat.attributes()[1])
#for i in range(10):
#print("BEFORE MERGE " + str(feats[i].attributes()))
#print("=================")
##BODGE This is needed because QGIS 3 handles Boolean layers incorrectly
#print("*********************")
#print("*** ORIGINAL: " + str(thisLayerFieldList))
#for fld in thisLayerFieldList:
#print(fld.type())
#print("*********************")
#for fld in thisLayerFieldList:
#fldName = fld.typeName()
#if fldName == "Boolean":
#fld.setTypeName("Integer64")
#fld.setType(4)
#print("**** CHANGED : " + str(thisLayerFieldList))
#for fld in thisLayerFieldList:
#print(fld.type())
#print("*********************")
# Add the field attributes to the merged layer
provider = mergedLayer.dataProvider()
#print("PROVIDER: " + str(provider))
#print("PROVIDER Number of attribute fields = " + str(len(thisLayerFieldList)))
if not provider.addAttributes(thisLayerFieldList):
errStr = "ERROR: could not add attributes to merged layer"
print(errStr)
shared.fpOut.write(errStr + "\n")
return -1, -1
mergedLayer.updateFields()
flds = mergedLayer.fields()
#print("MERGED FIELD NAMES " + str(flds.names()))
#print("MERGED number of attribute fields = " + str(len(flds.names())))
if not mergedLayer.startEditing():
errStr = "ERROR: could not edit merged layer"
print(errStr)
shared.fpOut.write(errStr + "\n")
return -1, -1
if not mergedLayer.addFeatures(feats):
errStr = "ERROR: could not add features to merged layer"
print(errStr)
shared.fpOut.write(errStr + "\n")
return -1, -1
#testFeats = mergedLayer.getFeatures()
#print("BEFORE testFeats = " + str(testFeats))
#nMergedFeat = 0
#for feat in features:
#nMergedFeat += 1
#print("BEFORE N features in testFeats =", nMergedFeat)
mergedLayer.commitChanges()
#testFeats = mergedLayer.getFeatures()
#print("AFTER testFeats = " + str(testFeats))
#nMergedFeat = 0
#for feat in features:
#nMergedFeat += 1
#print("AFTER N features in testFeats =", nMergedFeat)
#features = mergedLayer.getFeatures()
#for feat in features:
#shared.fpOut.write(str(feat.attributes()))
#for field in mergedLayer.fields().toList():
#shared.fpOut.write(str(field.name()))
# Sort out style
i = vectorLayers[-1]
if not shared.vectorFileStyle[i]:
shared.vectorFileStyle[i] = mergedLayer.styleURI()
#shared.fpOut.write(shared.vectorFileStyle[i])
if not mergedLayer.loadDefaultStyle():
errStr = "ERROR: could not load default style '" + shared.vectorFileStyle[i] + "' for vector layer '" + shared.vectorFileTitle[i] + "'"
print(errStr)
shared.fpOut.write(errStr + "\n")
else:
if not mergedLayer.loadNamedStyle(shared.vectorFileStyle[i]):
errStr = "ERROR: could not load style '" + shared.vectorFileStyle[i] + "' for vector layer '" + shared.vectorFileTitle[i] + "'"
print(errStr)
shared.fpOut.write(errStr + "\n")
# Set transparency
mergedLayer.setOpacity(shared.vectorFileOpacity[i])
# Add this layer to the app's registry
QgsProject.instance().addMapLayer(mergedLayer)
mergedNames = ""
for i in vectorLayers:
mergedNames += "'"
mergedNames += shared.vectorFileTitle[i]
mergedNames += "' "
shared.fpOut.write("\tMerged layers " + mergedNames + "\n")
print("Merged layers " + mergedNames)
return mergedLayer, category
#======================================================================================================================
#======================================================================================================================
#
# Creates a new vector layer
#
#======================================================================================================================
def createVector(initString, title, fieldDefn, style, opacity):
layer = QgsVectorLayer(initString, title, "memory")
if not layer.isValid():
shared.fpOut.write("Could not create vector layer '" + title + "'")
return -1
shared.fpOut.write("Vector layer '" + title + "' created\n")
# OK now add the fields
layer.dataProvider().addAttributes(fieldDefn)
layer.updateFields()
# Sort out style and opacity
layer.loadNamedStyle(style)
layer.setOpacity(opacity)
# Add this layer to the app's registry
QgsProject.instance().addMapLayer(layer)
return layer
#======================================================================================================================
#======================================================================================================================
#
# Writes a vector layer as a shapefile
#
#======================================================================================================================
def WriteVector(layer, fileName, CRS):
error, errorString = QgsVectorFileWriter.writeAsVectorFormat(layer, fileName, "UTF-8", driverName = "ESRI Shapefile")
if error != QgsVectorFileWriter.NoError:
printStr = "ERROR: could not create '" + fileName + "' '" + errorString + "'\n"
shared.fpOut.write(printStr)
print(printStr)
else:
shared.fpOut.write("Vector layer '" + fileName + "' saved\n")
return
#======================================================================================================================
#======================================================================================================================
#
# Adds a point to the flow marker points vector layer
#
#======================================================================================================================
def AddFlowMarkerPoint(thisPoint, desc, fieldCode, elev):
#print("In AddFlowMarkerPoint " + desc + " " + DisplayOS(thisPoint.x(), thisPoint.y()) + " for field " + str(fieldCode))
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPointXY(QgsPointXY(thisPoint)))
fields = shared.outFlowMarkerPointLayer.fields()
feature.setFields(fields)
feature[OUTPUT_TYPE] = ToSentenceCase(desc)
feature[OUTPUT_FIELD_CODE] = fieldCode
feature[OUTPUT_ELEVATION] = elev
provider = shared.outFlowMarkerPointLayer.dataProvider()
if not provider.addFeatures([feature]):
shared.fpOut.write("Could not add flow marker point (coord = {" + str(thisPoint.x()) + ", " + str(thisPoint.y()) + "}, '" + desc + "', field code = " + fieldCode + ", elev = " + str(elev) + ")")
return
#======================================================================================================================
#======================================================================================================================
#
# Adds a line to the flow lines vector layer
#
#======================================================================================================================
def AddFlowLine(lineStartPoint, lineEndPoint, desc, fieldCode, elev):
#shared.fpOut.write("lineStartPoint = " + str(lineStartPoint) + " lineEndPoint = " + str(lineEndPoint) + "\n")
feature = QgsFeature()
feature.setGeometry(QgsGeometry.fromPolylineXY([lineStartPoint, lineEndPoint]))
fields = shared.outFlowLineLayer.fields()
feature.setFields(fields)
feature[OUTPUT_TYPE] = ToSentenceCase(desc)
feature[OUTPUT_FIELD_CODE] = fieldCode
feature[OUTPUT_ELEVATION] = elev
provider = shared.outFlowLineLayer.dataProvider()
if not provider.addFeatures([feature]):
shared.fpOut.write("Could not add flow line from {" + str(lineStartPoint.x()) + ", " + str(lineStartPoint.y()) + "} to {" + str(lineEndPoint.x()) + ", " + str(lineEndPoint.y()) + "}, '" + desc + "', field code = " + fieldCode + ", elev = " + str(elev) + ")")
return
shared.thisFieldFlowLine.append(lineStartPoint)
shared.thisFieldFlowLineFieldCode.append(fieldCode)
return
#======================================================================================================================