def section_to_geojson(section, tl): """ This is the trickiest part of the visualization. The section is basically a collection of points with a line through them. So the representation is a feature in which one feature which is the line, and one feature collection which is the set of point features. :param section: the section to be converted :return: a feature collection which is the geojson version of the section """ ts = esta.TimeSeries.get_time_series(section.user_id) entry_it = ts.find_entries(["analysis/recreated_location"], esda.get_time_query_for_trip_like( "analysis/cleaned_section", section.get_id())) # TODO: Decide whether we want to use Rewrite to use dataframes throughout instead of python arrays. # dataframes insert nans. We could use fillna to fill with default values, but if we are not actually # using dataframe features here, it is unclear how much that would help. feature_array = [] section_location_entries = [ecwe.Entry(entry) for entry in entry_it] if len(section_location_entries) != 0: logging.debug("first element in section_location_array = %s" % section_location_entries[0]) if not ecc.compare_rounded_arrays(section.data.end_loc.coordinates, section_location_entries[-1].data.loc.coordinates, digits=4): logging.info("section_location_array[-1].data.loc %s != section.data.end_loc %s even after df.ts fix, filling gap" % \ (section_location_entries[-1].data.loc, section.data.end_loc)) assert(False) last_loc_doc = ts.get_entry_at_ts("background/filtered_location", "data.ts", section.data.end_ts) if last_loc_doc is None: logging.warning("can't find entry to patch gap, leaving gap") else: last_loc_entry = ecwe.Entry(last_loc_doc) logging.debug("Adding new entry %s to fill the end point gap between %s and %s" % (last_loc_entry.data.loc, section_location_entries[-1].data.loc, section.data.end_loc)) section_location_entries.append(last_loc_entry) points_line_feature = point_array_to_line(section_location_entries) points_line_feature.id = str(section.get_id()) points_line_feature.properties.update(copy.copy(section.data)) # Update works on dicts, convert back to a section object to make the modes # work properly points_line_feature.properties = ecwcs.Cleanedsection(points_line_feature.properties) points_line_feature.properties["feature_type"] = "section" points_line_feature.properties["sensed_mode"] = str(points_line_feature.properties.sensed_mode) _del_non_derializable(points_line_feature.properties, ["start_loc", "end_loc"]) # feature_array.append(gj.FeatureCollection(points_feature_array)) feature_array.append(points_line_feature) return gj.FeatureCollection(feature_array)
def get_filtered_section(new_trip_entry, section): """ Save the filtered points associated with this section. Do I need a cleaned section as well? At first glance, it doesn't seem necessary, since all sections are valid. But don't we need a cleaned section that has a trip id == the cleaned trip. Otherwise, we need to modify the trip_id of the raw section, which means that this is no longer a progressive system that we can recreate. So let's create a new entry for the cleaned section that is identical to the old section, but pointing to the cleaned trip. :param ts: the timeseries for this user :param trip: the trip that this section is a part of :param section: the section that we are saving :return: None """ filtered_section_data = ecwcs.Cleanedsection() _copy_non_excluded(old_data=section.data, new_data=filtered_section_data, excluded_list=filtered_section_excluded) filtered_section_data.trip_id = new_trip_entry.get_id() # Now that we have constructed the section object, we have to filter and # store the related data points with_speeds_df = get_filtered_points(section, filtered_section_data) speeds = list(with_speeds_df.speed) distances = list(with_speeds_df.distance) filtered_section_data.speeds = speeds filtered_section_data.distances = distances filtered_section_data.distance = sum(distances) filtered_section_entry = ecwe.Entry.create_entry(section.user_id, esda.CLEANED_SECTION_KEY, filtered_section_data, create_id=True) ts = esta.TimeSeries.get_time_series(section.user_id) for row in with_speeds_df.to_dict('records'): loc_row = ecwrl.Recreatedlocation(row) loc_row.mode = section.data.sensed_mode loc_row.section = filtered_section_entry.get_id() ts.insert_data(section.user_id, esda.CLEANED_LOCATION_KEY, loc_row) return filtered_section_entry
def testCalAccelsWithThreePoints(self): testSec = ecwcs.Cleanedsection() testSec.speeds = [3, 6] print(fc.calAccels(testSec)) self.assertEqual(fc.calAccels(testSec).tolist(), [3 / 30, 3 / 30])
def testCalAccelsWithTwoPoints(self): testSec = ecwcs.Cleanedsection() testSec.speeds = [3] self.assertEqual(fc.calAccels(testSec).tolist(), [0.1])
def testCalAccelsWithOnePoint(self): testSec = ecwcs.Cleanedsection() testSec.speeds = [] self.assertEqual(fc.calAccels(testSec), None)
def testCalSpeedsWithTwoPoints(self): testSec = ecwcs.Cleanedsection() testSec.speeds = [5] self.assertEqual(fc.calSpeeds(testSec), [5])
def testCalSpeedsWithZeroTime(self): testSec = ecwcs.Cleanedsection() testSec.speeds = [] self.assertEqual(fc.calSpeeds(testSec), [])