summaryrefslogtreecommitdiff
path: root/navit/tools/asc2navit
blob: 096f56dedf0afaee98fe473741d5a79ef2e9795d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Long qw(:config permute);
use Data::Dumper;
use Benchmark;

sub show_help;
sub preprocess_filelist;
sub open_outputhandle;
sub process_file;
sub run;

my $amenity = "point_unkn";
my $write_osm = 0;
my %files = ();
my $filename = "onebigfile";
my $poicount = 0;


GetOptions ('a|amenity=s'      => \$amenity,
            'x|write-osm!'     => \$write_osm,
            'h|?|help!'        => \&show_help,
            'O|output-file=s'  => \$filename,
            '<>'               => \&preprocess_filelist);


# when no file is given...
show_help() if (keys %files == 0);

# determine filename
determine_filename();

# open the pipe for the output...
my $pipe = open_outputhandle();

# ...feed the pipe...
run();

# ...and close it properly
close $pipe;

###############################################################################
###############################################################################
###############################################################################

sub show_help {
    print "parses one (or multiple) .asc-file(s) (mainly used by poiwarner) and\n";
    print "merges the result into one navit binary mapfile. requires maptool (when -x is not set)\n";
    print "usage: $0 [OPTION] OUT in1.asc [in2.asc [...]]\n";
    print "       creates one big file called onebigfile\n";
    print "usage: $0 [OPTION] IN.asc\n";
    print "       creates a file called IN.asc.bin / .osm\n\n";
    print "  -h, --help              shows this help\n";
    print "  -x, --write-osm         write in osm's xml format instead of navit's\n";
    print "  -a, --amenity=NAME      sets the amenity to NAME. default is point_unk\n";
    print "                          you can use -a several times for different types of pois\n";
    print "                          e.g.: $0 -a tec_common speedcamdb1 speedcamdb2 -a poi_biergarten boozestation1 boozestation2\n";
    print "  -O, --output-file=OUT   write the output to OUT. default for multiple files ist onebigfile\n";
    exit 0;
}

sub preprocess_filelist {
    my ($filename) = @_;
    push (@{$files{$amenity}}, @_);

}

sub open_outputhandle() {
    # open the pipe to maptool...
    if (!$write_osm) {
        open $pipe, "| maptool $filename" or die $!;
    } else {
    # or a filehandle
        open $pipe, ">$filename" or die $!;
    }
    return $pipe;
}

sub process_file {
    my ($file) = @_;
    my $filehandle;
    print "$0 processing $file...\n";
    open ($filehandle, "<$file") or next $!;
    # for every line in the file...
    while (<$filehandle>) {
        # ...check if it's a valid record...
        m/([0-9\.\-]*), ([0-9\.\-]*), "\[([0-9]*).*/ or next;
        my ($lon, $lat, $id) = ($1, $2, $3);
        # ...and write it into the handle then
        print $pipe "    <node id=\"-$id\" visible=\"true\" lon=\"$lon\" lat=\"$lat\">\n";
        print $pipe "        <tag k=\"name\" v=\"\" />\n";
        print $pipe "        <tag k=\"amenity\" v=\"$amenity\" />\n";
        print $pipe "    </node>\n";
        $poicount++;
    }
}

sub determine_filename {
    my $filecount = 0;
    foreach my $amenity (keys %files) {
        $filecount += $#{$files{$amenity}} + 1;
    }

    # when it's only one file and no output file has been given...
    if ($filecount == 1 && $filename eq "") {
        my $onefile = $files{(keys %files)[0]}[0];
        # ...generate a new one from the given filename
        $filename = "$onefile.bin";
        $filename = "$onefile.xml" if ($write_osm);
    }

    print "$0 writing output to $filename\n";
}

sub run {
    # write osm header
    print $pipe '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    print $pipe '<osm version="0.5">' . "\n";

    # set the amenity and
    my @amenities = keys %files;
    while (my $amenity = shift @amenities) {
        foreach my $file (@{$files{$amenity}}) {
            process_file($file);
        }
    }

    # close the osm file
    print $pipe '</osm>';
    # and show up some stats
    print "$0 processed $poicount poi's\n";
}