Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Interfacing Perl and SVG

by Baz (Friar)
on Aug 14, 2002 at 00:04 UTC ( [id://189956]=perlquestion: print w/replies, xml ) Need Help??

Baz has asked for the wisdom of the Perl Monks concerning the following question:


I've just dived into the deep end once again - this time itís SVG that Iím trying out.

Before now, I was producing maps using perl to extracting the relevant data from MySQL, to shade the sectors of the map using, and to spit the resulting html at the client. The problem with the sectors I use on my map is that they have no correspondence with county lines, which the client would be familiar with. Therefore, my preferred solution is to place an image map over the map and display the towns in a sector as the mouse passes over it. I was about half way through implementing this when I gave up, as the file size was become ridiculous and it was really, really awkward and time consuming.

Heres an example minus the image map

Anyway, I wish to explore animation this week so I'm trying to implement the above using SVG. So far I have a generic map (no shading), which displays the towns as earlier explained. In order to produce this map, I used the bitmap to svg utility, and this results in svg paths defining each sector.
Now I need to interface this with perl, so that the relevant shading can be done. Therefore, would it be sensible to load the SVG document into perl, parse it (SVG is XML), and simply add fill statements to the SVG doc stored in memory? Then spit this to the client. The reason I donít think its practical to code everything in perl is because I end up with a large SVG file after the bitmap conversion Ė and this basically consists of path lists defining the coordinates for each sector.



Non Perl Related: Iím also interested in having 2 versions of the same map as part of the one svg document. Just like in my current png versions (linked to above), where I have two identical maps side by side, but with different shading. To do this I could make a bitmap containing the 2 maps, and convert as before. But this will double the size of the file, which I wish to avoid. Is there a better alternative to this?

Replies are listed 'Best First'.
Re: Interfacing Perl and SVG
by Matts (Deacon) on Aug 14, 2002 at 07:48 UTC
    If you're doing the highlighting client-side (via rollovers) I'd just use the SVG DOM for it. Unfortunately perl doesn't have a fully capable SVG parser yet (though AxPoint contains an SVG path parser which you may find useful - that's probably the hardest part), so in order to get at those bits you're better off doing it client side.

    Unlike HTML, SVG has a very stable client-side Javascript spec, and works very well in the Adobe viewer (using the mozilla javascript engine).

    If that doesn't cover what you're after, then yes, parse with a SAX parser and extract the paths. I don't know how they'll be done in your SVG (there are various different ways in SVG to define a path).

    The other thing to do is to hand-refine your SVG, so that each region is a <g> group. Then you can do funky things with those groups like enlarge and bring them forward as you mouse over them. There's been some good examples of this on

      Thanks Matts,

      I've now defined my paths inside the "def" tags so I can make use of the "use" tags which allow me to display multiple versions of my map. I was thinking of making all the dynamic values, such as the type of fill required for each county, part of a Javascript array. That would make the parsing procedure much easier, as I'd just have to stick this in at the top somewhere.

      I was also wondering about whether it would be possible to send the SVG version to the client - which is a large file at this stage, and then for each statistical set (same maps, but with new shadings) I could send them this array. No idea as of yet how this might be done... For my site I have a list of surnames, and I shade a map to show the distribution of a selected surname. Here the current png version of this



Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://189956]
Approved by Zaxo
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-14 07:20 GMT
Find Nodes?
    Voting Booth?

    No recent polls found