-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunc_clip_class.py
228 lines (198 loc) · 8.2 KB
/
Func_clip_class.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os, sys
from qgis.core import QgsProject, QgsApplication, QgsVectorLayer, QgsRasterLayer
from qgis.gui import QgsMapCanvas, QgsMapToolPan, QgsMapToolZoom, QgsMapToolIdentify
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from Dlg_clip import Ui_Dialog as Dlg_clip_class
import gdal
import numpy as np
import cv2
class Func_clip_class(QDialog, Dlg_clip_class):
mySignal = pyqtSignal(int)
def __init__(self, layers):
super(Func_clip_class, self).__init__()
self.setupUi(self)
self.success = False
self.show()
# 对图层进行处理,处理成cv的numpy数组
self.bandName = []
self.bands = []
self.projections = []
self.geoTransform = []
self.layers = []
self.bandcount = []
self.index = 0
for i in range(0, len(layers)):
self.layers.append(layers[i])
tempCount = layers[i].bandCount()
self.bandcount.append(tempCount)
tempName = layers[i].name()
tempDs = gdal.Open(layers[i].source())
for j in range(1, tempCount + 1):
self.bandName.append(tempName + '@' + str(j))
tempband = np.array(tempDs.GetRasterBand(j).ReadAsArray())
self.bands.append(tempband)
self.projections.append(tempDs.GetProjection())
self.geoTransform.append(tempDs.GetGeoTransform())
for i in range(0, len(layers)):
tempName = layers[i].name()
self.comboBox_selectlayer.addItem(tempName)
self.init_mapcanvas()
self.loadMap2(layers[0])
self.pushButton.clicked.connect(self.action)
self.pushButton_show.clicked.connect(self.action_show_clicked)
self.comboBox_selectlayer.currentIndexChanged.connect(self.action_comboBox_changed)
def action_comboBox_changed(self):
self.index = self.comboBox_selectlayer.currentIndex()
print("currnetIndex")
print(self.index)
self.loadMap2(layer=self.layers[self.index])
def action(self):
self.Func_clip_process()
print("OK")
if self.checkBox.isChecked():
self.result = QgsRasterLayer(self.path, "result_clip")
self.mySignal.emit(1)
self.close()
def action_show_clicked(self):
print("show")
y0 = int(self.plainTextEdit_y0.toPlainText())
y1 = int(self.plainTextEdit_y1.toPlainText())
x0 = int(self.plainTextEdit_x0.toPlainText())
x1 = int(self.plainTextEdit_x1.toPlainText())
if not any([y0, y1, x0, x1]):
print("Height or Width setValue error!")
else:
print(x0, x1, y0, y1)
bandName = []
bands = []
projections = []
geoTransform = []
layers = self.layers
print("layers: {0}".format(layers))
index = self.index
print("index:{0}".format(self.index))
Count = layers[index].bandCount()
print("the layer band count:{0}".format(Count))
Name = layers[index].name()
Ds = gdal.Open(layers[index].source())
for j in range(1, Count + 1):
bandName.append(Name + '@' + str(j))
band = np.array(Ds.GetRasterBand(j).ReadAsArray())
bands.append(band)
projections.append(Ds.GetProjection())
geoTransform.append(Ds.GetGeoTransform())
print(bands)
if self.bandcount[self.index] == 1:
img1 = cv2.merge([bands[0]])
print("shape:{0}".format(img1.shape))
cropped1 = img1[y0:y1, x0:x1]
cv2.imshow('img1', cropped1)
else:
img2 = cv2.merge([bands[0], bands[1], bands[2]])
print("shape:{0}".format(img2.shape))
cropped2 = img2[y0:y1, x0:x1]
cv2.imshow('img2', cropped2)
def Func_clip_process(self):
print("This is func_clip, process starts now.")
y0 = int(self.plainTextEdit_y0.toPlainText())
y1 = int(self.plainTextEdit_y1.toPlainText())
x0 = int(self.plainTextEdit_x0.toPlainText())
x1 = int(self.plainTextEdit_x1.toPlainText())
if not any([y0, y1, x0, x1]):
print("Height or Width setValue error!")
else:
print(x0, x1, y0, y1)
bandName = []
bands = []
projections = []
geoTransform = []
layers = self.layers
print("layers: {0}".format(layers))
index = self.index
print("index:{0}".format(self.index))
Count = layers[index].bandCount()
print("the layer band count:{0}".format(Count))
Name = layers[index].name()
Ds = gdal.Open(layers[index].source())
for j in range(1, Count + 1):
bandName.append(Name + '@' + str(j))
band = np.array(Ds.GetRasterBand(j).ReadAsArray())
bands.append(band)
projections.append(Ds.GetProjection())
geoTransform.append(Ds.GetGeoTransform())
print(bands)
if self.bandcount[self.index] == 1:
img1 = cv2.merge([bands[0]])
print("shape:{0}".format(img1.shape))
cropped1 = img1[y0:y1, x0:x1]
# cv2.imshow('img1', img1)
path = "processImage/result_clip1.tif"
cv2.imwrite(path, cropped1)
print("img1 ouput success")
# self.result = QgsRasterLayer(path, "result_clip")
self.path = path
else:
img2 = cv2.merge([bands[0], bands[1], bands[2]])
print("shape:{0}".format(img2.shape))
cropped2 = img2[y0:y1, x0:x1]
# cv2.imshow('crp',cropped)
# cv2.imshow('img2', img2)
path = "processImage/result_clip2.tif"
self.create_tiff(path, projections[0], geoTransform[0], cropped2)
print("img2 ouput success")
# self.result = QgsRasterLayer(path, "result_clip")
self.path = path
def create_tiff(self, fullPath, projection, geoTransform, data):
driver = gdal.GetDriverByName("GTiff")
if 'int8' in data.dtype.name:
datatype = gdal.GDT_Byte
elif 'int16' in data.dtype.name:
datatype = gdal.GDT_UInt16
else:
datatype = gdal.GDT_Float32
outDs = driver.Create(fullPath, data.shape[1], data.shape[0], data.shape[2], datatype)
outDs.SetGeoTransform(geoTransform)
outDs.SetProjection(projection)
outDs.GetRasterBand(1).WriteArray(data[:, :, 2])
outDs.GetRasterBand(2).WriteArray(data[:, :, 1])
outDs.GetRasterBand(3).WriteArray(data[:, :, 0])
del outDs
# 画布初始化
def init_mapcanvas(self):
# 实例化地图画布
self.mapCanvas = QgsMapCanvas()
self.mapCanvas.setCanvasColor(Qt.white)
# self.mapCanvas.show()
layout = QVBoxLayout(self.widget)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.mapCanvas)
# 文件操作
def loadMap2(self, layer):
print("loadMap2")
# 打开栅格图层
self.layer = layer
# 注册图层
QgsProject.instance().addMapLayer(self.layer)
self.mapCanvas.setLayers([self.layer])
# 设置图层范围
self.mapCanvas.setExtent(self.layer.extent())
self.mapCanvas.refresh()
def main():
# 实例化 QGIS 应用对象
qgs = QgsApplication([], True)
qgs.setPrefixPath('qgis', True)
# 启动 QGIS
qgs.initQgis()
layers = []
layers.append(QgsRasterLayer("./Image/1.tif", "1"))
layers.append(QgsRasterLayer("./Image/2.tif", "2"))
window = Func_clip_class(layers)
# window.exec_()
exit_code = qgs.exec_()
# 退出 QGIS
qgs.exitQgis()
sys.exit(exit_code)
if __name__ == '__main__':
main()