/usr/share/doc/libmapscript-perl/examples/thin.pl is in libmapscript-perl 7.0.0-9ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | #!/usr/bin/perl
# Script : thin.pl
#
# Purpose: An adaption of the ArcView Avenue example script genfeat.ave. It's
# basically the Douglas-Peucker generalization algorithm.
# (http://mapserver.gis.umn.edu/community/scripts/thin.pl)
#
# $Id$
#
use strict;
use warnings;
use POSIX;
use XBase;
use mapscript;
use Getopt::Long;
use File::Copy;
my ($infile, $outfile, $tolerance);
GetOptions("input=s", \$infile, "output=s", \$outfile, "tolerance=n", \$tolerance);
if(!$infile or !$outfile or !$tolerance) {
print "Usage: $0 --input=[filename] --output=[filename] --tolerance=[maximum distance between vertices]\n";
exit 0;
}
die "Tolerance must be greater than zero." unless $tolerance > 0;
# initialize counters for reporting
my $incount = 0;
my $outcount = 0;
# open the input shapefile
my $inSHP = new mapscript::shapefileObj($infile, -1) or die "Unable to open shapefile $infile.";
die "Cannot thin point/multipoint shapefiles." unless ($inSHP->{type} == 5 or $inSHP->{type} == 3);
# create the output shapefile
unlink "$outfile.shp";
unlink "$outfile.shx";
unlink "$outfile.dbf";
my $outSHP = new mapscript::shapefileObj($outfile, $inSHP->{type}) or die "Unable to create shapefile '$outfile'. $!\n";
copy("$infile.dbf", "$outfile.dbf") or die "Can't copy file $infile.dbf to $outfile.dbf: $!\n";
my $inshape = new mapscript::shapeObj(-1); # something to hold shapes
for(my $i=0; $i<$inSHP->{numshapes}; $i++) {
$inSHP->get($i, $inshape);
my $outshape = new mapscript::shapeObj(-1);
for(my $j=0; $j<$inshape->{numlines}; $j++) {
my $inpart = $inshape->get($j);
my $outpart = new mapscript::lineObj();
my @stack = ();
$incount += $inpart->{numpoints};
my $anchor = $inpart->get(0); # save first point
$outpart->add($anchor);
my $aIndex = 0;
my $fIndex = $inpart->{numpoints} - 1;
push @stack, $fIndex;
# Douglas - Peucker algorithm
while(@stack) {
$fIndex = $stack[$#stack];
my $fPoint = $inpart->get($fIndex);
my $max = $tolerance; # comparison values
my $maxIndex = 0;
# process middle points
for (($aIndex+1) .. ($fIndex-1)) {
my $point = $inpart->get($_);
#my $dist = $point->distanceToLine($anchor, $fPoint);
my $dist = $point->distanceToSegment($anchor, $fPoint);
if($dist >= $max) {
$max = $dist;
$maxIndex = $_;
}
}
if($maxIndex > 0) {
push @stack, $maxIndex;
} else {
$outpart->add($fPoint);
$anchor = $inpart->get(pop @stack);
$aIndex = $fIndex;
}
}
# check for collapsed polygons, use original data in that case
if(($outpart->{numpoints} < 4) and ($inSHP->{type} == 5)) {
$outpart = $inpart;
}
$outcount += $outpart->{numpoints};
$outshape->add($outpart);
} # for each part
$outSHP->add($outshape);
undef($outshape); # free memory associated with shape
} # for each shape
$outSHP = ''; # write the file
undef $inSHP;
undef $outSHP;
my $reduction = ((($outcount / $incount) * 100) - 100) * -1;
print <<END;
$0 Summary:
Input File : $infile ($incount vertices)
Output File : $outfile ($outcount vertices)
Tolerance : $tolerance
Reduction : $reduction%
END
|