def track_bbox(request): if request.matchdict: #track_id = request.matchdict['trackid'] #track = Track.get_track_by_id(track_id) #etappes = DBSession.query(Etappe).all() tours = DBSession.query(Tour).all() #tracks = DBSession.query(Track).all() for tour in tours: trkpts = list() for etappe in tour.etappes: for track in etappe.tracks: trkpts = list() for pt in track.trackpoints: trkpts.append([pt.latitude,pt.longitude]) log.debug(track.id) #maxx,maxy = numpy.max(trkpts, axis=0) #minx,miny = numpy.min(trkpts, axis=0) #bbox = u'POLYGON(( \ # {maxx} {maxy}, \ # {maxx} {miny}, \ # {minx} {miny}, \ # {minx} {maxy}, \ # {maxx} {maxy}))'.format( \ # maxx=maxx, maxy=maxy, minx=minx, miny=miny) #track.bbox = DBSession.query(select([func.ST_AsText(func.ST_Transform(func.ST_GeomFromText(bbox, 4326),3857))]).label("bbox")).one()[0] DBSession.flush() return Response(tour.bbox)
def track_bbox(request): if request.matchdict: #track_id = request.matchdict['trackid'] #track = Track.get_track_by_id(track_id) #etappes = DBSession.query(Etappe).all() tours = DBSession.query(Tour).all() #tracks = DBSession.query(Track).all() for tour in tours: trkpts = list() for etappe in tour.etappes: for track in etappe.tracks: trkpts = list() for pt in track.trackpoints: trkpts.append([pt.latitude, pt.longitude]) log.debug(track.id) #maxx,maxy = numpy.max(trkpts, axis=0) #minx,miny = numpy.min(trkpts, axis=0) #bbox = u'POLYGON(( \ # {maxx} {maxy}, \ # {maxx} {miny}, \ # {minx} {miny}, \ # {minx} {maxy}, \ # {maxx} {maxy}))'.format( \ # maxx=maxx, maxy=maxy, minx=minx, miny=miny) #track.bbox = DBSession.query(select([func.ST_AsText(func.ST_Transform(func.ST_GeomFromText(bbox, 4326),3857))]).label("bbox")).one()[0] DBSession.flush() return Response(tour.bbox)
def link_etappe_image(request): etappes = DBSession.query(Etappe).all() for etappe in etappes: images = sum([trackpoint.images for trackpoint in sum([track.trackpoints for track in etappe.tracks],[])],[]) for image in images: etappe.images.append(image) DBSession.flush() return Response('OK')
def link_etappe_image(request): etappes = DBSession.query(Etappe).all() for etappe in etappes: images = sum([ trackpoint.images for trackpoint in sum( [track.trackpoints for track in etappe.tracks], []) ], []) for image in images: etappe.images.append(image) DBSession.flush() return Response('OK')
def reduce_tracks(request): if request.query_string: track_id = request.GET.get('trackid') epsilon = Decimal(request.GET.get('epsilon')) log.debug(epsilon) #track = Track.get_track_by_id(track_id) #tracks = DBSession.query(Track).filter(Track.id == track_id).all() #tracks = DBSession.query(Track).all() #etappes = DBSession.query(Etappe).all() tours = DBSession.query(Tour).all() for tour in tours: rtp = list() for etappe in tour.etappes: for track in etappe.tracks: rtp.append(reduce_trackpoints(track.trackpoints, 0.005)) tour.reduced_trackpoints = json.dumps(rtp) DBSession.flush() return Response('OK')
def add_trackpoints_to_db(trackpoints, track): for trackpoint in trackpoints: trackpoint.track = track try: DBSession.add(trackpoint) DBSession.flush() print((trackpoint.timestamp)) except Exception as e: print("\n\nTrackpoint could not be added!\n\n") print(e) DBSession.rollback()
def add_trackpoints_to_db(trackpoints, track): for trackpoint in trackpoints: trackpoint.track = track try: DBSession.add(trackpoint) DBSession.flush() print trackpoint.timestamp except Exception, e: print "\n\nTrackpoint could not be added!\n\n" print e DBSession.rollback()
def add_track_to_db(track_details): track = track_details['track'] trackpoints = track_details['trackpoints'] print type(track.reduced_trackpoints) track.uuid = str(uuid.uuid4()) track.start_timestamp = trackpoints[0].timestamp track.end_timestamp = trackpoints[-1].timestamp try: DBSession.add(track) DBSession.flush() except Exception, e: DBSession.rollback() print "\n\nTrack could not be added!\n\n" print e return None
def add_track_to_db(track_details): track = track_details['track'] trackpoints = track_details['trackpoints'] print((type(track.reduced_trackpoints))) track.uuid = str(uuid.uuid4()) track.start_timestamp = trackpoints[0].timestamp track.end_timestamp = trackpoints[-1].timestamp try: DBSession.add(track) DBSession.flush() except Exception as e: DBSession.rollback() print("\n\nTrack could not be added!\n\n") print(e) return None return track
def testmap(request): maxx, maxy, minx, miny = request.GET.get('bbox').split(',') #- define viewport for query viewport = 'POLYGON(( \ {maxx} {maxy}, \ {maxx} {miny}, \ {minx} {miny}, \ {minx} {maxy}, \ {maxx} {maxy}))'.format( \ maxx=maxx, maxy=maxy, minx=minx, miny=miny) viewport = select([func.ST_GeomFromText(viewport, 4326)]).label("viewport") tracks_in_viewport = DBSession.query(Track).filter(or_( func.ST_Overlaps(viewport, Track.bbox), func.ST_Contains(viewport, Track.bbox) )).all() features = convert_to_geojson_featurecollection(tracks_in_viewport) response = Response(json.dumps(dict(type='FeatureCollection', features=features))) response.content_type = 'application/json' return(response)
def map_popup_etappe(request): id = request.GET.get('id') etappe = DBSession.query(Etappe).filter(Etappe.id == id).one() return {'etappe': etappe}
def map_popup_tour(request): id = request.GET.get('id') tour = DBSession.query(Tour).filter(Tour.id == id).one() return {'tour': tour}
def map_popup_track(request): id = request.GET.get('id') track = DBSession.query(Track).filter(Track.id == id).one() return {'track': track}
def features_in_bbox(request): ## get request variables session = request.session if 'features' not in session: session['features'] = {} if 'tour' not in session['features']: session['features']['tour'] = [] if 'etappe' not in session['features']: session['features']['etappe'] = [] if 'track' not in session['features']: session['features']['track'] = [] maxx, maxy, minx, miny = request.GET.get('bbox').split(',') #- define viewport for query viewport = u'POLYGON(( \ {maxx} {maxy}, \ {maxx} {miny}, \ {minx} {miny}, \ {minx} {maxy}, \ {maxx} {maxy}))'.format( \ maxx=maxx, maxy=maxy, minx=minx, miny=miny) viewport = select([func.ST_GeomFromText(viewport, 4326)]).label("viewport") ## query tracks which are not yet loaded features = list() relatives = dict(tour=[], etappe=[],track=[], self=[]) # /// FETCH TOURS IN VIEWPORT /// tours_contained_query = DBSession.query(Tour).filter(func.ST_Contains(viewport, Tour.bbox)) features, relatives, tour_ids, tours = maptools.query_features( tours_contained_query, False, features, relatives, session=session['features']['tour'], level='tour' ) #Tours contained session['features']['tour'] = list( set(session['features']['tour'] + tour_ids) ) # /// FETCH ETAPPES IN VIEWPORT /// tour_partial_query = DBSession.query(Tour).filter(or_( func.ST_Overlaps(viewport, Tour.bbox), func.ST_Contains(Tour.bbox, viewport) # !!viewport is INSIDE of Tour.bbox!! )) features, relatives, tour_id_list, tours = maptools.query_features( tour_partial_query, branch=True, features=features, relatives=relatives, level='tour' ) if tour_id_list: # we need to limit the query to elements(etappe) that are children of # the parent_partial-query above, otherwise we would return elements # fully contained in the same viewport as it's parent, # displaying both concurrently etappes_contained_query = DBSession.query(Etappe).filter(and_( or_( func.ST_Overlaps(viewport, Etappe.bbox), func.ST_Contains(viewport, Etappe.bbox), ), Etappe.tour.in_(tour_id_list) )) features, relatives, etappe_ids, etappes = maptools.query_features( etappes_contained_query, False, features, relatives, session['features']['etappe'], level='etappe' ) session['features']['etappe'] = list( set(session['features']['etappe'] + etappe_ids) ) # /// FETCH TRACKS IN VIEWPORT /// etappes_partial_query = DBSession.query(Etappe).filter( #or_( # func.ST_Overlaps(viewport, Etappe.bbox), func.ST_Contains(Etappe.bbox, viewport) # !!viewport is INSIDE of Etappe.bbox #) ) features, relatives, etappe_id_list, etappes = maptools.query_features( etappes_partial_query, branch=True, features=features, relatives=relatives, level='etappe' ) if etappe_id_list: tracks_contained_query = DBSession.query(Track).filter(and_( or_( func.ST_Overlaps(viewport, Track.bbox), func.ST_Contains(viewport, Track.bbox), func.ST_Contains(Track.bbox, viewport) ), Track.etappe.in_(etappe_id_list) )) features, relatives, track_ids, tracks = maptools.query_features( tracks_contained_query, False, features, relatives, session['features']['track'], level='track' ) session['features']['track'] = list( set(session['features']['track'] + track_ids) ) # /// query for the remaining tracks which are not below a tour/etappe else: tracks_contained_query = DBSession.query(Track).filter(and_( or_( func.ST_Overlaps(viewport, Track.bbox), func.ST_Contains(viewport, Track.bbox), func.ST_Contains(Track.bbox, viewport) ), Track.etappe == None )) features, relatives, track_ids, tracks = maptools.query_features( tracks_contained_query, False, features, relatives, session['features']['track'], level='track' ) session['features']['track'] = list( set(session['features']['track'] + track_ids) ) # /// RETURN FEATURES /// #track_json = Response(json.dumps(dict(type='FeatureCollection', features=features, crs=dict(type='name',properties=dict(name='urn:ogc:def:crs:OGC:1.3:CRS84'))))) log.debug(json.dumps(dict(type='FeatureCollection', features=features))) track_json = Response(json.dumps([relatives, dict(type='FeatureCollection', features=features)])) track_json.content_type = 'application/json' return(track_json)
def set_mode(request): tracks = DBSession.query(Track).all() track_json = generate_json_from_tracks(tracks) return { 'track_json': track_json, }
def get_center(request): tours = DBSession.query(Tour).all() for tour in tours: distance = 0 #get the total distance of tour for etappe in tour.etappes: for track in etappe.tracks: previous_trkpt = [ track.trackpoints[0].latitude, track.trackpoints[0].longitude ] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude, trkpt.longitude] dist = VincentyDistance() distance = distance + dist.measure(previous_trkpt, curr_trkpt) previous_trkpt = curr_trkpt #log.debug('old : {0}, new: {1}'.format(old_distance, distance)) halfway = distance / 2 distance = 0 #find the trackpoint that is closest to distance/2 for etappe in tour.etappes: for track in etappe.tracks: previous_trkpt = [ track.trackpoints[0].latitude, track.trackpoints[0].longitude ] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude, trkpt.longitude] dist = VincentyDistance() #log.debug(trkpt.timestamp) if distance <= halfway: distance = distance + dist.measure( previous_trkpt, curr_trkpt) previous_trkpt = curr_trkpt #center=[previous_trkpt[1], previous_trkpt[0]] log.debug(trkpt.latitude) log.debug(previous_trkpt[0]) center = trkpt.id #log.debug('{0}, distance: {1}, halfway {2}'.format(distance > halfway, distance,halfway)) tour.center_id = center #log.debug(center) etappes = DBSession.query(Etappe).all() for etappe in etappes: distance = 0 for track in etappe.tracks: previous_trkpt = [ track.trackpoints[0].latitude, track.trackpoints[0].longitude ] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude, trkpt.longitude] dist = VincentyDistance() distance = distance + dist.measure(previous_trkpt, curr_trkpt) previous_trkpt = curr_trkpt #log.debug('old : {0}, new: {1}'.format(old_distance, distance)) halfway = distance / 2 distance = 0 for track in etappe.tracks: previous_trkpt = [ track.trackpoints[0].latitude, track.trackpoints[0].longitude ] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude, trkpt.longitude] dist = VincentyDistance() #log.debug(trkpt.timestamp) if distance <= halfway: distance = distance + dist.measure(previous_trkpt, curr_trkpt) previous_trkpt = curr_trkpt #center=[previous_trkpt[1], previous_trkpt[0]] center = trkpt.id log.debug(trkpt.id) #log.debug('{0}, distance: {1}, halfway {2}'.format(distance > halfway, distance,halfway)) etappe.center_id = center tracks = DBSession.query(Track).all() for track in tracks: distance = 0 previous_trkpt = [ track.trackpoints[0].latitude, track.trackpoints[0].longitude ] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude, trkpt.longitude] dist = VincentyDistance() distance = distance + dist.measure(previous_trkpt, curr_trkpt) previous_trkpt = curr_trkpt #log.debug('old : {0}, new: {1}'.format(old_distance, distance)) halfway = distance / 2 distance = 0 previous_trkpt = [ track.trackpoints[0].latitude, track.trackpoints[0].longitude ] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude, trkpt.longitude] dist = VincentyDistance() #log.debug(trkpt.timestamp) if distance <= halfway: distance = distance + dist.measure(previous_trkpt, curr_trkpt) previous_trkpt = curr_trkpt center_old = [previous_trkpt[1], previous_trkpt[0]] center = trkpt.id track.center_id = center #DBSession.flush() return Response(str(center))
def get_center(request): tours = DBSession.query(Tour).all() for tour in tours: distance = 0 #get the total distance of tour for etappe in tour.etappes: for track in etappe.tracks: previous_trkpt = [track.trackpoints[0].latitude,track.trackpoints[0].longitude] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude,trkpt.longitude] dist = VincentyDistance() distance = distance + dist.measure(previous_trkpt,curr_trkpt) previous_trkpt = curr_trkpt #log.debug('old : {0}, new: {1}'.format(old_distance, distance)) halfway = distance/2 distance = 0 #find the trackpoint that is closest to distance/2 for etappe in tour.etappes: for track in etappe.tracks: previous_trkpt = [track.trackpoints[0].latitude,track.trackpoints[0].longitude] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude,trkpt.longitude] dist = VincentyDistance() #log.debug(trkpt.timestamp) if distance <= halfway: distance = distance + dist.measure(previous_trkpt,curr_trkpt) previous_trkpt = curr_trkpt #center=[previous_trkpt[1], previous_trkpt[0]] log.debug(trkpt.latitude) log.debug(previous_trkpt[0]) center=trkpt.id #log.debug('{0}, distance: {1}, halfway {2}'.format(distance > halfway, distance,halfway)) tour.center_id = center #log.debug(center) etappes = DBSession.query(Etappe).all() for etappe in etappes: distance = 0 for track in etappe.tracks: previous_trkpt = [track.trackpoints[0].latitude,track.trackpoints[0].longitude] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude,trkpt.longitude] dist = VincentyDistance() distance = distance + dist.measure(previous_trkpt,curr_trkpt) previous_trkpt = curr_trkpt #log.debug('old : {0}, new: {1}'.format(old_distance, distance)) halfway = distance/2 distance = 0 for track in etappe.tracks: previous_trkpt = [track.trackpoints[0].latitude,track.trackpoints[0].longitude] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude,trkpt.longitude] dist = VincentyDistance() #log.debug(trkpt.timestamp) if distance <= halfway: distance = distance + dist.measure(previous_trkpt,curr_trkpt) previous_trkpt = curr_trkpt #center=[previous_trkpt[1], previous_trkpt[0]] center=trkpt.id log.debug(trkpt.id) #log.debug('{0}, distance: {1}, halfway {2}'.format(distance > halfway, distance,halfway)) etappe.center_id = center tracks = DBSession.query(Track).all() for track in tracks: distance = 0 previous_trkpt = [track.trackpoints[0].latitude,track.trackpoints[0].longitude] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude,trkpt.longitude] dist = VincentyDistance() distance = distance + dist.measure(previous_trkpt,curr_trkpt) previous_trkpt = curr_trkpt #log.debug('old : {0}, new: {1}'.format(old_distance, distance)) halfway = distance/2 distance = 0 previous_trkpt = [track.trackpoints[0].latitude,track.trackpoints[0].longitude] for trkpt in track.trackpoints: curr_trkpt = [trkpt.latitude,trkpt.longitude] dist = VincentyDistance() #log.debug(trkpt.timestamp) if distance <= halfway: distance = distance + dist.measure(previous_trkpt,curr_trkpt) previous_trkpt = curr_trkpt center_old=[previous_trkpt[1], previous_trkpt[0]] center=trkpt.id track.center_id = center #DBSession.flush() return Response(str(center))
def features_in_bbox(request): ## get request variables session = request.session if 'features' not in session: session['features'] = {} if 'tour' not in session['features']: session['features']['tour'] = [] if 'etappe' not in session['features']: session['features']['etappe'] = [] if 'track' not in session['features']: session['features']['track'] = [] maxx, maxy, minx, miny = request.GET.get('bbox').split(',') #- define viewport for query viewport = 'POLYGON(( \ {maxx} {maxy}, \ {maxx} {miny}, \ {minx} {miny}, \ {minx} {maxy}, \ {maxx} {maxy}))' .format( \ maxx=maxx, maxy=maxy, minx=minx, miny=miny) viewport = select([func.ST_GeomFromText(viewport, 4326)]).label("viewport") ## query tracks which are not yet loaded features = list() relatives = dict(tour=[], etappe=[], track=[], self=[]) # /// FETCH TOURS IN VIEWPORT /// tours_contained_query = DBSession.query(Tour).filter( func.ST_Contains(viewport, Tour.bbox)) features, relatives, tour_ids, tours = maptools.query_features( tours_contained_query, False, features, relatives, session=session['features']['tour'], level='tour') #Tours contained session['features']['tour'] = list( set(session['features']['tour'] + tour_ids)) # /// FETCH ETAPPES IN VIEWPORT /// tour_partial_query = DBSession.query(Tour).filter( or_( func.ST_Overlaps(viewport, Tour.bbox), func.ST_Contains(Tour.bbox, viewport) # !!viewport is INSIDE of Tour.bbox!! )) features, relatives, tour_id_list, tours = maptools.query_features( tour_partial_query, branch=True, features=features, relatives=relatives, level='tour') if tour_id_list: # we need to limit the query to elements(etappe) that are children of # the parent_partial-query above, otherwise we would return elements # fully contained in the same viewport as it's parent, # displaying both concurrently etappes_contained_query = DBSession.query(Etappe).filter( and_( or_( func.ST_Overlaps(viewport, Etappe.bbox), func.ST_Contains(viewport, Etappe.bbox), ), Etappe.tour.in_(tour_id_list))) features, relatives, etappe_ids, etappes = maptools.query_features( etappes_contained_query, False, features, relatives, session['features']['etappe'], level='etappe') session['features']['etappe'] = list( set(session['features']['etappe'] + etappe_ids)) # /// FETCH TRACKS IN VIEWPORT /// etappes_partial_query = DBSession.query(Etappe).filter( #or_( # func.ST_Overlaps(viewport, Etappe.bbox), func.ST_Contains(Etappe.bbox, viewport) # !!viewport is INSIDE of Etappe.bbox #) ) features, relatives, etappe_id_list, etappes = maptools.query_features( etappes_partial_query, branch=True, features=features, relatives=relatives, level='etappe') if etappe_id_list: tracks_contained_query = DBSession.query(Track).filter( and_( or_(func.ST_Overlaps(viewport, Track.bbox), func.ST_Contains(viewport, Track.bbox), func.ST_Contains(Track.bbox, viewport)), Track.etappe.in_(etappe_id_list))) features, relatives, track_ids, tracks = maptools.query_features( tracks_contained_query, False, features, relatives, session['features']['track'], level='track') session['features']['track'] = list( set(session['features']['track'] + track_ids)) # /// query for the remaining tracks which are not below a tour/etappe else: tracks_contained_query = DBSession.query(Track).filter( and_( or_(func.ST_Overlaps(viewport, Track.bbox), func.ST_Contains(viewport, Track.bbox), func.ST_Contains(Track.bbox, viewport)), Track.etappe == None)) features, relatives, track_ids, tracks = maptools.query_features( tracks_contained_query, False, features, relatives, session['features']['track'], level='track') session['features']['track'] = list( set(session['features']['track'] + track_ids)) # /// RETURN FEATURES /// #track_json = Response(json.dumps(dict(type='FeatureCollection', features=features, crs=dict(type='name',properties=dict(name='urn:ogc:def:crs:OGC:1.3:CRS84'))))) log.debug(json.dumps(dict(type='FeatureCollection', features=features))) track_json = Response( json.dumps( [relatives, dict(type='FeatureCollection', features=features)])) track_json.content_type = 'application/json' return (track_json)