first iteration

  • [x] read in the first line of a file
  • [x] if it matches the needed criteria, count it
  • [x] if not, pass
  • [x] also, write EACH line to another file
  • [x] once the count reaches a number, add the necessary tags and close both files
  • [x] end the program

second iteration

  • [] create functions for everything AND TEST THEM!
  • [] read in the first line of a file
  • [] if it matches the needed criteria, count it
  • [] if not, pass
  • [] also, write EACH line to another file
  • [] once the count reaches 300, close the file and create a new file with the necessary GPX tags
  • [] continue until the EOF is reached
  • [] close it out with the correct tags
  • [] create bounds for the file (first and last gps waypoints)

In [115]:
# variables
filename = 'CDT_2017_track.gpx'
wp_counter = 0
wp_counter_limit = 1000
file_counter = 0
f = open(filename,'r')
o = open('output_' + str(file_counter) + '.gpx', 'w+')
comparison_text = '</trkpt>\n'
previous_line = 'stuff'

In [116]:
# insert EOF information for a GPX file
# create a strung variable that has everything needed and return it
# it needs to have this at the end of each file:
# </trkpt>\n</trkseg>\n</trk>\n</gpx>
def file_tags(flag):
    if flag == 'BOF':
        return '<?xml version="1.0" ?>\n<gpx creator="GaiaGPS" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">\n<trk>\n<name>CDT Upload Track ' + str(file_counter) + '</name>\n<desc>\n</desc>\n<trkseg>\n'
    elif flag == 'EOF':
        return '</trkseg>\n</trk>\n</gpx>\n'
    else:
        return 'fail'

In [117]:
# method for reading the next line and compare it to some text
def line_compare(f_next_line):
    if f_next_line == comparison_text:
        return 'true'
    elif f_next_line == '':
        return 'EOF'
    else:
        return 'false'

In [118]:
# function to increment to a new file and close the previous file
# get file name and save it as a variable
# close the file
# increment the file counter
# open a new file
# return the new file object

def close_out_file(out_file):
    out_file.write(file_tags('EOF'))
    out_file.close()
    return

In [119]:
def create_and_increment_file():
    # create and open the new output file
    global file_counter
    file_counter += 1
    output_file_name = 'output_' + str(file_counter) + '.gpx'
    out_file = open(output_file_name, 'w+')
    out_file.write(file_tags('BOF'))

    return out_file

In [120]:
def previous_line_test(previous_line):
    if previous_line != '</gpx>\n':
        o.write('</gpx>')
    return

In [121]:
# while the waypoint counter is less than the waypoint limit, do the following
# get the next line
# write a new line to the output file
# save the previous line variable for testing at the end of the program
# compare the new line to the comparison text
# evaulate the results: true = increment wp_counter, EOL breaks out of while loop and ends program, false does nothing
# check to see if the wp_limit has been reached
# if reached, close old, open new file AND reset the wp_counter to 0

while wp_counter < wp_counter_limit:
    f_string = f.readline()
    o.write(f_string)
    previous_line = f_string
    compare_result = line_compare(f_string)

    if compare_result == 'true':
        wp_counter += 1
    elif compare_result == 'EOF':
        break
    
    if wp_counter == (wp_counter_limit-1):
        close_out_file(o)
        o = create_and_increment_file()
        wp_counter = 0

In [122]:
o.close()
f.close()