forked from LEOXINGU/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retalhar_linhas.py
116 lines (102 loc) · 3.71 KB
/
retalhar_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
# -*- coding: utf-8 -*-
"""
/***************************************************************************
3 CGEO
3th Brazilian Geoinformation Center
-------------------
begin : 2017-05-23
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. *
* *
***************************************************************************/
"""
# Corta linhas nas intersecoes com os poligonos
##2. Retalhar Linhas=name
##LF4) Vetor=group
##Camada_de_Linhas=vector
##Camada_de_Poligonos=vector
##Saida=output vector
from PyQt4.QtCore import *
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import processing
import time
poligono = Camada_de_Poligonos
linha = Camada_de_Linhas
saida = Saida
# Camada de Poligonos
poligonos = processing.getObject(poligono)
# Camada de Linhas
linhas = processing.getObject(linha)
# Criar camada de Saida (linhas cortadas)
fields = linhas.pendingFields()
CRS = linhas.crs()
encoding = 'utf-8'
formato = 'ESRI Shapefile'
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBLineString, CRS, formato)
del writer
recortado = QgsVectorLayer(saida, 'poligonos', 'ogr')
DataProvider = recortado.dataProvider()
# Gerar camada do anel exterior dos poligonos
anel = processing.runalg('script:extrairanelexterior', poligonos, None)
recortante = processing.getObject(anel['Saida'])
# Funcao para recortar linhas
def SplitLine(geom, cortador):
# split the line
result, new_geoms, test_points = geom.splitGeometry(cortador, True)
# Put pieces in the new list
lista = []
# part 1
lista += [geom.asPolyline()]
# part 2
for new_geom in new_geoms:
lista += [new_geom.asPolyline()]
return lista
# Ober lista de linhas
lista_lin = []
for lin in linhas.getFeatures():
geom = lin.geometry()
coord = geom.asPolyline()
if coord == []:
coord = geom.asMultiPolyline()[0]
lista_lin += [(coord, lin.attributes())]
# Ober lista de poligonos
lista_pol = []
for pol in recortante.getFeatures():
geom = pol.geometry()
coord = geom.asPolyline()
if coord == []:
coord = geom.asMultiPolyline()[0]
lista_pol += [coord]
# Comecar o recorte
feature = QgsFeature(fields)
while len(lista_lin)>0:
linha = QgsGeometry.fromPolyline(lista_lin[0][0])
cruzou = False
for pol in lista_pol:
poligono = QgsGeometry.fromPolyline(pol)
if linha.crosses(poligono):
recortes = SplitLine(linha, pol)
cruzou = True
att = lista_lin[0][1]
del lista_lin[0]
for recorte in recortes:
lista_lin = [(recorte, att)] + lista_lin
break
if cruzou == False:
feature.setGeometry(linha)
feature.setAttributes(lista_lin[0][1])
DataProvider.addFeatures([feature])
del lista_lin[0]
del DataProvider, recortado
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)