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

Last change on this file was 7192, checked in by frederik, 11 years ago

drop polys with less than 3 nodes

File size: 1.8 KB
Line 
1#!/usr/bin/perl
2
3use Math::Polygon;
4use Getopt::Long;
5
6my $percentage = 100;
7my $slope = 0.1;
8my $same = 0.001;
9my $help = 0;
10
11GetOptions ("percentage=i" => \$percentage,
12  "slope=f"   => \$slope, 
13  "same=f"  => \$same,
14  "help" => \$help) or help();
15
16help() if $help;
17
18# first line
19my $line = <>;
20print $line;
21
22while(1)
23{
24    my @poly;
25    my $line = <>;
26    last if ($line =~ /^END/); # end of file
27    my $hdr=$line;
28    print STDERR "polygon $line";
29    while($line = <>)
30    {
31        last if ($line =~ /^END/); # end of poly
32        my ($dummy, $x, $y) = split(/\s+/, $line);
33        push(@poly, [$x, $y]);
34    }
35    my $poly = Math::Polygon->new(@poly);
36    printf STDERR "%d points original\n", $poly->nrPoints;
37    my $max_points = $poly->nrPoints * $percentage / 100;
38    my $simplified = $poly->simplify(max_points => $max_points, 
39        same => $same, slope => $slope);
40    printf STDERR "%d points simplified\n", $simplified->nrPoints;
41    next if ($simplified->nrPoints<3);
42    print $hdr;
43    foreach my $pt($simplified->points)
44    {
45        printf "   %E   %E\n", $pt->[0], $pt->[1];
46    }
47    print "END\n";
48}
49print "END\n";
50
51sub help
52{
53    print STDERR <<EOF;
54simplify-poly.pl: simplifies a polygon file as used by Osmosis et al.
55
56options:
57
58--slope=x (default: $slope) - with three points X(n),X(n+1),X(n+2),
59    the point X(n+1) will be removed if the length of the path over
60    all three points is less than "x" longer than the direct path
61    between X(n) and X(n+2).
62
63--same=x (default: $same) - remove points that are nearer than "x" to
64    their neighbours.
65
66--percent=x (default: $percent) - remove corners with widest angles
67    until remaining polygon has only x% of the original number of nodes.
68
69Note that for --slope and --same, distance is measured in degrees, so
70gets distorted away from the equator.
71EOF
72    exit;
73}
74
Note: See TracBrowser for help on using the repository browser.