def post(self):
        json_data = request.get_json(force=True)

        database_name = "gmp_db"
        collection_gmp = "gmp_locations"

        conn = db_connector.Connection()
        client = conn.getConnection()
        db = client[database_name]
        collection_maps = db[collection_gmp]

        cursor = collection_maps.insert({"location": json_data})

        return 'Success'
Exemple #2
0
    def get(self, loc_id, source, destination, random_number):
        logger.info(
            " Request for shortest path came with location id: %s ,source id:%s and desination id :%s ",
            str(loc_id), source, destination)

        shortest_path_with_coordinates = generate_all_possible_paths.get_path(
            loc_id, int(source), int(destination))
        logger.info("Responded with : %s", shortest_path_with_coordinates)
        print(source, destination)
        print(shortest_path_with_coordinates)

        database_name = "gmp_db"
        collection_gmp = "gmp_locations"

        conn = db_connector.Connection()
        client = conn.getConnection()
        db = client[database_name]
        collection_locations = db[collection_gmp]
        cursor = collection_locations.find_one({"_id": ObjectId(loc_id)})

        locationObj = cursor["location"]

        imageString = locationObj["map"]

        im = Image.open(BytesIO(b64decode(imageString.split(',')[1])))
        draw = ImageDraw.Draw(im)

        lengthOfCoordinatesArray = len(shortest_path_with_coordinates)
        for arrayIndex in range(lengthOfCoordinatesArray):
            if arrayIndex == 0:
                pass
            else:
                draw.line(
                    ((shortest_path_with_coordinates[arrayIndex - 1]['x'],
                      shortest_path_with_coordinates[arrayIndex - 1]['y']),
                     (shortest_path_with_coordinates[arrayIndex]['x'],
                      shortest_path_with_coordinates[arrayIndex]['y'])),
                    fill=(0, 192, 192),
                    width=8)
                if arrayIndex == lengthOfCoordinatesArray - 1:
                    pinImage = Image.open('destinationPin.png')
                    im.paste(pinImage,
                             (shortest_path_with_coordinates[arrayIndex]['x'],
                              shortest_path_with_coordinates[arrayIndex]['y']))

        im.save("MapWithRoute.png")
        return send_file("MapWithRoute.png",
                         mimetype='image/jpg',
                         attachment_filename='python.jpg')
Exemple #3
0
    def get(self, loc_id, pos_id):
        database_name = "gmp_db"
        collection_gmp = "gmp_locations"

        conn = db_connector.Connection()
        client = conn.getConnection()
        db = client[database_name]
        collection_locations = db[collection_gmp]
        cursor = collection_locations.find_one({"_id": ObjectId(loc_id)})

        locationObj = cursor["location"]

        json_response = {}
        location = {}
        location["locationId"] = str(cursor["_id"])
        location["locationName"] = locationObj["location_name"]
        json_response["location"] = location

        destinations = []
        for document in locationObj["positions"]:
            position_name = document["position_name"]
            position_id = document["position_id"]
            is_destination = document["destination"]
            if position_id != int(pos_id) and "N" != is_destination:
                destination = {}
                destination["positionId"] = position_id
                destination["positionName"] = position_name
                destinations.append(destination)
            elif position_id == int(pos_id) and "N" != is_destination:
                source = {}
                source["positionId"] = position_id
                source["positionName"] = position_name
                json_response["source"] = source

        try:
            amenitiesFromDb = locationObj["amenities"]
            amenities = []
            for document in amenitiesFromDb:
                amenity = {}
                amenity["positionId"] = document["position_id"]
                amenity["positionName"] = document["position_name"]
                amenities.append(amenity)
            json_response["amenities"] = amenities
        except KeyError:
            pass

        json_response["detinations"] = destinations

        return json_response
Exemple #4
0
def get_preferred_path(loc_id, source_pid, destination_pid, path_type):
    final_paths.clear()
    database_name = "gmp_db"
    collection_gmp = "gmp_locations"
    try:
        conn = db_connector.Connection()
        client = conn.getConnection()
        db = client[database_name]
        collection_locations = db[collection_gmp]
        cursor = collection_locations.find_one({"_id": ObjectId(loc_id)})
        locationObj = cursor["location"]

        global positions_rel_dict, positions_coordinats_dict, position_id_names_dict, final_path
        positions_rel_dict = {}
        positions_coordinats_dict = {}
        position_id_names_dict = {}
        final_path = str(None)

        for document in locationObj["positions"]:
            position_relations = document["position_relations"]
            relations_array = []
            for position_rel in position_relations:
                relations_array.append(position_rel["related_position_id"])
            positions_rel_dict[document["position_id"]] = relations_array
            position_id_names_dict[
                document["position_id"]] = document["position_name"]
            positions_coordinats_dict[
                document["position_id"]] = document["position_coordinates"]

        final_path = str(source_pid) + appender
        get_path_to_destination(source_pid, destination_pid, final_path,
                                [source_pid])

        if path_type == 'L':
            path = get_longest_path(final_paths, positions_coordinats_dict)
        else:
            path = get_shortest_path(final_paths, positions_coordinats_dict)

        path_with_coordinates = shortest_path_coordinates(
            path, positions_coordinats_dict)

        return path_with_coordinates

    except Exception as e:
        logger.error("Error while getting shortest path  with trace back :%s",
                     e)
    finally:
        conn.closeConnection()
Exemple #5
0
    def get(self):
        database_name = "gmp_db"
        collection_gmp = "gmp_locations"

        client = db_connector.Connection()
        connection = client.getConnection()
        database = connection[database_name]
        collection = database[collection_gmp]

        cursor = collection.find({})

        response = {}
        locations = []

        for document in cursor:
            location = {}
            db_location = document["location"]
            location["location_id"] = str(document["_id"])
            location["location_name"] = db_location["location_name"]
            locations.append(location)

        response["locations"] = locations
        return response