-
Notifications
You must be signed in to change notification settings - Fork 2
/
fechar_anel.py
109 lines (101 loc) · 4.36 KB
/
fechar_anel.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
"""
/***************************************************************************
LEOXINGU
-------------------
begin : 2017-10-20
copyright : (C) 2017 by Leandro Franca - Cartographic Engineer
email : [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. *
* *
***************************************************************************/
"""
# Fechar Anel Linear
##Fechar Anel Linear=name
##LF04) Vetor=group
##Camada_de_entrada=vector
##Tolerancia=number 10.0
##Tipo_de_conexao=selection Ponto Medio;Ligar ultimo ao primeiro;Deslocar ultimo ao primeiro;Deslocar primeiro ao ultimo
from PyQt4.QtCore import *
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import processing
from processing.tools.vector import VectorWriter
import time
layer = processing.getObject(Camada_de_entrada)
def Conectar(Tipo_de_conexao, coord_list):
if Tipo_de_conexao == 0:
P_ini=coord_list[0]
P_fim=coord_list[-1]
P_medio = QgsPoint(0.5*(P_ini.x()+P_fim.x()),0.5*(P_ini.y()+P_fim.y()))
coord_list[0] = P_medio
coord_list[-1] = P_medio
elif Tipo_de_conexao == 1:
coord_list += [coord_list[0]]
elif Tipo_de_conexao == 2:
P_ini=coord_list[0]
coord_list[-1] = P_ini
elif Tipo_de_conexao == 3:
P_fim=coord_list[-1]
coord_list[0] = P_fim
return coord_list
if layer.crs().geographicFlag():
Tolerancia/= float(111000)
if layer.type()==0 and layer.geometryType() == QGis.Line:
DP = layer.dataProvider()
# Quebrar multipartes
for feat in layer.getFeatures():
geom = feat.geometry()
if geom:
coord = geom.asMultiPolyline()
if len(coord)>1:
# Atualizar a geometria da feicao com a geometria com apenas uma parte
new_geom = QgsGeometry.fromMultiPolyline([coord[0]])
newGeomMap = {feat.id() : new_geom}
ok = DP.changeGeometryValues(newGeomMap)
# Criar novas feicoes com as outras partes
att = feat.attributes()
new_att = [None] + att[1:]
new_feat = QgsFeature()
for i in range(1, len(coord)):
new_geom = QgsGeometry.fromMultiPolyline([coord[i]])
new_feat.setGeometry(new_geom)
new_feat.setAttributes(new_att)
ok = DP.addFeatures([new_feat])
# Fechar anel
for feat in layer.getFeatures():
geom = feat.geometry()
coord = geom.asPolyline()
if coord:
P_ini = coord[0]
P_fim = coord[-1]
Multiparte = False
else:
Multiparte = True
coord = geom.asMultiPolyline()
P_ini = coord[0][0]
P_fim = coord[0][-1]
# Verificar se o ponto inicial e final estao na tolerancia
Pnt_ini = QgsGeometry.fromPoint(P_ini)
Pnt_fim = QgsGeometry.fromPoint(P_fim)
if Pnt_ini.intersects(Pnt_fim.buffer(Tolerancia, 5)):
if not(P_ini == P_fim):
# Atualizar a geometria da feicao com a geometria com apenas uma parte
if Multiparte:
coord = coord[0]
coord = Conectar(Tipo_de_conexao, coord)
new_geom = QgsGeometry.fromMultiPolyline([coord])
else:
coord = Conectar(Tipo_de_conexao, coord)
new_geom = QgsGeometry.fromPolyline(coord)
newGeomMap = {feat.id() : new_geom}
ok = DP.changeGeometryValues(newGeomMap)
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=5)