-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcamada2camada.py
More file actions
156 lines (147 loc) · 6.06 KB
/
camada2camada.py
File metadata and controls
156 lines (147 loc) · 6.06 KB
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
"""
/***************************************************************************
LEOXINGU
-------------------
begin : 2018-05-02
copyright : (C) 2018 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. *
* *
***************************************************************************/
"""
# Alimentar Camada com outra Camada
##Alimentar camada=name
##LF08) EDGV=group
##Camada_de_Origem=vector
##Camada_de_Destino=vector
from PyQt4.QtCore import *
from qgis.gui import QgsMessageBar
from qgis.utils import iface
from qgis.core import *
import time
import processing
# Pegar SRC da camada de destino
origem = processing.getObject(Camada_de_Origem)
SRC_origem = origem.crs()
destino = processing.getObject(Camada_de_Destino)
SRC_destino = destino.crs()
DP = destino.dataProvider()
# Transformacao entre diferentes SRC
xform = QgsCoordinateTransform(SRC_origem, SRC_destino)
def reprojetar(geom):
if geom.type() == 0: #Ponto
if geom.isMultipart():
pnts = geom.asMultiPoint()
newPnts = []
for pnt in pnts:
newPnts += [xform.transform(pnt)]
newGeom = QgsGeometry.fromMultiPoint(newPnts)
return newGeom
else:
pnt = geom.asPoint()
newPnt = xform.transform(pnt)
newGeom = QgsGeometry.fromPoint(newPnt)
return newGeom
elif geom.type() == 1: #Linha
if geom.isMultipart():
linhas = geom.asMultiPolyline()
newLines = []
for linha in linhas:
newLine =[]
for pnt in linha:
newLine += [xform.transform(pnt)]
newLines += [newLine]
newGeom = QgsGeometry.fromMultiPolyline(newLines)
return newGeom
else:
linha = geom.asPolyline()
newLine =[]
for pnt in linha:
newLine += [xform.transform(pnt)]
newGeom = QgsGeometry.fromPolyline(newLine)
return newGeom
elif geom.type() == 2: #Poligono
if geom.isMultipart():
poligonos = geom.asMultiPolygon()
newPolygons = []
for pol in poligonos:
newPol = []
for anel in pol:
newAnel = []
for pnt in anel:
newAnel += [xform.transform(pnt)]
newPol += [newAnel]
newPolygons += [newPol]
newGeom = QgsGeometry.fromMultiPolygon(newPolygons)
return newGeom
else:
pol = geom.asPolygon()
newPol = []
for anel in pol:
newAnel = []
for pnt in anel:
newAnel += [xform.transform(pnt)]
newPol += [newAnel]
newGeom = QgsGeometry.fromPolygon(newPol)
return newGeom
else:
return None
# Mapeamento dos atributos
campos_origem = [field.name() for field in origem.pendingFields()]
campos_destino = [field.name() for field in destino.pendingFields()]
mapa_campo = []
for campo in campos_destino:
if campo in campos_origem and campo!= 'id':
mapa_campo += [campos_origem.index(campo)]
else:
mapa_campo += [None]
# Validar dados de entrada (deve ter mesma tipo de geometria e estrutura de atributos)
if origem.geometryType() != destino.geometryType():
progress.setInfo('<br/><br/><b>As camadas devem ser do mesmo tipo de geometria.</b>')
time.sleep(5)
iface.messageBar().pushMessage(u'Situacao', "As camadas devem ser do mesmo tipo de geometria!", level=QgsMessageBar.CRITICAL, duration=5)
else:
# Colar feicoes da camada de origem para a camada de destino reprojetando as coordenadas, se necessario
feature = QgsFeature()
if SRC_origem == SRC_destino:
for feat in origem.getFeatures():
att = feat.attributes()
new_att = []
for k, item in enumerate(mapa_campo):
if item != None:
new_att += [att[item]]
else:
new_att += [DP.defaultValue(k)]
geom = feat.geometry()
feature.setAttributes(new_att)
feature.setGeometry(geom)
ok=DP.addFeatures([feature])
if not ok:
progress.setText('<br/>Feicao de ID %d nao foi copiada' %feat.id())
QgsMessageLog.logMessage('<br/>Feicao de ID %d nao foi copiada' %feat.id())
else:
for feat in origem.getFeatures():
att = feat.attributes()
new_att = []
for k, item in enumerate(mapa_campo):
if item != None:
new_att += [att[item]]
else:
new_att += [DP.defaultValue(k)]
geom = feat.geometry()
newGeom = reprojetar(geom)
feature.setAttributes(new_att)
feature.setGeometry(newGeom)
ok=DP.addFeatures([feature])
if not ok:
progress.setText('<br/>Feicao de ID %d nao foi copiada' %feat.id())
QgsMessageLog.logMessage('<br/>Feicao de ID %d nao foi copiada' %feat.id())
progress.setInfo('<br/><br/><b>Operacao concluida com sucesso!</b>')
progress.setInfo('<br/><b>Leandro França - Eng Cart</b><br/>')
time.sleep(5)
iface.messageBar().pushMessage(u'Situacao', "Operacao Concluida com Sucesso!", level=QgsMessageBar.INFO, duration=5)