Skip to content

Commit

Permalink
Create static method to transform coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
fwestenberg committed Nov 29, 2023
1 parent 427843c commit 190c4ff
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
setup(
name = 'stookwijzer',
packages = ['stookwijzer'],
version = '1.4.3',
version = '1.4.4',
license='MIT',
description = 'Stookwijzer package',
long_description_content_type="text/markdown",
Expand Down
9 changes: 7 additions & 2 deletions stookwijzer/example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
"""Example usage of the Stookwijzer API."""
import stookwijzerapi
import stookwijzerapi

if __name__ == "__main__":
sw = stookwijzerapi.Stookwijzer(52.123456, 6.123456)
x, y = stookwijzerapi.Stookwijzer.transform_coordinates(52.123456, 6.123456)
print(x)
print(y)

sw = stookwijzerapi.Stookwijzer(x, y)
sw.update()

print(sw.state)
print(sw.alert)
print(sw.windspeed_bft)
Expand Down
46 changes: 24 additions & 22 deletions stookwijzer/stookwijzerapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ def __init__(self, latitude, longitude):
self._last_updated = None
self._stookwijzer = None

@staticmethod
def transform_coordinates(latitude: float,longitude: float):
"""Transform the coordinates from EPSG:4326 to EPSG:28992."""
url = f"https://epsg.io/srs/transform/{longitude},{latitude}.json?key=default&s_srs=4326&t_srs=28992"

try:
response = requests.get(
url,
timeout=10,
)
coordinates = response.json()

return coordinates["results"][0]["x"],coordinates["results"][0]["y"]

except requests.exceptions.RequestException:
_LOGGER.error("Error requesting coordinate conversion")
return None
except KeyError:
_LOGGER.error("Received invalid response transforming coordinates")
return None

@property
def state(self):
"""Return the state."""
Expand Down Expand Up @@ -83,28 +104,9 @@ def get_forecast_at_offset(self, runtime: datetime, offset: int) -> dict:
"alert": self.get_property("alert_" + str(offset)) == '1',
}

def get_boundary_box(self, latitude: float, longitude: float) -> str | None:
"""Convert the coordinates from EPSG:4326 to EPSG:28992 and create a boundary box"""
url = f"https://epsg.io/srs/transform/{longitude},{latitude}.json?key=default&s_srs=4326&t_srs=28992"

try:
response = requests.get(
url,
timeout=10,
)
coordinates = response.json()

x = coordinates["results"][0]["z"]
y = coordinates["results"][0]["y"]

return (str(x) + "%2C" + str(y) + "%2C" + str(x + 10) + "%2C" + str(y + 10))

except requests.exceptions.RequestException:
_LOGGER.error("Error requesting coordinate conversion")
return None
except KeyError:
_LOGGER.error("Received invalid response transforming coordinates")
return None
def get_boundary_box(self, x: str, y: str) -> str | None:
"""Create a boundary box with the coordinates"""
return (str(x) + "%2C" + str(y) + "%2C" + str(x + 10) + "%2C" + str(y + 10))

def get_color(self, advice: str) -> str:
"""Convert the Stookwijzer data into a color."""
Expand Down

0 comments on commit 190c4ff

Please sign in to comment.