Colouring maps with scripts

Post questions on how to use or achieve an effect in Inkscape.
TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Colouring maps with scripts

Postby TastyCakes » Thu Apr 23, 2009 8:41 am

Hello, I've been using Inkscape to make various maps for a while now. Specifically I've been making electoral district maps to illustrate the boundaries of various Canadian ridings. I do this by tracing out a raster image, giving each riding a polygon. I was hoping for two things: first to be able to cycle through each polygon on the map and make it a specific colour (leaving the rest of the polygons gray) and saving a version of each. I know that's not very clear, perhaps showing you this group of images makes it more clear: http://commons.wikimedia.org/wiki/Category:SVG_maps_of_Alberta_provincial_electoral_districts
Many are the same maps but with a different riding coloured red, could I make a script to automate the process?

The other thing I'd like to do is make a script that automatically sets the colours for various territories based on a list, likely in a .csv or .txt file. I would use that to quickly colour in world maps, like this one: http://en.wikipedia.org/wiki/File:World_vehicles_per_capita.svg or for election results like this one: http://en.wikipedia.org/wiki/File:Canada_Fed_election_2008_Ridings.svg.

I'm not sure how feasible these two things are... I've been looking around today trying to find out, but I'm a novice at python (well any scripting, really) and I don't really know where to start. Does anyone have any suggestions?

thanks a lot.

Simarilius
Posts: 626
Joined: Wed Jun 06, 2007 2:37 am

Re: Colouring maps with scripts

Postby Simarilius » Sat Apr 25, 2009 7:08 pm

I think the first bit should be doable without too much grief, its simply a case of cycling through the ids setting one red at a time and saving a copy of the svg,
the list is slightly more complex but should still be doable, main thing will be identifying what polygon is what region.
I'd suggest dropping by the jabber room, there are people there who know more about this stuff and its the kind of thing thats probably more easily discussed in a chat :D

TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Re: Colouring maps with scripts

Postby TastyCakes » Tue Apr 28, 2009 6:25 am

Ah ok, thanks a lot, sorry to be a total Newbie but what/where is the Jabber room exactly?

TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Re: Colouring maps with scripts

Postby TastyCakes » Wed Apr 29, 2009 1:26 am


TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Re: Colouring maps with scripts

Postby TastyCakes » Thu Apr 30, 2009 2:07 am

Well the people on the jabber channel didn't seem to know either (one person suggested I do it all in the XML file rather than inkscape itself, but I don't know enough to do that either). So if anyone has any ideas on how to do this, please let me know, otherwise I guess I'm gonna have to give up on this...

User avatar
EarlyBlake
Posts: 302
Joined: Tue Jun 03, 2008 5:42 pm

Re: Colouring maps with scripts

Postby EarlyBlake » Thu Apr 30, 2009 5:52 am

TastyCakes wrote:Well the people on the jabber channel didn't seem to know either (one person suggested I do it all in the XML file rather than inkscape itself, but I don't know enough to do that either). So if anyone has any ideas on how to do this, please let me know, otherwise I guess I'm gonna have to give up on this...


You can see the XML tree in inkscape, open the XML editor Shift Control X. Click on something on the page. The item you clicked on will be highlighted in the XML tree. Click on one of you paths then click on style. You see the fill and stoke attribute set there as well as a name for the path. But doing a search and replace on say fill color "#0000ff" with "#000000" on the XML tree probably would not be any faster than clicking on an object and changing the color by hand. How many objects/counties are you talking about?

TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Re: Colouring maps with scripts

Postby TastyCakes » Fri May 01, 2009 8:24 am

Right I've used the XML view a little and am aware of the ability to search and replace in it. Like you say, that alone is not really going to do what I want... The maps I'd like to use this on in the future range, from perhaps 20 polygons for a city or province map to hundreds for the federal election results by riding maps. I thought that by "do it in the XML file" he meant write a program that directly modifies the XML rather than uses the built in inkscape scripter (neither of which I have much clue about)

TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Re: Colouring maps with scripts

Postby TastyCakes » Fri May 01, 2009 8:27 am

My ultimate goal is to have a blank map of the electoral districts standing by, then immediately after an election be able to generate a list of the results by riding from elections Canada or wherever and then run a script that colours in each riding according to which party one it and by what percentage. I think having a script to do that would vastly simplify an otherwise monotonous task prone to errors.

User avatar
EarlyBlake
Posts: 302
Joined: Tue Jun 03, 2008 5:42 pm

Re: Colouring maps with scripts

Postby EarlyBlake » Fri May 01, 2009 5:33 pm

With only 20 districts you would spend orders of magnitude more time, 1) learning a scripting language, 2) writing the scrip and then 3) debugging then, you would doing it by hand. If you think you want to spend the time learning to scripting language, and they are useful thing to know, I think MircoSoft Excel or the OpenOffice equivalent Calc plus OpenOffice Draw, would have more of the functionality you are looking for. I think you can get Inkscape into some kind of plain text format that any program can manipulate, but there will always be glitches getting it back in to and SVG format.I haven't used Excel in years but it had almost all the functionality that you are looking for last time I use it. And Open Office Suit is supposed to have every thing Excel has and more. So if you want to do it I think finding an Open Office board would be the way to go.

TastyCakes
Posts: 8
Joined: Thu Apr 23, 2009 8:14 am

Re: Colouring maps with scripts

Postby TastyCakes » Sat May 02, 2009 12:42 am

