<< Back to Map Development Forum   Search

Posts 1 - 4 of 4   
Renaming Path to Territory_X: 7/20/2012 14:47:48


Gwy
Level 45
Report
Hello World !

As I was unable to make the renaming script or addon for territories provided in help pages, I managed to create mine.
I give you the result : a Perl script, feel free to use and improve it.
I'm using Strawberry Perl and svg files created with Inkscape 0.48.

--Be advised that all objects with id starting by "path" will become a territory,
rename the id of thoses you don't want to become so (like text...).

--Instructions
Create the START.BAT and PERLID.PL as explained below,
Rename your svg file to IN.SVG,
All theses 3 files need to be in the same directory.
delete OUT.SVG if existing
run START.BAT, the OUT.SVG file is created.

-----Content of START.BAT file :
perlid.pl
pause
-----

-----Content of PERLID.PL file :
# Renamer of svg object's id starting by "path"
# PERL script Created by Gwy 17july2012
use strict;
use warnings;

open FILE, "<", "in.svg"; # the name of the input file (read mode)
open OUTFILE, ">>", "out.svg"; # name of the output file (APPEND MODE), check that it did not exist !!!
my $line = $_; # initiate variable
my $cnt=1; # Counter of modified lines, will be the number after Territorry_
print ("Start\n"); # flag
while(<FILE>) # reading the input file a line at a time in loop
{
$line = $_; # get chars of the line from builtin variable
if (/id="path.*"/) { # modify only id lines starting by "path"
$cnt++; # counting one line modification
$line =~ s/id="path.*"/id="Territory_$cnt"/g; # Replace the name of the id
}
print OUTFILE $line; # add the line at the end of the output file
}
print ("END - modified lines : ");
print ($cnt); # gives the number of modified lines, use .bat file with pause to see it
close FILE;
close OUTFILE;
-----
Renaming Path to Territory_X: 7/20/2012 15:44:08

RvW 
Level 54
Report
Cool, very useful. :)

Couple suggestions for possible further improvement:
  • You should really replace "my $cnt=1;" with "my $cnt=0;". Just test it on an empty file and you'll see it will claim there was one modified line.
  • If you'd like, you can move the "pause" from the Batch script to the Perl script (and then get rid off the Batch script). I don't speak Perl, but it will definitely have some way to read input. (Don't forget to write a message such as "Press enter to close this program" before your "pause" command.)
  • Was this your second Perl program (after Hello World! :p ), or do you already have some experience (sorry, hard to judge from such a short program)? If you have some experience, you might want to consider using a command line parameter to tell the script which file to work with (again, I don't know Perl, but Googling "perl command line parameter" should do the trick).
    At first you can test it by changing the Batch script to
    perlid.pl "in.svg"
    Once that works, you can simply drag any SVG file onto the Perl script and it will be processed!

    If, for whatever reason, you really want to keep using the combination of a Batch and a Perl script, you will need to change the Batch script to
    perlid.pl "%~f1"
    to pass the name of the file along. I'm really sorry about the ugly code, but unfortunately that's the correct way to make Windows handle spaces in filenames properly.
Renaming Path to Territory_X: 7/20/2012 19:21:26


Moros 
Level 50
Report
There's already an ID tool out there, it's on the wiki.
Renaming Path to Territory_X: 7/20/2012 20:08:40

RvW 
Level 54
Report
Posts 1 - 4 of 4