aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJon duSaint2022-07-13 10:58:59 -0700
committerJon duSaint2022-07-13 10:58:59 -0700
commit20958863a49be776d7687f541979e0dbc185a220 (patch)
tree52e03917523c7bd90a27b0652f4a9c7128625182
parent6a290f10cf9aaa142db637834b54602286204394 (diff)

gpx2kml: Bugfixes and add –color option

-rwxr-xr-xgpx/gpx2kml.pl23
1 files changed, 15 insertions, 8 deletions
diff --git a/gpx/gpx2kml.pl b/gpx/gpx2kml.pl
index 22d09f4..e73c205 100755
--- a/gpx/gpx2kml.pl
+++ b/gpx/gpx2kml.pl
@@ -15,6 +15,7 @@ my $stuckToGround = 0;
my $pretend = 0;
my $rename = 0;
my $name = '';
+my $color;
my $folder = 0;
sub usage {
@@ -29,6 +30,7 @@ Options:
--ground,-g same as above
--autoname,-a rename KML file based on start date/time
--name,-n name of track
+ --color,-C color for track: name or RGB
--pretend,-n show what would be done, but don't do it
USAGE
exit;
@@ -41,6 +43,7 @@ GetOptions('outfile|o' => \$kml,
'ground|g' => \$stuckToGround,
'pretend|n' => \$pretend,
'name|n=s' => \$name,
+ 'color|C=s' => \$color,
'autorename|a' => \$rename) or usage();
@ARGV > 0 || usage;
@@ -50,7 +53,7 @@ unless ($kml) {
$kml = pop @ARGV;
} else {
$kml = $ARGV[0];
- $kml =~ s/\.gpx$/.kml/;
+ $kml =~ s/\.gpx$/.kml/i;
}
}
@@ -91,19 +94,17 @@ foreach my $gpx (@ARGV) {
if ($gx->{trk}) {
my $trkname = $gx->{trk}->{name}
// $gx->{metadata}->{name}
- // ($gx->{name} . ($name ? "-$name" : ''))
+ // (defined ($gx->{name}) ? ($gx->{name} . ($name ? "-$name" : '')) : undef)
// $name;
- my $color = color ($gx->{trk}->{extensions}->{"gpxx:TrackExtension"}->{"gpxx:DisplayColor"});
-
kml_push ('Placemark');
kml_print ('name', "<![CDATA[$trkname]]>");
kml_print ('visibility', '1');
kml_push ('Style');
kml_push ('LineStyle');
- kml_print ('color', $color);
- kml_print ('width', '1.9');
+ kml_print ('color', color ($color // $gx->{trk}->{extensions}->{"gpxx:TrackExtension"}->{"gpxx:DisplayColor"}));
+ kml_print ('width', '2.2');
kml_pop (); # LineStyle
kml_pop (); # Style
@@ -112,7 +113,8 @@ foreach my $gpx (@ARGV) {
kml_push ('coordinates');
foreach my $p (@{$gx->{trk}->{trkseg}->{trkpt}}) {
- my $coordinate = "$p->{lon}.,$p->{lat},$p->{ele}";
+ my $elevation = defined ($p->{ele}) ? ",$p->{ele}" : '';
+ my $coordinate = "$p->{lon}.,$p->{lat}$elevation";
kml_print (undef, $coordinate); # XXX: Strip out time info for now
push @total_track_points, $coordinate;
}
@@ -144,7 +146,8 @@ foreach my $gpx (@ARGV) {
kml_push ('Point');
kml_print ('altitudeMode', $altitudeMode);
kml_push ('coordinates');
- kml_print (undef, "$wpt->{lon}.,$wpt->{lat},$wpt->{ele}"); # XXX: Strip out time info for now
+ my $elevation = defined ($wpt->{ele}) ? ",$wpt->{ele}" : '';
+ kml_print (undef, "$wpt->{lon}.,$wpt->{lat}$elevation"); # XXX: Strip out time info for now
kml_pop (); # coordinates
kml_pop (); # Point
@@ -231,6 +234,10 @@ sub color {
my $havename = defined($name);
$name = ".+" unless $havename;
+ if ($havename && $name =~ m/^(?:0x)?([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/i) {
+ return "ff$3$2$1"; # RGB in -> ABGR out
+ }
+
my $rgbfile = '/usr/share/X11/rgb.txt';
my $rand = int(rand((split(/ /, `wc -l $rgbfile`))[0]));
open(RGB, "<$rgbfile") or goto END;