As I’ve mentioned before, I love data. The more of it there is, the more fun it is to dig into. And I also have a deep passion for programming. Recently I started building an app with Dart + Flutter + Material Design 3 and fell in love with the language. The experience is very similar to what I felt when I first played with Kotlin — an incredibly engaging and beautiful language from JetBrains.

For example, I really liked type extensions. It’s a mind-blowing feature for a PHP developer. I’d desperately want something like this in PHP:

extension Temperature on double{
  static const double kelvinOffset = 273.15;

  double asTemperature(int degreesUnit) {
    return 0 == degreesUnit ? toCelsius : toFahrenheit;
  }

  double get toCelsius => this - kelvinOffset;
  double get toFahrenheit => toCelsius * (9/5) + 32;
}
import 'package:weather/extension/temperature.dart';

final double temp = weather!.temperatureNow.asTemperature(degreesUnit);
Language:
Dart

But let’s get to the point: Dart is not only a JIT language, it’s also a compiled one. I really liked that flexibility, so I decided to write my first CLI script for converting KML to GPX. For some reason, none of the free online tools could produce a valid GPX that Garmin Connect would accept — it kept throwing a parsing error. So I set aside one evening, opened the docs, and in less than an hour wrote my first working Dart program that can be easily run from the command line. It looks like this:

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
String convertKmlToGpx(XmlDocument kml, {required bool verbose}) {
  final name = kml.xpath('/kml/Document/name').first.innerText;
  final track = kml.xpath('/kml/Document/Folder/Placemark');

  final date = DateFormat('y-M-dTH:m:s').parse(track
      .map((node) => node.findElements('TimeStamp').first.findElements('when'))
      .first
      .single
      .innerText
      .toString());

  final builder = XmlBuilder();

  builder.processing('xml', 'version="1.0" encoding="UTF-8"');
  builder.element('gpx', nest: () {
    builder.attribute('creator', 'enuage');
    builder.attribute('version', version);
    builder.attribute('xsi:schemaLocation', 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/11.xsd');
    builder.attribute('xmlns', 'http://www.topografix.com/GPX/1/1');
    builder.attribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
    builder.attribute('xmlns:ns2', 'http://www.garmin.com/xmlschemas/GpxExtensions/v3');
    builder.element('metadata', nest: () {
      builder.element('time', nest: '${date.toIso8601String()}Z');
    });
    builder.element('trk', nest: () {
      builder.element('name', nest: name);
      builder.element('trkseg', nest: () {
        for (var node in track) {
          final when = node
              .findElements('TimeStamp')
              .firstOrNull
              ?.findElements('when')
              .firstOrNull
              ?.innerText
              .trim();

          final coordinates = node
              .findElements('Point')
              .firstOrNull
              ?.findElements('coordinates')
              .firstOrNull
              ?.innerText
              .trim();

          if (when == null || coordinates == null) continue;

          final parts = coordinates.split(',');
          if (parts.length < 2) continue;

          final longitude = double.tryParse(parts[0].trim());
          final latitude = double.tryParse(parts[1].trim());
          if (longitude == null || latitude == null) continue;

          final elevationM = parts.length > 2 ? double.tryParse(parts[2].trim()) : null;
          // final elevationF = elevationM != null ? elevationM * 3.28084 : null;

          builder.element(
            'trkpt',
            attributes: {'lat': '$latitude', 'lon': '$longitude'},
            nest: () {
              if (elevationM != null) {
                final eleStr = elevationM == elevationM.truncateToDouble()
                    ? elevationM.toInt().toString()
                    : '$elevationM';
                builder.element('ele', nest: () => builder.text(eleStr));
              }

              builder.element('time', nest: () => builder.text(
                '${DateFormat('y-M-dTH:m:s+00:00').parse(when).toIso8601String()}Z'
              ));
            },
          );
        }
      });
    });
  });

  return builder.buildDocument().toXmlString(pretty: true);
}
Language:
Dart
Repository path:
bin/kml2gpx.dart
Commit:
924a1ac9

The script worked on the first try, parsed perfectly by Garmin Connect, and you can view the activity interactively:

Garmin Connect

Title: «BTI725 TBS (UGTB) ✈ RIX (EVRA)»