Example #1
0
def search_rectangle():
    rectangle = [
        Point("top_left_point", float(request.args["top_left_lat"]),
              float(request.args["top_left_long"])),
        Point("bottom_right_point", float(request.args["bottom_right_lat"]),
              float(request.args["bottom_right_long"]))
    ]
    current_point = Point("current_point", float(request.args["current_lat"]),
                          float(request.args["current_long"]))

    start = time.time()
    prefix = searcher.general_prefix_rectangle(rectangle)
    points = searcher.search_points_rectangle(
        rectangle, current_point, storage.get_points_by_pref(prefix))
    total_time = time.time() - start

    logger.info("Search with top_left_long: [{0}];"
                " top_left_lat: [{1}];"
                " bottom_right_long: [{2}];"
                " bottom_right_lat: [{3}];"
                " current_long: [{4}];"
                " current_lat: [{5}] found {6} points. Time: {7}".format(
                    rectangle[0].longitude, rectangle[0].latitude,
                    rectangle[1].longitude, rectangle[1].latitude,
                    current_point.longitude, current_point.latitude,
                    len(points), total_time))
    result = {"points": [point.to_json() for point in points]}

    return jsonify(result), 200
Example #2
0
 def test_in_or_not_in_circle(self):
     res = Search.search_points(Point(
         "", 17.43215425542, 63.3124235462342), 1000, [
             Point("1", 17.42565123123, 63.325814352343),
             Point("2", 17.42565123123, 63.325814352343)
         ])
     self.assertEqual(res, [])
Example #3
0
def search():
    longitude = float(request.args["longitude"])
    latitude = float(request.args["latitude"])
    radius = int(request.args["radius"])

    start_time = time.time()

    prefix = searcher.general_prefix(
        Point("centre_point", latitude, longitude), radius)
    points = searcher.search_points(Point("centre_point", latitude, longitude),
                                    radius, storage.get_points_by_pref(prefix))
    total_time = time.time() - start_time
    logger.info(
        "search with latitude {0} longitude {1} radius {2} found {3} points.Time: {4}"
        .format(latitude, longitude, radius, len(points), total_time))
    result = {
        "request": {
            "longitude": longitude,
            "latitude": latitude,
            "radius": radius
        },
        "points": [p.to_json() for p in points]
    }

    return jsonify(result), 200
Example #4
0
 def test_to_json(self):
     point = Point("id1", 50.45466, 30.5238)
     expected = {
         "point_id": "id1",
         "latitude": 50.45466,
         "longitude": 30.5238,
         "geohash": encode(50.45466, 30.5238)
     }
     self.assertEqual(point.to_json(), expected)
Example #5
0
 def test_from_json(self):
     json = {
         "point_id": "id",
         "latitude": 50.45466,
         "longitude": 30.5238,
         "geohash": str(encode(50.45466, 30.5238))
     }
     expected = Point("id", 50.45466, 30.5238)
     self.assertEqual(Point.from_json(json), expected)
Example #6
0
    def test_general_prefix_circle(self):
        point = Point("test_id", 19.97512724541457, 24.61693811416627)
        self.assertTrue(
            Search.general_prefix(
                Point("", 19.97512724541457, 24.61693811416627), 800) in
            point.geohash)

        self.assertEqual(
            Search.general_prefix(
                Point("", 49.98460165007597, -126.61710977554323), 500),
            "c0vuq")
        self.assertEqual(
            Search.general_prefix(
                Point("", 49.97512724541457, -126.61693811416627), 800),
            "c0vu")
        self.assertEqual(
            Search.general_prefix(
                Point("", 49.97512724541457, -126.61693811416627), 4000), "c0")
        self.assertEqual(
            Search.general_prefix(
                Point("", 49.97512724541457, -126.61693811416627), 90000), "c")

        self.assertEqual(
            Search.general_prefix(
                Point("", 32.97512724541457, -57.61693811416627), 1000),
            "dtz5")
        self.assertEqual(
            Search.general_prefix(
                Point("", 46.97512724541457, 47.61693811416627), 5000), "v03")
        self.assertEqual(
            Search.general_prefix(
                Point("", 46.97512724541457, 63.61693811416627), 3000), "v2m")
Example #7
0
    def get_point(self, point_id):
        doc = self.collection.document(point_id).get()
        if not doc.exists:
            raise errors.PointNotFound(point_id)

        point = Point.from_json(doc.to_dict())
        return point
Example #8
0
def update():
    if not request.json:
        logger.error("invalid json")
        abort(400)

    point = Point.from_json(request.json)
    is_created = storage.set_point(point)
    logger.info("Point {0} was {1}".format(
        point.point_id, "created" if is_created else "updated"))

    return jsonify(point_id=point.point_id), 201 if is_created else 200
Example #9
0
    def get_points_by_pref(self, prefix):
        docs = self.collection.where('geohash', '>=',
                                     prefix).where('geohash', '<=',
                                                   prefix + 'zzzz').stream()
        all_points = []
        for doc in docs:
            point = doc.to_dict()
            try:
                all_points.append(Point.from_json(point))
            except errors.InvalidJson:
                logger.error("found invalid point: {}".format(point))

        return all_points
Example #10
0
    def test_with_distance_circle(self):
        points_list = [
            Point("1.04", 49.98460165007597, -126.61710977554323),
            Point("2.04", 49.97512724541457, -126.61693811416627),
            Point("3.26", 49.96431118704076, -126.61401987075807),
            Point("4.22", 49.9061784189361, -126.61363363265993),
            Point("5.45", 49.86383118159863, -126.6121530532837),
            Point("6.37", 49.86499305885544, -126.61258220672609),
            Point("7.205", 49.857823724196905, -126.60713195800783)
        ]

        res = Search.search_points(
            Point("", 49.86431118704076, -126.6171530532837), 5000,
            points_list)

        expected_list = [
            PointWithDistance(points_list[5], 336),
            PointWithDistance(points_list[4], 362),
            PointWithDistance(points_list[6], 1018),
            PointWithDistance(points_list[3], 4662)
        ]

        self.assertEqual(res, expected_list)
Example #11
0
    def test_general_prefix_rectangle(self):
        actual = Search.general_prefix_rectangle([
            Point("top_left", 59.72386952131737, -113.01773071289062),
            Point("bot_right", 59.68386129364914, -112.92572021484375)
        ])
        self.assertEqual("c6xe", actual)

        actual = Search.general_prefix_rectangle([
            Point("top_left", 59.839295488500326, -112.89825439453125),
            Point("bot_right", 59.78577919120723, -112.79525756835938)
        ])
        self.assertEqual("c6x", actual)

        actual = Search.general_prefix_rectangle([
            Point("top_left", 60.2035192283986, -112.91772723197937),
            Point("bot_right", 60.20343925759669, -112.91756093502045)
        ])
        self.assertEqual("c6xwqrxy", actual)

        actual = Search.general_prefix_rectangle([
            Point("top_left", 60.13586367528046, -112.8738784790039),
            Point("bot_right", 60.13458148138504, -112.87078857421875)
        ])
        self.assertEqual("c6xwp", actual)
Example #12
0
 def test_on_empty_list_circle(self):
     self.assertEqual(
         Search.search_points(
             Point("", 46.483264729155586, 30.731506347656254), 3, []), [])
Example #13
0
    def test_with_distance_rectangle(self):
        points = [
            Point("in_rect1", 59.708114412194135, -112.99713134765625),
            Point("in_rect2", 59.692871645401674, -112.99198150634766),
            Point("in_rect3", 59.697029451864545, -112.9669189453125),
            Point("in_rect4", 59.71313607653958, -112.97309875488281),
            Point("in_rect5", 59.72213855345352, -113.00537109375),
            Point("not_in_rect1", 59.7363298459524, -112.95249938964844),
            Point("not_in_rect2", 59.6673938144924, -112.97138214111328),
            Point("not_in_rect3", 59.70361158972945, -112.88108825683594),
            Point("not_in_rect4", 59.7037847864095, -113.05103302001953),
            Point("not_in_rect5", 59.72767733532802, -113.01155090332031)
        ]

        actual = Search.search_points_rectangle([
            Point("top_left", 59.72386952131737, -113.01773071289062),
            Point("bot_right", 59.68386129364914, -112.92572021484375)
        ], Point("cur_p", 59.70222598402985, -112.9562759399414), points)

        expected_list = [
            PointWithDistance(points[2], 830),
            PointWithDistance(points[3], 1536),
            PointWithDistance(points[1], 2257),
            PointWithDistance(points[0], 2383),
            PointWithDistance(points[4], 3533)
        ]

        self.assertEqual(expected_list, actual)
Example #14
0
    def test_with_radius_increase_circle(self):
        points = [
            Point("1.00", 49.86875093132386, -126.63013458251955),
            Point("1.04", 49.86460165007597, -126.61710977554323),
            Point("2.04", 49.86512724541457, -126.61693811416627),
            Point("3.26", 49.86431118704076, -126.61401987075807),
            Point("4.22", 49.8661784189361, -126.61363363265993),
            Point("5.45", 49.86283118159863, -126.6121530532837),
            Point("6.37", 49.86399305885544, -126.61258220672609),
            Point("7.205", 49.857823724196905, -126.60713195800783)
        ]

        self.assertEqual(
            len(
                Search.search_points(
                    Point("", 49.86875093132386, -126.63013458251955), 0,
                    points)), 1)

        self.assertEqual(
            len(
                Search.search_points(
                    Point("", 49.86875093132386, -126.63013458251955), 1100,
                    points)), 3)

        self.assertEqual(
            len(
                Search.search_points(
                    Point("", 49.86875093132386, -126.63013458251955), 1300,
                    points)), 5)

        self.assertEqual(
            len(
                Search.search_points(
                    Point("", 49.86875093132386, -126.63013458251955), 1400,
                    points)), 6)

        self.assertEqual(
            len(
                Search.search_points(
                    Point("", 49.86875093132386, -126.63013458251955), 1500,
                    points)), 7)

        self.assertEqual(
            len(
                Search.search_points(
                    Point("", 49.86875093132386, -126.63013458251955), 2100,
                    points)), 8)