Created
April 13, 2017 15:29
-
-
Save Grezzo/e9c6454d30a51728b2ca8de0beac2c05 to your computer and use it in GitHub Desktop.
Useful for filling D/T Grid. Takes a finds.gpx, and a caches.gpx and spits out a caches.gpx containing only caches needed to get D/T grid
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python3.4 | |
import xml.etree.cElementTree as ET | |
namespaces = { | |
'xmlns': 'http://www.topografix.com/GPX/1/0', | |
'groundspeak': 'http://www.groundspeak.com/cache/1/0/1' | |
} | |
def d_t_grid(gpx): | |
'''Accepts a gps file path, then creates an array of arrays and populate it with | |
number of caches for each difficulty and terrain rating. Difficulty is the first | |
index, terrain is the second. | |
Ratings are doubled to make them integers so they can be used for array index | |
(probably should use dicts instead) | |
''' | |
grid = [[0 for x in range(9)] for x in range (9)] | |
tree = ET.parse(gpx) | |
for wpt in tree.iterfind('xmlns:wpt', namespaces=namespaces): | |
difficulty = wpt.find('groundspeak:cache/groundspeak:difficulty', namespaces=namespaces).text | |
terrain = wpt.find('groundspeak:cache/groundspeak:terrain', namespaces=namespaces).text | |
d_int = int(float(difficulty)*2) - 2 | |
t_int = int(float(terrain)*2) - 2 | |
grid[d_int][t_int] += 1 | |
return grid | |
def filter_by_grid(gpx, grid, output): | |
ET.register_namespace('', 'http://www.topografix.com/GPX/1/0') | |
ET.register_namespace('groundspeak', 'http://www.groundspeak.com/cache/1/0/1') | |
tree = ET.parse(gpx) | |
for wpt in tree.findall('xmlns:wpt', namespaces=namespaces): | |
difficulty = wpt.find('groundspeak:cache/groundspeak:difficulty', namespaces=namespaces).text | |
terrain = wpt.find('groundspeak:cache/groundspeak:terrain', namespaces=namespaces).text | |
d_int = int(float(difficulty)*2) - 2 | |
t_int = int(float(terrain)*2) - 2 | |
if grid[d_int][t_int] != 0: | |
tree.getroot().remove(wpt) | |
else: | |
#Add namespace attribute to groundspeak:cache because otherwise L4C doesn't get the name properly | |
cache = wpt.find('groundspeak:cache', namespaces=namespaces) | |
cache.set('xmlns:groundspeak', 'http://www.groundspeak.com/cache/1/0/1') | |
tree.write(output, encoding='UTF-8', xml_declaration=True) | |
def main(): | |
import argparse | |
parser = argparse.ArgumentParser(description='Filter Geocaches by D/T Grid') | |
parser.add_argument('finds', help='a gpx file containing all finds') | |
parser.add_argument('caches', help='a gpx file containing unfound caches') | |
parser.add_argument('output', help='filename to use for new GPX file') | |
args = parser.parse_args() | |
filter_by_grid(args.caches, d_t_grid(args.finds), args.output) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment