Ejemplo n.º 1
0
    def test_parse_gpx_timestamp(self):
        value = "2018-07-17T13:59:21Z"
        ts = utils.string_to_datetime(value)
        self.assertIsNotNone(ts, "gpx time stamp should not be null")

        ts_str = utils.datetime_to_string(ts)
        ts_copy = utils.string_to_datetime(ts_str)
        self.assertEqual(ts, ts_copy)
Ejemplo n.º 2
0
    def from_dict(data):
        gpx_file = GpxFile()
        try:
            gpx_file.id = utils.parse_int_or_none(data["id"])
        except KeyError:
            gpx_file.id = None

        gpx_file.parsed_time = utils.string_to_datetime(data["parsed_time"])
        gpx_file.gpx_timestamp = utils.string_to_datetime(
            data["gpx_timestamp"])
        gpx_file.name = data["name"]
        gpx_file.points = [GpxPoint.from_dict(x) for x in data["points"]]
        gpx_file.user_id = data["user_id"]
        gpx_file.hash = data["hash"]

        return gpx_file
Ejemplo n.º 3
0
 def test_str(self):
     now = point_utils.now()
     print(now)
     now_string = point_utils.datetime_to_string(now)
     now_time = point_utils.string_to_datetime(now_string)
     print(now_string)
     self.assertEqual(now, now_time, "expected dates to parse and un-parse")
Ejemplo n.º 4
0
    def from_dict(data: typing.Dict):
        d = data
        date_keys = ["file_gpx_timestamp", "file_parsed_time", "time"]
        for k in date_keys:
            try:
                d[k] = utils.string_to_datetime(d[k])
            except KeyError:
                d[k] = None

        return GpxFlatPoint(**d)
Ejemplo n.º 5
0
    def from_dict(data):
        created_at_string = data["created"]
        created_at = p_utils.string_to_datetime(created_at_string)

        point_set = PointSet()
        point_set.name = data["name"]
        point_set.id = data["id"]
        point_set.created = created_at
        point_set.points = [point.Point.from_dict(p) for p in data["points"]]

        return point_set
Ejemplo n.º 6
0
def parse_gpx_file(file_name: str) -> GpxFile:
    """parse_gpx_file file_name"""
    with open(file_name) as input_stream:
        xml_data = input_stream.read(-1)
        xml_hash = utils.md5_hash(xml_data)
        xml_dict = xmltodict.parse(xml_data)
        gpx = xml_dict["gpx"]

        time_created = gpx["metadata"]["time"]
        track_data = gpx["trk"]
        if type(track_data) != list:
            track_data = [track_data]

        all_points = []

        for current_track in track_data:
            track_segments = current_track["trkseg"]
            for segment in track_segments:
                if type(segment) == collections.OrderedDict:
                    point_list = segment["trkpt"]
                    for point in point_list:
                        extensions = point["extensions"][
                            "gpxtpx:TrackPointExtension"]
                        course = extensions["gpxtpx:course"]
                        p = GpxPoint()
                        p.longitude = utils.parse_float_or_none(point["@lon"])
                        p.latitude = utils.parse_float_or_none(point["@lat"])
                        p.elevation = utils.parse_float_or_none(point["ele"])
                        p.time = utils.string_to_datetime(point["time"])
                        p.course = utils.parse_float_or_none(course)
                        all_points.append(p)

        gpx_file = GpxFile()
        gpx_file.name = utils.get_file_name_from_path(file_name)
        gpx_file.parsed_time = utils.now()
        gpx_file.gpx_timestamp = utils.string_to_datetime(time_created)
        gpx_file.points = all_points
        gpx_file.hash = xml_hash
        return gpx_file
Ejemplo n.º 7
0
    def from_dict(data: typing.Dict):
        p = GpxPoint()
        try:
            p.id = data["id"]
        except KeyError:
            p.id = None

        p.time = utils.string_to_datetime(data["time"])
        p.latitude = utils.parse_float_or_none(data["latitude"])
        p.longitude = utils.parse_float_or_none(data["longitude"])
        try:
            p.gpx_file_id = utils.parse_int_or_none(data["gpx_file_id"])
        except KeyError:
            p.gpx_file_id = None

        p.course = utils.parse_float_or_none(data["course"])
        p.elevation = utils.parse_float_or_none(data["elevation"])

        return p
Ejemplo n.º 8
0
    def test_xmltodict(self):
        to_parse = os.environ["DATA_FILE"]
        with open(to_parse) as input_stream:
            xml_data = input_stream.read(-1)
            xml_dict = xmltodict.parse(xml_data)
            self.assertGreaterEqual(
                len(xml_dict), 0,
                "expected xml dictionary to be greater than 0")

            gpx = xml_dict["gpx"]

            time_created = gpx["metadata"]["time"]

            track_data = gpx["trk"]
            segment_name = track_data["name"]

            print("time created: {}".format(time_created))
            print("segment name: {}".format(segment_name))

            track_segments = track_data["trkseg"]

            all_points = []
            for segment in track_segments:
                point_list = segment["trkpt"]
                for point in point_list:
                    extensions = point["extensions"][
                        "gpxtpx:TrackPointExtension"]
                    course = extensions["gpxtpx:course"]
                    p = gpx_point.GpxPoint()
                    p.longitude = point["@lon"]
                    p.latitude = point["@lat"]
                    p.elevation = point["ele"]
                    p.time = utils.string_to_datetime(point["time"])
                    p.course = course
                    all_points.append(p)

            print("number of points parsed {}".format(len(all_points)))
            print(all_points[0])
Ejemplo n.º 9
0
    def test_point_set_to_dict(self):
        ps = point_set.PointSet()
        ps.created = point_utils.now()
        ps.name = "Test Case"
        ps.id = 448

        point_one = point.Point.from_dict({
            "x": 409.39,
            "y": 348.44,
            "z": 54.33,
            "point_set_id": 448
        })

        ps.points = [point_one]

        data = ps.to_json_dict()
        date_string = data["created"]
        date_time = point_utils.string_to_datetime(date_string)

        self.assertEqual(data["id"], ps.id, "expected the id to be the same")
        self.assertEqual(data["name"], ps.name,
                         "expected the name to be the same")
        self.assertEqual(date_time, ps.created,
                         "expected created at date to match")