Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get coordinates of an svg?

How can I find out the coordinates 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!!).

like image 717
Rob Avatar asked Sep 06 '25 09:09

Rob


1 Answers

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.

Screenshot of Inkscape -S command from DOS

Screenshot of Inkscape -S command from DOS

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.

![Screenshot of R's read_csv function reading this data

like image 181
simone Avatar answered Sep 09 '25 00:09

simone