def write_gpx(gpx, name, act_type): # You can add routes and waypoints, too... tzname = None npoints = None # polyline encoder default values numLevels = 18 zoomFactor = 2 epsilon = 0.0 forceEndpoints = True ##print('Created GPX:', gpx.to_xml()) ts = int(time.time()) filename = "%s/%i.gpx" % (filebase, ts) a = open(filename, "w") a.write(gpx.to_xml()) a.close() gpx.simplify() # gpx.reduce_points(1000) trk = pl.read_gpx_trk(gpx.to_xml(), tzname, npoints, 2, None) try: polyline = pl.print_gpx_google_polyline(trk, numLevels, zoomFactor, epsilon, forceEndpoints) except UnboundLocalError as er: print(er) print("Not enough points to create a polyline") polyline = "" # polyline="polyline" add_run(gpx, name, act_type, filename, polyline)
def write_gpx(gpx,name,act_type): # You can add routes and waypoints, too... tzname=None npoints=None # polyline encoder default values numLevels = 18; zoomFactor = 2; epsilon = 0.0; forceEndpoints = True; ##print('Created GPX:', gpx.to_xml()) ts = int(time.time()) filename = "%s/%i.gpx" % (filebase,ts) a = open(filename, 'w') a.write(gpx.to_xml()) a.close() gpx.simplify() #gpx.reduce_points(1000) trk = pl.read_gpx_trk(gpx.to_xml(),tzname,npoints,2,None) try: polyline=pl.print_gpx_google_polyline(trk,numLevels,zoomFactor,epsilon,forceEndpoints) except UnboundLocalError as er: print(er) print("Not enough points to create a polyline") polyline="" #polyline="polyline" add_run(gpx,name,act_type,filename,polyline)
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 get_gpx(cls, user: User, start_record_time: datetime, end_record_time: datetime, reduce=True) -> gpxpy.gpx.GPX: 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_segment = gpxpy.gpx.GPXTrackSegment() gpx_track.segments.append(gpx_segment) # Create points: points = GpxPointRepository.find_by_user_and_term( user, start_record_time, end_record_time) for point in points: gpx_segment.points.append( gpxpy.gpx.GPXTrackPoint(point.latitude, point.longitude, point.elevation, point.record_time)) if reduce: gpx.reduce_points(min_distance=1) gpx.simplify() return gpx
def main(): args = getargs() gpx = gpxpy.parse(args.infile) delta = args.delta log.debug("{} points before simplification".format(len(points(gpx)))) if delta: gpx.simplify(delta) log.debug("{} points after simplification".format(len(points(gpx)))) latlon_points = points(gpx) utm_path, utm_zone = track_to_utm(latlon_points) dump_csv(utm_path, args.outfile)
def get_gpx_points(self): points = [] gpx_file_name = self._get_gpx_file_name_without_path() gpx_file = self.filesystem.open(gpx_file_name, 'r') gpx = gpxpy.parse(gpx_file) gpx.simplify(5) first_segment = gpx.tracks[0].segments[0] for point in first_segment.points: new_point = (point.latitude, point.longitude) points.append(new_point) return points
def main(): args = getargs() gpx = gpxpy.parse(args.infile) delta = args.delta log.debug("{} points before simplification".format(len(points(gpx)))) gpx.simplify(delta) log.debug("{} points after simplification".format(len(points(gpx)))) if args.format == "points": print(json.dumps(points(gpx)), file=args.outfile) else: print(gpx.to_xml(), file=args.outfile)
def create_gpx_track(self, g, edges, simplify=False): # create GPX XML. gpx = gpxpy.gpx.GPX() gpx.name = f'burb {self.name}' gpx.author_name = 'optiburb' #gpx.author_email ='' gpx.creator = 'experimental burbing' gpx.description = f'experimental burbing route for {self.name}' track = gpxpy.gpx.GPXTrack() track.name = f'burb trk {self.name}' gpx.tracks.append(track) segment = gpxpy.gpx.GPXTrackSegment() track.segments.append(segment) distance = 0.0 i = 1 for n, edge in enumerate(edges): u, v = edge edge_data = g.get_edge_data(*edge, 0) log.debug('EDGE [%d] - edge=%s, data=%s', n, edge, edge_data) if edge_data is None: log.warning('null data for edge %s', edge) continue linestring = edge_data.get('geometry') augmented = edge_data.get('augmented') distance += edge_data.get('length', 0) log.debug(' leg [%d] -> %s (%s,%s,%s,%s,%s)', n, edge_data.get('name', ''), edge_data.get('highway', ''), edge_data.get('surface', ''), edge_data.get('oneway', ''), edge_data.get('access', ''), edge_data.get('length', 0)) if linestring: directional_linestring = self.directional_linestring(g, edge) for lon, lat in directional_linestring.coords: segment.points.append(gpxpy.gpx.GPXRoutePoint(latitude=lat, longitude=lon)) log.debug(' INDEX[%d] = (%s, %s)', i, lat, lon) i += 1 pass pass else: log.error(' no linestring for edge=%s', edge) pass pass log.info('total distance = %.1fkm', distance/1000.0) ## ## if simplify: log.info('simplifying GPX') gpx.simplify() pass data = gpx.to_xml() filename = f'burb_track_{self.name}.gpx' with open(filename, 'w') as f: f.write(data) pass return
def gpx_simplify(gpx, max_distance): gpx.simplify(max_distance) return gpx
logger.info(f"Found {len(args.input)} input files") if args.simplify: logger.info( f"Simplication is on with a maximum distance of {args.max_distance}m") else: logger.info(f"Simplification is off") routes_new = [] for gpxfile in args.input: logger.debug(f"Parsing {gpxfile}") gpx = gpxpy.parse(gpxfile) if args.simplify: gpxlen_before = gpx.get_points_no() gpx.simplify(max_distance=args.max_distance) gpxlen_after = gpx.get_points_no() logger.debug( f"Simplified track from {gpxlen_before} to {gpxlen_after} points") route_idx = 0 for track_idx, track in enumerate(gpx.tracks): point_idx = 0 for segment_idx, segment in enumerate(track.segments): # Creates a list of lists of points (sublists of segment) with at most max_route_length points # As many as necessary to cover the entire segment subsegments = [ segment.points[start:start + args.max_route_length] for start in range(0, len(segment.points), args.max_route_length) ] logger.debug(f"Split segment into {len(subsegments)} subsegments")
def create_gpx_track(self, g, edges, simplify=False): # create GPX XML. gpx = gpxpy.gpx.GPX() gpx.name = f'burb {self.name}' gpx.author_name = 'Jim Crumpler' #gpx.author_email ='' gpx.creator = 'experimental burbing' gpx.description = f'experimental burbing route for {self.name}' # Create first track in our GPX: track = gpxpy.gpx.GPXTrack() track.name = f'burb trk {self.name}' gpx.tracks.append(track) # Create first segment in our GPX track: segment = gpxpy.gpx.GPXTrackSegment() track.segments.append(segment) #route.comment = '' #route.source = '' #route.name = '' distance = 0.0 i = 1 for n, edge in enumerate(edges): u, v = edge edge_data = g.get_edge_data(*edge, 0) #directed_edge_data = self.g_directed.get_edge_data(*edge, 0) log.debug('EDGE [%d] - edge=%s, data=%s', n, edge, edge_data) if edge_data is None: log.warning('null data for edge %s', edge) continue name = edge_data.get('name', '') _highway = edge_data.get('highway', '') linestring = edge_data.get('geometry') node_from = edge_data.get('from') node_to = edge_data.get('to') _surface = edge_data.get('surface', '') _oneway = edge_data.get('oneway', '') _access = edge_data.get('access', '') _distance = edge_data.get('length', 0) distance += _distance log.debug(' leg [%d] -> %s (%s,%s,%s,%s,%s)', n, name, _highway, _surface, _oneway, _access, _distance) #linestring = edge_data.get('geometry') augmented = edge_data.get('augmented') ## how do we know that we're going in the rigth direction?? log.debug(' edge=%s, augmented=%s, from=%s, to=%s, linestring=%s', edge, augmented, node_from, node_to, linestring) if linestring: directional_linestring = self.directional_linestring(g, edge) for lon, lat in directional_linestring.coords: segment.points.append(gpxpy.gpx.GPXRoutePoint(latitude=lat, longitude=lon)) log.debug(' INDEX[%d] = (%s, %s)', i, lat, lon) i += 1 pass pass else: log.error(' no linestring for edge=%s', edge) #lat = g.nodes[u]['y'] #lon = g.nodes[v]['x'] #segment.points.append(gpxpy.gpx.GPXRoutePoint(latitude=lat, longitude=lon)) pass #log.info('lat=%s, lon=%s, edge=%s, name=%s, highway=%s', lat, lon, edge, name, highway) pass log.info('total distance = %.1fkm', distance/1000.0) ## ## if simplify: log.info('simplifying GPX') gpx.simplify() pass data = gpx.to_xml() filename = f'burb_track_{self.name}.gpx' with open(filename, 'w') as f: f.write(data) pass return