PERL - Geo::Shapelib help

Stephen Woodbridge woodbri at SWOODBRIDGE.COM
Tue Mar 8 16:33:41 PST 2005


JFD,

A couple of comments.

1) [lat, long] == [Y, X]  so all you X/Y coordinates are being stored as
Y/X which is not a problem in and of itself, until you try to line it up
with other data.

2) from the man page ... Vertices=>[[$x,$y]] which is not what you are
doing below. I think you might want to do something like reverse your
lat-lon's when you create your @verts array

Then when you create your shape:

   Vertices => \@verts

Also don't ser the bounds, they will get set automatically based on your
data when you do the save.

-Steve W.



JFD wrote:
> Hello,
>
> I'm trying to create a polygon shapefile using the PERL module
> Geo::Shapelib and find I can't quite get it.
>
> At this stage, I'm just trying to create a square polygon to get the hang
> of it, before moving on to bigger and better things.    Unfortunately, the
> script bombs on me despite many consultations of the man page and the ESRI
> shapefile whitepaper.   My best attemp to accomplish this task is below.
> Any guidance on what I'm doing wrong would be greatly appreciated.
>
> Thanks,
>
> JFD
> ------------------------------------------------------------------
> #!/usr/bin/perl
>
> #
> #  an attempt to create a polygon shapefile
> #
>
> use strict ;
> use Geo::Shapelib ;
>
> # points of the polygon
> my @lat = (45.0,   45.0,  55.0,  55.0,  45.0)  ;
> my @lon = (-75.0, -85.0, -85.0, -75.0, -75.0)  ;
>
> my @verts = (
>         [$lat[0], $lon[0]],
>         [$lat[1], $lon[1]],
>         [$lat[2], $lon[2]],
>         [$lat[3], $lon[3]],
>         [$lat[4], $lon[4]]
>         )  ;
> #print "@verts\n" ;
> my $shape = new Geo::Shapelib;
> $shape->{Shapetype} = 5;
>
> $shape->{MinBounds} = [20,-100] ;
> $shape->{MaxBounds} = [80,-10] ;
>
> $shape->{FieldNames} = ['ID',     'SPD','DIR'];
> $shape->{FieldTypes} = ['Integer','Integer','Integer'];
>
>         push @{$shape->{Shapes}}, {
>         SHPType=>5,
>         ShapeId=>1,
>         Nparts=>0,
>         NShapes=>0,
>         NVertices=>5,
>         Vertices=>[\@lat, \@lon]
>         };
>         push @{$shape->{ShapeRecords}}, [1,13,250];
>         #i++;
>         $shape->dump();
>
> $shape->save('/mydir/test');
>



More information about the MapServer-users mailing list