def join_gpx(first_track_start, second_track_start): first_track_start = minutes_to_military(first_track_start) second_track_start = minutes_to_military(second_track_start) directory_name = 'tracks/' saving_name = 'save/' saving_directory = os.path.join(directory_name, saving_name) #print "####", first_track_start, second_track_start files =[] for f in os.listdir(saving_directory): files.append(f) files.sort() segments_to_delete = [] for f_prev, f, f_next in neighborhood(files): filename = os.path.join(saving_directory, f) file = open(filename, 'rb') gpx_xml = file.read() file.close() gpx = gpxpy.parse(gpx_xml) if f_next is not None: filename_next = os.path.join(saving_directory, f_next) file_next = open(filename_next, 'rb') gpx_xml_next = file_next.read() file_next.close() gpx_next = gpxpy.parse(gpx_xml_next) for track, track_next in zip(gpx.tracks, gpx_next.tracks): for segment, segment_next in zip(track.segments, track_next.segments): if segment.points[0].time.strftime("%H%M") == first_track_start and \ segment_next.points[0].time.strftime("%H%M") == second_track_start: segment.join(segment_next) #segments_to_delete.append(filename) segments_to_delete.append(filename_next) gpx = gpxpy.gpx.GPX() # Create first track in our GPX: gpx_track = gpxpy.gpx.GPXTrack() gpx.tracks.append(gpx_track) # Create first segment in our GPX track: gpx_track.segments.append(segment) open(filename, 'w').close() fo = open(filename, "wb") fo.write(gpx.to_xml()) fo.close() for f in segments_to_delete: os.remove(f)
def get_parsed(self): if self.save_memory: with open(self.path, "r") as gpx_file: return gpxpy.parse(gpx_file) else: if not hasattr(self, "_parsed_track"): with open(self.path, "r") as gpx_file: try: self._parsed_track = gpxpy.parse(gpx_file) except: # We can do a bare except, as we're re-raising print("Errored path: %s" % self.path) raise return self._parsed_track
def get_lat_lon_time(gpx_file): ''' Read location and time stamps from a track in a GPX file. Returns a list of tuples (time, lat, lon). GPX stores time in UTC, assume your camera used the local timezone and convert accordingly. ''' with open(gpx_file, 'r') as f: gpx = gpxpy.parse(f) points = [] if len(gpx.tracks)>0: for track in gpx.tracks: for segment in track.segments: for point in segment.points: points.append( (utc_to_localtime(point.time), point.latitude, point.longitude, point.elevation) ) if len(gpx.waypoints) > 0: for point in gpx.waypoints: points.append( (utc_to_localtime(point.time), point.latitude, point.longitude, point.elevation) ) # sort by time just in case points.sort() return points
def load(filename, delta = None): """ Return a list of (lat, lon) pairs retrieved from gpx file Args: filename: path of gpx file delta: If provided, simplify path to max deviation of delta meters. 100 meters will generally simplify a RideWithGPS route radically. Returns: A list of (lattitude, longitude) pairs. """ contents = open(filename, 'r', encoding="utf-8", errors="replace") gpx = gpxpy.parse(contents) if delta: gpx.simplify(delta) points =[ ] # to hold points (latitude, longitude) times =[ ] # to hold times arrays =[ ] # to hold both of the above arrays for track in gpx.tracks: for segment in track.segments: for point in segment.points: points.append( [point.latitude, point.longitude] ) times.append(arrow.get(point.time).datetime) # use arrow to interperet the time, then convert to datetime object arrays.append(points) arrays.append(times) return arrays
def handle_uploaded_file(gpx_file): content = gpx_file.read().decode("utf-8") gpx = gpxpy.parse(content) for track in gpx.tracks: for segment in track.segments: for point in segment.points: create_point(point)
def get_gps_data(): gpx_file = open('/Users/web/Sites/orgnet/data/random.gpx', 'r') gpsDict = {} gpsList = [] gpx = gpxpy.parse(gpx_file) counter = 0 for track in gpx.tracks: for segment in track.segments: for point in segment.points: gpsDict.update({ counter : { 'lat': point.latitude, 'lon':point.longitude }}) counter+=1 # gpsList.append((point.latitude, point.longitude)) # print 'Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation) for waypoint in gpx.waypoints: print 'waypoint {0} -> ({1},{2})'.format(waypoint.name, waypoint.latitude, waypoint.longitude) for route in gpx.routes: print 'Route:' for point in route: print 'Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation) #print 'GPX:', gpx.to_xml() jsonData = simplejson.dumps(gpsDict) return jsonData
def open_gpx(self,fn): """Opens a gpx file and stores it""" with open(fn, 'r') as gpx_file: self._gpx_track = gpxpy.parse(gpx_file) self.track_point_count = self.get_track_point_count() self.gpx_filename = fn print 'Opened', fn
def __init__(self, data): self.gpx_file = data try: self.gpx = gpxpy.parse(data) except gpxpy.gpx.GPXXMLSyntaxException: raise BackendException() self.start_time, self.end_time = self.gpx.get_time_bounds()
def readGpxTracksFromFile(inputPath): if verbose: print "Reading GPX file %s" % inputPath inputFileName = os.path.splitext(os.path.basename(inputPath))[0] gpxFile = open(inputPath, 'r') gpx = gpxpy.parse(gpxFile) return inputFileName, gpx.tracks
def clean_pix(filepath, bounds=None, plot_timeseries=False): ''' Clean the gpx data from the pixhawk for the trials ''' pix_df = gpxpy.parse(open(filepath[0])) track = pix_df.tracks[0] segment = track.segments[0] data = [] segment_length = segment.length_3d() for i, point in enumerate(segment.points): data.append([point.longitude, point.latitude, point.elevation, point.time, segment.get_speed(i)]) p_df = pd.DataFrame(data, columns=['Longitude', 'Latitude', 'Altitude', 'Time', 'Speed']) p_df.loc[:,'Year'] = p_df.apply(lambda x : int(x['Time'].year),axis=1) p_df.loc[:,'Month'] = p_df.apply(lambda x : int(x['Time'].month),axis=1) p_df.loc[:,'Day'] = p_df.apply(lambda x : int(x['Time'].day),axis=1) p_df.loc[:,'Hour'] = p_df.apply(lambda x : float(x['Time'].hour+4.),axis=1) p_df.loc[:,'Minute'] = p_df.apply(lambda x : float(x['Time'].minute),axis=1) p_df.loc[:,'Second'] = p_df.apply(lambda x : float(x['Time'].second),axis=1) p_df = sc.global_time_column(p_df) if bounds is not None: p_df = p_df.drop(p_df[p_df.Julian_Date <= (bounds[0])].index) p_df = p_df.drop(p_df[p_df.Julian_Date >= (bounds[1])].index) p_df = p_df.dropna() if plot_timeseries == True: plt.plot(p_df['Latitude'], p_df['Longitude'], 'ro', alpha=0.4) print p_df.head(2) return p_df
def remove_gpx_file(date, start_time): start_time = minutes_to_military(start_time) directory_name = 'tracks/' saving_name = 'save/' saving_directory = os.path.join(directory_name, saving_name) files =[] for f in os.listdir(saving_directory): files.append(f) files.sort() for f in files: filename = os.path.join(saving_directory, f) file = open(filename, 'rb') gpx_xml = file.read() file.close() gpx = gpxpy.parse(gpx_xml) for track in gpx.tracks: for segment in track.segments: if segment.points[0].time.strftime("%H%M") == start_time and \ segment.points[0].time.strftime("%Y_%m_%d") == date: os.remove(filename)
def convert(self): if (self.filename == '') or (self.path == ''): self.ids['message1'].text = 'Load a GPX file first!!' return infile = os.path.join(self.path, self.filename[0]) gpx_file = open(infile, 'r') outfile = os.path.join(self.path, outputfilename) try: ozi_file = open(outfile, 'w') except Exception as e: # Catch any error, e.g. non-writable directory, etc. self.ids['message2'].text = str(e).encode('utf-8') self.ids['message3'].text = 'If you got a permissions error.\nTry moving the file a folder that\'s writeable.\nOn Android try: /storage/emulated/0/Download' return gpx = gpxpy.parse(gpx_file) #Next 4 lines are defaults. WGS 84 is default datum of GPX file so it's hard coded. #http://www.oziexplorer3.com/eng/help/fileformats.html - describes ozi wpt file format ozi_file.write('OziExplorer Waypoint File Version 1.1\n') ozi_file.write('OziExplorer Waypoint File Version 1.1\n') ozi_file.write('WGS 84\n') ozi_file.write('Reserved - future use\n') ozi_file.write('Reserved - future use\n') count = 0 for waypoint in gpx.waypoints: count += 1 ozi_file.write('{0},{1},{2},{3},{4},0,1,3,0,65535,{1},0,0,0,-777,6,0,17\n'.\ format(count, waypoint.name, waypoint.latitude, waypoint.longitude,waypoint.time)) self.ids['message1'].text = '{0} waypoints exported to:\n{1}'.format(count,os.path.join(self.path, outputfilename)) return
def GPXFile_post_save(sender, instance, created, **kwargs): if not created: instance.gpxtrack_set.all().delete() gpx = gpxpy.parse(instance.file) for track in gpx.tracks: new_track = models.GPXTrack() new_track.user = instance.user new_track.file = instance new_track.name = track.name center = track.get_center() new_track.center = Point(center.longitude, center.latitude, center.elevation) time_bounds = track.get_time_bounds() g = geocoders.GoogleV3() timezone = g.timezone((center.latitude, center.longitude)) new_track.date_start = time_bounds.start_time.replace(tzinfo=pytz.UTC) new_track.date_end = time_bounds.end_time.replace(tzinfo=pytz.UTC) new_track.date_start_at_location = new_track.date_start \ .astimezone(timezone).isoformat() new_track.date_end_at_location = new_track.date_end \ .astimezone(timezone).isoformat() segments = [] for segment in track.segments: points = [Point(e.longitude, e.latitude).coords for e in segment.points] segments.append(LineString(points)) new_track.multiline = MultiLineString(segments) new_track.save()
def play_gpx(client, gpx_fh, speed=1.0): gpx = gpxpy.parse(gpx_fh) track_seg = gpx.tracks[0].segments[0] prev_point = None points = list(track_seg.points) points.sort(key=lambda x: x.time) # do the first update client.update(points[0].latitude, points[0].longitude) start_time = datetime.datetime.now(pytz.utc) first_point_time = points[0].time offset = start_time - first_point_time for point in points[1:]: # wait until we're ready delta = ((point.time + offset) - datetime.datetime.now(pytz.utc)).total_seconds() / speed if delta > 0: print 'napping for %.2f second(s)...' % delta time.sleep(delta) elif delta < 0: print 'skipping point, we are %0.2f second(s) late...' % delta continue try: client.update(point.latitude, point.longitude, point.elevation) print point except requests.exceptions.ConnectionError, e: print 'Failure posting update, %r' % e
def main(args): gpx_file = open(args[0], 'r') gpx = gpxpy.parse(gpx_file) mod_summarize_tracks(gpx) #summarize_tracks(gpx) for t in gpx.tracks: save_speed(t)
def waypoint_load (request, pk): o = get_object_or_404(Object, pk=pk) if request.method == 'POST': filename = '/Users/andrey/Documents/workspace/Catalog/data/' + str(request.FILES['file']) # загружаем файл with open(filename, 'wb+') as file: for chunk in request.FILES['file'].chunks(): file.write(chunk) # загружаем точки из файла file=open(filename) gpx = gpxpy.parse(file) if gpx.waypoints: for waypoint in gpx.waypoints: if waypoint.name: w=Waypoint() w.name=waypoint.name w.lat=waypoint.latitude w.lon=waypoint.longitude w.owner = request.user w.published_date = timezone.now() w.object=o w.save() else: print("Name: unknown") file.close() os.remove(filename) return redirect('object.views.object_view', pk=o.pk)
def get_tracks(fname): """read gpx-file and returns tracks as a pandas DataFrame""" gpx_in = open(fname, 'r') gpx = gpxpy.parse(gpx_in) tracks, segments = [], [] name, longitude, latitude, altitude, time = [], [], [], [], [] for track in gpx.tracks: for nseg, segment in enumerate(track.segments): for point in segment.points: tracks.append(track.name) segments.append(nseg) name.append(point.name) longitude.append(point.longitude) latitude.append(point.latitude) altitude.append(point.elevation) time.append(point.time) df = pd.DataFrame(data=time, columns=['datetime']) df['track'] = tracks df['segment'] = segments df['longitude'] = longitude df['latitude'] = latitude df['altitude'] = altitude return df
def extract_gpxes(cache_path): if cache_path.lower().endswith('.gpx'): print('Parsing', cache_path) with open(cache_path) as f: yield gpxpy.parse(f) elif cache_path.lower().endswith('.zip'): print('Opening ZIP', cache_path) z = ZipFile(cache_path) for file_info in z.filelist: if file_info.filename.lower().endswith('.gpx'): print('Parsing', cache_path + '/' + file_info.filename) with z.open(file_info) as f: xml = f.read().decode() yield gpxpy.parse(xml) else: raise Exception('Unsupported file type ' + cache_path)
def save_gpx(user, content): logging.debug("saving gpx") parsed = gpxpy.parse(content) started, finished = parsed.get_time_bounds() if gps_workout.exists(user, started, finished): raise WorkoutAlreadyExists() workout = models.Workout.objects.create(user=user, started=started, finished=finished, activity_type=activity.TYPE) gpx = models.Gpx.objects.create(workout=workout, name=parsed.tracks[0].type, distance=int(parsed.length_2d())) for track in parsed.tracks: for segment in track.segments: for point in segment.points: gpx.gpxtrackpoint_set.create(lat=point.latitude, lon=point.longitude, hr=point.extensions.get('hr', None), cad=point.extensions.get('cad', None), time=point.time) return workout.id
def simplify_gpx(gpx_file_name, alpha_error, out_dir, select_gpsbabel=False): assert gpx_file_name.endswith(".gpx") (in_dir, file_name) = os.path.split(gpx_file_name) out_file_name = "%s/%s_simplified_d_%i.gpx" % (out_dir, file_name[:-4], alpha_error) if select_gpsbabel: selection_method = False else: selection_method = is_gpxpy_installed() if selection_method: # Use gpxpy package. import gpxpy with open(gpx_file_name, "r") as gpx_file: gpx = gpxpy.parse(gpx_file) gpx.simplify(alpha_error) with open(out_file_name, "w") as out_file: print("writing file: %s" % out_file_name) out_file.write(gpx.to_xml()) else: # Use gpsbabel on the commandline. alpha_in_kilometers = alpha_error / 1000 filter_option = "simplify,error=%sk" % alpha_in_kilometers command_string = "gpsbabel -i gpx -f %s -x %s -o gpx -F %s" % (gpx_file_name, filter_option, out_file_name) print(command_string) os.system(command_string) return
def gpx_for_path(self, path): """ Given a full path name, attempt to open the file and create a GPX object from it. Return None if the path is a directory or not a gpx file, or a gpx file that isn't well formed. @param path: full pathname to a gpx file @type path: str @return: a GPX object representing the gpx file @rtype: GPX """ if os.path.isdir(path): return None try: gpx_xml = open(path, 'r').read() except OSError: self.logger.debug('Unable to process gpx file: %s' % path) return None try: gpx = gpxpy.parse(gpx_xml) except gpxpy.GPXException as ex: self.logger.debug('Unable to parse GPX file: %s. Leaving out of splices. %s' % (path, ex)) return None return gpx
def GPXparse(InputFileName, OutputFileName): InFile = open(InputFileName, 'r') fp_temp = open(InputFileName, 'r') m = md5.new(fp_temp.read()).hexdigest() fp_temp.close() global md5list if m in md5list: return 0 md5list.add(m) gpx = gpxpy.parse(InFile) try: OutputFileNameTrack = OutputFileName OutFile = open(OutputFileNameTrack, 'a') except IOError: print 'cannot creat ' + OutputFileNameTrack exit(0) for track in gpx.tracks: print_content = False for segment in track.segments: for wpt in segment.points: try: waypoint = "%d %s," % ( datetime2seconds(wpt.time), urban_rural(wpt.latitude, wpt.longitude)) OutFile.write(waypoint) print_content = True except: continue if print_content: OutFile.write("\n") OutFile.close() InFile.close() print 'Successfuly parsed file ' + InputFileName return 1
def read_day_tracks(day): day = day.replace("_","-") files = [] points = [] directory_name = 'tracks/' saving_name = 'save/' saving_directory = os.path.join(directory_name, saving_name) for f in os.listdir(saving_directory): if f[:10]==day: files.append(f) files.sort() for f in files: file = open(os.path.join(saving_directory, f), 'rb') gpx_xml = file.read() file.close() gpx = gpxpy.parse(gpx_xml) for track in gpx.tracks: for segment in track.segments: points += [segment.points] return points
def DrawGpx(level, gpxs, outfn, box=None): tracks=[] bboxes=[] color=colorgen() minx,maxx,miny,maxy=(None,None,None,None) for gpx in gpxs: gpx_file = open(gpx, 'r') gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: gpxpoints=[] for segment in track.segments: for point in segment.points: x,y=ll2p(level, point.longitude, point.latitude) gpxpoints.append((x,y)) if minx==None or x<minx: minx=x if maxx==None or x>maxx: maxx=x if miny==None or y<miny: miny=y if maxy==None or y>maxy: maxy=y tracks.append(gpxpoints) if box==None: sx,sy,ex,ey=enlarge((int(minx),int(miny), int(maxx), int(maxy)),1.5) else: sx,sy=utm2p(level, box[0], box[1]) ex,ey=utm2p(level, box[2], box[3]) bg=GetPicture(level,[sx, sy, ex, ey]) bg.save(outfn) draw=ImageDraw.Draw(bg) for track in tracks: draw.line([(int(x-sx),int(y-sy)) for x,y in track] ,fill=color.next(),width=3) bg.save(outfn)
def from_gpx(file_path): """ Creates a Track from a GPX file. No preprocessing is done. Arguments: file_path (str): file path and name to the GPX file Return: :obj:`list` of :obj:`Track` """ gpx = gpxpy.parse(open(file_path, 'r')) file_name = basename(file_path) tracks = [] for i, track in enumerate(gpx.tracks): segments = [] for segment in track.segments: segments.append(Segment.from_gpx(segment)) if len(gpx.tracks) > 1: name = file_name + "_" + str(i) else: name = file_name tracks.append(Track(name, segments)) return tracks
def process_tracks(gpx_path, output_dir, overwrite=False): f = open(gpx_path, 'r') gpx = gpxpy.parse(f) n_processed = 0 n_written = 0 for t in gpx.tracks: if len(t.segments) > 0 and len(t.segments[0].points) > 0: p = t.segments[0].points[0] name = '%s-%s' % (p.time.strftime('%Y-%m-%d'), re.sub(r'\W+','-', t.name)) gpx_filename = '%s.gpx' % name track_path = path.join(output_dir, gpx_filename) if overwrite or not path.exists(track_path): with open(track_path, 'w') as track_file: track_file.write(gpxpy.gpx.GPX(tracks=[t]).to_xml()) with open(path.join(output_dir, '%s.json' % name), 'w') as meta_file: meta_file.write(json.dumps({ 'name': t.name, 'time': t.get_time_bounds(), 'duration': t.get_duration(), 'distance': t.length_2d(), 'location': geocode(t) }, cls=DateTimeJSONEncoder, indent=True)) n_written = n_written + 1 n_processed = n_processed + 1 return (n_processed, n_written)
def parse_data_from_dir(data_dir): data_files = [join(data_dir, f) for f in listdir(data_dir) if isfile(join(data_dir, f)) and re.match("storyline.*.gpx", f)] # Format lat, long, start_time, end_time data = [] latlng_data = [] arrival_time_string = "" print "Parsing data..." for data_file in data_files: with open(data_file, 'r') as gpx_file: gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: if len(track.segments) > 0 and len(track.segments[0].points) > 0: departure_point = track.segments[0].points[0] location = { 'lat': departure_point.latitude, 'lng': departure_point.longitude, 'arrival_time': arrival_time_string, 'departure_time': departure_point.time.isoformat(), } data.append(location) latlng_data.append((departure_point.latitude,departure_point.longitude)) arrival_time_string = track.segments[-1].points[-1].time.isoformat() # Cluster lat, lng and add that cluster info to the data dict print "Clustering..." centroids, labels, counts = cluster_location_data(latlng_data) for i in range(len(data)): if counts[labels[i]] > 4: data[i]['cluster'] = labels[i] else: data[i]['cluster'] = -1 return data, centroids
def gpxclean(input, **options): gpxutils.applyDefaults(options) new_segment = None current_track_name = None with input.open(encoding='utf-8') as gpx_file: gpx = gpxpy.parse(gpx_file) # Extract tracks for track in gpx.tracks: for segment in track.segments: # Create new segment, and write out the current one new_segment = writeAndCreateNewFile(new_segment, input.stem, current_track_name, **options) current_track_name = track.name previous_point = None for point in segment.points: if previous_point: if point.distance_2d(previous_point) > options['split']: # Start new segment new_segment = writeAndCreateNewFile(new_segment, input.stem, current_track_name, **options) previous_point = point new_segment.points.append(point) # Write final segment new_segment = writeAndCreateNewFile(new_segment, input.stem, current_track_name, **options) # Extract waypoints for waypoint in gpx.waypoints: writeWaypoint(waypoint, input.stem, **options)
def Calibrate(startvid,gpxfname,videoname): gpx_file = open(gpxfname, 'r') gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: for segment in track.segments: for point in segment.points: p5 = point # One Point print print " GPS file: ",gpxfname print " Camera file: ",videoname print " GPS start time: ",str(p5.time) print "Camera start time: ",startvid print print "Camera start time needs to match GPS start time" print "Please watch",videoname,"and note when you start moving" print target = raw_input("Hit enter to start video with vlc (Quit vlc once you know the time)") runVid = "vlc " + videoname os.system(runVid) print print target = raw_input("Enter time movement started in mm:ss format: ") t1 = float(target[0:2]) t2 = float(target[3:5]) tSeconds = t1 * 60 + t2 print tSeconds startvid = startvid + datetime.timedelta(0,tSeconds) print print "Update info" print print " GPS file: ",gpxfname print " Camera file: ",videoname print " GPS start time: ",str(p5.time) print "Camera start time: ",startvid print if (startvid > p5.time): calibrate = str(startvid - p5.time) print "calibration offset: -",calibrate ftr = [3600,60,1] c2 = -1 * (sum([a*b for a,b in zip(ftr, map(int,calibrate.split(':')))])) else: calibrate = str(p5.time - startvid) print "calibration offset: ",calibrate ftr = [3600,60,1] c2 = sum([a*b for a,b in zip(ftr, map(int,calibrate.split(':')))]) print "Calibration offset in seconds: ",c2 target = raw_input("Enter different offset (Hit enter to use calculated number) ") if (target != ""): c2 = int(target) print c2 break gpx_file.close() return c2
def test_simple_parse_function_invalid_xml(self): try: mod_gpxpy.parse('<gpx></gpx', parser=self.get_parser_type()) self.fail() except mod_gpx.GPXException as e: self.assertTrue(('unclosed token: line 1, column 5' in str(e)) or ('expected \'>\'' in str(e))) self.assertTrue(isinstance(e, mod_gpx.GPXXMLSyntaxException)) self.assertTrue(e.__cause__) try: # more checks if lxml: import lxml.etree as mod_etree import xml.parsers.expat as mod_expat self.assertTrue(isinstance(e.__cause__, mod_etree.XMLSyntaxError) or isinstance(e.__cause__, mod_expat.ExpatError)) except: pass
def extract_segments(gpx_file): gpx_file = open(gpx_file, 'r') gpx = gpxpy.parse(gpx_file) lat = [] lon = [] for track in gpx.tracks: for segment in track.segments: for point in segment.points: lat.append(point.latitude) lon.append(point.longitude) segments = list(zip(lat, lon)) return segments
def load_gpx(self): gpx_file = open(self.path_file, 'r') gpx = gpxpy.parse(gpx_file) if len(gpx.tracks) == 0: raise RuntimeError('GPX file does not contain a track') points = [] track = gpx.tracks[0] for segment in track.segments: for point in segment.points: points.append({"lat": point.latitude, "lng": point.longitude, "alt": point.elevation}) return points
def load_gpx(self, file_name): try: self.file_names = [os.path.basename(file_name)] # Handle empty gpx files # (for example, treadmill runs pulled via garmin-connect-export) if os.path.getsize(file_name) == 0: raise TrackLoadError("Empty GPX file") with open(file_name, "r") as file: self._load_gpx_data(mod_gpxpy.parse(file)) except: print( f"Something went wrong when loading GPX. for file {self.file_names[0]}, we just ignore this file and continue" ) pass
def parse_gpx(track): try: gpx = gpxpy.parse(track) multiline = [] if gpx.tracks: multiline += parse_tracks(gpx.tracks) if gpx.routes: multiline += parse_routes(gpx.routes) return MultiLineString(multiline) except gpxpy.gpx.GPXException as e: logger.error("Valid GPX file: %s" % e) raise ValidationError(u"Vadný GPX soubor: %s" % e)
def gpx_to_df(folder): list_of_files = os.listdir(folder) dataframe_list = [] for file in list_of_files: gpx_file = gpxpy.parse(open('{}/{}'.format(folder, file))) if len(gpx_file.tracks) != 0: segment = gpx_file.tracks[0].segments[0] df = pd.DataFrame([ {'time': p.time, 'lat': p.latitude, 'long': p.longitude, 'ele': p.elevation} for p in segment.points]) dataframe_list.append(df) return dataframe_list
def run(gpx_files: List[str], miles: bool, seconds: bool, only_track: bool) -> None: if not gpx_files: print('No GPX files given') mod_sys.exit(1) for gpx_file in gpx_files: try: gpx = mod_gpxpy.parse(open(gpx_file)) print_gpx_info(gpx, gpx_file, miles, seconds, only_track) except Exception as e: mod_logging.exception(e) print('Error processing %s' % gpx_file) mod_sys.exit(1)
def read_gps(gpx_path, filter=""): """ :param gpx_path: :param filter: :return: """ # Import Packages import glob import gpxpy import os import pandas as pd # Run gpx_files = glob.glob(os.path.join(gpx_path, filter + "*.gpx")) run_data = [] for file_idx, gpx_file in enumerate(gpx_files): gpx = gpxpy.parse(open(gpx_file, 'r')) # Loop through tracks for track_idx, track in enumerate(gpx.tracks): track_name = track.name track_time = track.get_time_bounds().start_time track_length = track.length_3d() track_duration = track.get_duration() track_speed = track.get_moving_data().max_speed for seg_idx, segment in enumerate(track.segments): segment_length = segment.length_3d() for point_idx, point in enumerate(segment.points): run_data.append([ file_idx, os.path.basename(gpx_file), track_idx, track_name, track_time, track_length, track_duration, track_speed, seg_idx, segment_length, point.time, point.latitude, point.longitude, point.elevation, segment.get_speed(point_idx) ]) df = pd.DataFrame(run_data, columns=[ 'File_Index', 'File_Name', 'Index', 'Name', 'Time', 'Length', 'Duration', 'Max_Speed', 'Segment_Index', 'Segment_Length', 'Point_Time', 'Point_Latitude', 'Point_Longitude', 'Point_Elevation', 'Point_Speed' ]) return df
def gpx_to_dataframe(filename): with open(filename, "r") as f: gpx = gpxpy.parse(f) data = [] for track in gpx.tracks: for segment in track.segments: for i, point in enumerate(segment.points): data.append([point.time, point.latitude, point.longitude]) df = pd.DataFrame(data, columns=["time", "latitude", "longitude"]) df = clean(df) return df
def main() -> None: parser = argparse.ArgumentParser(description='Clean GPX tracks') parser.add_argument('-e', '--extensions', action='store_true', help='Remove extensions') parser.add_argument('-t', '--time', action='store_true', help='Remove time') parser.add_argument('-x', '--elevations', action='store_true', help='Remove extensions') parser.add_argument('-r', '--routes', action='store_true', help='Remove routes') parser.add_argument('-tr', '--tracks', action='store_true', help='Remove tracks') parser.add_argument('-a', '--author', action='store_true', help='Remove author data') parser.add_argument('-w', '--waypoints', action='store_true', help='Waypoints') parser.add_argument('-o', '--output', metavar='F', type=str, default='clean.gpx', help='Output GPX file') args, gpx_files = parser.parse_known_args() extensions: bool = args.extensions time: bool = args.time elevations: bool = args.elevations routes: bool = args.routes tracks: bool = args.tracks author: bool = args.author waypoints: bool = args.waypoints output = args.output for gpx_file in gpx_files: with open(gpx_file) as f: g = gpx_parser.parse(f) if extensions: common.clean_extensions(g) if time: g.time = None g.remove_time() if elevations: g.remove_elevation() if routes: g.routes = [] if tracks: g.tracks = [] if author: g.author_name = None g.author_email = None g.author_link = None g.author_link_text = None g.author_link_type = None if waypoints: g.waypoints = [] if not output: output = common.prefix_filename(gpx_file, "clean_") with open(output, "w") as f: f.write(g.to_xml())
def read_gpx(path): from subprocess import Popen, PIPE if not os.path.exists(path): raise FileNotFoundError(path) p = Popen(['gpxinfo', path], stdin=PIPE, stdout=PIPE, stderr=PIPE) output, err = p.communicate() dic = {} for line in str(output).split('\\n'): if ': ' not in line: continue dic[line.split(': ')[0].strip()] = line.split(': ')[1].strip() gpxf = open(path, 'r') try: gpx = gpxpy.parse(gpxf) firstp = gpx.tracks[0].segments[0].points[0] origin = os.environ['START_POINT'] # for example, P7 dest = "%f,%f" % (firstp.latitude, firstp.longitude) distance, duration, dest_addr = callGoogleMapsApi( origin, dest, os.environ['MY_GCP_API_KEY']) dic['p7_distance'] = distance dic['p7_duration'] = duration dic['gmap_dest_address'] = dest_addr slope_mean, slope_std = getSlopeStats(gpx) dic['up_slope_mean'] = slope_mean dic['up_slope_std'] = slope_std dic['num_tracks'] = len(gpx.tracks) countp = 0 for track in gpx.tracks: for seg in track.segments: countp = countp + len(seg.points) dic['num_points_0'] = len(gpx.tracks[0].segments[0].points) dic['num_points_total'] = countp return dic except: return dic
def parse_trackfile(self): """ Parse GPX file, create Trackpoints for all trackpoints found in it, set start and end times and finally generate track segment objects related to this Trackfile. """ with transaction.atomic(): with self.get_file_handle() as f: gpx = gpxpy.parse(f) points = parse_gpxfile(gpx) if points is None: return None save_trackpoints(points, self) self.set_trackpoint_fields() self.generate_tracksegments()
def get_coordinates(filename): # utvonal koordinatak mentese gpx_file = open(filename, 'r') gpx = gpxpy.parse(gpx_file) x_geo = [] y_geo = [] for track in gpx.tracks: for segment in track.segments: for point in segment.points: # print('Point at ({0},{1})'.format(point.latitude, point.longitude)) x_geo.append(point.longitude) y_geo.append(point.latitude) return (x_geo, y_geo)
def all_traces(root): """ Keeps retrieving gps traces until all have been exhausted. :param root: :return: """ for file_name in only_gpx(root): with open(file_name, 'r') as gpx_file: gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: traj = [] for segment in track.segments: traj.extend([(point.latitude, point.longitude, point.time) for point in segment.points]) yield traj
def get_gps_list(name): gpxf = open(name, 'r') gpx = gpxpy.parse(gpxf) gps_list = [] for track in gpx.tracks: for segment in track.segments: for point in segment.points: d = point.time p = gps_point(D2R(point.latitude), \ D2R(point.longitude), \ point.elevation, \ time.mktime(d.timetuple()) + \ 1e-6 * d.microsecond, point.speed) gps_list.append(p) return gps_list
def parsegpx(f): points2 = [] with open("Pollution_walk.gpx", 'r') as gpxfile: # print f gpx = gpxpy.parse(gpxfile) for track in gpx.tracks: for segment in track.segments: for point in segment.points: dict = {'Timestamp' : point.time, 'Latitude' : point.latitude, 'Longitude' : point.longitude, 'Elevation' : point.elevation } points2.append(dict) return points2
def _extract_locations(path: Path) -> Iterator[Location]: with path.open("r") as gf: gpx_obj = gpxpy.parse(gf) for track in gpx_obj.tracks: for segment in track.segments: for point in segment.points: if point.time is None: continue yield Location( lat=point.latitude, lng=point.longitude, accuracy=config.accuracy, elevation=point.elevation, dt=datetime.replace(point.time, tzinfo=timezone.utc), )
def get_gpx_data(): alts = [] with open('test.gpx', 'r') as gpx_file: gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: for segment in track.segments: for point in segment.points: alts.append( ll_to_proj((point.longitude + random.choice(xrange(1, 10)) / 1000, point.latitude + random.choice(xrange(1, 10)) / 1000))) return alts
def add_route(request): if request.method == 'POST': form = UploadRouteForm(request.POST, request.FILES) if form.is_valid(): gpx_file = open(form.name, 'r') gpx = gpxpy.parse(gpx_file) for route in gpx.routes: print 'Route:' for point in route.points: print 'Point at ({0},{1}) -> {2}'.format( point.latitude, point.longitude, point.elevation) return HttpResponseRedirect('buddyapp/home.html') else: form = UploadRouteForm() return render(request, 'buddyapp/add_route.html', {'form': form})
def convert(gpx_filename): # gpxファイルの読み込み with open(gpx_filename, 'r', encoding='utf-8') as infile: gpx = gpxpy.parse(infile) # 読み込んだgpxファイルをgeojsonオブジェクトに変換 features = getFeatures(gpx) feature_collection = geojson.FeatureCollection(features) # geojsonオブジェクトをファイルに出力 output_filename = getOutputFilename(gpx_filename) with open(output_filename, 'w') as outfile: geojson.dump(feature_collection, outfile, indent=2) return output_filename
def plot_data(filename, linecolor): lat = [] lon = [] gpx_filename = join(data_path,filename) gpx_file = open(filename, 'r') gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: for segment in track.segments: for point in segment.points: lat.append(point.latitude) lon.append(point.longitude) lat, lon = deline(lat, lon) ax.plot(lon, lat, color = linecolor, lw = 0.1, alpha = 0.8) lat = [] lon = []
def plotPoints(canvas, data): unitW = data.unitW unitH = data.unitH marginX = unitW marginY = unitH (_, _, gpxFile, _, min_lat, min_lon, max_lat, max_lon, _) = data.plot totalChangex = max_lon - min_lon totalChangey = max_lat - min_lat gpx = gpxpy.parse(gpxFile) i = 0 elevationChange = 0 elevation = 0 point1 = None for track in gpx.tracks: for segment in track.segments: for point in segment.points: posX = point.longitude - min_lon posY = point.latitude - min_lat ratioX = posX / totalChangex ratioY = posY / totalChangey if i == 0: point1 = ((ratioX * 18 * unitW) + marginX, (ratioY * 18 * unitH) + marginY) elevation = point.elevation lastLat = point.latitude lastLon = point.longitude else: elevationChange = point.elevation - elevation distance = findDistance(lastLat, lastLon, point.latitude, point.longitude) * 1000 ratio = abs(atan(elevationChange / distance)) if ratio > .15: r = 255 else: r = int((255 / .15) * ratio) rgb = rgbString(r, 255 - r, 0) canvas.create_line(point1, ((ratioX * 18 * unitW) + marginX, (ratioY * 18 * unitH) + marginY), fill=rgb, width=2) point1 = ((ratioX * 18 * unitW) + marginX, (ratioY * 18 * unitH) + marginY) lastLat = point.latitude lastLon = point.longitude elevation = point.elevation i += 1
def analizar_traza(ruta_gpx: str, debug=False) -> None: """ Recibe una ruta a un archivo GPX el cual cada uno de sus waypoints debe ser analizado. Imprime en pantalla los resultados del análisis apoyándose en las en los argumentos recibidos por el análisis de etiquetas. No devuelve ningún valor. """ imprimir_encabezado_traza(ruta_gpx) # Parsear el gpx archivo_gpx = open(ruta_gpx, 'r') gpx = gpxpy.parse(archivo_gpx) archivo_gpx.close() for waypoint in gpx.waypoints: # Obtener los atributos del waypoint latitud = waypoint.latitude longitud = waypoint.longitude nombre = waypoint.name # Mostrar informacion del waypoint nodos_cercanos = descargar_nodos_en_rango(latitud, longitud, debug) imprimir_encabezado_waypoint(nombre, latitud, longitud) # Se asume que las etiquetas del waypoint no se encuentran en OSM nodo_es_nuevo = True # Si no hay etiquetas en el esquema con ese nombre se omite etiquetas_esquema = esquema.get(nombre) if etiquetas_esquema is not None: for nodo in nodos_cercanos.nodes: etiquetas_osm = nodo.tags resultado = analizar_etiquetas(etiquetas_osm, etiquetas_esquema, debug) # Si el resultado fue un caso esperado quiere decir que # las etiquetas analizadas ya estaban en OSM if resultado is not None: nodo_es_nuevo = False imprimir_resultado(resultado, nodo.id, latitud, longitud) if nodo_es_nuevo: imprimir_crear(latitud, longitud, etiquetas_esquema)
def load(self, fname=None): f = fname or self.fname if f is None: raise ValueError("GPX file is None") if not os.path.exists(f): raise RuntimeError("GPX file %s not found" % f) gpx_file = open(f, 'r') gpx_data = gpxpy.parse(gpx_file) points = [] pm = ProjectionMapper() # call my optimizer (remove contiguous points) points = [] for track in gpx_data.tracks: for segment in track.segments: points += segment.points if self.optimize: gpx_optmizer = GPXOptimizer() opt_points = gpx_optmizer.Optimize(points) gpx_optmizer.Print_stats() points = opt_points elevs = [] ret_points = [] for point in points: z, l, x, y = pm.project((point.longitude, point.latitude)) #x,y = pm.project_2(point.longitude, point.latitude) point.x = x point.y = y ret_points += [x, y, point.elevation] self.optimize and elevs.append(point.elevation) if self.optimize: #smoothed_elevations = np.array(savitzky_golay( np.array(elevs) , 135, 5)) smoothed_elevations = savitzky_golay(np.array(elevs), 11, 5) #idx = np.arange(0,44) #import matplotlib.pyplot as plt #plt.plot(idx,elevs[0:44]) #plt.plot(idx,smoothed_elevations[0:44]) #plt.show() ret_points = np.array(ret_points).reshape(int(len(ret_points) / 3), 3) ret_points[:, 2] = smoothed_elevations[0:len(elevs)] return (ret_points)
def load_points_and_polygon(folder): """Loads all gpx files into a list of points""" print("Loading files...") with open('polylines.js', 'w') as outputfile: outputfile.write('function addLines(map) {') with click.progressbar(os.listdir(folder)) as bar: for filename in bar: if filename.endswith(".gpx"): gpx_file = open(f'{folder}/' + filename) try: color = '#3388ff' gpx = gpxpy.parse(gpx_file) if gpx.time < datetime.datetime( 2019, 8, 1, 0, 0, 0, 0, datetime.timezone.utc): gpx_file.close() os.rename(f'{folder}/' + filename, f'{folder}/archive/' + filename) else: tooltip = filename + ' - ' + gpx.time.strftime( '%x') polygon = "L.polyline([" for track in gpx.tracks: if track.name: tooltip += ' - ' + track.name #+ ' (' + track.type + ')' try: moving_data = track.get_moving_data() if moving_data: tooltip += ' - duration:' + format_time( moving_data.moving_time) except: pass if track.type == 'Ride' or track.type == '1': color = '#ff22ff' for segment in track.segments: for point in segment.points: polygon += "[" + str( point.latitude) + "," + str( point.longitude) + "]," polygon = polygon[:-1] + "],{color:'" + color + "',weight:2}).bindTooltip('" + html.escape( tooltip ) + "').on('mouseover',function(e){e.target.setStyle({color:'red'});}).on('mouseout',function(e){e.target.setStyle({color:'" + color + "'});}).addTo(map);" outputfile.write(polygon) except: print(f"Failed to load {filename} : ", sys.exc_info()[0]) outputfile.write('}') print("Done...")
def plot_many_rides(data_path): # data_path = 'lpq' data = [f for f in listdir(data_path) if isfile(join(data_path, f))] lat = [] lon = [] lat_array = [] lon_array = [] fig = plt.figure(facecolor='0.05') ax = plt.Axes( fig, [0., 0., 1., 1.], ) ax.set_aspect('equal') ax.set_axis_off() fig.add_axes(ax) for activity in data: gpx_filename = join(data_path, activity) gpx_file = open(gpx_filename, 'r') gpx = gpxpy.parse(gpx_file) for track in gpx.tracks: for segment in track.segments: for point in segment.points: lat.append(point.latitude) lon.append(point.longitude) plt.plot(lon, lat, color='deepskyblue', lw=0.2, alpha=0.8) lat_array.append(lat) lon_array.append(lon) lat = [] lon = [] map_generator(lat_array, lon_array, np.mean(lat_array[0]), np.mean(lon_array[0]), 14) webbrowser.open('map.html') filename = data_path + '.png' plt.savefig(filename, facecolor=fig.get_facecolor(), bbox_inches='tight', pad_inches=0, dpi=300)
def read_gpx_files(self, runs_directory): filenames = os.listdir(runs_directory) for filename in filenames: gpx_data = gpxpy.parse(open(runs_directory + '/' + filename)) date = gpx_data.name[0:10] time = gpx_data.name[11:20] distance = gpx_data.length_3d() duration = gpx_data.get_duration() coordinates = [[point.latitude, point.longitude, point.elevation] for track in gpx_data.tracks for segment in track.segments for point in segment.points] self.runs.append(Run(date, time, distance, duration, coordinates))
def parse_request_data(json_data): paths = [] for json_object in json.loads(json_data): with open("gpx_data/{}.gpx".format(json_object['user']), 'w') as f: f.write(json_object['gpx']) gpx_file = gpxpy.parse(json_object['gpx']) for track in gpx_file.tracks: path = [(point.latitude, point.longitude) for segment in track.segments for point in segment.points] paths.append(path) break # request should only have 1 person return paths, json_object['user']
def gpx2csv(path2gpx, outFile): path2gpx = open(path2gpx) gpxFile = gpxpy.parse(path2gpx) rows = [] for track in gpxFile.tracks: for seg in track.segments: for pt in seg.points: rows.append({ 'vid': 0, 'sts': 0, 'lat': pt.latitude, 'lon': pt.longitude }) res = pd.DataFrame(rows) res.to_csv(outFile) print "Written: " + outFile
def wczytaj_plik(filename): lat = [] lon = [] el = [] dates = [] with open(filename, 'r') as gpx_file: gpx_dane = gpxpy.parse(gpx_file) for track in gpx_dane.tracks: for seg in track.segments: for point in seg.points: lon.append(point.longitude) lat.append(point.latitude) el.append(point.elevation) point.time = point.time.replace(tzinfo=None) dates.append(point.time) return lon, lat, el, dates
def read_route_from_gpx(file): """ Read route from gpx file :param file: str, path to the .gpx file :return: list, all routes """ gpx_file = open(file) gpx = gpxpy.parse(gpx_file) all_routes = [] for route in gpx.routes: route_list = [] for point in route.points: route_list.append([point.latitude, point.longitude]) all_routes.append(route_list) return all_routes