Commit c5d7d19a authored by Kris Vanneste's avatar Kris Vanneste
Browse files

Made python version detection more robust.

Added determine_scalebar_length method to LayeredBasemap.
Set transparency for thematic legends in draw_legend method of LayeredBasemap.
Improved draw_scalebar method of LayeredBasemap, allowing position to be specified in axis coordinates.
Fixed return value of map_to_display_coordinates and map_from_display_coordinates methods of LayeredBasemap.
Added display_from_ax_coordinates, display_to_ax_coordinates, lonlat_from_ax_coordinates and lonlat_to_ax_coordinates methods to LayeredBasemap.

git-svn-id: https://svn.seismo.oma.be/svn/seismo/mapping/layeredbasemap/trunk@7647 40b490c5-b4d9-47cb-8714-9bef99b524d5
parent 971d6240
......@@ -4,12 +4,12 @@ LayeredBasemap class
from __future__ import absolute_import, division, print_function, unicode_literals
import sys
try:
if sys.version_info[0] == 2:
## Python 2
basestring
PY2 = True
except:
else:
## Python 3
PY2 = False
basestring = str
......@@ -248,6 +248,35 @@ class LayeredBasemap:
return resolution
def determine_scalebar_length(self):
"""
Auto-determine scalebar length
:return:
float, scalebar length (in km)
"""
dlon = self.region[1] - self.region[0]
if dlon == 360:
scalebar_length = 5000
elif dlon > 180:
scalebar_length = 2500
elif dlon > 60:
scalebar_length = 1000
elif dlon > 30:
scalebar_length = 500
elif dlon > 10:
scalebar_length = 200
elif dlon > 5:
scalebar_length = 100
elif dlon > 2.5:
scalebar_length = 50
elif dlon > 1:
scalebar_length = 20
else:
scalebar_length = 5
return scalebar_length
def init_basemap(self, ax=None):
self.zorder = 0
lon_0, lat_0 = self.origin
......@@ -1997,6 +2026,7 @@ class LayeredBasemap:
frame.set_edgecolor(thematic_legend.style.frame_color)
frame.set_facecolor(thematic_legend.style.fill_color)
frame.set_linewidth(thematic_legend.style.frame_width)
frame.set_alpha(thematic_legend.style.alpha)
tl.set_zorder(self.zorder)
self.ax.add_artist(tl)
......@@ -2072,17 +2102,30 @@ class LayeredBasemap:
def draw_scalebar(self):
if self.scalebar_style:
scalebar_style = self.scalebar_style
scalebar_style = self.scalebar_style.copy()
lon0, lat0 = self.lon_0, self.lat_0
#lon0, lat0 = scalebar_style.center
if scalebar_style.length == 'auto':
scalebar_style.length = self.determine_scalebar_length()
if scalebar_style.yoffset and scalebar_style.yoffset < 1:
scalebar_style.yoffset *= (self.map.ymax - self.map.ymin)
clon, clat = scalebar_style.center
if isinstance(clon, basestring) and isinstance(clat, basestring):
clon, clat = float(clon), float(clat)
[clon], [clat] = self.lonlat_from_ax_coordinates([clon], [clat])
scalebar_style.center = (clon, clat)
## Fix too short scalebar length for mercator projection
## See https://github.com/matplotlib/basemap/issues/165
if self.projection == "merc":
corr_factor = np.cos(np.radians(lat0))
scalebar_style.length /= corr_factor
scale = self.map.drawmapscale(lon0=lon0, lat0=lat0, zorder=self.zorder, **scalebar_style.to_kwargs())
scale = self.map.drawmapscale(lon0=lon0, lat0=lat0, zorder=self.zorder,
**scalebar_style.to_kwargs())
self.zorder += 1
if self.projection == "merc":
......@@ -2122,7 +2165,7 @@ class LayeredBasemap:
def lonlat_to_display_coordinates(self, lons, lats):
## Convert lon, lat to display coordinates
x, y = self.map(lons, lats)
x, y = self.lonlat_to_map_coordinates(lons, lats)
return self.map_to_display_coordinates(x, y)
def lonlat_to_map_coordinates(self, lons, lats):
......@@ -2143,7 +2186,7 @@ class LayeredBasemap:
return (lons, lats)
def map_to_display_coordinates(self, x, y):
return zip(*self.ax.transData.transform(list(zip(x, y))))
return list(zip(*self.ax.transData.transform(list(zip(x, y)))))
def map_to_lonlat_coordinates(self, x, y):
return self.map(x, y, inverse=True)
......@@ -2154,7 +2197,35 @@ class LayeredBasemap:
return self.map(x, y, inverse=True)
def map_from_display_coordinates(self, display_x, display_y):
return zip(*self.ax.transData.inverted().transform(list(zip(display_x, display_y))))
return list(zip(*self.ax.transData.inverted().transform(list(zip(display_x, display_y)))))
def display_from_ax_coordinates(self, ax_x, ax_y):
"""
Convert axis [0-1] coordinates to display coordinates (pixels)
"""
transform = self.ax.transAxes.transform
return list(zip(*transform(list(zip(ax_x, ax_y)))))
def display_to_ax_coordinates(self, display_x, display_y):
"""
Convert display coordinates (pixels) to axis [0-1] coordinates
"""
transform = self.ax.transAxes.inverted().transform
return list(zip(*transform(list(zip(display_x, display_y)))))
def lonlat_from_ax_coordinates(self, ax_x, ax_y):
"""
Convert geographic to axis [0-1] coordinates
"""
display_x, display_y = self.display_from_ax_coordinates(ax_x, ax_y)
return self.lonlat_from_display_coordinates(display_x, display_y)
def lonlat_to_ax_coordinates(self, lons, lats):
"""
Convert axis [0-1] to geographic coordinates
"""
display_x, display_y = self.lonlat_to_display_coordinates(lons, lats)
return self.display_to_ax_coordinates(display_x, display_y)
def get_srs(self):
import osr
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment