Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements a method to obtain air quality data for a range of coordinates bounds fixes #44 #59

Merged
merged 2 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/ozone/ozone.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import numpy
import requests
import json
import itertools
from ratelimit import limits, sleep_and_retry
from .urls import URLs

Expand Down Expand Up @@ -218,6 +219,32 @@ def _AQI_meaning(self, aqi: float) -> Tuple[str, str]:

return AQI_meaning, AQI_health_implications

def _locate_all_coordinates(
self,
lower_bound: Tuple[float, float],
upper_bound: Tuple[float, float]
) -> List[Tuple]:
"""Get all locations between two pair of coordinates

Args:
lower_bound (tuple): start location
upper_bound (tuple): end location

Returns:
list: a list of all coordinates located between lower_bound and
upper_bound
"""

coordinates_flattened: List[float] = list(itertools.chain(lower_bound, upper_bound))
latlng: str = ",".join(map(str, coordinates_flattened))
response = self._make_api_request(
f"{URLs.find_coordinates_url}bounds/?token={self.token}&latlng={latlng}"
)
if self._check_status_code(response):
data = json.loads(response.content)["data"]
coordinates: List[Tuple] = [(element['lat'], element['lon']) for element in data]
return coordinates

def get_coordinate_air(
self,
lat: float,
Expand Down Expand Up @@ -314,6 +341,31 @@ def get_multiple_coordinate_air(
df.reset_index(inplace=True, drop=True)
return self._format_output(data_format, df)

def get_range_coordinates_air(
self,
lower_bound: Tuple[float, float],
upper_bound: Tuple[float, float],
data_format: str = "df",
df: pandas.DataFrame = pandas.DataFrame(),
params: List[str] = [""]
) -> pandas.DataFrame:
"""Get air quality data for range of coordinates between lower_bound and upper_bound

Args:
lower_bound (tuple): start coordinate
upper_bound (tuple): end coordinate
data_format (str): File format. Defaults to 'df'. Choose from 'csv', 'json', 'xslx'.
df (pandas.DataFrame, optional): An existing dataframe to append the data to.
params (List[str], optional): A list of parameters to get data for.
Gets all parameters by default.

Returns:
pandas.DataFrame: The dataframe containing the data. (If you
selected another data format, this dataframe will be empty)
"""
locations = self._locate_all_coordinates(lower_bound=lower_bound, upper_bound=upper_bound)
return self.get_multiple_coordinate_air(locations, data_format=data_format, df=df, params=params)

def get_multiple_city_air(
self,
cities: List[str],
Expand Down
3 changes: 3 additions & 0 deletions src/ozone/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class URLs:
# For search for air quality measuring stations in area.
find_stations_url: str = f"{_base_url}search/"

# For Map Queries
find_coordinates_url: str = f"{_base_url}map/"


if __name__ == "__main__":
pass