Doing it once or twice, that's true (and I've done it several times now). But doing it with larger maps, doing it for all provincial ridings in Canada and colouring in a finished map according to results are all things that I think lend themselves well to an automated process. And yes, I would quite like to learn a bit of python, or whatever ends up being the solution.
Thanks for your suggestions regarding office. I've used VBA quite extensively in Excel, and I can see that directly editing text files with VBA macros might be a possible solution. It just seems a little strange that inkscape would have this build in scripting functionality but that this stuff isn't possible within the program.

User avatar
EarlyBlake
Posts: 302
Joined: Tue Jun 03, 2008 5:42 pm

Re: Colouring maps with scripts

Postby EarlyBlake » Sat May 02, 2009 5:29 am

TastyCakes wrote:Doing it once or twice, that's true (and I've done it several times now). But doing it with larger maps, doing it for all provincial ridings in Canada and colouring in a finished map according to results are all things that I think lend themselves well to an automated process. And yes, I would quite like to learn a bit of python, or whatever ends up being the solution.
Thanks for your suggestions regarding office. I've used VBA quite extensively in Excel, and I can see that directly editing text files with VBA macros might be a possible solution. It just seems a little strange that inkscape would have this build in scripting functionality but that this stuff isn't possible within the program.


I'm sure you can do it in inkscape. I just meant man hours trade off might be stepper then you'd think with inkscape. There are a lot fewer tutorials, less documentation and support around for inkscape than for "established programs" that don't have 0. in front of them.

majikins
Posts: 1
Joined: Tue Oct 15, 2013 8:59 am

Re: Colouring maps with scripts

Postby majikins » Tue Oct 15, 2013 9:13 am

This is for nix users. Create a csv file with ID name if item to shade ie region on map, and the color next to it. eg this is content:(file1)
MP314, d40000
MP315, ff0000

Then reference svg file you want to recolour - file2.

Run this script in command line:
awk -F, 'FNR==NR{A["\""$1"\""]=$2;next}{for(i in A)if($0~i)R=A[i];if(R && $0~/style/){j++;if(j==2){gsub(/#.*;/,"#"R";");R=j=""}}}1' file1 file2 > file3

Script recolors file2 according to the colors specified in file1 and creates new file - file3.
Works brilliantly according to this content format in your file2:

snippet:
<g
transform="translate(-312.51
id="MP301"
style="fill:#d40000;fill-opa
<title
id="title4022">blah blah</title>
<path
d="m 1264,311 1,0 1,0 0,-1 1,0 1,-1 0,-1 2,-1 0,-1 1,-1 -1,0 0,-1 1,0 1,0 0,2 1,-1 1,-1 1,-1 -1,-1 -1,-1 1,-2 1,0 2,1 0,1 1,1 1,0 3,1 -2,-2 1,-2 1,-1 1,0 1,0 0,-1 1,0 4,2 2,1 0,1 0,1 1,1 1,1 0,2 1,2 0,1 1,0 1,0 2,0 0,1 0,2 -1,1 1,0 2,1 1,0 2,0 3,0 2,0 1,0 1,0 0,-1 1,0 2,-2 2,-2 0,2 0,2 1,1 1,0 4,-1 0,1 0,1 0,1 0,1 -1,0 0,1 0,1 0,1 -1,0 0,1 -1,1 0,1 0,1 -1,0 -1,2 0,1 -1,1 -1,2 0,1 -1,0 0,1 0,1 -1,0 0,1 -1,1 0,1 -1,1 -1,1 0,1 -3,3 -1,0 -1,2 -1,0 -1,2 0,1 -1,2 -1,0 0,1 -1,0 0,1 -2,4 0,1 -1,2 -1,-1 -1,0 -2,0 -2,-1 -1,-3 0,-1 1,0 0,-1 2,0 2,0 0,-1 -1,-1 0,-1 -1,0 0,-1 -1,0 1,0 1,-2 -1,-1 -1,-1 -1,0 -1,0 -1,0 -3,0 -1,0
-1,0 -1,1 0,-2 1,-2 0,-1 0,1 0,-1 0,1 0,-1 -1,-2 -1,-1 0,-1 -1,0 -1,1 -1,0 -1,0 -1,0 -1,1 -1,0 -1,0 -1,0 -1,-1 -3,-2 -1,0 -1,0 -1,-1 -2,-1 -1,2 1,1 -1,1 -1,0
0,-1 -1,-1 0,-1 -1,1 -2,1 -2,0 -1,0 -1,1 -1,0 -1,0 0,-1 -1,2 -1,1 -1,1 -1,0 0,-1 0,-1 0,-1 -1,0 -1,0 -1,0 -1,0 0,-1 -2,0 -1,0 -1,-1 -1,0 0,1 -2,0 0,1 -1,0 0,1 -1,0 -1,1 0,-1 -1,0 -1,-1 -1,0 -1,0 -1,0 -1,0 -1,0 -1,0 -1,0 -1,1 0,1 -1,-1 0,-1 -1,-1 0,-1 -1,-2 1,-2 0,-1 0,-1 0,-1 0,-1 1,0 0,-1 1,0 0,-1 0,-1 1,0 0,-1 1,-2 0,-1 -1,0 0,-1 1,0 0,-1 1,0 0,-1 -
,0 1,0 1,-1 2,0 0,-1 1,-1 2,0 1,0 1,-
id="path443"
style="fill:#d40000;fill-o
</g>

Script looks for first value in file1, finds it, then changes the 2nd style="fill...." value

Got the script from a kind bash guru on Unix.com. Hope this helps someone.


Return to “Help with using Inkscape”