source: subversion/applications/utils/osm-extract/polygons/osm2poly.pl @ 18468

Revision 18468, 1.5 KB checked in by frederik, 4 years ago (diff)

fix regex to not mistake uid=... for id=...

Line 
1#!/usr/bin/perl
2
3# script to convert an OSM file to a polygon file.
4# the OSM file must follow certain conventions, namely
5# each way must have a polygon_file and polygon_id tag,
6# may have a note tag and must not have others.
7#
8# written by Frederik Ramm <frederik@remote.org>, public domain.
9
10use strict;
11
12my $poly_id;
13my $poly_file;
14my $polybuf;
15my $outbuf;
16my $nodes;
17my $id=0;
18
19while(<>) 
20{
21    if (/^\s*<node.*\sid=["']([0-9-]+)['"].*lat=["']([0-9.eE-]+)["'] lon=["']([0-9.eE-]+)["']/)
22    {
23        $nodes->{$1}=[$2,$3];
24    }
25    elsif (/^\s*<way /)
26    {
27        undef $poly_id;
28        undef $poly_file;
29        $polybuf = "";
30    }
31    elsif (defined($polybuf) && /k=["'](.*)["']\s*v=["'](.*?)["']/)
32    {
33        if ($1 eq "polygon_file") 
34        {
35            $poly_file=$2;
36        }
37        elsif ($1 eq "polygon_id")
38        {
39            $poly_id=$2;
40        }
41        elsif ($1 ne "note")
42        {
43            die("cannot process tag '$1'");
44        }
45    }
46    elsif (/^\s*<nd ref=['"]([0-9-]+)["']/)
47    {
48        my $id=$1;
49        die("dangling reference to node $id") unless defined($nodes->{$id});
50        $polybuf .= sprintf("   %E   %E\n", $nodes->{$id}->[1], $nodes->{$id}->[0]);
51    }
52    elsif (/^\s*<\/way/) 
53    {
54        if (!defined($polybuf))
55        {
56            die("incomplete way definition");
57        }
58        $poly_id = ++$id unless defined($poly_id);
59        $outbuf .= "$poly_id\n$polybuf"."END\n";
60        undef $polybuf;
61    }
62}
63$poly_file = "polygon" unless defined($poly_file);
64print "$poly_file\n$outbuf"."END\n";
65
Note: See TracBrowser for help on using the repository browser.