in reply to Geo::Shapefile::Writer help

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.

Replies are listed 'Best First'.
Re^2: Geo::Shapefile::Writer help
by kjslover (Initiate) on Jan 28, 2016 at 19:57 UTC

    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 );

        tangent, thank you, that makes perfect sense. I have tried the following code and am now getting an error regarding "Can't use string ("0") as an ARRAY ref while "strict refs" in use at <path to> line 141". I have tried adding the use strict; and declaring $x/$y/$dummy, but did not help.
        The following code is what I am using to try to get it to work:

        use Geo::Shapefile::Writer; use List::Util qw(pairmap); $shape = Geo::Shapefile::Writer->new("Temp", 'POLYGON', [COL1 => 'C', +10],); my @coords = (0, 0, 0, 1, 1, 1, 1, 0, 0, 0); my @pairs = pairmap { [ $a, $b ] } @coords; # Checking to make sure pairs look correct foreach $dummy (@pairs) { ($x, $y) = @$dummy; print "$x, $y\n"; } $shape->add_shape(\@pairs, {Col1 => "Dumb1"}); $shape->finalize();

      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.