source: subversion/applications/utils/import/wdb2osm/wdb2osm.pl @ 5763

Last change on this file since 5763 was 5761, checked in by hakan, 13 years ago

Create river tags too

  • Property svn:executable set to *
File size: 5.5 KB
Line 
1#!/usr/bin/perl
2
3=pod
4
5=head1 NAME
6
7wdb2osm.pl -- Converts CIA World Databank II datafiles into .osm files
8suitable for loading into JOSM
9
10=head1 SYNOPSIS
11
12perl wdb2osm.pl filename
13
14=head1 DESCRIPTION
15
16Converts a WDB-II datafile as downloaded from
17http://www.evl.uic.edu/pape/data/WDB/ into .osm files suitable for
18loading into JOSM.
19
20The CIA World DataBank II has land outlines, rivers, and political
21boundaries collected by the USA government during the 1980's. Make
22sure what you are importing hasn't changed substantially since then.
23
24To get the border for a country in Europe, download the European data
25file, then run this script and point it to europe-bdy.txt. It will
26create a catalog alongside the txt file containing one file for each
27segment in the WDB data.
28
29Then load all these files into JOSM (Warning: this can consume quite a
30lot of memory...) and use the hide/show layer feature to find the data
31for the border you want. Merge all these into a single layer and
32delete the other layers. Tag as appropriate and upload.
33
34=head1 BUGS
35
36This script uses neither warnings, strict or -T. A bit of a hack
37really.
38
39The data has lots of flaws. Make sure to check it for sanity before
40uploading
41
42=head1 AUTHOR
43
44Knut Arne Bjørndal, bob+osm at cakebox dot net, or bobkare@irc
45
46=head1 COPYRIGHT
47
48(c) 2007 Knut Arne Bjørndal. This program is free software; you can
49redistribute it and/or modify it under the GNU GPL.
50
51See http://www.gnu.org/licenses/gpl.txt
52
53=cut
54
55
56sub save_way()
57{
58    # Output the way with all the segments, then close the file
59    if( $#segments )
60    {
61        print OUT "<way id='-1'>\n";
62        foreach(@nodes)
63        {
64            print OUT "  <nd ref='$_' />\n";
65        }
66
67        @foo = split(/[\/\\]/, $filename);
68        $source = $foo[$#foo];
69        $source = "CIA World database II - " . $source . " - segment " . $segmentNumber;
70
71        print OUT "  <tag k=\"source\" v=\"CIA World database II\" />\n";
72        print OUT "  <tag k=\"wdb:source\" v=\"" . $source . "\" />\n";
73       
74        if ($filename =~ /-bdy/)
75        {
76            print OUT "  <tag k=\"name\" v=\"Border ???? - ????\" />\n";
77            print OUT "  <tag k=\"boundary\" v=\"administrative\" />\n";
78            print OUT "  <tag k=\"border_type\" v=\"nation\" />\n";
79            print OUT "  <tag k=\"left:country\" v=\"????\" />\n";
80            print OUT "  <tag k=\"right:country\" v=\"????\" />\n";
81        }
82        elsif ($filename =~ /-riv/)
83        {
84            print OUT "  <tag k=\"boat\" v=\"yes\" />\n";
85            print OUT "  <tag k=\"name\" v=\"???\" />\n";
86            print OUT "  <tag k=\"waterway\" v=\"river\" />\n";         
87        }
88
89        print OUT "  <!-- $min_lat ... lat ... $max_lat -->\n";
90        print OUT "  <!-- $min_lon ... lon ... $max_lon -->\n";
91
92        print OUT "</way>\n";
93
94        print "    $min_lat ... lat ... $max_lat , $min_lon ... lon ... $max_lon \n";
95    }
96}
97
98
99sub main()
100{
101    # Get the filename from argument
102    $filename = $ARGV[0];
103    if($ARGV[0] eq "")
104    {
105        print "Usage: perl wdb2osm.pl filename\n";
106    }
107
108    print "Opening $filename:\n";
109
110    # Create directory alongside source file
111    $dirName = $filename;
112    $dirName =~ s/\.txt//g;
113    mkdir "$dirName";
114
115    ## Open file
116    open( FILE, "< $filename" ) or die "Can't open $filename: $!\n";
117
118    $j=0;
119    ## foreach line in the file (read line)
120    while( my $line = <FILE> )
121    {
122        # Remove tabs
123        $line =~ s/\t//g;
124   
125        # Split line on spaces
126        @lineSplit = split(" ",$line);
127
128        if(@lineSplit[0] eq "segment")
129        {
130            save_way();
131            print OUT "</osm>";
132            close(OUT);
133
134            # Get the remaining "special elements"
135            $segmentNumber = @lineSplit[1];
136            $rankNumber = @lineSplit[3];
137            $points = @lineSplit[5];
138       
139            # name the file according to the segment number
140            # $newFile = "segment$segmentNumber";
141            $newFile = 100000 + $segmentNumber;
142            $newFile =~ s/^1//;
143            $newFile = "segment-" . $newFile;
144            # open the file for writing
145            $outfile = "$dirName/$newFile.osm";
146            print "Opening $outfile\n";
147            open OUT, "> $outfile" or die "Can't open $outfile : $!";
148           
149            $min_lat =  10000;
150            $max_lat = -10000;
151
152            $min_lon =  10000;
153            $max_lon = -10000;
154
155            # Initialize IDs.
156            # We start nodes at -1 and segments at -2 then alternate as we
157            # go along.
158            # The first node will actually be -3 and the first segment
159            # -5. The way get ID -1 This makes sure we get unique IDs for
160            # every element.
161            $nodeid = -1; $segmentid = -2;
162
163            # @segments = ();
164            @nodes = ();
165
166            # Output header
167            print OUT "<?xml version='1.0' encoding='UTF-8'?>\n";
168            print OUT "<osm version='0.5' generator='wdb2osm.pl'>\n";
169        }
170        elsif( @lineSplit[0] ne "segment" && @lineSplit[0] ne "" )
171        {
172            $nodeid-=2;
173
174            $lat = $lineSplit[0];
175            $lon = $lineSplit[1];
176
177            print OUT "<node id=\"$nodeid\" lat=\"$lat\" lon=\"$lon\"/>\n";
178
179            $min_lat = $lat if ($lat < $min_lat);
180            $max_lat = $lat if ($lat > $max_lat);
181
182            $min_lon = $lon if ($lon < $min_lon);
183            $max_lon = $lon if ($lon > $max_lon);
184
185            push(@nodes, $nodeid);
186        }
187    }
188
189    # There might be left-over data...
190    save_way();
191    print OUT "</osm>";
192    close(OUT);
193}
194
195
196#
197#   Run that script...
198#
199main();
200
Note: See TracBrowser for help on using the repository browser.