From 05bfd43614ac7b8a75852706d92081cab48979c5 Mon Sep 17 00:00:00 2001 From: abrammer Date: Mon, 27 Aug 2018 23:55:37 -0400 Subject: [PATCH 1/2] edits to iter_rings, to properly handle 'Z' and prep for other flags --- mplleaflet/utils.py | 47 +++++++++++++++++++++++++++++++++------- tests/test_mplleaflet.py | 10 +++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/mplleaflet/utils.py b/mplleaflet/utils.py index 102894e..c12b81c 100644 --- a/mplleaflet/utils.py +++ b/mplleaflet/utils.py @@ -3,17 +3,48 @@ def iter_rings(data, pathcodes): + ''' + Segment and follow SVG paths + INPUT + ---------- + data: + (n,2) list of [x,y] points + pathcodes: + list of SVG character commands + YIELDS + ------ + (n,2) list of path coords + NOTES + ----- + `data` and `pathcodes` are not neccesarily the same length. + Some codes use multiple data points, some use none. + Loop through path codes popping off elements as needed, + yeild a path whenever it's complete + ''' + # TODO implement C, S and others: + # info on svg commands here: + # https://css-tricks.com/svg-path-syntax-illustrated-guide/ + ring = [] - # TODO: Do this smartly by finding when pathcodes changes value and do - # smart indexing on data, instead of iterating over each coordinate - for point, code in zip(data, pathcodes): - if code == 'M': - # Emit the path and start a new one + while pathcodes: + pathcode = pathcodes.pop(0) + if pathcode == 'Z': + ring.append(ring[0]) + yield ring + ring=[] + elif pathcode == 'M': if len(ring): yield ring - ring = [point] - elif code == 'L' or code == 'Z' or code == 'S': - ring.append(point) + ring = [data.pop(0)] + elif pathcode == 'L': + ring.append(data.pop(0)) + elif pathcode == 'C': + _ = data.pop(0) + _ = data.pop(0) + ring.append(data.pop(0)) + elif pathcode == 'S': + _ = data.pop(0) + ring.append(data.pop(0)) else: raise ValueError('Unrecognized code: {}'.format(code)) diff --git a/tests/test_mplleaflet.py b/tests/test_mplleaflet.py index 22b6577..0490ee2 100644 --- a/tests/test_mplleaflet.py +++ b/tests/test_mplleaflet.py @@ -1,6 +1,16 @@ import matplotlib.pyplot as plt +import numpy as np import mplleaflet +def test_contourf(): + x = np.linspace(-10,10,101) + y = np.linspace(-10,10,101) + xx, yy = np.meshgrid(x,y) + grid = np.sin(xx) + np.sin(yy) + plt.contourf(xx, yy, grid) + mplleaflet.fig_to_html() + + def test_basic(): plt.plot([0, 1], [0, 1]) mplleaflet.fig_to_html() From 324387b2a139233e463ebcdb21c7ac1b05787ac6 Mon Sep 17 00:00:00 2001 From: abrammer Date: Tue, 28 Aug 2018 00:13:51 -0400 Subject: [PATCH 2/2] edit so as to not pop from the iterated list --- mplleaflet/utils.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mplleaflet/utils.py b/mplleaflet/utils.py index c12b81c..d40b147 100644 --- a/mplleaflet/utils.py +++ b/mplleaflet/utils.py @@ -26,7 +26,8 @@ def iter_rings(data, pathcodes): # https://css-tricks.com/svg-path-syntax-illustrated-guide/ ring = [] - while pathcodes: + i=0 + while i < len(pathcodes): pathcode = pathcodes.pop(0) if pathcode == 'Z': ring.append(ring[0])