Step 3 - Performing spatial queries
This is what I think is one of the most powerful features of GPSBabel. You can perform basic spatial queries - including selecting only those waypoints within a polygon, and those waypoints within a certain distance of a line.
I haven't counted how many caches are in my caches-nz file - but there are a lot, and many of them I don't need to have in my GPS and PDA. What I do need are the caches of those places I travel most frequently to, and any routes I may travel along. For me - this includes Christchurch, Nelson, Wellington and Auckland.
Google Earth also comes in very handy at this stage, as we can use it to produce the polygons and lines that we are going to use.
Filtering within a certain distance of a location
This is a great technique for selecting all caches around an area that you are interested in. For example - when I usually travel to Auckland or Wellington, I don't get too far out of the city. I only really need all caches within a certain distance. Using Google Earth I identify the co-ordinates I'd like to use as centre, and with the measuring tool guestimate a suitable radius. I now have all I need to grab the caches around Auckland.
gpsbabel -i gpx -f caches-nz-ni-active.gpx \ -x radius,distance=20K,lat=-36.85,lon=174.8 \ -o gpx -F caches-nz-ni-akl-active.gpx echo "Produced: Caches Active/Unfound (NZ-NI-AKL)"
Filtering with Polygons
[img_assist|nid=149|title=New Zealand Polygons in Google Earth|link=popup|align=right|width=155|height=200]Before we go much further, we will use this technique to be able to separate out North Island and South Island caches.
Using Google Earth I drew a couple of polygons around the North and South islands. Once they appear in My Places, you can right click on them and using "Save As" you can export them as KML (the default is KMZ). I prefer KML for small files as it is easier to hand-craft co-ordinates if you want to tidy them up at all.
Now, GPSBabel can only use 'arc' files for spatial queries and KML isn't an arc file. GPSBabel however is very good at conversions, so it was easy to convert the KML into and arc file.
Say I have created a file 'si-poly.kml' which is a polygon of the South Island in KML format. The following command will convert it to arc which can be used for filtering.
gpsbabel -t -i kml -f si-poly.kml -o arc -F si-poly.txt
This is in fact a very simple file that defines co-ordinates of the polygon surrounding the South Island that I made in Google Earth. Note that the last co-ordinates is exactly the same as the first - this is how the polygon is 'closed'.
-40.25 174.5 -43.5 174.5 -47.5 169 -47.5 166 -45 166 -40.25 172 -40.25 174.5
With this file in hand, it is trival now to take my caches-nz.gpx that contains every cache in NZ, and just select those in the South Island.
gpsbabel -i gpx -f caches-nz-active.gpx -x polygon,file=si-poly.txt \ -o gpx -F caches-nz-si-active.gpx echo "Produced: Caches Active/Unfound (NZ-SI)"
Easy!
Filtering within a certain distance of a line
[img_assist|nid=150|title=Google Earth road line|desc=|link=popup|align=right|width=155|height=200]I have family in Nelson, and it is a great part of the country to cache - so naturally it is like a second home to me. This also means that I travel the road from Christchurch to Nelson reasonably frequently and like doing the odd cache on the way.
Just the same as the polygon, you export it as KML and convert it to an arc file. In this case the file looks fairly similar. I've only shown the first eight lines here. You'll note that I didn't extend the road directly to Nelson or Christchurch as these areas are covered by circluar distance filters. So, I've got an arc file called chc-nsn.txt that looks like the following...
-41.42272 173.03485 -41.49763 172.89277 -41.48574 172.84902 -41.45927 172.83410 -41.55435 172.78633 -41.63121 172.74989 -41.65317 172.65989 -41.70830 172.61716 ...
One relatively simple GPSBabel command and I can select all caches within 4km of the road line.
gpsbabel -i gpx -f caches-nz-si-active.gpx \ -x arc,file=chc-nsn.txt,distance=4K \ -o gpx -F caches-nz-si-chcnsn-active.gpx echo "Produced: Caches Active/Unfound (NZ-SI-CHCNSN)"
Bundling it all up
Finally, amongst all the other files, I want one that contains caches of locations I am likely to be frequently. So, to combine these various spatial queries together...
gpsbabel -i gpx -f caches-nz-si-chc-active.gpx \ -f caches-nz-si-nsn-active.gpx \ -f caches-nz-ni-wlg-active.gpx \ -f caches-nz-ni-akl-active.gpx \ -f caches-nz-si-chcnsn-active.gpx \ -x duplicate,shortname,all \ -o gpx -F caches-nz-popular-active.gpx
And the result is... (I've left Auckland off and just centred on Christchurch, Wellington and Nelson to give you a better idea.
[img_assist|nid=151|title=Resulting GPX at the end of processing|desc=|link=popup|align=left|width=155|height=200]So now what?
Well, you generally don't want to be performing these processes every time you get a new pocket query in your inbox - certainly not by hand. The reason I went through this was to automate and script the process so I've a GPX file ready to load in Google Earth, GPX Sonar, or upload to the GPS.
The next step was to automate the process so it all happens automatically - from the attachments being saved and the pocket query email being deleted, through to regular running of the script to build the GPX during the day so it would have the most recent information available.
