
Turn Your Tracklogs into ESRI Shapefiles #71
Chapter 6, Mapping on Your Desktop
|
339
HACK
chomp;
my ($type, $date, $lat, $long) = split /\t/, $_, 4;
my $now = eval { Time::Piece->strptime($date, TIME_FORMAT) };
next unless $now;
if (@vertices) {
add_shape( ) if $now - $previous > TIME_THRESHOLD * 60;
}
push @vertices, [$long, $lat]; # note, long before lat!
$previous = $now;
}
add_shape( );
$shp->save($out);
In its broadest outline, track2shp.pl reads each line from the given tracklog,
splitting the line up into its component fields and throwing away the lines it
can’t parse. The script builds up a set of vertices from the tracklog points,
storing them together as needed in a single PolyLine shape. (PolyLine is just
ESRI’s term for a series of connected line segments.) Finally, all of the accu-
mulated PolyLines are dumped to the shapefile(s).
Running the Code
Assuming the script is marked as executable and is in your current direc-
tory, you can run it as follows:
$ gpstrans -dt > tracklog.txt
$ ./track2shp.pl tracklog.txt tracklog.shp
$ ls
track2shp.pl tracklog.dbf tracklog.shp tracklog.shx tracklog.txt
As you can see, you should have three new files in the same directory: the .shp
file containing the shapes, the .shx index file, and the .dbf datafile.
Figure 6-31 shows a sample map of a trip we made through Los Angeles,
stopping off in Hollywood to pick up friends and then visiting Venice Beach, ...