peterstangl / svg2data Goto Github PK
View Code? Open in Web Editor NEWA Python module for reading data from a plot provided as SVG file.
License: GNU General Public License v2.0
A Python module for reading data from a plot provided as SVG file.
License: GNU General Public License v2.0
Hi,
I'm trying to extract data from this PDF containing several hundred plots. https://fortress.wa.gov/ecy/publications/documents/0803037appc.pdf
For example, I'm starting with the second plot on page 14 for Alki East Chlorophyll-a. If all I do is save this page as an SVG using Inkscape 1.0, I get this:
svg2data/svg2data.py in get_axes(lines, width, height)
730 cleaned_axes[i].append(axes[i][j])
731 axes = cleaned_axes
--> 732 axes_min = np.array([axes[0][0]['min'][0],axes[1][0]['min'][1]])
733 axes_max = np.array([axes[0][0]['max'][0],axes[1][0]['max'][1]])
734 new_lines = []
IndexError: list index out of range
Next I tried to make the job easier by deleting everything from the page except the plot I want. Same error.
Next, I used Inkscape to Resize Page to Selection (the resulting SVG is attached with the extension changed to please github)
[0803037appc_p14.txt](https://github.com/peterstangl/svg2data/files/5046091/0803037appc_p14.txt
This produces:
svg2data/svg2data.py in __init__(self, filename, test, debug)
104 and debug != 'get_axes'
105 and debug != 'connect_graphs'):
--> 106 grids = calibrate_grid(axes,phrases,width,height)
107 elif debug == 'calibrate_grid':
108 self.debug = {'axes':axes,
svg2data/svg2data.py in calibrate_grid(axes, phrases, width, height)
1033 axis_scaling = 'linear'
1034 else:
-> 1035 raise Exception('no grid found!')
1036 grids_calibr[axis_type]['type']=axis_scaling
1037 grids_calibr[axis_type]['grid']=grid_calibr
Exception: no grid found!
Any ideas why it can't find the plot? Is a grid required?
Could this be due to the version of Python I am running?
data = svg2data("test.svg")
Traceback (most recent call last):
File ~\AppData\Roaming\Python\Python311\site-packages\IPython\core\interactiveshell.py:3505 in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
Cell In[6], line 1
data = svg2data("test.svg")
File D:\Users\Nick\Documents\Python\svg2data.py:49 in init
child = pass_transformation(child)
File D:\Users\Nick\Documents\Python\svg2data.py:233 in pass_transformation
child = pass_transformation(child)
File D:\Users\Nick\Documents\Python\svg2data.py:213 in pass_transformation
parent_matrix = transform2matrix(parent_transform)
File D:\Users\Nick\Documents\Python\svg2data.py:169 in transform2matrix
matrix = ast.literal_eval(matrix_string)
File C:\Program Files\Python311\Lib\ast.py:64 in literal_eval
node_or_string = parse(node_or_string.lstrip(" \t"), mode='eval')
File C:\Program Files\Python311\Lib\ast.py:50 in parse
return compile(source, filename, mode, flags,
File :1
[1 0 0 1 8 8]
^
SyntaxError: invalid syntax. Perhaps you forgot a comma?
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_102044/517397449.py in
----> 1 from svg2data import svg2data
D:\OneDrive\Work\ICM\QCircle\sandbox\options\svgexport\svg2data\svg2data.py in
8 import os.path
9 from math import sin, cos, tan, pi
---> 10 afm_dir = os.path.join(rcParams['datapath'],'fonts', 'afm')
11 afm_dict = {}
12 for afm_file in os.listdir(afm_dir):
d:\Miniconda3\envs\icm\lib\site-packages\matplotlib_init_.py in getitem(self, key)
672 plt.switch_backend(rcsetup._auto_backend_sentinel)
673
--> 674 return dict.getitem(self, key)
675
676 def repr(self):
KeyError: 'datapath'
Hello, my SVG file was generated with Adobe Illustrator. To obtain height and width Use:
root.attrib
>>> {'viewBox': 'min max width height'}
width = root.attrib['viewBox'].split()[2]
height = root.attrib['viewBox'].split()[3]
But then this error appears:
562 if 'style' in path.attrib:
563 style = path.attrib['style']
--> 564 path.attrib['style'] = style
565 path.attrib['d'] = lines_and_curves_to_path(line_list,curve_list)
566 if path.attrib not in paths:
UnboundLocalError: local variable 'style' referenced before assignment
How can I fix that?
Originally posted by @JCLArriaga5 in #3 (comment)
Hi!
I found your utility here and feel like it may be very useful for me. I try to use it as manual says but got an error:
from svg2data import svg2data
data = svg2data("/data/test.svg")
Produces:
ValueError Traceback (most recent call last)
<ipython-input-2-15f3cf48a742> in <module>
----> 1 data = svg2data("/data/test.svg")
2 data.writesvg('/data/newfile.svg')
~/Downloads/svg2data-master/svg2data.py in __init__(self, filename, test, debug)
38 self._tree = ET.parse(filename)
39 root = self._tree.getroot()
---> 40 width = float(root.attrib['width'])
41 height = float(root.attrib['height'])
42
ValueError: could not convert string to float: '210mm'
when I changed sources to:
width = float(root.attrib['width'][:-2])
height = float(root.attrib['height'][:-2])
I was able to read and write svg file in the debug mode debug='get_axes'
, but I can't figure out how to fix other parts of the code which are responsible for plotting.
Is it a problem of my sample svg file or not? Could you help me to fix this issue?
Thanks,
Anton
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.