File: shapeprops.pl

package info (click to toggle)
mapserver 7.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,620 kB
  • sloc: ansic: 176,974; cpp: 58,161; python: 2,969; xml: 1,676; cs: 997; yacc: 856; lex: 766; java: 588; perl: 428; makefile: 374; sh: 184; tcl: 158; ruby: 55
file content (46 lines) | stat: -rwxr-xr-x 873 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
#!/usr/bin/perl
#
# Script : shapeprops.pl
#
# Purpose: Applies area, perimeter and centroid calculations to a shapes
#          in a shapefile (requires mapserver/mapscript to be built with GEOS)
#
# $Id$
#

use strict;
use warnings;
use mapscript;

@ARGV == 1 or die "Usage: $0 <shapefile>\n";

# open the shapefile
my $sf = new mapscript::shapefileObj($ARGV[0], -1) or die "Unable to open shapefile $ARGV[0]: $!\n";

# loop over every shape
for(my $i = 0; $i < $sf->{numshapes}; $i++) {
  # fetch the shape
  my $s = $sf->getShape($i);

  # calculate area
  my $a = $s->getArea();

  # calculate length / perimeter
  my $l = $s->getLength();

  # calculate centroid
  my $c = $s->getCentroid();

  print <<END;

Area of shape $i: $a
Length / Perimeter of shape $i: $l
Centroid of shape $i: $c->{x}, $c->{y}
END

  # free shape
  undef $s;
}

# free shapefile
undef $sf;