-
Notifications
You must be signed in to change notification settings - Fork 157
/
Map.py
597 lines (549 loc) · 23.4 KB
/
Map.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# -*- coding: utf-8 -*-
# Map类
# Author: Liu xianyao
# Email: [email protected]
# Update: 2019-11-20
# Copyright: ©江西省气象台 2017
# Version: 3.0.20191120
from __future__ import print_function
import os
from datetime import datetime
import matplotlib
import matplotlib.image as img
from matplotlib.collections import LineCollection
from matplotlib.font_manager import FontProperties
from matplotlib.offsetbox import OffsetImage, AnnotationBbox
from matplotlib.path import Path
from matplotlib.ticker import FormatStrFormatter
from mpl_toolkits.axes_grid1.inset_locator import inset_axes
import shapefile
from matplotlib import patches
from Border import Border
from ClipBorder import ClipBorder
from Projection import Projection
import matplotlib.pyplot as plt
import numpy as np
class Map:
"""
地图类
"""
def __init__(self, root):
# 地图投影
self.projection = Projection(root[0])
# 边界集合
bordersleaf = list(root[0].find("Borders"))
self.borders = []
for borderleaf in bordersleaf:
self.borders.append(Border(borderleaf))
# clip区域集合
clipsleaf = list(root[0].find("ClipBorders"))
self.clipborders = []
for clipleaf in clipsleaf:
self.clipborders.append(ClipBorder(clipleaf))
# 站点文件
from Stations import Stations
self.stations = Stations(root[0])
pass
@staticmethod
def DrawContourfAndLegend(
contourf, legend, clipborder, patch, cmap, levels, extend, extents, x, y, z, m
):
"""
:param contourf:
:param legend:
:param clipborder:
:param patch:
:param cmap:
:param levels:
:param extend:
:param extents:
:param x:
:param y:
:param z:
:param m:
:return:
"""
# 是否绘制色斑图 ------ 色版图、图例、裁切是一体的
xmax = extents.xmax
xmin = extents.xmin
ymax = extents.ymax
ymin = extents.ymin
if contourf.contourfvisible:
# 绘制色斑图
if legend.micapslegendvalue:
CS = m.contourf(x, y, z, cmap=cmap, levels=levels, extend=extend)
else:
CS = m.contourf(
x,
y,
z, # cax=axins,
levels=legend.legendvalue,
colors=legend.legendcolor,
extend=extend,
hatches=legend.hatches,
)
# 用区域边界裁切色斑图
if clipborder.path is not None and clipborder.using:
for collection in CS.collections:
collection.set_clip_on(True)
collection.set_clip_path(patch)
CB = None
if m is plt:
# 插入一个新坐标系 以使图例在绘图区内部显示
ax2 = plt.gca()
axins = inset_axes(ax2, width="100%", height="100%", loc=1, borderpad=0)
axins.axis("off")
axins.margins(0, 0)
axins.xaxis.set_ticks_position("bottom")
axins.yaxis.set_ticks_position("left")
axins.set_xlim(xmin, xmax)
axins.set_ylim(ymin, ymax)
# 画图例
if legend.islegendpic:
# 插入图片
arr_lena = img.imread(legend.legendpic)
image_box = OffsetImage(arr_lena, zoom=legend.legendopacity)
ab = AnnotationBbox(image_box, legend.legendpos, frameon=False)
plt.gca().add_artist(ab)
else:
fmt = None
CB = plt.colorbar(
CS,
cmap="RdBu",
anchor=tuple(legend.anchor),
shrink=legend.shrink,
# ticks=ticks,
# fraction=0.15, # products.fraction,
drawedges=True, # not products.micapslegendvalue,
filled=False,
spacing="uniform",
use_gridspec=False,
orientation=legend.orientation,
# extendfrac='auto',
format=fmt,
)
else:
CB = m.colorbar(
CS, location=legend.location, size=legend.size, pad=legend.pad
)
if CB is not None:
fp = Map.GetFontProperties(legend.font)
fp_title = Map.GetFontProperties(legend.titlefont)
CB.set_label(
legend.title,
fontproperties=fp_title,
color=legend.titlefont["color"],
)
ylab = CB.ax.yaxis.get_label()
ylab.set_rotation(legend.titlepos["rotation"])
ylab.set_va(legend.titlepos["va"])
ylab.set_ha(legend.titlepos["ha"])
ylab.set_y(legend.titlepos["ypercent"])
if not legend.micapslegendvalue and legend.legendvaluealias:
legendvalue = [
v
for i, v in enumerate(legend.legendvalue)
if i % legend.thinning == 0
]
legendvaluealias = [
v
for i, v in enumerate(legend.legendvaluealias)
if i % legend.thinning == 0
]
CB.set_ticks(legendvalue, update_ticks=True)
CB.set_ticklabels(legendvaluealias, update_ticks=True)
CB.ax.tick_params(axis="y", direction="in", length=0)
for label in (
CB.ax.xaxis.get_ticklabels() + CB.ax.yaxis.get_ticklabels()
):
label.set_color(legend.font["color"])
label.set_fontproperties(fp)
@staticmethod
def DrawContourAndMark(contour, x, y, z, level, clipborder, patch, m):
# 是否绘制等值线 ------ 等值线和标注是一体的
if contour.contour["visible"]:
matplotlib.rcParams["contour.negative_linestyle"] = "dashed"
if contour.contour["colorline"]:
CS1 = m.contour(
x, y, z, levels=level, linewidths=contour.contour["linewidth"]
)
else:
CS1 = m.contour(
x,
y,
z,
levels=level,
linewidths=contour.contour["linewidth"],
colors=contour.contour["linecolor"],
)
# 是否绘制等值线标注
CS2 = None
if contour.contourlabel["visible"]:
CS2 = plt.clabel(
CS1,
inline=1,
fmt=contour.contourlabel["fmt"],
inline_spacing=contour.contourlabel["inlinespacing"],
fontsize=contour.contourlabel["fontsize"],
colors=contour.contourlabel["fontcolor"],
)
# 用区域边界裁切等值线图
if clipborder.path is not None and clipborder.using:
for collection in CS1.collections:
# collection.set_clip_on(True)
collection.set_clip_path(patch)
if CS2 is not None:
for text in CS2:
if not clipborder.path.contains_point(text.get_position()):
text.remove()
@staticmethod
def DrawClipBorders(clipborders):
# 绘制裁切区域边界并返回
path = clipborders[0].path
linewidth = clipborders[0].linewidth
linecolor = clipborders[0].linecolor
if path is not None:
patch = patches.PathPatch(
path, linewidth=linewidth, facecolor="none", edgecolor=linecolor
)
plt.gca().add_patch(patch)
else:
patch = None
return patch
@staticmethod
def DrawBorders(m, products):
"""
画县市边界
:param m: 画布对象(plt或投影后的plt)
:param products: 产品参数
:return:
"""
try:
for area in products.map.borders:
if not area.draw:
continue
if area.filetype == "SHP": # shp文件
if m is plt:
# Map.DrawShapeFile(area)
Map.readshapefile(
area.file.replace(".shp", ""),
os.path.basename(area.file),
color=area.linecolor,
linewidth=area.linewidth,
default_encoding=area.encoding,
)
else:
m.readshapefile(
area.file.replace(".shp", ""),
os.path.basename(area.file),
color=area.linecolor,
)
else: # 文本文件 , 画之前 路径中的点已经被投影了
if area.path is None:
continue
if area.polygon == "ON":
area_patch = patches.PathPatch(
area.path,
linewidth=area.linewidth,
linestyle="solid",
facecolor="none",
edgecolor=area.linecolor,
)
plt.gca().add_patch(area_patch)
else:
x, y = list(zip(*area.path.vertices))
m.plot(
x, y, "k-", linewidth=area.linewidth, color=area.linecolor
)
except Exception as err:
print("【{0}】{1}-{2}".format(products.xmlfile, err, datetime.now()))
@staticmethod
def DrawShapeFile(area):
"""
在画布上绘制shp文件
:param area: 包含shp文件名及线宽和线颜色的一个字典
:return:
"""
try:
shpfile = area.file
border_shape = shapefile.Reader(shpfile)
border = border_shape.shapes()
for b in border:
border_points = b.points
path_data = []
count = 0
for cell in border_points:
if count == 0:
trans = (Path.MOVETO, (cell[0], cell[1]))
path_data += [trans]
cell_end = cell
else:
trans = (Path.CURVE4, (cell[0], cell[1]))
path_data += [trans]
trans = (Path.CLOSEPOLY, (cell_end[0], cell_end[1]))
path_data += [trans]
codes, verts = list(zip(*path_data))
path = Path(verts, codes)
x, y = list(zip(*path.vertices))
plt.plot(x, y, "k-", linewidth=area.linewidth, color=area.linecolor)
except Exception as err:
print("【{0}】{1}-{2}".format(area["file"], err, datetime.now()))
@staticmethod
def DrawWorld(products, m):
pj = products.map.projection
if pj.coastlines:
m.drawcoastlines(linewidth=0.25)
if pj.countries:
m.drawcountries(linewidth=0.25)
if pj.lsmask["visible"]:
m.drawlsmask(
land_color=pj.lsmask["land_color"],
ocean_color=pj.lsmask["ocean_color"],
resolution="l",
)
@staticmethod
def DrawGridLine(products, m):
pj = products.map.projection
if m is plt:
# 坐标轴
plt.axis(pj.axis)
# 设置坐标轴刻度值显示格式
if pj.axis == "on":
x_majorFormatter = FormatStrFormatter(pj.axisfmt[0])
y_majorFormatter = FormatStrFormatter(pj.axisfmt[1])
plt.gca().xaxis.set_major_formatter(x_majorFormatter)
plt.gca().yaxis.set_major_formatter(y_majorFormatter)
xaxis = plt.gca().xaxis
for label in xaxis.get_ticklabels():
label.set_fontproperties("DejaVu Sans")
label.set_fontsize(10)
yaxis = plt.gca().yaxis
for label in yaxis.get_ticklabels():
label.set_fontproperties("DejaVu Sans")
label.set_fontsize(10)
xaxis.set_visible(pj.lonlabels[3] == 1)
yaxis.set_visible(pj.latlabels[0] == 1)
return
else:
# draw parallels and meridians.
if pj.axis == "on":
m.drawparallels(
np.arange(-80.0, 81.0, 10.0),
labels=pj.latlabels,
family="DejaVu Sans",
fontsize=10,
)
m.drawmeridians(
np.arange(-180.0, 181.0, 10.0),
labels=pj.lonlabels,
family="DejaVu Sans",
fontsize=10,
)
@staticmethod
def GetFontProperties(font):
fontfile = r"C:\WINDOWS\Fonts\{0}".format(font["family"])
if not os.path.exists(fontfile):
fp = FontProperties(
family=font["family"], weight=font["weight"], size=font["size"]
)
else:
fp = FontProperties(
fname=fontfile, weight=font["weight"], size=font["size"]
)
return fp
@staticmethod
def readshapefile(
shapefile,
name,
is_web_merc=False,
drawbounds=True,
zorder=None,
linewidth=0.5,
linestyle=(0, ()),
color="k",
antialiased=1,
ax=None,
default_encoding="utf-8",
):
"""
Read in shape file, optionally draw boundaries on map.
.. note::
- Assumes shapes are 2D
- only works for Point, MultiPoint, Polyline and Polygon shapes.
- vertices/points must be in geographic (lat/lon) coordinates.
Mandatory Arguments:
.. tabularcolumns:: |l|L|
============== ====================================================
Argument Description
============== ====================================================
shapefile path to shapefile components. Example:
shapefile='/home/jeff/esri/world_borders' assumes
that world_borders.shp, world_borders.shx and
world_borders.dbf live in /home/jeff/esri.
name name for Basemap attribute to hold the shapefile
vertices or points in map projection
coordinates. Class attribute name+'_info' is a list
of dictionaries, one for each shape, containing
attributes of each shape from dbf file, For
example, if name='counties', self.counties
will be a list of x,y vertices for each shape in
map projection coordinates and self.counties_info
will be a list of dictionaries with shape
attributes. Rings in individual Polygon
shapes are split out into separate polygons, and
additional keys 'RINGNUM' and 'SHAPENUM' are added
to the shape attribute dictionary.
============== ====================================================
The following optional keyword arguments are only relevant for Polyline
and Polygon shape types, for Point and MultiPoint shapes they are
ignored.
.. tabularcolumns:: |l|L|
============== ====================================================
Keyword Description
============== ====================================================
drawbounds draw boundaries of shapes (default True).
zorder shape boundary zorder (if not specified,
default for mathplotlib.lines.LineCollection
is used).
linewidth shape boundary line width (default 0.5)
color shape boundary line color (default black)
antialiased antialiasing switch for shape boundaries
(default True).
ax axes instance (overrides default axes instance)
============== ====================================================
A tuple (num_shapes, type, min, max) containing shape file info
is returned.
num_shapes is the number of shapes, type is the type code (one of
the SHPT* constants defined in the shapelib module, see
http://shapelib.maptools.org/shp_api.html) and min and
max are 4-element lists with the minimum and maximum values of the
vertices. If ``drawbounds=True`` a
matplotlib.patches.LineCollection object is appended to the tuple.
"""
import shapefile as shp
from shapefile import Reader
shp.default_encoding = default_encoding
if not os.path.exists("%s.shp" % shapefile):
raise IOError("cannot locate %s.shp" % shapefile)
if not os.path.exists("%s.shx" % shapefile):
raise IOError("cannot locate %s.shx" % shapefile)
if not os.path.exists("%s.dbf" % shapefile):
raise IOError("cannot locate %s.dbf" % shapefile)
# open shapefile, read vertices for each object, convert
# to map projection coordinates (only works for 2D shape types).
try:
shf = Reader(shapefile, encoding=default_encoding)
except:
raise IOError("error reading shapefile %s.shp" % shapefile)
fields = shf.fields
coords = []
attributes = []
msg = """
shapefile must have lat/lon vertices - it looks like this one has vertices
in map projection coordinates. You can convert the shapefile to geographic
coordinates using the shpproj utility from the shapelib tools
(http://shapelib.maptools.org/shapelib-tools.html)"""
shapes = shf.shapes()
if len(shapes) == 0:
raise IndexError("%s shapes is null" % shapefile)
shptype = shapes[0].shapeType
bbox = shf.bbox.tolist()
info = dict()
info["info"] = (
shf.numRecords,
shptype,
bbox[0:2] + [0.0, 0.0],
bbox[2:] + [0.0, 0.0],
)
npoly = 0
for shprec in shf.shapeRecords():
shp = shprec.shape
rec = shprec.record
npoly = npoly + 1
if shptype != shp.shapeType:
raise ValueError(
"readshapefile can only handle a single shape type per file"
)
if shptype not in [1, 3, 5, 8]:
raise ValueError("readshapefile can only handle 2D shape types")
verts = shp.points
if shptype in [1, 8]: # a Point or MultiPoint shape.
lons, lats = list(zip(*verts))
if (
max(lons) > 721.0
or min(lons) < -721.0
or max(lats) > 90.01
or min(lats) < -90.01
):
raise ValueError(msg)
# if latitude is slightly greater than 90, truncate to 90
lats = [max(min(lat, 90.0), -90.0) for lat in lats]
if len(verts) > 1: # MultiPoint
if is_web_merc:
# x, y = Projection.ToMerc(lons, lats)
pass
else:
x, y = lons, lats
coords.append(list(zip(x, y)))
else: # single Point
if is_web_merc:
pass
# x, y = Projection.ToMerc(lons[0], lats[0])
else:
x, y = lons[0], lats[0]
coords.append((x, y))
attdict = {}
for r, key in zip(rec, fields[1:]):
attdict[key[0]] = r
attributes.append(attdict)
else: # a Polyline or Polygon shape.
parts = shp.parts.tolist()
ringnum = 0
for indx1, indx2 in zip(parts, parts[1:] + [len(verts)]):
ringnum = ringnum + 1
lons, lats = list(zip(*verts[indx1:indx2]))
if (
max(lons) > 721.0
or min(lons) < -721.0
or max(lats) > 90.01
or min(lats) < -90.01
):
raise ValueError(msg)
# if latitude is slightly greater than 90, truncate to 90
lats = [max(min(lat, 90.0), -90.0) for lat in lats]
if is_web_merc:
# x, y = Projection.ToMerc(lons, lats)
pass
else:
x, y = lons, lats
coords.append(list(zip(x, y)))
attdict = {}
for r, key in zip(rec, fields[1:]):
attdict[key[0]] = r
# add information about ring number to dictionary.
attdict["RINGNUM"] = ringnum
attdict["SHAPENUM"] = npoly
attributes.append(attdict)
# draw shape boundaries for polylines, polygons using LineCollection.
if shptype not in [1, 8] and drawbounds:
# get current axes instance (if none specified).
import matplotlib.pyplot as plt
ax = ax or plt.gca()
# make LineCollections for each polygon.
lines = LineCollection(coords, antialiaseds=(antialiased,))
lines.set_color(color)
lines.set_linewidth(linewidth)
lines.set_linestyle(linestyle)
lines.set_label("_nolabel_")
if zorder is not None:
lines.set_zorder(zorder)
ax.add_collection(lines)
# set axes limits to fit map region.
# self.set_axes_limits(ax=ax)
# # clip boundaries to map limbs
# lines,c = self._cliplimb(ax,lines)
# info = info + (lines,)
info["lines"] = lines
info[name] = coords
info[name + "_info"] = attributes
return info