forked from LEOXINGU/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conserta_linhas.py
135 lines (119 loc) · 4.56 KB
/
conserta_linhas.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
"""
/***************************************************************************
3 CGEO
3th Brazilian Geoinformation Center
-------------------
begin : 2017-05-18
copyright : (C) 2017 by Leandro Franca - Cartographic Engineer @ Brazilian Army
email : franca.leandro@eb.mil.br
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
"""
# Conserta Linhas invalidas (nos duplicados, filetes, e ziguezagues)
##Conserta Linhas=name
##LF4) Vetor=group
##Camada_de_entrada=vector
##Tolerancia_para_comprimento_minimo=number 0.001
##Tolerancia_para_distantica_minima=number 0
##Shapefile_de_saida=output vector
saida = Shapefile_de_saida
tol_compr = Tolerancia_para_comprimento_minimo
tol_dist = Tolerancia_para_distantica_minima
from PyQt4.QtCore import *
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import processing
import time
input = processing.getObject(Camada_de_entrada)
fields = input.pendingFields()
CRS = input.crs()
encoding = 'utf-8'
formato = 'ESRI Shapefile'
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBLineString, CRS, formato)
del writer
linhas = QgsVectorLayer(saida, 'linhas', 'ogr')
DataProvider = linhas.dataProvider()
# Copiar todas as feicoes do input para classe poligonos
for feature in input.getFeatures():
ok = DataProvider.addFeatures([feature])
del input, fields
tam = linhas.featureCount()
deletar = []
if linhas.crs().geographicFlag():
tol_area /= (111000)*(111000)
tol_dist /= 111000
# Distancia unitaria
def DistUnit(p1, p2):
return abs(p1.x() - p2.x())+abs(p1.y() - p2.y())
# Verificacao para cada feicao
for index, feature in enumerate(linhas.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
# 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)
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)