kjslover has asked for the wisdom of the Perl Monks concerning the following question:

I am seeking some wisdom on Geo::Shapefile::Writer and how to define the vertices of a polygon. I am able to successfully create points and lines and simple polygons (i.e. a few vertices), but for more complex polygons (i.e. > 100 vertices), I am at a lost. Is there anyway to use an array to define the vertices?

Update: Resolved... thanks to all that helped me, it was the [\@pairs] that was needed. Arghhh, KISS and read!

Replies are listed 'Best First'.
Re: Geo::Shapefile::Writer help
by tangent (Vicar) on Jan 28, 2016 at 16:14 UTC
    Not sure where you are having a problem - if you can create a simple polygon with a few vertices it is easy to add as many vertices as you like:
    my $shp_writer = Geo::Shapefile::Writer->new( $name, 'POLYGON', @attr_ +descriptions ); my $shape = [ [86.925, 27.988], [86.5278, 27.744], [86.43, 27.365], # ...many more vertices ]; $shp_writer->add_shape( $shape, @attributes );

      Because POLYGON supports multi-part polygons you have to put each part in an array reference too.

      my $pt1 = [ $x, $y ]; my $part1 = [ $pt1, $pt2, ...]; my $polygon = [ $part1, $part2, ...];
Re: Geo::Shapefile::Writer help
by u65 (Chaplain) on Jan 28, 2016 at 16:22 UTC

    Welcome, kjslover! As tangent showed, it is easy to input arrays of any size. It should be possible to generate the required data with a suitable tool with the data output to a format easily read by your script and fed to the shape generator.

      Thanks to both tangent and u65. While the ease of inputting the numbers into the $shape object makes sense, how might I do this with an array that I am creating? I currently have an @coords array that I am placing my x,y values in. How do I transform this into the $shape object that tangent has referenced?
      I apologize if this is a basic question... I am self-taught in the ways of Perl and learn mainly from the O'Reilly books and online resources

        How do I transform this into the $shape object that tangent has referenced?

        If @coords is created as:
        @coords = ([$x1, $y1], [$x2, $y2], [$x3, $y3], ....);
        then it's just a matter of doing:
        $shape = \@coords;
        If just one array of (x1,y1,x2,y2...) the easiest way would be to use List::Util
        use List::Util qw(pairmap); my @coords = (86.925, 27.988, 86.5278, 27.744, 86.43, 27.365); my @pairs = pairmap { [ $a, $b ] } @coords; $shp_writer->add_shape( \@pairs, @attributes );

        I'm sorry I can't say more without knowing more about the format of the shapefiles which I don't have the time to look into at the moment, but it looks like your @coords array will have to be a part of the "add_shape" subroutines argument somehow. I have dealt with shapefiles long ago and I remember it was ugly. You probably have shapefile help around, but here is a link that I would have to start with.