From eab5ab2f4f2c73132c0ffd37ff272b41ea7b494e Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Sat, 17 Aug 2024 15:46:14 +0200 Subject: [PATCH 1/5] Color maps --- geo_heatmap.py | 12 +++++++++--- utils.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/geo_heatmap.py b/geo_heatmap.py index 5122b12..561bd26 100644 --- a/geo_heatmap.py +++ b/geo_heatmap.py @@ -208,6 +208,7 @@ def generateMap(self, settings): blur = settings["blur"] min_opacity = settings["min_opacity"] max_zoom = settings["max_zoom"] + color_map = settings["color_map"] map_data = [(coords[0], coords[1], magnitude) for coords, magnitude in self.coordinates.items()] @@ -219,12 +220,14 @@ def generateMap(self, settings): attr="geo-heatmap") # Generate heat map + gradient = getColormapAsGradient(color_map) if color_map else None heatmap = HeatMap(map_data, max_val=self.max_magnitude, min_opacity=min_opacity, radius=radius, blur=blur, - max_zoom=max_zoom) + max_zoom=max_zoom, + gradient=gradient) m.add_child(heatmap) return m @@ -307,7 +310,9 @@ def run(self, data_files, output_file, date_range, stream_data, settings): help="The minimum opacity of the heatmap. (default: %(default)s)", default=0.2) parser.add_argument("-mz", "--max-zoom", dest="max_zoom", type=int, required=False, help="The maximum zoom of the heatmap. (default: %(default)s)", default=4) - + parser.add_argument("-c", "--color-map", dest="color_map", type=str, required=False, + help="The color map to use.\n" \ + "(from branca (default), matplotlib ('mpl.'), cmcrameri ('cmc.').", default=None) args = parser.parse_args() data_file = args.files @@ -320,7 +325,8 @@ def run(self, data_files, output_file, date_range, stream_data, settings): "radius": args.radius, "blur": args.blur, "min_opacity": args.min_opacity, - "max_zoom": args.max_zoom + "max_zoom": args.max_zoom, + "color_map": args.color_map, } generator = Generator() diff --git a/utils.py b/utils.py index 462bf85..d7830f6 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,5 @@ from datetime import datetime, timezone +import numpy as np import webbrowser @@ -63,3 +64,47 @@ def dateInRange(date, date_range) -> bool: date = datetime.strptime(date, "%Y-%m-%d") return (min_date is None or min_date <= date) and \ (max_date is None or max_date >= date) + + +def getColormapAsGradient(name: str, n_steps: int=20) -> dict[int, str]: + """Returns the desired colormap in gradient format. + + Colormaps are retrieved from branca.colormap.linear by default. + Names starting with mpl. are retrieved from matplotlib (optional import). + Names starting with cmc. are retrieved from cmcrameri (optional import). + + Arguments: + name {str} -- Colormap name + + Returns: + dict -- Keys are a range from 0 to 1, values are hex strings. + """ + # Define steps + steps = np.linspace(0, 1, n_steps) + + # Default behavior: Get from branca + if "." not in name or name.lower().startswith("branca."): + from branca.colormap import linear as cm + cmap = getattr(cm, name) + gradient = {i: cmap(i) for i in steps} + + # Get from defined library + else: + lib, name = name.split(".") + lib = lib.lower() + + # Matplotlib and derivatives + if lib in ("mpl", "cmc"): + from matplotlib.colors import to_hex + if lib == "mpl": + from matplotlib import colormaps as cm + cmap = cm.get_cmap(name) + elif lib == "cmc": + from cmcrameri import cm + cmap = getattr(cm, name) + gradient = {i: to_hex(cmap(i)) for i in steps} + + else: + raise ValueError(f"Unknown colormap library '{lib}'.") + + return gradient From 3ba4c12bbb7481f8c2c9cc7ecd798066fdbe1f62 Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Sat, 17 Aug 2024 15:48:16 +0200 Subject: [PATCH 2/5] Update arguments in doc --- utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/utils.py b/utils.py index d7830f6..2f4079e 100644 --- a/utils.py +++ b/utils.py @@ -74,7 +74,8 @@ def getColormapAsGradient(name: str, n_steps: int=20) -> dict[int, str]: Names starting with cmc. are retrieved from cmcrameri (optional import). Arguments: - name {str} -- Colormap name + name {str} -- Colormap name. + n_steps {int} -- Number of quantization steps. Returns: dict -- Keys are a range from 0 to 1, values are hex strings. From 966e00e2ee7eb0de7733c3dc76226360a5b13e7e Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Sat, 17 Aug 2024 16:14:29 +0200 Subject: [PATCH 3/5] Improve argument doc --- geo_heatmap.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geo_heatmap.py b/geo_heatmap.py index 561bd26..4820e87 100644 --- a/geo_heatmap.py +++ b/geo_heatmap.py @@ -311,8 +311,8 @@ def run(self, data_files, output_file, date_range, stream_data, settings): parser.add_argument("-mz", "--max-zoom", dest="max_zoom", type=int, required=False, help="The maximum zoom of the heatmap. (default: %(default)s)", default=4) parser.add_argument("-c", "--color-map", dest="color_map", type=str, required=False, - help="The color map to use.\n" \ - "(from branca (default), matplotlib ('mpl.'), cmcrameri ('cmc.').", default=None) + help="The color map to use, from:\n" \ + "branca (), matplotlib (mpl.), cmcrameri (cmc.).", default=None) args = parser.parse_args() data_file = args.files From 919b64848f7d87c3fcadd97cfd2fd3f37a28e608 Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Sat, 17 Aug 2024 18:07:11 +0200 Subject: [PATCH 4/5] Add cmap to README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 6a70f85..762b1b5 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,11 @@ Set a date range: python geo_heatmap.py --min-date 2017-01-02 --max-date 2018-12-30 Records.json ``` +Change the color map: +```shell +python geo_heatmap.py -c Set1_09 Records.json +``` + Advanced heatmap settings: ```shell @@ -123,6 +128,9 @@ optional arguments: The minimum opacity of the heatmap. (default: 0.2) -mz MAX_ZOOM, --max-zoom MAX_ZOOM The maximum zoom of the heatmap. (default: 4) + -c COLOR_MAP, --color-map COLOR_MAP + The color map to use, from: + branca (), matplotlib (mpl.), cmcrameri (cmc.) ``` ### 6. Review the Results From 72c7390e097890c4826019104c71a7ca56953251 Mon Sep 17 00:00:00 2001 From: Olivier Burggraaff Date: Sat, 17 Aug 2024 18:08:22 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 762b1b5..b514112 100644 --- a/README.md +++ b/README.md @@ -129,8 +129,8 @@ optional arguments: -mz MAX_ZOOM, --max-zoom MAX_ZOOM The maximum zoom of the heatmap. (default: 4) -c COLOR_MAP, --color-map COLOR_MAP - The color map to use, from: - branca (), matplotlib (mpl.), cmcrameri (cmc.) + The color map to use, from: + branca (), matplotlib (mpl.), cmcrameri (cmc.) ``` ### 6. Review the Results