source: subversion/applications/utils/osm-extract/polygons/poly2osm.pl

Last change on this file was 28782, checked in by frederik, 6 years ago

make poly2osm support poly files without leading spaces

File size: 2.1 KB
Line 
1#!/usr/bin/perl
2
3# script to convert a polygon file to an OSM file for
4# editing in JOSM etc.
5# the polygon file is expected to have the structure
6#
7# line 1:     symbolic name
8# line 2:     id of first polygon
9# lines 3-n:  coordinates of first polygon, each line beginning with
10#             whitespace, then having lon and lat of the point in scientific notation
11# line n+1:   END
12# the lines 2 to n+1 may then be repeated for further polygons, and the last line must be
13# END (so that the file ends with 2 lines having "END").
14#
15# written by Frederik Ramm <frederik@remote.org>, public domain.
16
17my %nodehash;
18
19# first line
20# (employ workaround for polygon files without initial text line)
21my $poly_file = <>; chomp($poly_file);
22my $workaround = 0;
23if ($poly_file =~ /^\d+$/)
24{
25    $workaround=$poly_file;
26    $poly_file="none";
27}
28
29my $nodecnt = -1;
30my $waycnt = -1;
31
32my $nodes;
33my $ways;
34my $note = "    <tag k='note' v='created by poly2osm.pl from a polygon file. not for uploading!' />\n";
35
36while(process_poly()) { undef $workaround; };
37print "<osm generator='osm2poly.pl' version='0.5'>\n";
38print $nodes;
39print $ways;
40print "</osm>\n";
41
42sub process_poly()
43{
44    my $poly_id;
45    if ($workaround==0) {
46       $poly_id=<>;
47    } else {
48       $poly_id=$workaround;
49    }
50    chomp($poly_id);
51    my $startnode = $nodecnt;
52    return 0 if ($poly_id =~ /^END/); # end of file
53
54    $ways .= sprintf("  <way id='%d'>\n    <tag k='polygon_id' v='%d' />\n    <tag k='polygon_file' v='%s' />\n",
55        $waycnt--, $poly_id, $poly_file);
56    $ways .= $note;
57
58    while($line = <>)
59    {
60        last if ($line =~ /^END/); # end of poly
61        $line =~ /^\s*(\S+)\s+(\S+)/ or die("cannot parse input line: $_");
62        my ($x, $y) = ($1, $2);
63        my $existingnode = $nodehash{"$x|$y"};
64        if (defined($existingnode))
65        {
66            $ways .= sprintf("    <nd ref='%d' />\n", $existingnode);
67        }
68        else
69        {
70            $nodehash{"$x|$y"} = $nodecnt;
71            $ways .= sprintf("    <nd ref='%d' />\n", $nodecnt);
72            $nodes .= sprintf("  <node id='%d' lat='%f' lon='%f' />\n", $nodecnt--, $y, $x);
73        }
74    }
75    $ways .= "  </way>\n";
76    return 1;
77}
Note: See TracBrowser for help on using the repository browser.