[Mapserver-users] PERL mapscript polygon shapefile creation

Eric Bridger eric at gomoos.org
Tue Feb 25 09:12:55 PST 2003


--=-tXobYR1b8yN9iCuMfdz5
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Joe,

Turns out I just wrote such a script today.  The only  problem I see
with your script is that I think you should call $shape->setBounds()
after you've added all your lines and before $shapeFile->add($shape).

I've attached my script.  It reads a list of polygon centroids in
decimal degrees and creates 10 minute squares.

Eric




On Tue, 2003-02-25 at 19:41, Joe Bussell wrote:
> Greetings Listers,
>     I am having difficulty porting a technique we use to create point 
> and line type shapefiles to create a polygon shapefile.  Has anyone used 
> PERL to create polygon shapefiles from data files?  I have attached my 
> attempt for review.  The script creates a shapefile that is readable by 
> dump.pl, but I have not been able to use it in a map render.
>     I tried this with a data file with the following lines:
> 40.0 -116.0
> 45.0 -116.0
> 45.0 -120.0
> 40.0 -120.0
> 
> The script "closes" the polygon by inserting the first record at the end.
> 
>     Any ideas?
> 
> Cordially,
> 
> Joe Bussell


--=-tXobYR1b8yN9iCuMfdz5
Content-Disposition: attachment; filename=nmfs2shp.pl
Content-Transfer-Encoding: quoted-printable
Content-Type: text/x-perl; name=nmfs2shp.pl; charset=ISO-8859-15

#!/usr/bin/perl -w
use strict;

use mapscript;
use XBase;

# NNFS INPUT comma separated centroids.

#0 NFNS 10 minute square id
#1 Total catch pounds
#2 Centroid Latitude (decimal degrees)
#3 Centroid Longitude (decimal degrees)


my $file =3D shift or die "No input file\n";

my $shapeFile =3D new mapscript::shapefileObj("nmfs_squares", $mapscript::M=
S_SHAPEFILE_POLYGON);

my $dbf_file =3D XBase->create(
"name" =3D> "nmfs_squares.dbf",
"field_names" =3D> ["NMFSID", "TOTAL_CATCH"],
"field_types" =3D> ["C", "N"],
"field_lengths" =3D> [7, 6],
"field_decimals" =3D> [undef, 0]);

my $count =3D 0;
open(IN, $file) or die "Could not open $file\n";
while(my $line =3D <IN>){
	chomp $line;
	$line =3D~ s/"//g;
	my @flds =3D split(/,/, $line);
	my $shape =3D create_rect($flds[2], $flds[3]);
	if(!$shape){
		warn "No shape\n";=09
		next;
	}
	$shapeFile->add($shape);
	$dbf_file->set_record($count, $flds[0], $flds[1]);
	$count++;
}
close (IN);

undef $shapeFile;
$dbf_file->close();

print "Created: $count\n";
exit;

sub create_rect{
	my($lat, $lon) =3D @_;=09
	my $point =3D new mapscript::pointObj();
    my $line =3D new mapscript::lineObj();
	# max xy point
	$point->{x} =3D $lon + .0833;
	$point->{y} =3D $lat + .0833;
	$line->add($point);
	# max x min y
	$point->{x} =3D $lon + .0833;
	$point->{y} =3D $lat - .0833;
	$line->add($point);
	# min xy
	$point->{x} =3D $lon - .0833;
	$point->{y} =3D $lat - .0833;
	$line->add($point);
	# min x max y
	$point->{x} =3D $lon - .0833;
	$point->{y} =3D $lat + .0833;
	$line->add($point);

	# max xy point again to close polygon
	$point->{x} =3D $lon + .0833;
	$point->{y} =3D $lat + .0833;
	$line->add($point);

	my $shp =3D new mapscript::shapeObj($mapscript::MS_SHAPE_POLYGON);
	$shp->add($line);
	$shp->setBounds();

	return $shp;
}

--=-tXobYR1b8yN9iCuMfdz5--




More information about the MapServer-users mailing list