1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
5from __future__ import absolute_import, print_function
7import math
8import random
9import logging
11try:
12 from StringIO import StringIO as BytesIO
13except ImportError:
14 from io import BytesIO
16import numpy as num
18from pyrocko.guts import (Object, Float, Bool, Int, Tuple, String, List,
19 Unicode, Dict)
20from pyrocko.guts_array import Array
21from pyrocko.dataset import topo
22from pyrocko import orthodrome as od
23from . import gmtpy
25try:
26 newstr = unicode
27except NameError:
28 newstr = str
30points_in_region = od.points_in_region
32logger = logging.getLogger('pyrocko.plot.automap')
34earthradius = 6371000.0
35r2d = 180./math.pi
36d2r = 1./r2d
37km = 1000.
38d2m = d2r*earthradius
39m2d = 1./d2m
40cm = gmtpy.cm
43def darken(c, f=0.7):
44 return (c[0]*f, c[1]*f, c[2]*f)
47def corners(lon, lat, w, h):
48 ll_lat, ll_lon = od.ne_to_latlon(lat, lon, -0.5*h, -0.5*w)
49 ur_lat, ur_lon = od.ne_to_latlon(lat, lon, 0.5*h, 0.5*w)
50 return ll_lon, ll_lat, ur_lon, ur_lat
53def extent(lon, lat, w, h, n):
54 x = num.linspace(-0.5*w, 0.5*w, n)
55 y = num.linspace(-0.5*h, 0.5*h, n)
56 slats, slons = od.ne_to_latlon(lat, lon, y[0], x)
57 nlats, nlons = od.ne_to_latlon(lat, lon, y[-1], x)
58 south = slats.min()
59 north = nlats.max()
61 wlats, wlons = od.ne_to_latlon(lat, lon, y, x[0])
62 elats, elons = od.ne_to_latlon(lat, lon, y, x[-1])
63 elons = num.where(elons < wlons, elons + 360., elons)
65 if elons.max() - elons.min() > 180 or wlons.max() - wlons.min() > 180.:
66 west = -180.
67 east = 180.
68 else:
69 west = wlons.min()
70 east = elons.max()
72 return topo.positive_region((west, east, south, north))
75class NoTopo(Exception):
76 pass
79class OutOfBounds(Exception):
80 pass
83class FloatTile(Object):
84 xmin = Float.T()
85 ymin = Float.T()
86 dx = Float.T()
87 dy = Float.T()
88 data = Array.T(shape=(None, None), dtype=float, serialize_as='table')
90 def __init__(self, xmin, ymin, dx, dy, data):
91 Object.__init__(self, init_props=False)
92 self.xmin = float(xmin)
93 self.ymin = float(ymin)
94 self.dx = float(dx)
95 self.dy = float(dy)
96 self.data = data
97 self._set_maxes()
99 def _set_maxes(self):
100 self.ny, self.nx = self.data.shape
101 self.xmax = self.xmin + (self.nx-1) * self.dx
102 self.ymax = self.ymin + (self.ny-1) * self.dy
104 def x(self):
105 return self.xmin + num.arange(self.nx) * self.dx
107 def y(self):
108 return self.ymin + num.arange(self.ny) * self.dy
110 def get(self, x, y):
111 ix = int(round((x - self.xmin) / self.dx))
112 iy = int(round((y - self.ymin) / self.dy))
113 if 0 <= ix < self.nx and 0 <= iy < self.ny:
114 return self.data[iy, ix]
115 else:
116 raise OutOfBounds()
119class City(Object):
120 def __init__(self, name, lat, lon, population=None, asciiname=None):
121 name = newstr(name)
122 lat = float(lat)
123 lon = float(lon)
124 if asciiname is None:
125 asciiname = name.encode('ascii', errors='replace')
127 if population is None:
128 population = 0
129 else:
130 population = int(population)
132 Object.__init__(self, name=name, lat=lat, lon=lon,
133 population=population, asciiname=asciiname)
135 name = Unicode.T()
136 lat = Float.T()
137 lon = Float.T()
138 population = Int.T()
139 asciiname = String.T()
142class Map(Object):
143 lat = Float.T(optional=True)
144 lon = Float.T(optional=True)
145 radius = Float.T(optional=True)
146 width = Float.T(default=20.)
147 height = Float.T(default=14.)
148 margins = List.T(Float.T())
149 illuminate = Bool.T(default=True)
150 skip_feature_factor = Float.T(default=0.02)
151 show_grid = Bool.T(default=False)
152 show_topo = Bool.T(default=True)
153 show_scale = Bool.T(default=False)
154 show_topo_scale = Bool.T(default=False)
155 show_center_mark = Bool.T(default=False)
156 show_rivers = Bool.T(default=True)
157 show_plates = Bool.T(default=False)
158 show_boundaries = Bool.T(default=False)
159 illuminate_factor_land = Float.T(default=0.5)
160 illuminate_factor_ocean = Float.T(default=0.25)
161 color_wet = Tuple.T(3, Int.T(), default=(216, 242, 254))
162 color_dry = Tuple.T(3, Int.T(), default=(172, 208, 165))
163 color_boundaries = Tuple.T(3, Int.T(), default=(1, 1, 1))
164 topo_resolution_min = Float.T(
165 default=40.,
166 help='minimum resolution of topography [dpi]')
167 topo_resolution_max = Float.T(
168 default=200.,
169 help='maximum resolution of topography [dpi]')
170 replace_topo_color_only = FloatTile.T(
171 optional=True,
172 help='replace topo color while keeping topographic shading')
173 topo_cpt_wet = String.T(default='light_sea')
174 topo_cpt_dry = String.T(default='light_land')
175 axes_layout = String.T(optional=True)
176 custom_cities = List.T(City.T())
177 gmt_config = Dict.T(String.T(), String.T())
178 comment = String.T(optional=True)
180 def __init__(self, gmtversion='newest', **kwargs):
181 Object.__init__(self, **kwargs)
182 self._gmt = None
183 self._scaler = None
184 self._widget = None
185 self._corners = None
186 self._wesn = None
187 self._minarea = None
188 self._coastline_resolution = None
189 self._rivers = None
190 self._dems = None
191 self._have_topo_land = None
192 self._have_topo_ocean = None
193 self._jxyr = None
194 self._prep_topo_have = None
195 self._labels = []
196 self._area_labels = []
197 self._gmtversion = gmtversion
199 def save(self, outpath, resolution=75., oversample=2., size=None,
200 width=None, height=None, psconvert=False):
202 '''
203 Save the image.
205 Save the image to ``outpath``. The format is determined by the filename
206 extension. Formats are handled as follows: ``'.eps'`` and ``'.ps'``
207 produce EPS and PS, respectively, directly with GMT. If the file name
208 ends with ``'.pdf'``, GMT output is fed through ``gmtpy-epstopdf`` to
209 create a PDF file. For any other filename extension, output is first
210 converted to PDF with ``gmtpy-epstopdf``, then with ``pdftocairo`` to
211 PNG with a resolution oversampled by the factor ``oversample`` and
212 finally the PNG is downsampled and converted to the target format with
213 ``convert``. The resolution of rasterized target image can be
214 controlled either by ``resolution`` in DPI or by specifying ``width``
215 or ``height`` or ``size``, where the latter fits the image into a
216 square with given side length. To save transparency use
217 ``psconvert=True``.
218 '''
220 gmt = self.gmt
221 self.draw_labels()
222 self.draw_axes()
223 if self.show_topo and self.show_topo_scale:
224 self._draw_topo_scale()
226 gmt.save(outpath, resolution=resolution, oversample=oversample,
227 size=size, width=width, height=height, psconvert=psconvert)
229 @property
230 def scaler(self):
231 if self._scaler is None:
232 self._setup_geometry()
234 return self._scaler
236 @property
237 def wesn(self):
238 if self._wesn is None:
239 self._setup_geometry()
241 return self._wesn
243 @property
244 def widget(self):
245 if self._widget is None:
246 self._setup()
248 return self._widget
250 @property
251 def layout(self):
252 if self._layout is None:
253 self._setup()
255 return self._layout
257 @property
258 def jxyr(self):
259 if self._jxyr is None:
260 self._setup()
262 return self._jxyr
264 @property
265 def pxyr(self):
266 if self._pxyr is None:
267 self._setup()
269 return self._pxyr
271 @property
272 def gmt(self):
273 if self._gmt is None:
274 self._setup()
276 if self._have_topo_ocean is None:
277 self._draw_background()
279 return self._gmt
281 def _setup(self):
282 if not self._widget:
283 self._setup_geometry()
285 self._setup_lod()
286 self._setup_gmt()
288 def _setup_geometry(self):
289 wpage, hpage = self.width, self.height
290 ml, mr, mt, mb = self._expand_margins()
291 wpage -= ml + mr
292 hpage -= mt + mb
294 wreg = self.radius * 2.0
295 hreg = self.radius * 2.0
296 if wpage >= hpage:
297 wreg *= wpage/hpage
298 else:
299 hreg *= hpage/wpage
301 self._wreg = wreg
302 self._hreg = hreg
304 self._corners = corners(self.lon, self.lat, wreg, hreg)
305 west, east, south, north = extent(self.lon, self.lat, wreg, hreg, 10)
307 x, y, z = ((west, east), (south, north), (-6000., 4500.))
309 xax = gmtpy.Ax(mode='min-max', approx_ticks=4.)
310 yax = gmtpy.Ax(mode='min-max', approx_ticks=4.)
311 zax = gmtpy.Ax(mode='min-max', inc=1000., label='Height',
312 scaled_unit='km', scaled_unit_factor=0.001)
314 scaler = gmtpy.ScaleGuru(data_tuples=[(x, y, z)], axes=(xax, yax, zax))
316 par = scaler.get_params()
318 west = par['xmin']
319 east = par['xmax']
320 south = par['ymin']
321 north = par['ymax']
323 self._wesn = west, east, south, north
324 self._scaler = scaler
326 def _setup_lod(self):
327 w, e, s, n = self._wesn
328 if self.radius > 1500.*km:
329 coastline_resolution = 'i'
330 rivers = False
331 else:
332 coastline_resolution = 'f'
333 rivers = True
335 self._minarea = (self.skip_feature_factor * self.radius/km)**2
337 self._coastline_resolution = coastline_resolution
338 self._rivers = rivers
340 self._prep_topo_have = {}
341 self._dems = {}
343 cm2inch = gmtpy.cm/gmtpy.inch
345 dmin = 2.0 * self.radius * m2d / (self.topo_resolution_max *
346 (self.height * cm2inch))
347 dmax = 2.0 * self.radius * m2d / (self.topo_resolution_min *
348 (self.height * cm2inch))
350 for k in ['ocean', 'land']:
351 self._dems[k] = topo.select_dem_names(k, dmin, dmax, self._wesn)
352 if self._dems[k]:
353 logger.debug('using topography dataset %s for %s'
354 % (','.join(self._dems[k]), k))
356 def _expand_margins(self):
357 if len(self.margins) == 0 or len(self.margins) > 4:
358 ml = mr = mt = mb = 2.0
359 elif len(self.margins) == 1:
360 ml = mr = mt = mb = self.margins[0]
361 elif len(self.margins) == 2:
362 ml = mr = self.margins[0]
363 mt = mb = self.margins[1]
364 elif len(self.margins) == 4:
365 ml, mr, mt, mb = self.margins
367 return ml, mr, mt, mb
369 def _setup_gmt(self):
370 w, h = self.width, self.height
371 scaler = self._scaler
373 if gmtpy.is_gmt5(self._gmtversion):
374 gmtconf = dict(
375 MAP_TICK_PEN_PRIMARY='1.25p',
376 MAP_TICK_PEN_SECONDARY='1.25p',
377 MAP_TICK_LENGTH_PRIMARY='0.2c',
378 MAP_TICK_LENGTH_SECONDARY='0.6c',
379 FONT_ANNOT_PRIMARY='12p,1,black',
380 FONT_LABEL='12p,1,black',
381 PS_CHAR_ENCODING='ISOLatin1+',
382 MAP_FRAME_TYPE='fancy',
383 FORMAT_GEO_MAP='D',
384 PS_MEDIA='Custom_%ix%i' % (
385 w*gmtpy.cm,
386 h*gmtpy.cm),
387 PS_PAGE_ORIENTATION='portrait',
388 MAP_GRID_PEN_PRIMARY='thinnest,0/50/0',
389 MAP_ANNOT_OBLIQUE='6')
390 else:
391 gmtconf = dict(
392 TICK_PEN='1.25p',
393 TICK_LENGTH='0.2c',
394 ANNOT_FONT_PRIMARY='1',
395 ANNOT_FONT_SIZE_PRIMARY='12p',
396 LABEL_FONT='1',
397 LABEL_FONT_SIZE='12p',
398 CHAR_ENCODING='ISOLatin1+',
399 BASEMAP_TYPE='fancy',
400 PLOT_DEGREE_FORMAT='D',
401 PAPER_MEDIA='Custom_%ix%i' % (
402 w*gmtpy.cm,
403 h*gmtpy.cm),
404 GRID_PEN_PRIMARY='thinnest/0/50/0',
405 DOTS_PR_INCH='1200',
406 OBLIQUE_ANNOTATION='6')
408 gmtconf.update(
409 (k.upper(), v) for (k, v) in self.gmt_config.items())
411 gmt = gmtpy.GMT(config=gmtconf, version=self._gmtversion)
413 layout = gmt.default_layout()
415 layout.set_fixed_margins(*[x*cm for x in self._expand_margins()])
417 widget = layout.get_widget()
418 widget['P'] = widget['J']
419 widget['J'] = ('-JA%g/%g' % (self.lon, self.lat)) + '/%(width)gp'
420 scaler['R'] = '-R%g/%g/%g/%gr' % self._corners
422 # aspect = gmtpy.aspect_for_projection(
423 # gmt.installation['version'], *(widget.J() + scaler.R()))
425 aspect = self._map_aspect(jr=widget.J() + scaler.R())
426 widget.set_aspect(aspect)
428 self._gmt = gmt
429 self._layout = layout
430 self._widget = widget
431 self._jxyr = self._widget.JXY() + self._scaler.R()
432 self._pxyr = self._widget.PXY() + [
433 '-R%g/%g/%g/%g' % (0, widget.width(), 0, widget.height())]
434 self._have_drawn_axes = False
435 self._have_drawn_labels = False
437 def _draw_background(self):
438 self._have_topo_land = False
439 self._have_topo_ocean = False
440 if self.show_topo:
441 self._have_topo = self._draw_topo()
443 self._draw_basefeatures()
445 def _get_topo_tile(self, k):
446 t = None
447 demname = None
448 for dem in self._dems[k]:
449 t = topo.get(dem, self._wesn)
450 demname = dem
451 if t is not None:
452 break
454 if not t:
455 raise NoTopo()
457 return t, demname
459 def _prep_topo(self, k):
460 gmt = self._gmt
461 t, demname = self._get_topo_tile(k)
463 if demname not in self._prep_topo_have:
465 grdfile = gmt.tempfilename()
467 is_flat = num.all(t.data[0] == t.data)
469 gmtpy.savegrd(
470 t.x(), t.y(), t.data, filename=grdfile, naming='lonlat')
472 if self.illuminate and not is_flat:
473 if k == 'ocean':
474 factor = self.illuminate_factor_ocean
475 else:
476 factor = self.illuminate_factor_land
478 ilumfn = gmt.tempfilename()
479 gmt.grdgradient(
480 grdfile,
481 N='e%g' % factor,
482 A=-45,
483 G=ilumfn,
484 out_discard=True)
486 ilumargs = ['-I%s' % ilumfn]
487 else:
488 ilumargs = []
490 if self.replace_topo_color_only:
491 t2 = self.replace_topo_color_only
492 grdfile2 = gmt.tempfilename()
494 gmtpy.savegrd(
495 t2.x(), t2.y(), t2.data, filename=grdfile2,
496 naming='lonlat')
498 if gmt.is_gmt5():
499 gmt.grdsample(
500 grdfile2,
501 G=grdfile,
502 n='l',
503 I='%g/%g' % (t.dx, t.dy), # noqa
504 R=grdfile,
505 out_discard=True)
506 else:
507 gmt.grdsample(
508 grdfile2,
509 G=grdfile,
510 Q='l',
511 I='%g/%g' % (t.dx, t.dy), # noqa
512 R=grdfile,
513 out_discard=True)
515 gmt.grdmath(
516 grdfile, '0.0', 'AND', '=', grdfile2,
517 out_discard=True)
519 grdfile = grdfile2
521 self._prep_topo_have[demname] = grdfile, ilumargs
523 return self._prep_topo_have[demname]
525 def _draw_topo(self):
526 widget = self._widget
527 scaler = self._scaler
528 gmt = self._gmt
529 cres = self._coastline_resolution
530 minarea = self._minarea
532 JXY = widget.JXY()
533 R = scaler.R()
535 try:
536 grdfile, ilumargs = self._prep_topo('ocean')
537 gmt.pscoast(D=cres, S='c', A=minarea, *(JXY+R))
538 gmt.grdimage(grdfile, C=topo.cpt(self.topo_cpt_wet),
539 *(ilumargs+JXY+R))
540 gmt.pscoast(Q=True, *(JXY+R))
541 self._have_topo_ocean = True
542 except NoTopo:
543 self._have_topo_ocean = False
545 try:
546 grdfile, ilumargs = self._prep_topo('land')
547 gmt.pscoast(D=cres, G='c', A=minarea, *(JXY+R))
548 gmt.grdimage(grdfile, C=topo.cpt(self.topo_cpt_dry),
549 *(ilumargs+JXY+R))
550 gmt.pscoast(Q=True, *(JXY+R))
551 self._have_topo_land = True
552 except NoTopo:
553 self._have_topo_land = False
555 def _draw_topo_scale(self, label='Elevation [km]'):
556 dry = read_cpt(topo.cpt(self.topo_cpt_dry))
557 wet = read_cpt(topo.cpt(self.topo_cpt_wet))
558 combi = cpt_merge_wet_dry(wet, dry)
559 for level in combi.levels:
560 level.vmin /= km
561 level.vmax /= km
563 topo_cpt = self.gmt.tempfilename() + '.cpt'
564 write_cpt(combi, topo_cpt)
566 (w, h), (xo, yo) = self.widget.get_size()
567 self.gmt.psscale(
568 D='%gp/%gp/%gp/%gph' % (xo + 0.5*w, yo - 2.0*gmtpy.cm, w,
569 0.5*gmtpy.cm),
570 C=topo_cpt,
571 B='1:%s:' % label)
573 def _draw_basefeatures(self):
574 gmt = self._gmt
575 cres = self._coastline_resolution
576 rivers = self._rivers
577 minarea = self._minarea
579 color_wet = self.color_wet
580 color_dry = self.color_dry
582 if self.show_rivers and rivers:
583 rivers = ['-Ir/0.25p,%s' % gmtpy.color(self.color_wet)]
584 else:
585 rivers = []
587 fill = {}
588 if not self._have_topo_land:
589 fill['G'] = color_dry
591 if not self._have_topo_ocean:
592 fill['S'] = color_wet
594 if self.show_boundaries:
595 fill['N'] = '1/1p,%s,%s' % (
596 gmtpy.color(self.color_boundaries), 'solid')
598 gmt.pscoast(
599 D=cres,
600 W='thinnest,%s' % gmtpy.color(darken(gmtpy.color_tup(color_dry))),
601 A=minarea,
602 *(rivers+self._jxyr), **fill)
604 if self.show_plates:
605 self.draw_plates()
607 def _draw_axes(self):
608 gmt = self._gmt
609 scaler = self._scaler
610 widget = self._widget
612 if self.axes_layout is None:
613 if self.lat > 0.0:
614 axes_layout = 'WSen'
615 else:
616 axes_layout = 'WseN'
617 else:
618 axes_layout = self.axes_layout
620 scale_km = gmtpy.nice_value(self.radius/5.) / 1000.
622 if self.show_center_mark:
623 gmt.psxy(
624 in_rows=[[self.lon, self.lat]],
625 S='c20p', W='2p,black',
626 *self._jxyr)
628 if self.show_grid:
629 btmpl = ('%(xinc)gg%(xinc)g:%(xlabel)s:/'
630 '%(yinc)gg%(yinc)g:%(ylabel)s:')
631 else:
632 btmpl = '%(xinc)g:%(xlabel)s:/%(yinc)g:%(ylabel)s:'
634 if self.show_scale:
635 scale = 'x%gp/%gp/%g/%g/%gk' % (
636 6./7*widget.width(),
637 widget.height()/7.,
638 self.lon,
639 self.lat,
640 scale_km)
641 else:
642 scale = False
644 gmt.psbasemap(
645 B=(btmpl % scaler.get_params())+axes_layout,
646 L=scale,
647 *self._jxyr)
649 if self.comment:
650 font_size = self.gmt.label_font_size()
652 _, east, south, _ = self._wesn
653 if gmt.is_gmt5():
654 row = [
655 1, 0,
656 '%gp,%s,%s' % (font_size, 0, 'black'), 'BR',
657 self.comment]
659 farg = ['-F+f+j']
660 else:
661 row = [1, 0, font_size, 0, 0, 'BR', self.comment]
662 farg = []
664 gmt.pstext(
665 in_rows=[row],
666 N=True,
667 R=(0, 1, 0, 1),
668 D='%gp/%gp' % (-font_size*0.2, font_size*0.3),
669 *(widget.PXY() + farg))
671 def draw_axes(self):
672 if not self._have_drawn_axes:
673 self._draw_axes()
674 self._have_drawn_axes = True
676 def _have_coastlines(self):
677 gmt = self._gmt
678 cres = self._coastline_resolution
679 minarea = self._minarea
681 checkfile = gmt.tempfilename()
683 gmt.pscoast(
684 M=True,
685 D=cres,
686 W='thinnest,black',
687 A=minarea,
688 out_filename=checkfile,
689 *self._jxyr)
691 points = []
692 with open(checkfile, 'r') as f:
693 for line in f:
694 ls = line.strip()
695 if ls.startswith('#') or ls.startswith('>') or ls == '':
696 continue
697 plon, plat = [float(x) for x in ls.split()]
698 points.append((plat, plon))
700 points = num.array(points, dtype=float)
701 return num.any(points_in_region(points, self._wesn))
703 def have_coastlines(self):
704 self.gmt
705 return self._have_coastlines()
707 def project(self, lats, lons, jr=None):
708 onepoint = False
709 if isinstance(lats, float) and isinstance(lons, float):
710 lats = [lats]
711 lons = [lons]
712 onepoint = True
714 if jr is not None:
715 j, r = jr
716 gmt = gmtpy.GMT(version=self._gmtversion)
717 else:
718 j, _, _, r = self.jxyr
719 gmt = self.gmt
721 f = BytesIO()
722 gmt.mapproject(j, r, in_columns=(lons, lats), out_stream=f, D='p')
723 f.seek(0)
724 data = num.loadtxt(f, ndmin=2)
725 xs, ys = data.T
726 if onepoint:
727 xs = xs[0]
728 ys = ys[0]
729 return xs, ys
731 def _map_box(self, jr=None):
732 ll_lon, ll_lat, ur_lon, ur_lat = self._corners
734 xs_corner, ys_corner = self.project(
735 (ll_lat, ur_lat), (ll_lon, ur_lon), jr=jr)
737 w = xs_corner[1] - xs_corner[0]
738 h = ys_corner[1] - ys_corner[0]
740 return w, h
742 def _map_aspect(self, jr=None):
743 w, h = self._map_box(jr=jr)
744 return h/w
746 def _draw_labels(self):
747 points_taken = []
748 regions_taken = []
750 def no_points_in_rect(xs, ys, xmin, ymin, xmax, ymax):
751 xx = not num.any(la(la(xmin < xs, xs < xmax),
752 la(ymin < ys, ys < ymax)))
753 return xx
755 def roverlaps(a, b):
756 return (a[0] < b[2] and b[0] < a[2] and
757 a[1] < b[3] and b[1] < a[3])
759 w, h = self._map_box()
761 label_font_size = self.gmt.label_font_size()
763 if self._labels:
765 n = len(self._labels)
767 lons, lats, texts, sx, sy, colors, fonts, font_sizes, \
768 angles, styles = list(zip(*self._labels))
770 font_sizes = [
771 (font_size or label_font_size) for font_size in font_sizes]
773 sx = num.array(sx, dtype=float)
774 sy = num.array(sy, dtype=float)
776 xs, ys = self.project(lats, lons)
778 points_taken.append((xs, ys))
780 dxs = num.zeros(n)
781 dys = num.zeros(n)
783 for i in range(n):
784 dx, dy = gmtpy.text_box(
785 texts[i],
786 font=fonts[i],
787 font_size=font_sizes[i],
788 **styles[i])
790 dxs[i] = dx
791 dys[i] = dy
793 la = num.logical_and
794 anchors_ok = (
795 la(xs + sx + dxs < w, ys + sy + dys < h),
796 la(xs - sx - dxs > 0., ys - sy - dys > 0.),
797 la(xs + sx + dxs < w, ys - sy - dys > 0.),
798 la(xs - sx - dxs > 0., ys + sy + dys < h),
799 )
801 arects = [
802 (xs, ys, xs + sx + dxs, ys + sy + dys),
803 (xs - sx - dxs, ys - sy - dys, xs, ys),
804 (xs, ys - sy - dys, xs + sx + dxs, ys),
805 (xs - sx - dxs, ys, xs, ys + sy + dys)]
807 for i in range(n):
808 for ianch in range(4):
809 anchors_ok[ianch][i] &= no_points_in_rect(
810 xs, ys, *[xxx[i] for xxx in arects[ianch]])
812 anchor_choices = []
813 anchor_take = []
814 for i in range(n):
815 choices = [ianch for ianch in range(4)
816 if anchors_ok[ianch][i]]
817 anchor_choices.append(choices)
818 if choices:
819 anchor_take.append(choices[0])
820 else:
821 anchor_take.append(None)
823 def cost(anchor_take):
824 noverlaps = 0
825 for i in range(n):
826 for j in range(n):
827 if i != j:
828 i_take = anchor_take[i]
829 j_take = anchor_take[j]
830 if i_take is None or j_take is None:
831 continue
832 r_i = [xxx[i] for xxx in arects[i_take]]
833 r_j = [xxx[j] for xxx in arects[j_take]]
834 if roverlaps(r_i, r_j):
835 noverlaps += 1
837 return noverlaps
839 cur_cost = cost(anchor_take)
840 imax = 30
841 while cur_cost != 0 and imax > 0:
842 for i in range(n):
843 for t in anchor_choices[i]:
844 anchor_take_new = list(anchor_take)
845 anchor_take_new[i] = t
846 new_cost = cost(anchor_take_new)
847 if new_cost < cur_cost:
848 anchor_take = anchor_take_new
849 cur_cost = new_cost
851 imax -= 1
853 while cur_cost != 0:
854 for i in range(n):
855 anchor_take_new = list(anchor_take)
856 anchor_take_new[i] = None
857 new_cost = cost(anchor_take_new)
858 if new_cost < cur_cost:
859 anchor_take = anchor_take_new
860 cur_cost = new_cost
861 break
863 anchor_strs = ['BL', 'TR', 'TL', 'BR']
865 for i in range(n):
866 ianchor = anchor_take[i]
867 color = colors[i]
868 if color is None:
869 color = 'black'
871 if ianchor is not None:
872 regions_taken.append([xxx[i] for xxx in arects[ianchor]])
874 anchor = anchor_strs[ianchor]
876 yoff = [-sy[i], sy[i]][anchor[0] == 'B']
877 xoff = [-sx[i], sx[i]][anchor[1] == 'L']
878 if self.gmt.is_gmt5():
879 row = (
880 lons[i], lats[i],
881 '%i,%s,%s' % (font_sizes[i], fonts[i], color),
882 anchor,
883 texts[i])
885 farg = ['-F+f+j+a%g' % angles[i]]
886 else:
887 row = (
888 lons[i], lats[i],
889 font_sizes[i], angles[i], fonts[i], anchor,
890 texts[i])
891 farg = ['-G%s' % color]
893 self.gmt.pstext(
894 in_rows=[row],
895 D='%gp/%gp' % (xoff, yoff),
896 *(self.jxyr + farg),
897 **styles[i])
899 if self._area_labels:
901 for lons, lats, text, color, font, font_size, style in \
902 self._area_labels:
904 if font_size is None:
905 font_size = label_font_size
907 if color is None:
908 color = 'black'
910 if self.gmt.is_gmt5():
911 farg = ['-F+f+j']
912 else:
913 farg = ['-G%s' % color]
915 xs, ys = self.project(lats, lons)
916 dx, dy = gmtpy.text_box(
917 text, font=font, font_size=font_size, **style)
919 rects = [xs-0.5*dx, ys-0.5*dy, xs+0.5*dx, ys+0.5*dy]
921 locs_ok = num.ones(xs.size, dtype=num.bool)
923 for iloc in range(xs.size):
924 rcandi = [xxx[iloc] for xxx in rects]
926 locs_ok[iloc] = True
927 locs_ok[iloc] &= (
928 0 < rcandi[0] and rcandi[2] < w
929 and 0 < rcandi[1] and rcandi[3] < h)
931 overlap = False
932 for r in regions_taken:
933 if roverlaps(r, rcandi):
934 overlap = True
935 break
937 locs_ok[iloc] &= not overlap
939 for xs_taken, ys_taken in points_taken:
940 locs_ok[iloc] &= no_points_in_rect(
941 xs_taken, ys_taken, *rcandi)
943 if not locs_ok[iloc]:
944 break
946 rows = []
947 for iloc, (lon, lat) in enumerate(zip(lons, lats)):
948 if not locs_ok[iloc]:
949 continue
951 if self.gmt.is_gmt5():
952 row = (
953 lon, lat,
954 '%i,%s,%s' % (font_size, font, color),
955 'MC',
956 text)
958 else:
959 row = (
960 lon, lat,
961 font_size, 0, font, 'MC',
962 text)
964 rows.append(row)
966 regions_taken.append([xxx[iloc] for xxx in rects])
967 break
969 self.gmt.pstext(
970 in_rows=rows,
971 *(self.jxyr + farg),
972 **style)
974 def draw_labels(self):
975 self.gmt
976 if not self._have_drawn_labels:
977 self._draw_labels()
978 self._have_drawn_labels = True
980 def add_label(
981 self, lat, lon, text,
982 offset_x=5., offset_y=5.,
983 color=None,
984 font='1',
985 font_size=None,
986 angle=0,
987 style={}):
989 if 'G' in style:
990 style = style.copy()
991 color = style.pop('G')
993 self._labels.append(
994 (lon, lat, text, offset_x, offset_y, color, font, font_size,
995 angle, style))
997 def add_area_label(
998 self, lat, lon, text,
999 color=None,
1000 font='3',
1001 font_size=None,
1002 style={}):
1004 self._area_labels.append(
1005 (lon, lat, text, color, font, font_size, style))
1007 def cities_in_region(self):
1008 from pyrocko.dataset import geonames
1009 cities = geonames.get_cities_region(region=self.wesn, minpop=0)
1010 cities.extend(self.custom_cities)
1011 cities.sort(key=lambda x: x.population)
1012 return cities
1014 def draw_cities(self,
1015 exact=None,
1016 include=[],
1017 exclude=[],
1018 nmax_soft=10,
1019 psxy_style=dict(S='s5p', G='black')):
1021 cities = self.cities_in_region()
1023 if exact is not None:
1024 cities = [c for c in cities if c.name in exact]
1025 minpop = None
1026 else:
1027 cities = [c for c in cities if c.name not in exclude]
1028 minpop = 10**3
1029 for minpop_new in [1e3, 3e3, 1e4, 3e4, 1e5, 3e5, 1e6, 3e6, 1e7]:
1030 cities_new = [
1031 c for c in cities
1032 if c.population > minpop_new or c.name in include]
1034 if len(cities_new) == 0 or (
1035 len(cities_new) < 3 and len(cities) < nmax_soft*2):
1036 break
1038 cities = cities_new
1039 minpop = minpop_new
1040 if len(cities) <= nmax_soft:
1041 break
1043 if cities:
1044 lats = [c.lat for c in cities]
1045 lons = [c.lon for c in cities]
1047 self.gmt.psxy(
1048 in_columns=(lons, lats),
1049 *self.jxyr, **psxy_style)
1051 for c in cities:
1052 try:
1053 text = c.name.encode('iso-8859-1').decode('iso-8859-1')
1054 except UnicodeEncodeError:
1055 text = c.asciiname
1057 self.add_label(c.lat, c.lon, text)
1059 self._cities_minpop = minpop
1061 def add_stations(self, stations, psxy_style=dict()):
1063 default_psxy_style = {
1064 'S': 't8p',
1065 'G': 'black'
1066 }
1067 default_psxy_style.update(psxy_style)
1069 lats, lons = zip(*[s.effective_latlon for s in stations])
1071 self.gmt.psxy(
1072 in_columns=(lons, lats),
1073 *self.jxyr, **default_psxy_style)
1075 for station in stations:
1076 self.add_label(
1077 station.effective_lat,
1078 station.effective_lon,
1079 '.'.join(x for x in (station.network, station.station) if x))
1081 def add_kite_scene(self, scene):
1082 tile = FloatTile(
1083 scene.frame.llLon,
1084 scene.frame.llLat,
1085 scene.frame.dLon,
1086 scene.frame.dLat,
1087 scene.displacement)
1089 return tile
1091 def add_gnss_campaign(self, campaign, psxy_style=None, offset_scale=None,
1092 labels=True, vertical=False, fontsize=10):
1094 stations = campaign.stations
1096 if offset_scale is None:
1097 offset_scale = num.zeros(campaign.nstations)
1098 for ista, sta in enumerate(stations):
1099 for comp in sta.components.values():
1100 offset_scale[ista] += comp.shift
1101 offset_scale = num.sqrt(offset_scale**2).max()
1103 size = math.sqrt(self.height**2 + self.width**2)
1104 scale = (size/10.) / offset_scale
1105 logger.debug('GNSS: Using offset scale %f, map scale %f',
1106 offset_scale, scale)
1108 lats, lons = zip(*[s.effective_latlon for s in stations])
1110 if vertical:
1111 rows = [[lons[ista], lats[ista],
1112 0., -s.up.shift,
1113 (s.east.sigma + s.north.sigma) if s.east.sigma else 0.,
1114 s.up.sigma, 0.,
1115 s.code if labels else None]
1116 for ista, s in enumerate(stations)
1117 if s.up is not None]
1119 else:
1120 rows = [[lons[ista], lats[ista],
1121 -s.east.shift, -s.north.shift,
1122 s.east.sigma, s.north.sigma, s.correlation_ne,
1123 s.code if labels else None]
1124 for ista, s in enumerate(stations)
1125 if s.east is not None or s.north is not None]
1127 default_psxy_style = {
1128 'h': 0,
1129 'W': '2p,black',
1130 'A': '+p2p,black+b+a40',
1131 'G': 'black',
1132 'L': True,
1133 'S': 'e%dc/0.95/%d' % (scale, fontsize),
1134 }
1136 if not labels:
1137 for row in rows:
1138 row.pop(-1)
1140 if psxy_style is not None:
1141 default_psxy_style.update(psxy_style)
1143 self.gmt.psvelo(
1144 in_rows=rows,
1145 *self.jxyr,
1146 **default_psxy_style)
1148 def draw_plates(self):
1149 from pyrocko.dataset import tectonics
1151 neast = 20
1152 nnorth = max(1, int(round(num.round(self._hreg/self._wreg * neast))))
1153 norths = num.linspace(-self._hreg*0.5, self._hreg*0.5, nnorth)
1154 easts = num.linspace(-self._wreg*0.5, self._wreg*0.5, neast)
1155 norths2 = num.repeat(norths, neast)
1156 easts2 = num.tile(easts, nnorth)
1157 lats, lons = od.ne_to_latlon(
1158 self.lat, self.lon, norths2, easts2)
1160 bird = tectonics.PeterBird2003()
1161 plates = bird.get_plates()
1163 color_plates = gmtpy.color('aluminium5')
1164 color_velocities = gmtpy.color('skyblue1')
1165 color_velocities_lab = gmtpy.color(darken(gmtpy.color_tup('skyblue1')))
1167 points = num.vstack((lats, lons)).T
1168 used = []
1169 for plate in plates:
1170 mask = plate.contains_points(points)
1171 if num.any(mask):
1172 used.append((plate, mask))
1174 if len(used) > 1:
1176 candi_fixed = {}
1178 label_data = []
1179 for plate, mask in used:
1181 mean_north = num.mean(norths2[mask])
1182 mean_east = num.mean(easts2[mask])
1183 iorder = num.argsort(num.sqrt(
1184 (norths2[mask] - mean_north)**2 +
1185 (easts2[mask] - mean_east)**2))
1187 lat_candis = lats[mask][iorder]
1188 lon_candis = lons[mask][iorder]
1190 candi_fixed[plate.name] = lat_candis.size
1192 label_data.append((
1193 lat_candis, lon_candis, plate, color_plates))
1195 boundaries = bird.get_boundaries()
1197 size = 2
1199 psxy_kwargs = []
1201 for boundary in boundaries:
1202 if num.any(points_in_region(boundary.points, self._wesn)):
1203 for typ, part in boundary.split_types(
1204 [['SUB'],
1205 ['OSR', 'OTF', 'OCB', 'CTF', 'CCB', 'CRB']]):
1207 lats, lons = part.T
1209 kwargs = {}
1210 if typ[0] == 'SUB':
1211 if boundary.kind == '\\':
1212 kwargs['S'] = 'f%g/%gp+t+r' % (
1213 0.45*size, 3.*size)
1214 elif boundary.kind == '/':
1215 kwargs['S'] = 'f%g/%gp+t+l' % (
1216 0.45*size, 3.*size)
1218 kwargs['G'] = color_plates
1220 kwargs['in_columns'] = (lons, lats)
1221 kwargs['W'] = '%gp,%s' % (size, color_plates),
1223 psxy_kwargs.append(kwargs)
1225 if boundary.kind == '\\':
1226 if boundary.plate_name2 in candi_fixed:
1227 candi_fixed[boundary.plate_name2] += \
1228 neast*nnorth
1230 elif boundary.kind == '/':
1231 if boundary.plate_name1 in candi_fixed:
1232 candi_fixed[boundary.plate_name1] += \
1233 neast*nnorth
1235 candi_fixed = [name for name in sorted(
1236 list(candi_fixed.keys()), key=lambda name: -candi_fixed[name])]
1238 candi_fixed.append(None)
1240 gsrm = tectonics.GSRM1()
1242 for name in candi_fixed:
1243 if name not in gsrm.plate_names() \
1244 and name not in gsrm.plate_alt_names():
1246 continue
1248 lats, lons, vnorth, veast, vnorth_err, veast_err, corr = \
1249 gsrm.get_velocities(name, region=self._wesn)
1251 fixed_plate_name = name
1253 self.gmt.psvelo(
1254 in_columns=(
1255 lons, lats, veast, vnorth, veast_err, vnorth_err,
1256 corr),
1257 W='0.25p,%s' % color_velocities,
1258 A='9p+e+g%s' % color_velocities,
1259 S='e0.2p/0.95/10',
1260 *self.jxyr)
1262 for _ in range(len(lons) // 50 + 1):
1263 ii = random.randint(0, len(lons)-1)
1264 v = math.sqrt(vnorth[ii]**2 + veast[ii]**2)
1265 self.add_label(
1266 lats[ii], lons[ii], '%.0f' % v,
1267 font_size=0.7*self.gmt.label_font_size(),
1268 style=dict(
1269 G=color_velocities_lab))
1271 break
1273 for (lat_candis, lon_candis, plate, color) in label_data:
1274 full_name = bird.full_name(plate.name)
1275 if plate.name == fixed_plate_name:
1276 full_name = '@_' + full_name + '@_'
1278 self.add_area_label(
1279 lat_candis, lon_candis,
1280 full_name,
1281 color=color,
1282 font='3')
1284 for kwargs in psxy_kwargs:
1285 self.gmt.psxy(*self.jxyr, **kwargs)
1288def rand(mi, ma):
1289 mi = float(mi)
1290 ma = float(ma)
1291 return random.random() * (ma-mi) + mi
1294def split_region(region):
1295 west, east, south, north = topo.positive_region(region)
1296 if east > 180:
1297 return [(west, 180., south, north),
1298 (-180., east-360., south, north)]
1299 else:
1300 return [region]
1303class CPTLevel(Object):
1304 vmin = Float.T()
1305 vmax = Float.T()
1306 color_min = Tuple.T(3, Float.T())
1307 color_max = Tuple.T(3, Float.T())
1310class CPT(Object):
1311 color_below = Tuple.T(3, Float.T(), optional=True)
1312 color_above = Tuple.T(3, Float.T(), optional=True)
1313 color_nan = Tuple.T(3, Float.T(), optional=True)
1314 levels = List.T(CPTLevel.T())
1316 def scale(self, vmin, vmax):
1317 vmin_old, vmax_old = self.levels[0].vmin, self.levels[-1].vmax
1318 for level in self.levels:
1319 level.vmin = (level.vmin - vmin_old) / (vmax_old - vmin_old) * \
1320 (vmax - vmin) + vmin
1321 level.vmax = (level.vmax - vmin_old) / (vmax_old - vmin_old) * \
1322 (vmax - vmin) + vmin
1324 def discretize(self, nlevels):
1325 colors = []
1326 vals = []
1327 for level in self.levels:
1328 vals.append(level.vmin)
1329 vals.append(level.vmax)
1330 colors.append(level.color_min)
1331 colors.append(level.color_max)
1333 r, g, b = num.array(colors, dtype=float).T
1334 vals = num.array(vals, dtype=float)
1336 vmin, vmax = self.levels[0].vmin, self.levels[-1].vmax
1337 x = num.linspace(vmin, vmax, nlevels+1)
1338 rd = num.interp(x, vals, r)
1339 gd = num.interp(x, vals, g)
1340 bd = num.interp(x, vals, b)
1342 levels = []
1343 for ilevel in range(nlevels):
1344 color = (
1345 float(0.5*(rd[ilevel]+rd[ilevel+1])),
1346 float(0.5*(gd[ilevel]+gd[ilevel+1])),
1347 float(0.5*(bd[ilevel]+bd[ilevel+1])))
1349 levels.append(CPTLevel(
1350 vmin=x[ilevel],
1351 vmax=x[ilevel+1],
1352 color_min=color,
1353 color_max=color))
1355 cpt = CPT(
1356 color_below=self.color_below,
1357 color_above=self.color_above,
1358 color_nan=self.color_nan,
1359 levels=levels)
1361 return cpt
1364class CPTParseError(Exception):
1365 pass
1368def read_cpt(filename):
1369 with open(filename) as f:
1370 color_below = None
1371 color_above = None
1372 color_nan = None
1373 levels = []
1374 try:
1375 for line in f:
1376 line = line.strip()
1377 toks = line.split()
1379 if line.startswith('#'):
1380 continue
1382 elif line.startswith('B'):
1383 color_below = tuple(map(float, toks[1:4]))
1385 elif line.startswith('F'):
1386 color_above = tuple(map(float, toks[1:4]))
1388 elif line.startswith('N'):
1389 color_nan = tuple(map(float, toks[1:4]))
1391 else:
1392 values = list(map(float, line.split()))
1393 vmin = values[0]
1394 color_min = tuple(values[1:4])
1395 vmax = values[4]
1396 color_max = tuple(values[5:8])
1397 levels.append(CPTLevel(
1398 vmin=vmin,
1399 vmax=vmax,
1400 color_min=color_min,
1401 color_max=color_max))
1403 except Exception:
1404 raise CPTParseError()
1406 return CPT(
1407 color_below=color_below,
1408 color_above=color_above,
1409 color_nan=color_nan,
1410 levels=levels)
1413def color_to_int(color):
1414 return tuple(max(0, min(255, int(round(x)))) for x in color)
1417def write_cpt(cpt, filename):
1418 with open(filename, 'w') as f:
1419 for level in cpt.levels:
1420 f.write(
1421 '%e %i %i %i %e %i %i %i\n' %
1422 ((level.vmin, ) + color_to_int(level.color_min) +
1423 (level.vmax, ) + color_to_int(level.color_max)))
1425 if cpt.color_below:
1426 f.write('B %i %i %i\n' % color_to_int(cpt.color_below))
1428 if cpt.color_above:
1429 f.write('F %i %i %i\n' % color_to_int(cpt.color_above))
1431 if cpt.color_nan:
1432 f.write('N %i %i %i\n' % color_to_int(cpt.color_nan))
1435def cpt_merge_wet_dry(wet, dry):
1436 levels = []
1437 for level in wet.levels:
1438 if level.vmin < 0.:
1439 if level.vmax > 0.:
1440 level.vmax = 0.
1442 levels.append(level)
1444 for level in dry.levels:
1445 if level.vmax > 0.:
1446 if level.vmin < 0.:
1447 level.vmin = 0.
1449 levels.append(level)
1451 combi = CPT(
1452 color_below=wet.color_below,
1453 color_above=dry.color_above,
1454 color_nan=dry.color_nan,
1455 levels=levels)
1457 return combi
1460if __name__ == '__main__':
1461 from pyrocko import util
1462 util.setup_logging('pyrocko.automap', 'info')
1464 import sys
1465 if len(sys.argv) == 2:
1467 n = int(sys.argv[1])
1469 for i in range(n):
1470 m = Map(
1471 lat=rand(-60., 60.),
1472 lon=rand(-180., 180.),
1473 radius=math.exp(rand(math.log(500*km), math.log(3000*km))),
1474 width=30., height=30.,
1475 show_grid=True,
1476 show_topo=True,
1477 color_dry=(238, 236, 230),
1478 topo_cpt_wet='light_sea_uniform',
1479 topo_cpt_dry='light_land_uniform',
1480 illuminate=True,
1481 illuminate_factor_ocean=0.15,
1482 show_rivers=False,
1483 show_plates=True)
1485 m.draw_cities()
1486 print(m)
1487 m.save('map_%02i.pdf' % i)