Commit 834c702e by Hoek, Steven

### Library added with tools that help to plot maps

parent c0948289
 # Copyright (c) 2004-2021 WUR, Wageningen """MapPlotLib - a Python library with tools that help to plot maps""" import re from shapely.geometry import Polygon from descartes import PolygonPatch import numpy as np class IntervalLegend(): intervals = [] colors = [] minval = 0.0 # default maxval = 1.0 # default def __get_min_value(self): result = min([cls[0] for cls in self.intervals]) return result def __get_max_value(self): result = max([cls[1] for cls in self.intervals]) return result def __init__(self, intervals, colors): # Process input self.intervals = intervals self.colors = colors # Check that intervals and colors match somehow if len(intervals) != len(colors): raise ValueError("Number of intervals and number of colors not the same!") # Check given intervals for interval in self.intervals: if interval[0] > interval[1]: raise ValueError("Invalid interval: " + str(interval)) eps = 0.0000000001 for i in range(len(self.intervals) - 1): curcls = self.intervals[i] nextcls = self.intervals[i+1] if abs(curcls[1] - nextcls[0]) > eps: raise ValueError("At least one gap found in given intervals!") # Check given colors expr = r'^#(?:[0-9a-fA-F]{3}){1,2}\$' for color in colors: if not re.search(expr, color): raise ValueError("Invalid color code: " + color) # Finally calculate minimum and maximum values self.minval = self.__get_min_value() self.maxval = self.__get_max_value() def get_color(self, value): # Check input if self.minval > value or value > self.maxval: raise ValueError("Index not in range [%s:%s] as expected!" % (self.minval, self.maxval)) # Assign an appropriate color result = self.colors[-1] for cls, color in zip(self.intervals, self.colors): if cls[0] <= value and value < cls[1]: result = color break return result def get_html(self, fmt, title=""): result = '' if fmt == 'table': # Assume a horizontal table is required result += '\n' result + '
' + title + ' %s - %s
\n' if title != '': result += '\n' for cls, color in zip(self.intervals, self.colors): result += '\n' % (color, cls[0], cls[1]) result += '\n' result += '\n' else: raise ValueError("Format %s not implemented" % fmt) return result def get_colored_patch(listwithcoords, fillcolor, edgecolor): # The result can be added to the current axes of a matplotlib figure by means of the method add_patch polygon = Polygon(np.array(listwithcoords)) result = PolygonPatch(polygon, fc=fillcolor, ec=edgecolor, alpha=1.0) return result \ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!