File: buffer.pl

package info (click to toggle)
mapserver 8.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 18,020 kB
  • sloc: ansic: 139,708; cpp: 111,541; python: 3,004; xml: 1,722; yacc: 1,108; cs: 997; lex: 747; sh: 606; java: 588; perl: 489; makefile: 370; tcl: 158; ruby: 55
file content (66 lines) | stat: -rw-r--r-- 1,602 bytes parent folder | download | duplicates (11)
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
#!/usr/bin/perl
#
# Script : buffer.pl
#
# Purpose: Applies buffer to shapefile dataset using geos support
#          buffer units as are per units of data
#
# $Id$
#

use strict;
use warnings;
use POSIX;
use XBase;
use mapscript;
use Getopt::Long;
use File::Copy;

my ($infile, $outfile, $buffer);

GetOptions("input=s", \$infile, "output=s", \$outfile, "buffer=n", \$buffer);

if(!$infile or !$outfile or !$buffer) {
  print "Usage: $0 --input=[filename] --output=[filename] --buffer=[native units]\n";
  exit 0;
}

die "Tolerance must be greater than zero." unless $buffer > 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.";

# 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);

  print "buffering feature: $i\n";

  $outshape = $inshape->buffer($buffer) or die "Unable to buffer feature #$i: $!\n"; # in native map units

  $outSHP->add($outshape);
  undef($outshape); # free memory associated with shape

} # for each shape

$outSHP = ''; # write the file

undef $inSHP;
undef $outSHP;