Sophie

Sophie

distrib > Mageia > 4 > x86_64 > media > core-updates > by-pkgid > ed66227122e3242b4be06b9620dcec5b > files > 11

mapserver-perl-6.2.2-1.2.mga4.x86_64.rpm

#!/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;