-
Notifications
You must be signed in to change notification settings - Fork 2
/
consertar_geometrias.py
290 lines (264 loc) · 10.9 KB
/
consertar_geometrias.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
"""
/***************************************************************************
LEOXINGU
-------------------
begin : 2017-10-16
copyright : (C) 2017 by Leandro Franca - Cartographic Engineer
email : geoleandro.franca@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. *
* *
***************************************************************************/
"""
# Consertar Geometrias
##5. Conserta Geometrias=name
##LF04) Vetor=group
##Camada_de_entrada=vector
##Comprimento_minimo=number 0.001
##Area_minima=number 0.001
##Tolerancia_entre_vertices=number 0
##Angulo_minimo=number 15.0
##Shapefile_de_saida=output vector
saida = Shapefile_de_saida
tol_compr = Comprimento_minimo
tol_area = Area_minima
tol_dist = Tolerancia_entre_vertices
from PyQt4.QtCore import *
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import processing
import time
from math import radians, cos, sqrt
COS_ALFA = cos(radians(180-Angulo_minimo))
input = processing.getObject(Camada_de_entrada)
fields = input.pendingFields()
CRS = input.crs()
encoding = 'utf-8'
formato = 'ESRI Shapefile'
if input.geometryType() == QGis.Line:
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBLineString, CRS, formato)
elif input.geometryType() == QGis.Polygon:
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBPolygon, CRS, formato)
del writer
layer = QgsVectorLayer(saida, '', 'ogr')
DataProvider = layer.dataProvider()
# Copiar todas as feicoes do input para nova camada
for feature in input.getFeatures():
ok = DataProvider.addFeatures([feature])
del input, fields
tam = layer.featureCount()
deletar = []
if layer.crs().geographicFlag():
tol_area /= float((111000)*(111000))
tol_dist /= float(111000)
tol_compr/= float(111000)
# Distancia unitaria
def DistUnit(p1, p2):
return abs(p1.x() - p2.x())+abs(p1.y() - p2.y())
# Cosseno de Alfa
def CosAlfa(v1, v2):
return (v1[0]*v2[0]+v1[1]*v2[1])/(sqrt(v1[0]*v1[0]+v1[1]*v1[1])*sqrt(v2[0]*v2[0]+v2[1]*v2[1]))
# VERIFICACAO
if layer.geometryType() == QGis.Line:
# LINHAS
# Verificacao para cada feicao
for index, feature in enumerate(layer.getFeatures()):
geom = feature.geometry()
# Deixar quieto geometrias NoType
if geom == None:
continue
# Remocao de poligonos com area menor que a tolerancia
if geom.length() <= tol_compr:
deletar += [feature.id()]
continue
else:
pointList = geom.asPolyline()
if pointList == []:
pointList = geom.asMultiPolyline()[0]
# Remocao de Nos N-plicados
ind = 0
while ind < len(pointList)-1:
pA = pointList[ind]
pB = pointList[ind+1]
n = 0
while DistUnit(pA, pB) <=tol_dist:
print 'no duplicado'
n+=1
if (ind+1+n) <= (len(pointList)-1):
pB = pointList[ind+1+n]
else:
break
if n>0:
for k in range(1, n+1):
del pointList[ind+1]
print 'nos deletados'
ind+=1
# Remocao de Filetes
ind = 0
while ind < len(pointList)-2:
pA = pointList[ind]
pB = pointList[ind+1]
pC = pointList[ind+2]
n = 0
while DistUnit(pA, pC)<=tol_dist:
print 'filete'
n +=1
if ((ind+2+n) <= (len(pointList)-1)) and ((ind - n) >= 0):
pA = pointList[ind - n]
pC = pointList[ind +2 +n]
else:
break
if n>0:
for k in range(1, 2*n+1):
del pointList[ind+1]
ind -= n-1
print 'filete deletado'
else:
ind+=1
# Remocao de Pontas
while True:
mudou = False
ind = 0
while ind < len(pointList)-2:
p1 = pointList[ind]
p2 = pointList[ind+1]
p3 = pointList[ind+2]
v1 = [p2.x()-p1.x(), p2.y()-p1.y()]
v2 = [p3.x()-p2.x(), p3.y()-p2.y()]
if CosAlfa(v1, v2) <=COS_ALFA:
mudou = True
del pointList[ind+1]
else:
ind+=1
if not mudou:
break
# Adicionando nova geometria a feicao
new_geom = QgsGeometry.fromPolyline(pointList)
newGeomMap = {feature.id() : new_geom}
ok = DataProvider.changeGeometryValues(newGeomMap)
progress.setPercentage(int((index/float(tam))*100))
# Apagar feicoes com comprimento menor que a tolerancia
DataProvider.deleteFeatures(deletar)
elif layer.geometryType() == QGis.Polygon:
# POLIGONOS
# Verificacao para cada feicao
for index, feature in enumerate(layer.getFeatures()):
geom = feature.geometry()
# Deixar quieto geometrias NoType
if geom == None:
continue
# Remocao de poligonos com area menor que a tolerancia
if geom.area() <= tol_area:
deletar += [feature.id()]
continue
else:
pol = geom.asPolygon()
if pol == []:
pol = geom.asMultiPolygon()[0]
new_pol =[]
for pointList in pol:
# Remocao de buracos com area menor que a tolerancia
simples = QgsGeometry.fromPolygon([pointList])
if simples.area() <= tol_area:
continue
else:
# Remocao de Nos N-plicados
ind = 0
while ind < len(pointList)-1:
pA = pointList[ind]
pB = pointList[ind+1]
n = 0
while DistUnit(pA, pB) <=tol_dist:
n+=1
if (ind+1+n) <= (len(pointList)-1):
pB = pointList[ind+1+n]
else:
break
if n>0:
for k in range(1, n+1):
del pointList[ind+1]
ind+=1
# Remocao de Filetes
ind = 0
while ind < len(pointList)-2:
pA = pointList[ind]
pB = pointList[ind+1]
pC = pointList[ind+2]
n = 0
while DistUnit(pA, pC)<=tol_dist:
n +=1
if ((ind+2+n) <= (len(pointList)-1)) and ((ind - n) >= 0):
pA = pointList[ind - n]
pC = pointList[ind +2 +n]
else:
break
if n>0:
for k in range(1, 2*n+1):
del pointList[ind+1]
ind -= n-1
else:
ind+=1
# Remocao de Pontas no ponto inicial
while True:
mudou = False
p1 = pointList[-2]
p2 = pointList[0]
p3 = pointList[1]
v1 = [p2.x()-p1.x(), p2.y()-p1.y()]
v2 = [p3.x()-p2.x(), p3.y()-p2.y()]
if CosAlfa(v1, v2) <=COS_ALFA:
mudou = True
del pointList[0]
del pointList[-1]
pointList +=[pointList[0]]
if not mudou:
break
# Remocao de Pontas no ponto final
while True:
mudou = False
p1 = pointList[-2]
p2 = pointList[-1]
p3 = pointList[1]
v1 = [p2.x()-p1.x(), p2.y()-p1.y()]
v2 = [p3.x()-p2.x(), p3.y()-p2.y()]
if CosAlfa(v1, v2) <=COS_ALFA:
mudou = True
del pointList[-1]
del pointList[0]
pointList +=[pointList[0]]
if not mudou:
break
# Remocao de Pontas
while True:
mudou = False
ind = 0
while ind < len(pointList)-2:
p1 = pointList[ind]
p2 = pointList[ind+1]
p3 = pointList[ind+2]
v1 = [p2.x()-p1.x(), p2.y()-p1.y()]
v2 = [p3.x()-p2.x(), p3.y()-p2.y()]
if CosAlfa(v1, v2) <=COS_ALFA:
mudou = True
del pointList[ind+1]
else:
ind+=1
if not mudou:
break
new_pol += [pointList]
# Adicionando nova geometria a feicao
new_geom = QgsGeometry.fromPolygon(new_pol)
newGeomMap = {feature.id() : new_geom}
ok = DataProvider.changeGeometryValues(newGeomMap)
progress.setPercentage(int((index/float(tam))*100))
# Apagar feicoes com area menor que a tolerancia
DataProvider.deleteFeatures(deletar)
progress.setInfo('<b>Operacao concluida!</b><br/><br/>')
progress.setInfo('<b>Leandro França - Eng Cart</b><br/>')
time.sleep(3)
iface.messageBar().pushMessage(u'Situacao', "Operacao Concluida com Sucesso!", level=QgsMessageBar.INFO, duration=7)