Skip to content

Commit

Permalink
Add park and ride dataset (#6)
Browse files Browse the repository at this point in the history
* Add park and ride dataset

* Update the documentation

* Update test file
  • Loading branch information
klaasnicolaas authored Jul 13, 2022
1 parent 33d17bc commit 73c86c2
Show file tree
Hide file tree
Showing 11 changed files with 561 additions and 17 deletions.
1 change: 1 addition & 0 deletions .codespell-ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
adresse
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ repos:
types: [text]
exclude: ^poetry\.lock$
entry: poetry run codespell
args: [--ignore-words=.codespell-ignore]
- id: debug-statements
name: 🪵 Debug Statements and imports (Python)
language: system
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ A python package with which you can retrieve data from the Urban Data Platform o
pip install hamburg
```

## Data

You can read the following data with this package:

- Disabled parking spaces (Behindertenstellplätze)
- Park and ride occupancy (Park + Ride Anlagen)

## Usage

There are a number of variables you can set to retrieve the data:
Expand All @@ -50,20 +57,16 @@ from hamburg import UDPHamburg
async def main() -> None:
"""Show example on using the Parking Hamburg API client."""
async with UDPHamburg() as client:
locations = await client.disabled_parkings()
print(locations)
disabled_parkings = await client.disabled_parkings()
park_and_ride = await client.park_and_ride()
print(disabled_parkings)
print(park_and_ride)


if __name__ == "__main__":
asyncio.run(main())
```

## Data

You can read the following data with this package:

- Disabled parking spaces (Behindertenstellplätze)

## Use cases

[NIPKaart.nl][nipkaart]
Expand Down
3 changes: 2 additions & 1 deletion hamburg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

from .exceptions import UDPHamburgConnectionError, UDPHamburgError
from .hamburg import UDPHamburg
from .models import DisabledParking
from .models import DisabledParking, ParkAndRide

__all__ = [
"UDPHamburg",
"UDPHamburgConnectionError",
"UDPHamburgError",
"DisabledParking",
"ParkAndRide",
]
24 changes: 23 additions & 1 deletion hamburg/hamburg.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from yarl import URL

from .exceptions import UDPHamburgConnectionError, UDPHamburgError
from .models import DisabledParking
from .models import DisabledParking, ParkAndRide


@dataclass
Expand Down Expand Up @@ -98,6 +98,28 @@ async def disabled_parkings(
results.append(DisabledParking.from_json(item))
return results

async def park_and_ride(
self, limit: int = 10, bulk: str = "false"
) -> list[ParkAndRide]:
"""Get all park and ride spaces.
Args:
limit: Number of items to return.
bulk: Whether to return all items or the limit.
Returns:
A list of ParkAndRide objects.
"""
results: list[ParkAndRide] = []
locations = await self._request(
"p_und_r/collections/p_und_r/items",
params={"limit": limit, "bulk": bulk},
)
print(locations)
for item in locations["features"]:
results.append(ParkAndRide.from_json(item))
return results

async def close(self) -> None:
"""Close open client session."""
if self.session and self._close_session:
Expand Down
57 changes: 57 additions & 0 deletions hamburg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import annotations

from dataclasses import dataclass
from datetime import datetime
from typing import Any


Expand Down Expand Up @@ -50,3 +51,59 @@ def strip_spaces(string: str) -> str | None:
longitude=geo[0],
latitude=geo[1],
)


@dataclass
class ParkAndRide:
"""Object representing a park and ride spot."""

spot_id: str
name: str
construction_year: int
address: str
public_transport_line: str
park_type: str
free_space: int
capacity: int
occupancy_percentage: float
disabled_parking_spaces: int
tickets: dict[str, int]
longitude: float
latitude: float
updated_at: datetime

@classmethod
def from_json(cls, data: dict[str, Any]) -> ParkAndRide:
"""Return a ParkAndRide object from a JSON dictionary.
Args:
data: The JSON data from the API.
Returns:
A ParkAndRide object.
"""

attr = data["properties"]
geo = data["geometry"]["coordinates"]
return cls(
spot_id=str(data.get("id")),
name=attr.get("name"),
construction_year=attr.get("baujahr"),
address=attr.get("adresse"),
public_transport_line=attr.get("linie"),
park_type=attr.get("art"),
free_space=attr.get("stellplaetze_frei"),
capacity=attr.get("stellplaetze_gesamt"),
occupancy_percentage=round(attr.get("stellpl_frei_in_prozent"), 2),
disabled_parking_spaces=attr.get("stellplaetze_behinderte_gesamt"),
tickets={
"day": attr.get("ticket_1_tag"),
"month": attr.get("ticket_30_tage"),
"year": attr.get("ticket_1_jahr"),
},
longitude=geo[0],
latitude=geo[1],
updated_at=datetime.strptime(
attr.get("aktualitaet_belegungsdaten"), "%Y-%m-%d %H:%M:%S"
),
)
7 changes: 5 additions & 2 deletions test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
async def main() -> None:
"""Show example on using the Hamburg API client."""
async with UDPHamburg() as client:
locations = await client.disabled_parkings(bulk="true")
disabled_parkings = await client.disabled_parkings(bulk="true")
print(disabled_parkings)

park_and_ride = await client.park_and_ride(bulk="true")
count: int

for index, item in enumerate(locations, 1):
for index, item in enumerate(park_and_ride, 1):
count = index
print(item)
print(f"{count} locations found")
Expand Down
File renamed without changes.
Loading

0 comments on commit 73c86c2

Please sign in to comment.