|
| 1 | +# Author: Kelvin Lai <kelvin@firststreet.org> |
| 2 | +# Copyright: This module is owned by First Street Foundation |
| 3 | + |
| 4 | +# Standard Imports |
| 5 | +import logging |
| 6 | + |
| 7 | +# Internal Imports |
| 8 | +from firststreet.api import csv_format |
| 9 | +from firststreet.api.api import Api |
| 10 | +from firststreet.errors import InvalidArgument |
| 11 | +from firststreet.models.economic import AVMProperty, AVMProvider, AALSummaryProperty, AALSummaryOther, NFIPPremium |
| 12 | + |
| 13 | + |
| 14 | +class AAL(Api): |
| 15 | + """This class receives a list of search_items and handles the creation of an aal product from the request. |
| 16 | +
|
| 17 | + Methods: |
| 18 | + get_summary: Retrieves a list of AAL Summary for the given list of IDs |
| 19 | + """ |
| 20 | + |
| 21 | + def get_summary(self, search_items, location_type, csv=False, output_dir=None, extra_param=None): |
| 22 | + """Retrieves AAL summary product data from the First Street Foundation API given a list of search_items and |
| 23 | + returns a list of AAL Summary objects. |
| 24 | +
|
| 25 | + Args: |
| 26 | + search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a |
| 27 | + file of First Street Foundation IDs |
| 28 | + location_type (str): The location lookup type |
| 29 | + csv (bool): To output extracted data to a csv or not |
| 30 | + output_dir (str): The output directory to save the generated csvs |
| 31 | + extra_param (dict): Extra parameter to be added to the url |
| 32 | +
|
| 33 | + Returns: |
| 34 | + A list of AAL Summary |
| 35 | + Raises: |
| 36 | + InvalidArgument: The location provided is empty |
| 37 | + TypeError: The location provided is not a string |
| 38 | + """ |
| 39 | + |
| 40 | + if not location_type: |
| 41 | + raise InvalidArgument(location_type) |
| 42 | + elif not isinstance(location_type, str): |
| 43 | + raise TypeError("location is not a string") |
| 44 | + |
| 45 | + # Get data from api and create objects |
| 46 | + if extra_param and "depths" in extra_param: |
| 47 | + extra_param["depths"] = ','.join(map(str, extra_param["depths"])) |
| 48 | + |
| 49 | + api_datas = self.call_api(search_items, "economic/aal", "summary", location_type, extra_param=extra_param) |
| 50 | + |
| 51 | + product = [] |
| 52 | + for api_data, fsid in api_datas: |
| 53 | + api_data["fsid"] = fsid |
| 54 | + |
| 55 | + if location_type == "property": |
| 56 | + product.append(AALSummaryProperty(api_data)) |
| 57 | + else: |
| 58 | + product.append(AALSummaryOther(api_data)) |
| 59 | + |
| 60 | + if csv: |
| 61 | + csv_format.to_csv(product, "economic_aal", "summary", location_type, output_dir=output_dir) |
| 62 | + |
| 63 | + logging.info("AAL Summary Data Ready.") |
| 64 | + |
| 65 | + return product |
| 66 | + |
| 67 | + |
| 68 | +class AVM(Api): |
| 69 | + """This class receives a list of search_items and handles the creation of an AVM product from the request. |
| 70 | +
|
| 71 | + Methods: |
| 72 | + get_avm: Retrieves a list of AVM for the given list of IDs |
| 73 | + get_provider: Retrieves a list of AVM providers for the given list of IDs |
| 74 | + """ |
| 75 | + |
| 76 | + def get_avm(self, search_items, csv=False, output_dir=None, extra_param=None): |
| 77 | + """Retrieves AVM product data from the First Street Foundation API given a list of search_items and |
| 78 | + returns a list of AVM objects. |
| 79 | +
|
| 80 | + Args: |
| 81 | + search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a |
| 82 | + file of First Street Foundation IDs |
| 83 | + csv (bool): To output extracted data to a csv or not |
| 84 | + output_dir (str): The output directory to save the generated csvs |
| 85 | + extra_param (dict): Extra parameter to be added to the url |
| 86 | +
|
| 87 | + Returns: |
| 88 | + A list of AVM |
| 89 | + """ |
| 90 | + # Get data from api and create objects |
| 91 | + api_datas = self.call_api(search_items, "economic", "avm", "property", extra_param=extra_param) |
| 92 | + |
| 93 | + product = [AVMProperty(api_data) for api_data in api_datas] |
| 94 | + |
| 95 | + if csv: |
| 96 | + csv_format.to_csv(product, "economic_avm", "avm", "property", output_dir=output_dir) |
| 97 | + |
| 98 | + logging.info("AVM Data Ready.") |
| 99 | + |
| 100 | + return product |
| 101 | + |
| 102 | + def get_provider(self, search_items, csv=False, output_dir=None, extra_param=None): |
| 103 | + """Retrieves AVM provider product data from the First Street Foundation API given a list of search_items and |
| 104 | + returns a list of AVM provider objects. |
| 105 | +
|
| 106 | + Args: |
| 107 | + search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a |
| 108 | + file of First Street Foundation IDs |
| 109 | + csv (bool): To output extracted data to a csv or not |
| 110 | + output_dir (str): The output directory to save the generated csvs |
| 111 | + extra_param (dict): Extra parameter to be added to the url |
| 112 | +
|
| 113 | + Returns: |
| 114 | + A list of AVM Provider |
| 115 | + """ |
| 116 | + # Get data from api and create objects |
| 117 | + api_datas = self.call_api(search_items, "economic/avm", "provider", extra_param=extra_param) |
| 118 | + |
| 119 | + product = [AVMProvider(api_data) for api_data in api_datas] |
| 120 | + |
| 121 | + if csv: |
| 122 | + csv_format.to_csv(product, "economic_avm", "provider", output_dir=output_dir) |
| 123 | + |
| 124 | + logging.info("AVM Provider Data Ready.") |
| 125 | + |
| 126 | + return product |
| 127 | + |
| 128 | + |
| 129 | +class Economic(Api): |
| 130 | + """This class receives a list of search_items and handles the creation of an economic product from the request. |
| 131 | +
|
| 132 | + Methods: |
| 133 | + get_property_nfip: Retrieves a list of property nfip premiums for the given list of IDs |
| 134 | + """ |
| 135 | + |
| 136 | + def get_property_nfip(self, search_items, csv=False, output_dir=None, extra_param=None): |
| 137 | + """Retrieves AVM product data from the First Street Foundation API given a list of search_items and |
| 138 | + returns a list of AVM objects. |
| 139 | +
|
| 140 | + Args: |
| 141 | + search_items (list/file): A First Street Foundation IDs, lat/lng pair, address, or a |
| 142 | + file of First Street Foundation IDs |
| 143 | + csv (bool): To output extracted data to a csv or not |
| 144 | + output_dir (str): The output directory to save the generated csvs |
| 145 | + extra_param (dict): Extra parameter to be added to the url |
| 146 | +
|
| 147 | + Returns: |
| 148 | + A list of property NFIP premiums |
| 149 | + """ |
| 150 | + # Get data from api and create objects |
| 151 | + api_datas = self.call_api(search_items, "economic", "nfip", "property", extra_param=extra_param) |
| 152 | + |
| 153 | + product = [NFIPPremium(api_data) for api_data in api_datas] |
| 154 | + |
| 155 | + if csv: |
| 156 | + csv_format.to_csv(product, "economic", "nfip", "property", output_dir=output_dir) |
| 157 | + |
| 158 | + logging.info("NFIP Premium Data Ready.") |
| 159 | + |
| 160 | + return product |
0 commit comments