<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/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 &lt;shapefile&gt;\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 &lt; $sf-&gt;{numshapes}; $i++) {
  # fetch the shape
  my $s = $sf-&gt;getShape($i);

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

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

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

  print &lt;&lt;END;

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

  # free shape
  undef $s;
}

# free shapefile
undef $sf;
</pre></body></html>