Snippets

wrosner gpsbabel xcsv stylesheet to convert human readable/editable routes to gpx

Created by wrosner last modified
# testfile
# FIELD_DELIMITER PIPE
FIELD_DELIMITER COMMA
# FIELD_ENCLOSER WHITESPACE

RECORD_DELIMITER NEWLINE
# BADCHARS COMMA
# SHORTLEN 24

# IFIELD LATLON_HUMAN_READABLE,"","%c %d %f"
# IFIELD LATLON_HUMAN_READABLE,"","%f,%f"
IFIELD LAT_DECIMAL,"","%f"
IFIELD LON_DECIMAL,"","%f"

IFIELD DESCRIPTION,"","%s"
1
2
3
4
5
49.43226,11.09153 , Nürnberg Hermundurenstraße 17
49.73233,11.07244 , Forchheim Hugo-Post-Straße 84
49.42029,11.89065 , Kümmersbruck Grasiger Weg
49.15800,11.71157 , parsberg lindlbergstraße
49.43226,11.09153 , Nürnberg Hermundurenstraße 17
<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.0" creator="GPSBabel - http://www.gpsbabel.org" xmlns="http://www.topografix.com/GPX/1/0">
  <time>2016-06-16T19:56:15.310Z</time>
  <bounds minlat="49.158000000" minlon="11.072440000" maxlat="49.732330000" maxlon="11.890650000"/>
  <wpt lat="49.432260000" lon="11.091530000">
    <name>Nürnberg Hermundurenstraße 17</name>
    <cmt>Nürnberg Hermundurenstraße 17</cmt>
    <desc>Nürnberg Hermundurenstraße 17</desc>
  </wpt>
  <wpt lat="49.732330000" lon="11.072440000">
    <name>Forchheim Hugo-Post-Straße 84</name>
    <cmt>Forchheim Hugo-Post-Straße 84</cmt>
    <desc>Forchheim Hugo-Post-Straße 84</desc>
  </wpt>
  <wpt lat="49.420290000" lon="11.890650000">
    <name>Kümmersbruck Grasiger Weg</name>
    <cmt>Kümmersbruck Grasiger Weg</cmt>
    <desc>Kümmersbruck Grasiger Weg</desc>
  </wpt>
  <wpt lat="49.158000000" lon="11.711570000">
    <name>parsberg lindlbergstraße</name>
    <cmt>parsberg lindlbergstraße</cmt>
    <desc>parsberg lindlbergstraße</desc>
  </wpt>
  <wpt lat="49.432260000" lon="11.091530000">
    <name>Nürnberg Hermundurenstraße 17</name>
    <cmt>Nürnberg Hermundurenstraße 17</cmt>
    <desc>Nürnberg Hermundurenstraße 17</desc>
  </wpt>
  <rte>
    <rtept lat="49.432260000" lon="11.091530000">
      <name>Nürnberg Hermundurenstraße 17</name>
      <cmt>Nürnberg Hermundurenstraße 17</cmt>
      <desc>Nürnberg Hermundurenstraße 17</desc>
    </rtept>
    <rtept lat="49.732330000" lon="11.072440000">
      <name>Forchheim Hugo-Post-Straße 84</name>
      <cmt>Forchheim Hugo-Post-Straße 84</cmt>
      <desc>Forchheim Hugo-Post-Straße 84</desc>
    </rtept>
    <rtept lat="49.420290000" lon="11.890650000">
      <name>Kümmersbruck Grasiger Weg</name>
      <cmt>Kümmersbruck Grasiger Weg</cmt>
      <desc>Kümmersbruck Grasiger Weg</desc>
    </rtept>
    <rtept lat="49.158000000" lon="11.711570000">
      <name>parsberg lindlbergstraße</name>
      <cmt>parsberg lindlbergstraße</cmt>
      <desc>parsberg lindlbergstraße</desc>
    </rtept>
    <rtept lat="49.432260000" lon="11.091530000">
      <name>Nürnberg Hermundurenstraße 17</name>
      <cmt>Nürnberg Hermundurenstraße 17</cmt>
      <desc>Nürnberg Hermundurenstraße 17</desc>
    </rtept>
  </rte>
</gpx>

