2008/10/15

create and export shape data to csv file


#!/usr/bin/perl

#
# an attempt to create a polygon shapefile
# ref http://lists.osgeo.org/pipermail/mapserver-users/2005-March/005414.html
#

use strict ;
use warnings;
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]]
) ;
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=>\@verts
};
push @{$shape->{ShapeRecords}}, [1,13,250];
$shape->dump();

$shape->save('./test.shp');

#!/usr/bin/perl

use strict;
use Geo::Shapelib qw/:all/;

my $shapefile = new Geo::Shapelib {
Name => 'stations',
Shapetype => POINT,
FieldNames => ['Name','Code','Founded'];
FieldTypes => ['String:50','String:10','Integer:8'];
};

while () {
chomp;
my($station,$code,$founded,$x,$y) = split /\|/;
push @{$shapefile->{Shapes}},{ Vertices => [[$x,$y,0,0]] };
push @{$shapefile->{ShapeRecords}}, [$station,$code,$founded];
}

$shapefile->save();



0 件のコメント: