-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
218 lines (187 loc) · 7.32 KB
/
utils.py
File metadata and controls
218 lines (187 loc) · 7.32 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
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
import os
from qgis.core import QgsProcessingFeedback
from PyQt5.QtWidgets import QMessageBox
class MyFeedBack(QgsProcessingFeedback):
def setProgressText(self, text):
print(text)
def pushInfo(self, info):
print(info)
def pushCommandInfo(self, info):
print(info)
def pushDebugInfo(self, info):
print(info)
def pushConsoleInfo(self, info):
print(info)
def reportError(self, error, fatalError=False):
print(error)
def process_vector_layer(in_layer, longitude_range):
import processing
from qgis.core import QgsProcessingException
# local variables
extent1 = None
extent2 = None
delta_x = None
# set up an empty message box
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper")
msg.setStandardButtons(QMessageBox.Ok)
if longitude_range == '360':
extent1 = '-180, 0, -90, 90'
extent2 = '0, 180, -90, 90'
delta_x = 360
elif longitude_range == '180':
extent1 = '180, 360, -90, 90'
extent2 = '0, 180, -90, 90'
delta_x = -360
else:
print("something went wrong with the longitude range variable")
# clip left side
params = dict()
params['INPUT'] = in_layer
params['EXTENT'] = extent1
params['CLIP'] = True
params['OUTPUT'] = 'memory:'
try:
part1 = processing.run("native:extractbyextent", params, feedback=MyFeedBack())
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# clip right side
params = dict()
params['INPUT'] = in_layer
params['EXTENT'] = extent2
params['CLIP'] = True
params['OUTPUT'] = 'memory:'
try:
part2 = processing.run("native:extractbyextent", params)
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# do the wrapping part
params = dict()
params["INPUT"] = part1['OUTPUT']
params['DELTA_X'] = int(delta_x)
params['OUTPUT'] = 'memory:'
try:
part1 = processing.run("native:translategeometry", params)
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# append part2 to part1
params = dict()
params['LAYERS'] = [part1['OUTPUT'], part2['OUTPUT']]
params['OUTPUT'] = 'memory:'
try:
output = processing.run("native:mergevectorlayers", params)
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
return output['OUTPUT']
def process_vector_file(in_file, longitude_range):
from qgis.core import QgsVectorLayer
vector_layer = QgsVectorLayer(in_file)
return process_vector_layer(vector_layer, longitude_range)
def process_raster_file(in_file, longitude_range, out_file):
import tempfile
import processing
from qgis.core import QgsRasterLayer
from qgis.core import QgsProcessingException
# create a temporary folder for intermediate datasets
with tempfile.TemporaryDirectory() as temp_dir:
# local variables
projwin1 = None
projwin2 = None
extent2_shift = None
if longitude_range == "180":
projwin1 = '0, 180, -90, 90' # comma delimited list of x min, x max, y min, y max.
projwin2 = '180, 360, -90, 90'
extent2_shift = ' -180, 0, -90, 90'
elif longitude_range == "360":
projwin1 = '0, 180, -90, 90'
projwin2 = '-180, 0, -90, 90'
extent2_shift = '180, 360, -90, 90'
part1_file = os.path.join(temp_dir, "pt1.tif")
part2_file = os.path.join(temp_dir, "pt2.tif")
part2_shift_file = os.path.join(temp_dir, "pt2s.tif")
vrt_file = os.path.join(temp_dir, "merge.vrt")
if not os.path.exists(out_file):
# clip part 1
params = dict()
params['INPUT'] = in_file
params['PROJWIN'] = projwin1
params['OUTPUT'] = part1_file
try:
processing.run("gdal:cliprasterbyextent", params, feedback=MyFeedBack())
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# clip part 2
params = dict()
params['INPUT'] = in_file
params['PROJWIN'] = projwin2
params['OUTPUT'] = part2_file
try:
processing.run("gdal:cliprasterbyextent", params, feedback=MyFeedBack())
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# move part 2
params = dict()
params['INPUT'] = part2_file
params['TARGET_CRS'] = part2_file
params['TARGET_EXTENT'] = extent2_shift
params['TARGET_EXTENT_CRS'] = 'EPSG:4326'
params['OUTPUT'] = part2_shift_file
try:
processing.run("gdal:warpreproject", params, feedback=MyFeedBack())
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# make virtual raster with both parts
params = dict()
params['INPUT'] = [part1_file, part2_shift_file]
params['SEPARATE'] = False
params['OUTPUT'] = vrt_file
try:
processing.run("gdal:buildvirtualraster", params, feedback=MyFeedBack())
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
# make write virtual raster to tif
params = dict()
params['INPUT'] = vrt_file
params['OUTPUT'] = out_file
try:
processing.run("gdal:translate", params, feedback=MyFeedBack())
except QgsProcessingException:
msg = QMessageBox()
msg.setIcon(QMessageBox.Warning)
msg.setWindowTitle("Geometry Wrapper Error")
msg.setText("Error: open python console to view error")
return None
return QgsRasterLayer(out_file, baseName=os.path.basename(out_file))