Comments (3)

  1. wrosner

    Rationale:

    provide a way to convert simple human readable / editable text files to gpx for further use e.g. in QMapShack, and/or to send to navigation devices.

    See also: https://bitbucket.org/maproom/qmapshack/issues/145/address-lookup-maybe-us-nominatim

    The input format was chosen to collect waypoints from the OSM web interface nominatim : http://nominatim.openstreetmap.org/

    Compared to adding waypoints from adresses using the QMS "Google" functionality and then manually clicking a route around this waypoints, I see some advantages: * it hits the points precisely, which you really will appreciate when saving the route to your navi device * I can refine the position easily in nominatim (eg if OSM just hits the middle of a road, but you know the precise location of your target) * I can keep the preplanned human readable / editable list for further use * I can easily play with the list (add / remove / rearrange adresses) and quickly look how the route changes

    We use gpsbabel as converter, so we can rely on its wide range of options and its tested capability to produce reasonably conform gpx files (or whatever else you may require).

    gpsbabel provides a format option called xcsv , which is able to read quite a range of csv and similiar shaped files. The details are controlled by a style file, as the the nominatim.style given above. The file nominatim.style can be used without change, as long as the format of the address list does not change. If you want to modify it, refer to the gpsbagel pdf Documentation. the specification for xcsv styles are described in the Appendix there.

    To fill the lines of your input route, simply copy/paste the coordinates from the nominatim web page, add a comma, and paste the search address (or whatever description you will attach to the waypoint). Compare to wpts-test5.csv given above as a format template. Whitespace around the separating comma does not seem to hurt.

    Then call gpsbabel like this

    #!
    gpsbabel -i xcsv,style=nominatim.style -f wpts-test5.csv -x transform,rte=wpt -o gpx -F wpts-test5.gpx
    

    and you will yield the output wpts-test5.gpx as shown above as third file. Open it in QMapShack, and you will find your points shown both as waypoints and as ordered route points, connected with straight lines. Call "calclulate route" and QMapShack will call your router (if properly configured).

    wps-test5.jpg

    If you omit the transform extension, like this,

    #!
    gpsbabel -i xcsv,style=nominatim.style -f wpts-test5.csv -o gpx -F wpts-test5.gpx
    

    you will have only the waypoints.

    If you want to have only the route, without the waypoints, you have to tell gpsbabel to explicitly remove them:

    #!
    gpsbabel -i xcsv,style=nominatim.style -f wpts-test5.csv  -x transform,rte=wpt  -x nuketypes,waypoints -o gpx,garminextensions -F wpts-test5.gpx
    

    For further options, read the manual you may find here: https://www.gpsbabel.org/htmldoc-1.4.4/gpsbabel-1.4.4.pdf

    Remark: I initially had the plan to have a comma as separator between lat and lon, but something else (like | e.g.) between the coordinates and the adress. You will notice the tradces of my trial from the lines commented out (#) in nominatim.style . I did not get it to work. gpsbabel xcsv does not seem to allow the change of delimiters between fields. So if we use the comma - as we get from pasting the lat,lon pair form nominatim - as delimiter, we must not use further commas within the address. If we switch to another delimiter, we were required to edit the space between every lat,lon pair, which may easily end up in accidentlial screwing up coordinates. I tried to convince gpsbabel to accept the lat,lon pair as a single field of type LATLON_HUMAN_READABLE , but obviously, this format does not like the comma as lat-lon separator, either. We could add some wrapper script (e.g. perl) around the gpsbabel call. but this adds just another layer of complexity and obfuscates the immediate acces to the rich set of available features in gpsbabel. Well, we could still prepend a transforer scipt (maybe even awk or sed or something like that) to fine tune the formats and still access native gpsbabel options. Anyway, for me, it is fine to live without commas in the Address.

  2. wrosner

    Hang on. Things may get even simpler: We even can forget the style file.

    Since after futile trials we are back to simple basic csv format anyway, we do not even need the extended features of xcsv.

    This simplified command will do the same job:.

    #!
    gpsbabel -i csv -f wpts-test5.csv -x  transform,rte=wpt -o gpx -F wpts-test5.gpx
    

    gives you both waypoints and route. The different -x options apply as above.

  3. wrosner

    Never give up!

    Using this little perl script

    #!/usr/bin/perl
    while(<>) {
      next if /^\s*$/ ;     # skip empty lines
      next if /^\s*\#/ ;    # skip lines with comments 
      /^([+-]?\d*\.\d*)\,([+-]?\d*\.\d*)\s\|\s(.*)$/ ;
      printf "%s\|%s\|%s\n",  $1,  $2,  $3 ;
    }
    

    and this xcsv style

    #!
    FIELD_DELIMITER PIPE
    RECORD_DELIMITER NEWLINE
    IFIELD LAT_DECIMAL,"","%f"
    IFIELD LON_DECIMAL,"","%f"
    IFIELD DESCRIPTION,"","%s"
    

    I can even process input as ugly as

    #!
    
    49.43226,11.09153 | Nürnberg Hermundurenstraße 17
    
    # now we 
       # even may 
                    # play
    
      ### with comments
    49.73233,11.07244 | Forchheim, Hugo-Post-Straße 84
    49.42029,11.89065 | Kümmersbruck Grasiger Weg
    49.15800,11.71157 | Parsberg, Lindlbergstraße
    49.43226,11.09153 | Nürnberg Hermundurenstraße 17
    

    using a comand like this

    #!
    
    ./xsv2csv.pl < wpts-test6.csv | gpsbabel -i xcsv,style=pipquot.style -f - -x  transform,rte=wpt -o gpx -F wpts-test6.gpx
    

    This way we can

    • leave the commas in the LAT,LON coordinate pair untouched
    • use a | aka PIPE as second delimiter between coordinates and description
    • use commas in the description field, which are preserved in the final gpx output
    • use empty lines and comment # in the input file - which may make it easier to play with routes

    unfortunately, it is not that simple any more ....

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.