def track_length(track): ''' Run through the track, calculate distance in kilometers and return that. ''' kilometers = 0.0 n = 1 for tp in track[0:-1]: distance = haversine.haversine(tp['lon'], tp['lat'], track[n]['lon'], track[n]['lat']) kilometers += distance n += 1 return kilometers
def get_geoJSON(): data = json.load(bottle.request.body) # needs LOTS of error handling usertid = data.get('usertid') from_date = data.get('fromdate') to_date = data.get('todate') spacing = int(data.get('spacing', POINT_KM)) tzname = data.get('tzname', 'UTC') track = getDBdata(usertid, from_date, to_date, spacing, tzname) last_point = [None, None] collection = { 'type' : 'FeatureCollection', 'features' : [], # [ geo, <list of points> ] } geo = { 'type' : 'Feature', 'geometry' : { 'type' : 'LineString', 'coordinates' : [] }, 'properties' : { 'description' : "an OwnTracks track", # updated below }, } pointlist = [] track_coords = [] kilometers = track_length(track) # bounding box lat_min = 180 lat_max = -180 lon_min = 90 lon_max = -90 for tp in track: lat = tp['lat'] lon = tp['lon'] tst = tp['tst'] if lat > lat_max: lat_max = lat if lat < lat_min: lat_min = lat if lon > lon_max: lon_max = lon if lon < lon_min: lon_min = lon track_coords.append( [ lon, lat ] ) distance = None if last_point[0] is not None: distance = haversine.haversine(lon, lat, last_point[0], last_point[1]) if last_point[0] is None or distance > spacing: last_point = [lon, lat] point = { 'type' : 'Feature', 'geometry' : { 'type' : "Point", 'coordinates' : [lon, lat], }, 'properties' : { 'description' : "%s: %s" % (tst, tp.get('addr', 'Unknown location')) } } pointlist.append(point) geo['geometry']['coordinates'] = track_coords geo['properties']['description'] = "Track length: %.2f km" % kilometers collection['features'] = [ geo ] for p in pointlist: collection['features'].append(p) # Experiment: geofences fences = [] for f in getDBwaypoints(usertid, lat_min, lat_max, lon_min, lon_max): fence = { 'type' : 'Feature', 'geometry' : { 'type' : "Point", 'coordinates' : [f['lon'], f['lat']], }, 'properties' : { 'description' : f['name'], 'geofence': { 'radius' : f['rad'], }, } } collection['features'].append(fence) return collection
def get_geoJSON(): data = json.load(bottle.request.body) # needs LOTS of error handling usertid = data.get('usertid') from_date = data.get('fromdate') to_date = data.get('todate') spacing = int(data.get('spacing', POINT_KM)) tzname = data.get('tzname', 'UTC') track = getDBdata(usertid, from_date, to_date, spacing, tzname) last_point = [None, None] collection = { 'type': 'FeatureCollection', 'features': [], # [ geo, <list of points> ] } geo = { 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': [] }, 'properties': { 'description': "an OwnTracks track", # updated below }, } pointlist = [] track_coords = [] kilometers = track_length(track) # bounding box lat_min = 180 lat_max = -180 lon_min = 90 lon_max = -90 for tp in track: lat = tp['lat'] lon = tp['lon'] tst = tp['tst'] if lat > lat_max: lat_max = lat if lat < lat_min: lat_min = lat if lon > lon_max: lon_max = lon if lon < lon_min: lon_min = lon track_coords.append([lon, lat]) distance = None if last_point[0] is not None: distance = haversine.haversine(lon, lat, last_point[0], last_point[1]) if last_point[0] is None or distance > spacing: last_point = [lon, lat] point = { 'type': 'Feature', 'geometry': { 'type': "Point", 'coordinates': [lon, lat], }, 'properties': { 'description': "%s: %s" % (tst, tp.get('addr', 'Unknown location')) } } pointlist.append(point) geo['geometry']['coordinates'] = track_coords geo['properties']['description'] = "Track length: %.2f km" % kilometers collection['features'] = [geo] for p in pointlist: collection['features'].append(p) # Experiment: geofences fences = [] for f in getDBwaypoints(usertid, lat_min, lat_max, lon_min, lon_max): fence = { 'type': 'Feature', 'geometry': { 'type': "Point", 'coordinates': [f['lon'], f['lat']], }, 'properties': { 'description': f['name'], 'geofence': { 'radius': f['rad'], }, } } collection['features'].append(fence) return collection