开发者

How to get coordinates of an svg?

开发者 https://www.devze.com 2023-03-26 06:41 出处:网络
How can I find out the coor开发者_如何学Pythondinates of an svg? I have an Adobe Illustrator file that contains a map, this has been drawn and separated into US states, how can I find the coordinates

How can I find out the coor开发者_如何学Pythondinates of an svg? I have an Adobe Illustrator file that contains a map, this has been drawn and separated into US states, how can I find the coordinates of each state?

I'm just using the US map as an example, I'm going to potentially use this technique for several other maps (much more local!!).


Inkscape does that beautifully. It has a command interface, described in http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine.html . (The link is to a copy of the manual on the website of its author, Tavmjong Bah. Note that it warns that the manual hasn't been updated for the latest version of Inkscape. However, the command worked fine when I tried it.)

This command

inkscape -S some_file.svg

will output lines containing an element id, the x and y coordinates of the top-left corner, and the element's width and height. There is one line for each element of the SVG. Here's an example:

svg2293,26.447175,24,97.105652,92.450851
layer1,26.447175,24,97.105652,92.450851
MyStar,26.447175,24,97.105652,92.450851

This example comes from http://tavmjong.free.fr/INKSCAPE/MANUAL/html/CommandLine-Query.html . It extracts information from an SVG which includes a star shape, shown on my first link.

On my Windows 10 system, Inkscape lives in c:\Program Files\Inkscape\ , and the executables are in the bin\ subdirectory of that. If I cd to that subdirectory, Windows will recognise the inkscape command; likewise if I use the full path to the executable from somewhere else, e.g.

"c:\Program Files\Inkscape"\bin\inkscape -S some_file.svg

Putting it on my PATH would presumably also work.

Inkscape has a lot of other commands, which include others for extracting information about object positions and sizes. The latter are called "query commands". One can extract information about a specified object, e.g.

inkscape --query-id=zoom-in -X /usr/share/inkscape/icons/icons.svg

This is an example of finding the x position of the zoom-in icon in the default icon file on a Linux system.

To save the output to a file, use > . E.g.

"c:\Program Files\Inkscape"\bin\inkscape -S some_file.svg > coords.txt

As it's reassuring to see actual examples, here are two screenshots of this working.

How to get coordinates of an svg?

How to get coordinates of an svg?

Once you have the data in a file, you can read it into programs. Below is a screenshot of me doing this in the R programming language, using the read_csv function ( https://readr.tidyverse.org/reference/read_delim.html ). This puts the data into a table, which I then displayed.

How to get coordinates of an svg?


SVG have an XML structure. The states will be in <path> tags, hopefully with the name of the state somewhere as an attribute. The coordinates of a path are defined by the d attribute, but they can get quite complex as they can be relative or absolute and have various types of curves. With curves, it's probably simplest to consider just the final two values, which is where the curve ends.

For full details, see: http://www.w3.org/TR/SVG/paths.html#PathDataGeneralInformation

The situation may be more complex if further transforms are applied to the paths. Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消