forked from LEOXINGU/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sobreposicao.py
144 lines (132 loc) · 5.19 KB
/
sobreposicao.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
"""
/***************************************************************************
3 CGEO
3th Brazilian Geoinformation Center
-------------------
begin : 2017-05-24
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. *
* *
***************************************************************************/
"""
# Verificacao de sobreposicao em uma camada
##Verifica Sobreposicao=name
##LF4) Vetor=group
##Entrada=vector
##Saida=output vector
entrada = Entrada
saida = Saida
from PyQt4.QtCore import *
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import processing
import time
# Poligono de entrada
input = processing.getObject(entrada)
if input.geometryType() == QGis.Polygon:
# Pegar coordenadas dos poligonos
lista = []
for feat in input.getFeatures():
geom = feat.geometry()
coord = geom.asPolygon()
if coord == []:
coord = geom.asMultiPolygon()[0]
if coord != []:
lista += [coord]
# Cirando camada de sobreposicao
fields = QgsFields()
fields.append(QgsField('id', QVariant.Int))
fields.append(QgsField('Feat1', QVariant.Int))
fields.append(QgsField('Feat2', QVariant.Int))
CRS = input.crs()
encoding = 'utf-8'
formato = 'ESRI Shapefile'
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBPolygon, CRS, formato)
del writer
sobrep = QgsVectorLayer(saida, 'sobreposicao', 'ogr')
DataProvider = sobrep.dataProvider()
# Verificar Sobreposicao
fet =QgsFeature()
tam = input.featureCount()
cont = 0
for i in range(tam-1):
for j in range(i+1, tam):
A = QgsGeometry.fromPolygon(lista[i])
B = QgsGeometry.fromPolygon(lista[j])
if A.overlaps(B):
C = A.intersection(B)
if C.asPolygon() != []:
cont +=1
att = [cont, i, j]
fet.setGeometry(C)
fet.setAttributes(att)
DataProvider.addFeatures([fet])
progress.setPercentage(int(((i+1)/float(tam))*100))
elif input.geometryType() == QGis.Line:
# Colocar todas as geometrias em uma lista
lista = []
for linha in input.getFeatures():
geom = linha.geometry()
coord = geom.asPolyline()
if coord == []:
coord = geom.asMultiPolyline()[0]
lista += [coord]
tam = len(lista)
# Cirando camada de sobreposicao
fields = QgsFields()
fields.append(QgsField('id', QVariant.Int))
fields.append(QgsField('Feat1', QVariant.Int))
fields.append(QgsField('Feat2', QVariant.Int))
CRS = input.crs()
encoding = 'utf-8'
formato = 'ESRI Shapefile'
writer = QgsVectorFileWriter(saida, encoding, fields, QGis.WKBLineString, CRS, formato)
del writer
sobrep = processing.getObject(saida)
DataProvider = sobrep.dataProvider()
# Verificando sobreposicoes
fet =QgsFeature()
cont = 0
for index, i in enumerate(range(tam-1)):
for j in range(i+1, tam):
A = QgsGeometry.fromPolyline(lista[i])
B = QgsGeometry.fromPolyline(lista[j])
if A.intersects(B):
C = A.intersection(B)
D = C.asMultiPolyline()
if D:
E = QgsGeometry.fromPolyline(D[0])
for k in range(1, len(D)):
E = E.combine(QgsGeometry.fromPolyline(D[k]))
cont +=1
att = [cont, i, j]
fet.setGeometry(E)
fet.setAttributes(att)
DataProvider.addFeatures([fet])
elif C.asPolyline():
E = QgsGeometry.fromPolyline(C.asPolyline())
cont +=1
att = [cont, i, j]
fet.setGeometry(E)
fet.setAttributes(att)
DataProvider.addFeatures([fet])
elif C.asMultiPoint():
E = QgsGeometry.fromPolyline(C.asMultiPoint())
cont +=1
att = [cont, i, j]
fet.setGeometry(E)
fet.setAttributes(att)
DataProvider.addFeatures([fet])
progress.setPercentage(int((index/float(tam-1))*100))
progress.setInfo('<b>Operacao concluida!</b><br/><br/>')
progress.setInfo('<b>3 CGEO</b><br/>')
progress.setInfo('<b>Cap Leandro - Eng Cart</b><br/>')
iface.messageBar().pushMessage(u'Situacao', "Operacao Concluida com Sucesso!", level=QgsMessageBar.INFO, duration=5)
time.sleep(3)