|
| 1 | +# Author: Kelvin Lai <kelvin@firststreet.org> |
| 2 | +# Copyright: This module is owned by First Street Foundation |
| 3 | + |
| 4 | +# Standard Imports |
| 5 | +import datetime |
| 6 | +import logging |
| 7 | + |
| 8 | +# Internal Imports |
| 9 | +import os |
| 10 | + |
| 11 | +from firststreet.api.api import Api |
| 12 | +from firststreet.errors import InvalidArgument |
| 13 | +from firststreet.models.tile import ProbabilityDepthTile, HistoricEventTile |
| 14 | + |
| 15 | + |
| 16 | +class Tile(Api): |
| 17 | + """This class receives a list of coordinate and parameters, and handles the return of a tile from the request. |
| 18 | +
|
| 19 | + Methods: |
| 20 | + get_probability_depth: Retrieves a list of Probability Depth for the given list of IDs |
| 21 | + get_historic_event: Retrieves a list of Probability Depth for the given list of IDs |
| 22 | + """ |
| 23 | + |
| 24 | + def get_probability_depth(self, year, return_period, coordinate, image=False, connection_limit=100, |
| 25 | + output_dir=None, extra_param=None): |
| 26 | + """Retrieves probability depth tile data from the First Street Foundation API given a list of search_items |
| 27 | + and returns a list of Probability Depth Tile objects. |
| 28 | +
|
| 29 | + Args: |
| 30 | + year (int): The year to get the tile |
| 31 | + return_period (int): The return period to get the tile |
| 32 | + coordinate (list of tuple): A list of coordinates in the form of [(x_1, y_1, z_1), (x_2, y_2, z_2), ...] |
| 33 | + image (bool): To output extracted image to a png or not |
| 34 | + connection_limit (int): max number of connections to make |
| 35 | + output_dir (str): The output directory to save the generated tile |
| 36 | + extra_param (str): Extra parameter to be added to the url |
| 37 | +
|
| 38 | + Returns: |
| 39 | + A list of Probability Depth tiles |
| 40 | + """ |
| 41 | + |
| 42 | + if not year: |
| 43 | + raise InvalidArgument(year) |
| 44 | + elif not isinstance(year, int): |
| 45 | + raise TypeError("year is not an int") |
| 46 | + elif year not in [2020, 2035, 2050]: |
| 47 | + logging.error("Year provided is not one of: 2020, 2035, 2050") |
| 48 | + raise InvalidArgument(year) |
| 49 | + |
| 50 | + if not return_period: |
| 51 | + raise InvalidArgument(return_period) |
| 52 | + elif not isinstance(return_period, int): |
| 53 | + raise TypeError("return period is not an int") |
| 54 | + elif return_period not in [500, 100, 20, 5, 2]: |
| 55 | + logging.error("Year provided is not one of: 500, 100, 20, 5, 2. " |
| 56 | + "(2 year return period is only available for coastal areas.)") |
| 57 | + raise InvalidArgument(year) |
| 58 | + |
| 59 | + # Get data from api and create objects |
| 60 | + api_datas = self.call_api(coordinate, "tile", "probability", tile_product="depth", year=year, |
| 61 | + return_period=return_period, connection_limit=connection_limit, |
| 62 | + extra_param=extra_param) |
| 63 | + |
| 64 | + if image: |
| 65 | + for data in api_datas: |
| 66 | + if data: |
| 67 | + date = datetime.datetime.today().strftime('%Y_%m_%d_%H_%M_%S') |
| 68 | + |
| 69 | + # Set file name to the current date, time, and product |
| 70 | + file_name = "_".join([date, "probability_depth", str(year), str(return_period), |
| 71 | + str(data.get("coordinate"))]) + ".png" |
| 72 | + |
| 73 | + if not output_dir: |
| 74 | + output_dir = os.getcwd() + "/data_csv" |
| 75 | + |
| 76 | + if not os.path.exists(output_dir): |
| 77 | + os.makedirs(output_dir) |
| 78 | + |
| 79 | + with open(output_dir + '/' + file_name, "wb") as f: |
| 80 | + f.write(data['image']) |
| 81 | + |
| 82 | + logging.info("Images generated to '{}'.".format(output_dir)) |
| 83 | + |
| 84 | + product = [ProbabilityDepthTile(api_data, year, return_period) for api_data in api_datas] |
| 85 | + |
| 86 | + logging.info("Probability Depth Tile Ready.") |
| 87 | + |
| 88 | + return product |
| 89 | + |
| 90 | + def get_historic_event(self, event_id, coordinate, image=False, connection_limit=100, |
| 91 | + output_dir=None, extra_param=None): |
| 92 | + """Retrieves historic event tile data from the First Street Foundation API given a list of search_items |
| 93 | + and returns a list of Historic Event Tile objects. |
| 94 | +
|
| 95 | + Args: |
| 96 | + event_id (int): A First Street Foundation eventId |
| 97 | + coordinate (list of tuple): A list of coordinates in the form of [(x_1, y_1, z_1), (x_2, y_2, z_2), ...] |
| 98 | + image (bool): To output extracted image to a png or not |
| 99 | + connection_limit (int): max number of connections to make |
| 100 | + output_dir (str): The output directory to save the generated tile |
| 101 | + extra_param (str): Extra parameter to be added to the url |
| 102 | +
|
| 103 | + Returns: |
| 104 | + A list of Probability Count |
| 105 | + Raises: |
| 106 | + InvalidArgument: The location provided is empty |
| 107 | + TypeError: The location provided is not a string |
| 108 | + """ |
| 109 | + |
| 110 | + if not event_id: |
| 111 | + raise InvalidArgument(event_id) |
| 112 | + elif not isinstance(event_id, int): |
| 113 | + raise TypeError("event id is not an int") |
| 114 | + |
| 115 | + # Get data from api and create objects |
| 116 | + api_datas = self.call_api(coordinate, "tile", "historic", tile_product="event", event_id=event_id, |
| 117 | + connection_limit=connection_limit, extra_param=extra_param) |
| 118 | + |
| 119 | + if image: |
| 120 | + for data in api_datas: |
| 121 | + if data: |
| 122 | + date = datetime.datetime.today().strftime('%Y_%m_%d_%H_%M_%S') |
| 123 | + |
| 124 | + # Set file name to the current date, time, and product |
| 125 | + file_name = "_".join([date, "historic_event", str(event_id), str(data.get("coordinate"))]) + ".png" |
| 126 | + |
| 127 | + if not output_dir: |
| 128 | + output_dir = os.getcwd() + "/data_csv" |
| 129 | + |
| 130 | + if not os.path.exists(output_dir): |
| 131 | + os.makedirs(output_dir) |
| 132 | + |
| 133 | + with open(output_dir + '/' + file_name, "wb") as f: |
| 134 | + f.write(data.get("image")) |
| 135 | + |
| 136 | + logging.info("Images generated to '{}'.".format(output_dir)) |
| 137 | + |
| 138 | + product = [HistoricEventTile(api_data, event_id) for api_data in api_datas] |
| 139 | + |
| 140 | + logging.info("Historic Event Tile Ready.") |
| 141 | + |
| 142 | + return product |
0 commit comments