forked from flashlxy/PyMICAPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Projection.py
310 lines (290 loc) · 10.1 KB
/
Projection.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# -*- coding: utf-8 -*-
# 投影类
# Author: Liu xianyao
# Email: flashlxy@qq.com
# Update: 2017-04-06
# Copyright: ©江西省气象台 2017
# Version: 3.0.20191120
from __future__ import print_function
from __future__ import print_function
from mpl_toolkits.basemap import Basemap
from Main import parseInt
from pylab import *
import matplotlib.pyplot as plt
class Projection:
def __init__(self, root):
leaf = root.find("Projection")
if leaf is None:
self.name = "sall"
self.name = self.leaf_to_string(leaf, "Name", "sall")
self.lon_0 = self.leaf_to_float(leaf, "Lon_0")
self.lat_0 = self.leaf_to_float(leaf, "Lat_0")
self.lat_ts = self.leaf_to_float(leaf, "Lat_ts")
self.boundinglat = self.leaf_to_float(leaf, "BoundingLat")
self.llcrnrlat = self.leaf_to_float(leaf, "LlcrnrLat")
self.llcrnrlon = self.leaf_to_float(leaf, "LlcrnrLon")
self.urcrnrlat = self.leaf_to_float(leaf, "UrcrnrLat")
self.urcrnrlon = self.leaf_to_float(leaf, "UrcrnrLon")
if self.lon_0 is None or self.lat_0 is None:
self.lon_0 = None
self.lat_0 = None
if (
self.llcrnrlat is None
or self.llcrnrlon is None
or self.urcrnrlat is None
or self.urcrnrlon is None
):
self.llcrnrlat = None
self.llcrnrlon = None
self.urcrnrlat = None
self.urcrnrlon = None
self.coastlines = self.leaf_to_bool(leaf=leaf, code="Coastlines")
self.countries = self.leaf_to_bool(leaf=leaf, code="Countries")
subleaf = leaf.find("Lsmask")
if subleaf is None:
self.lsmask = {
"visible": False,
"land_color": "#BF9E30",
"ocean_color": "#689CD2",
}
else:
self.lsmask = {
"visible": self.leaf_to_bool(leaf=subleaf, code="Visible"),
"land_color": self.leaf_to_string(subleaf, "Land_color", "#BF9E30"),
"ocean_color": self.leaf_to_string(subleaf, "Ocean_color", "#689CD2"),
}
self.axis = Projection.leaf_to_string(leaf=leaf, code="Axis", defvalue="off")
if self.axis not in ["on", "off"]:
self.axis = "off"
self.axisthick = self.leaf_to_float(leaf, "AxisThick", 1.0)
self.axisfmt = Projection.leaf_to_list(
leaf=leaf, code="AxisFmt", defvalue=["%d°E", "%d°N"]
)
self.latlabels = Projection.leaf_to_list(
leaf=leaf, code="LatLabels", defvalue=[0, 0, 0, 0]
)
self.lonlabels = Projection.leaf_to_list(
leaf=leaf, code="LonLabels", defvalue=[0, 0, 0, 0]
)
@staticmethod
def ValidExtents(extents):
for extent in extents:
if extent is None:
return False
return True
@staticmethod
def GetProjection(products):
"""
根据获得产品参数获得投影后的画布对象
:param products: 产品参数
:return: 画布对象
"""
xmax = products.picture.extents.xmax
xmin = products.picture.extents.xmin
ymax = products.picture.extents.ymax
ymin = products.picture.extents.ymin
lon_0 = xmin + (xmax - xmin) / 2
lat_0 = ymin + (ymax - ymin) / 2
projection = products.map.projection
pjname = projection.name
if Projection.ValidExtents((projection.lon_0, projection.lon_0)):
lon_0 = projection.lon_0
lat_0 = projection.lat_0
if Projection.ValidExtents(
(
projection.llcrnrlat,
projection.llcrnrlat,
projection.urcrnrlat,
projection.urcrnrlon,
)
):
xmax = projection.urcrnrlon
xmin = projection.llcrnrlon
ymax = projection.urcrnrlat
ymin = projection.llcrnrlat
lat_ts = projection.lat_ts
if lat_ts is None:
lat_ts = lat_0
boundinglat = projection.boundinglat
if boundinglat is None:
boundinglat = 0
if pjname == "sall":
m = plt
elif pjname == "cyl":
m = Basemap(
projection="cyl",
llcrnrlat=ymin,
urcrnrlat=ymax,
llcrnrlon=xmin,
urcrnrlon=xmax,
lon_0=lon_0,
lat_0=lat_0,
)
elif pjname == "mill":
m = Basemap(
projection="mill",
llcrnrlon=xmin,
llcrnrlat=ymin,
urcrnrlon=xmax,
urcrnrlat=ymax,
)
elif pjname == "gnom":
m = Basemap(
projection="gnom",
llcrnrlon=xmin,
llcrnrlat=ymin,
urcrnrlon=xmax,
urcrnrlat=ymax,
lat_0=lat_0,
lon_0=lon_0,
)
elif pjname == "ortho":
m = Basemap(projection="ortho", lat_0=lat_0, lon_0=lon_0, resolution="l")
elif pjname == "hammer":
m = Basemap(projection="hammer", lon_0=lon_0)
elif pjname == "kav7":
m = Basemap(projection="kav7", lon_0=lon_0, resolution=None)
elif pjname == "merc":
ymin = ymin if ymin >= -80 else -80
ymax = ymax if ymax <= 80 else 80
lat_ts = lat_ts if lat_ts < 90 else 80.0
m = Basemap(
llcrnrlon=xmin,
llcrnrlat=ymin,
urcrnrlon=xmax,
urcrnrlat=ymax,
rsphere=(6378137.00, 6356752.3142),
resolution="l",
projection="merc",
lat_0=lat_0,
lon_0=lon_0,
lat_ts=lat_ts,
)
elif pjname == "lcc":
# lat_1=lat_0, lat_2=60, lat_0=lat_0, lon_0=lon_0 width=12000000, height=9000000,
# lon_0 = 120.
# lat_0 = 90.
# ymin = 5.13
# ymax = 53.51
# xmin = 85.86
# xmax = 174.69
m = Basemap(
projection="lcc",
# width=w, height=h,
llcrnrlon=xmin,
llcrnrlat=ymin,
urcrnrlon=xmax,
urcrnrlat=ymax,
rsphere=(6378137.00, 6356752.3142),
resolution="l",
area_thresh=1000.0,
lat_2=60,
lat_1=30,
lon_0=lon_0,
lat_0=lat_0,
)
elif pjname == "stere":
# lon_0 = 116.
# lat_0 = 90.
# ymin = 5.13
# ymax = 53.51
# xmin = 85.86
# xmax = 174.69
m = Basemap(
projection="stere",
lon_0=lon_0,
lat_0=lat_0,
lat_ts=lat_ts,
llcrnrlat=ymin,
urcrnrlat=ymax,
llcrnrlon=xmin,
urcrnrlon=xmax,
# boundinglat=ymin,
# width=w, height=h,
rsphere=6371200.0,
resolution="l",
area_thresh=10000,
)
elif pjname == "npstere":
m = Basemap(
projection="npstere",
lon_0=lon_0,
lat_0=lat_0,
lat_ts=lat_ts,
boundinglat=boundinglat,
# llcrnrlat=ymin, urcrnrlat=ymax,
# llcrnrlon=xmin, urcrnrlon=xmax,
# width=12000000, height=8000000,
rsphere=6371200.0,
area_thresh=10000,
)
else:
m = plt
return m
@staticmethod
def leaf_to_float(leaf, code, defvalue=None):
#
try:
tpos = leaf.find(code)
if tpos is None or tpos.text.strip() == "":
return defvalue
else:
return float(tpos.text.strip())
except:
return defvalue
@staticmethod
def leaf_to_string(leaf, code, defvalue=None):
#
try:
tpos = leaf.find(code)
if tpos is None or tpos.text.strip() == "":
return defvalue
else:
return tpos.text.strip()
except Exception as err:
return defvalue
@staticmethod
def leaf_to_bool(leaf, code, defvalue=False, ok="ON"):
try:
tpos = leaf.find(code)
if tpos is None or tpos.text.strip() == "":
return defvalue
else:
return str.upper(tpos.text.strip()) == ok
except Exception as err:
return defvalue
@staticmethod
def leaf_to_int(leaf, code, defvalue=None):
#
try:
tpos = leaf.find(code)
if tpos is None or tpos.text.strip() == "":
return defvalue
else:
return parseInt(tpos.text.strip())
except Exception as err:
return defvalue
@staticmethod
def leaf_to_list(leaf, code, defvalue=None):
#
try:
tpos = leaf.find(code)
if tpos is None or tpos.text.strip() == "":
return defvalue
else:
labels = eval("[{0}]".format(tpos.text.strip()))
return labels
except Exception as err:
return defvalue
@staticmethod
def leaf_to_dict(leaf, code, defvalue=None):
#
try:
tpos = leaf.find(code)
if tpos is None or tpos.text.strip() == "":
return defvalue
else:
labels = eval("{0}".format(tpos.text.strip()))
return labels
except Exception as err:
return defvalue