Playing around with GPX, Ruby and sqlite

For personal requirements, I wanted to convert some POIs in GPX format to a SQLite database file. I did it in Ruby, which was my very first Ruby code ever. It turned out to be not that difficult, though Ruby (object orientation aside) is different compared to C++. The code I wrote, however, is not much object oriented; instead it looks more like C-code or a bash script containing lots of functions. Nevertheless it does the job.

ruby-2008.png


While writing to a sqlite database file really was simple, I first needed to figure out which XML library to use. First I tried GPX GEM. The example code, which reads and writes a GPX file, did fail. I dropped it because the docs stated:

The track side of the library has seen much more use than the route/waypoint side, so if you’re doing something with routes or waypoints, you may need to tweak some things.

The next I wanted to try was libxml, as it promised excellent performance and the capability to process XML as a stream. This would have been useful as file sizes often exaggerate when dealing with openstreetmap data. Unfortunately, gem install failed while compiling the binary module. Frankly, I was to lazy to figure out why.

So I ended with the built-in REXML (“Ruby Electric XML”). For my needs (reading some GPX) it was easy enough to use and does a good job. It would be interesting, though, how its performance compares to libxml. Processing 847348 POIs in 163 GPX-files did last a while, so any improvement would be appreciated.

All over all, the script is just about 60 lines of code. Being new to Ruby and SQL, it took me three evenings and one Saturday, including some reading of »The Ruby Programming Language« by David Flanagan & Yukihiro Matsumoto (aka Matz), the Ruby master himself.

Comments are closed.