1 | #!/usr/bin/perl |
---|

2 | |
---|

3 | use Math::Polygon; |
---|

4 | use Getopt::Long; |
---|

5 | |
---|

6 | my $percentage = 100; |
---|

7 | my $slope = 0.1; |
---|

8 | my $same = 0.001; |
---|

9 | my $help = 0; |
---|

10 | |
---|

11 | GetOptions ("percentage=i" => \$percentage, |
---|

12 | "slope=f" => \$slope, |
---|

13 | "same=f" => \$same, |
---|

14 | "help" => \$help) or help(); |
---|

15 | |
---|

16 | help() if $help; |
---|

17 | |
---|

18 | # first line |
---|

19 | my $line = <>; |
---|

20 | print $line; |
---|

21 | |
---|

22 | while(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 | } |
---|

49 | print "END\n"; |
---|

50 | |
---|

51 | sub help |
---|

52 | { |
---|

53 | print STDERR <<EOF; |
---|

54 | simplify-poly.pl: simplifies a polygon file as used by Osmosis et al. |
---|

55 | |
---|

56 | options: |
---|

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 | |
---|

69 | Note that for --slope and --same, distance is measured in degrees, so |
---|

70 | gets distorted away from the equator. |
---|

71 | EOF |
---|

72 | exit; |
---|

73 | } |
---|

74 | |
---|