
338
|
Chapter 6, Mapping on Your Desktop
#71 Turn Your Tracklogs into ESRI Shapefiles
HACK
As you can see, the gpstrans output format is very simple. There’s a header
line with some metadata, followed by several lines of data, each containing
four tab-separated fields: the data type (i.e.,
T for tracklog), the timestamp,
and a latitude/longitude pair. Obviously, the output from other GPS data
download programs will look different, but the principle is the same.
Extending the following code to work with the output of GARNIX and
other programs is left as an exercise for the reader.
On the Mapping Hacks web site, you’ll find a GPX-based
version of track2shp.pl under http://mappinghacks.com/code/.
The Code
#!/usr/bin/perl
use Geo::Shapelib;
use Time::Piece;
use constant TIME_THRESHOLD => 10; # minutes
use constant TIME_FORMAT => "%m/%d/%Y %H:%M:%S";
use strict;
use warnings;
my ($in, $out) = @ARGV;
die "Usage: $0 \n" unless $in and $out;
my $shp = new Geo::Shapelib;
$shp->{Name} = $out;
$shp->{Shapetype} = 3; # PolyLine, according to Geo::Shapelib source
$shp->{FieldNames} = ['ID', 'Date'];
$shp->{FieldTypes} = ['Integer', 'String:16'];
open TRACK, "<", $in or die "Can't open $in: $!";
my $id = 0;
my (@vertices, $previous);
sub add_shape {
return unless @vertices > 2;
push @{$shp->{Shapes}}, {
SHPType => 3, # PolyLine
ShapeId => $id,
NVertices => scalar @vertices,
Vertices => [@vertices]
};
push @{$shp->{ShapeRecords}}, ...