Exemple #1
0
    def test_json(self):
        g = map_point.GpxFile()
        g.id = 34
        g.gpx_timestamp = utils.now()
        g.parsed_time = utils.now()
        g.name = "fart"

        p = map_point.GpxPoint.from_dict({
            "id":
            35,
            "elevation":
            35.35,
            "longitude":
            98.4,
            "latitude":
            98.2,
            "course":
            998.2,
            "time":
            utils.datetime_to_string(utils.now()),
        })
        g.points = [p]

        g2 = map_point.GpxFile.from_json_string(utils.to_json(g))

        self.assertDictEqual(g.to_json_dict(), g2.to_json_dict())
Exemple #2
0
def main():
    config = pg_config.Config.from_env(os.environ)
    session = pg.connect(
        "host={host} user={username} password={password} dbname={database}".
        format_map({
            "host": config.host,
            "username": config.username,
            "password": config.password,
            "database": config.database
        }))
    dao = Dao(session)

    ps = point_set.PointSet()
    ps.name = "Test Point Set"
    ps.created = utils.now()
    point_list = create_random_points(100, 100)
    ps.points = point_list

    try:
        ps = dao.save_point_set(ps)
    except Exception as e:
        sys.stderr.write("error saving point_set: {}\n".format(e, ))
        os.exit(1)

    print(str(ps))

    try:
        ps = dao.get_point_set_by_id(ps.id)
    except Exception as e:
        sys.stderr.write("error getting point_set of id = {}: {}\n".format(
            ps.id, e))
        os.exit(1)

    print(str(ps))
Exemple #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")
Exemple #4
0
 def test_from_dict(self):
     d = {
         "id": 35,
         "elevation": 35.35,
         "longitude": 98.4,
         "latitude": 98.2,
         "course": 998.2,
         "time": utils.datetime_to_string(utils.now()),
         "gpx_file_id": 5,
     }
     p = map_point.GpxPoint.from_dict(d)
     self.assertDictEqual(d, p.to_json_dict())
def main():
    config = dao_config.Config.from_env(os.environ)
    dao = dao_module.create_dao(config)

    ps = point_set.PointSet()
    ps.points = create_random_points(10, 10)
    ps.created = utils.now()
    ps.name = "Hello SQLAlchemy"

    ps = dao.save_or_update_point_set(ps)
    ps = dao.get_point_set_by_id(ps.id)
    print(utils.to_json(ps))
Exemple #6
0
    def test_map_point_json(self):
        p = map_point.GpxPoint()

        p.id = 45
        p.elevation = 34.54
        p.gpx_file_id = 2
        p.latitude = 534.55
        p.longitude = 445.334
        p.course = 34.43
        p.time = utils.now()

        json = utils.to_json(p)

        p2 = map_point.GpxPoint.from_json_string(json)

        self.assertDictEqual(p.to_json_dict(), p2.to_json_dict())
Exemple #7
0
def main():
    point_set = ps.PointSet()
    point_set.name = "My Point Set"
    point_set.id = 40
    point_set.created = point_utils.now()

    for i in range(0, 10):
        point = p.Point()
        point.point_set_id = point_set.id
        point.x = random.random()
        point.y = random.random()
        point.x = random.random()
        point_set.points.append(point)

    #sys.stdout.write(str(point_set))
    sys.stdout.write(point_utils.to_json(point_set))
Exemple #8
0
    def test_json(self):
        n = utils.now()
        flat_point = map_point.GpxFlatPoint()
        flat_point.course = 1.0
        flat_point.longitude = 33.2
        flat_point.latitude = 228.3
        flat_point.elevation = 23.2
        flat_point.id = utils.create_uuid_str()
        flat_point.file_parsed_time = n
        flat_point.file_gpx_timestamp = n
        flat_point.time = n
        flat_point.file_id = 3
        flat_point.file_user_id = utils.create_uuid_str()
        flat_point.file_hash = "file_hash"
        flat_point.file_name = "file_name"

        json_data = utils.to_json(flat_point)
        flat_point2 = map_point.GpxFlatPoint.from_json_string(json_data)

        self.assertDictEqual(
            flat_point.__dict__, flat_point2.__dict__,
            "expected flat point to serialize in and out of json as the same")
Exemple #9
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
Exemple #10
0
    def test_point_set_from_dict(self):
        now = point_utils.now()
        dict_data = {
            "id":
            34,
            "created":
            point_utils.datetime_to_string(now),
            "name":
            "Test Case Point Set Is Fantastic",
            "points": [{
                "x": 3.4,
                "y": 4.2,
                "z": 54.3,
                "point_set_id": 34
            }, {
                "x": 3.6,
                "y": 4.3,
                "z": 54.5,
                "point_set_id": 34
            }]
        }

        ps = point_set.PointSet.from_dict(dict_data)

        self.assertEqual(ps.id, 34, "expected id to match")
        self.assertEqual(ps.name, "Test Case Point Set Is Fantastic",
                         "expected name to match")
        self.assertEqual(ps.created, now, "expected created to match")
        self.assertEqual(ps.number_of_points(), 2,
                         "expected point count to match")
        self.assertEqual([p.point_set_id for p in ps.points], [34, 34],
                         "expected the point set ids to match")
        self.assertEqual([p.x for p in ps.points], [3.4, 3.6],
                         "expected the x values to match")
        self.assertEqual([p.y for p in ps.points], [4.2, 4.3],
                         "expected the y values to match")
        self.assertEqual([p.z for p in ps.points], [54.3, 54.5],
                         "expected the z values to match")
Exemple #11
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")