Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions mplleaflet/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,49 @@


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
i=0
while i < len(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))

Expand Down
10 changes: 10 additions & 0 deletions tests/test_mplleaflet.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down