-
Notifications
You must be signed in to change notification settings - Fork 3
/
preparation.py
191 lines (170 loc) · 6.22 KB
/
preparation.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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Lines Ranking
A QGIS plugin
-------------------
begin : 2020-07-07
copyright : (C) 2020 by Julia Borisova, Mikhail Sarafanov
email : [email protected], [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 qgis.PyQt.QtCore import QVariant
from qgis.core import QgsEllipsoidUtils, QgsProject, QgsMapLayer, QgsWkbTypes, Qgis, QgsApplication, QgsFeature, QgsVectorLayer, QgsField, QgsVectorFileWriter, QgsSpatialIndex, QgsFeatureRequest, QgsPointXY, QgsDistanceArea, QgsUnitTypes
import processing
def fix_geometries(layer, output):
algorithmOutput = processing.run(
'native:fixgeometries',
{
"INPUT": layer,
"OUTPUT": output
}
)
fixedVectorLayer=algorithmOutput["OUTPUT"]
return fixedVectorLayer
def clip_line_to_segment(layer):
algorithmOutput = processing.run(
'qgis:splitwithlines',
{
"INPUT": layer,
"LINES": layer,
"OUTPUT": 'memory:split_no_attr'
}
)
clipedVectorLayer = algorithmOutput["OUTPUT"]
#Setting id field for split layer
algorithmOutput = processing.run(
'qgis:fieldcalculator',
{
'INPUT': clipedVectorLayer,
'FIELD_NAME': 'id',
'FIELD_TYPE': 1,
'FIELD_LENGTH': 7,
'FIELD_PRECISION': 1,
'NEW_FIELD': True,
'FORMULA': '$id',
'OUTPUT': 'memory:with_id_attr'
}
)
with_id_attr_layer = algorithmOutput["OUTPUT"]
algorithmOutput = processing.run(
'qgis:fieldcalculator',
{
'INPUT': with_id_attr_layer,
'FIELD_NAME': 'length',
'FIELD_TYPE':1,
'FIELD_LENGTH':15,
'FIELD_PRECISION':1,
'NEW_FIELD':True,
'FORMULA':'$length',
'OUTPUT': 'memory:split_with_attr'
}
)
attrClipedVectorLayer=algorithmOutput["OUTPUT"]
attrClipedVectorLayer.commitChanges()
return [clipedVectorLayer, attrClipedVectorLayer]
def clean_gaps(layer, threshold, output):
algorithmOutput = processing.run(
"grass7:v.clean",
{'-b': False,
'-c': False,
'GRASS_MIN_AREA_PARAMETER': 0.0001,
'GRASS_OUTPUT_TYPE_PARAMETER': 0,
'GRASS_REGION_PARAMETER': None,
'GRASS_SNAP_TOLERANCE_PARAMETER': -1,
'GRASS_VECTOR_DSCO': '',
'GRASS_VECTOR_EXPORT_NOCAT': False,
'GRASS_VECTOR_LCO': '',
'error': 'memory:',
'input': layer,
'output': output,
'threshold': threshold,
'tool': [1],
'type': [1]})
return algorithmOutput['output']
def get_lines_intersections(layer, set_progress_funk):
algorithmOutput = processing.run(
'qgis:lineintersections',
{
"INPUT": layer,
"INTERSECT":layer,
"OUTPUT": 'memory:intersection'
}
)
intersectPointLayer=algorithmOutput["OUTPUT"]
pr = intersectPointLayer.dataProvider()
pr.addAttributes([QgsField("geometry", QVariant.String)])
intersectPointLayer.updateFields()
intersectPointLayer.startEditing()
all_f=intersectPointLayer.featureCount()
for feature in intersectPointLayer.getFeatures():
progress=36+(int(feature.id())*10)/all_f
set_progress_funk(progress)
feature['geometry']=feature.geometry().asWkt()
intersectPointLayer.updateFeature(feature)
intersectPointLayer.commitChanges()
return intersectPointLayer
def get_nearest_segmentId(layer, QgsPointXY, set_progress_funk):
spIndex = QgsSpatialIndex()
all_f=layer.featureCount()
for feature in layer.getFeatures():
progress=47+(int(feature.id())*10)/all_f
set_progress_funk(progress)
spIndex.addFeature(feature)
nearestIds = spIndex.nearestNeighbor(QgsPointXY,1)
nearest_feature = layer.getFeatures(QgsFeatureRequest().setFilterFid(nearestIds[0]))
ftr = QgsFeature()
nearest_feature.nextFeature(ftr)
return ftr["fid"]
def createSpatialIndex(layer):
processing.run(
'qgis:createspatialindex',
{
"INPUT": layer
}
)
def createCleanedBuffer(layer):
algorithmOutput = processing.run(
'native:buffer',
{
'DISSOLVE' : False,
'DISTANCE' : 0.001,
'END_CAP_STYLE' : 0,
'INPUT' : layer,
'JOIN_STYLE' : 0,
'MITER_LIMIT' : 2,
'OUTPUT' : 'memory:buffer_layer',
'SEGMENTS' : 5
}
)
buffer_layer=algorithmOutput["OUTPUT"]
return buffer_layer
def joinAttributes(outlayer, reallayer, user_fields_names):
prov = reallayer.dataProvider()
field_names = [field.name() for field in prov.fields()]
for name in user_fields_names:
if name in field_names:
field_names.remove(name)
algorithmOutput = processing.run(
'native:joinattributesbylocation',
{
'DISCARD_NONMATCHING' : False,
'INPUT' : outlayer,
'JOIN' : reallayer,
'JOIN_FIELDS' : field_names,
'METHOD' : 0,
'OUTPUT' : 'memory:rank_attributes',
'PREDICATE' : [5],
'PREFIX' : ''
}
)
outlayerWithAttributes=algorithmOutput["OUTPUT"]
return outlayerWithAttributes