Skip to content

Commit

Permalink
Add dataset from Partago vehicles (#155)
Browse files Browse the repository at this point in the history
* Add dataset from Partago vehicles

* Add extra links
  • Loading branch information
klaasnicolaas authored May 7, 2023
1 parent 7464a7c commit 8a5e894
Show file tree
Hide file tree
Showing 11 changed files with 405 additions and 76 deletions.
75 changes: 7 additions & 68 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,72 +39,10 @@ You can read the following datasets with this package:

- [Parking garages occupancy][garages] (12 locations)
- [Park and Ride occupancy][parkandride] (5 locations)
- [BlueBike locations][bluebike] (6 locations)

<details>
<summary>Click here to get more details</summary>

### Parking garages

Parameters:

- **limit** (default: 10) - How many results you want to retrieve.

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `garage_id` | string | The id of the garage |
| `name` | string | The name of the garage |
| `parking_type` | string | The type of parking |
| `url` | string | The url with more information about the garage |
| `is_open` | boolean | Whether the garage is open or not |
| `free_parking` | boolean | Whether there is free parking or not |
| `temporary_closed` | boolean | Whether the garage is temporarily closed or not |
| `free_space` | integer | The amount of free parking spaces |
| `total_capacity` | integer | The total capacity of the garage |
| `availability_pct` | float | The percentage of free parking spaces |
| `occupancy_pct` | integer | The percentage of occupied parking spaces |
| `longitude` | float | The longitude of the garage |
| `latitude` | float | The latitude of the garage |
| `updated_at` | datetime | The last time the data was updated |

### Park and Ride

Parameters:

- **limit** (default: 10) - How many results you want to retrieve.
- **gentse_feesten** - Whether a park and ride location is used for the [Gentse Feesten](https://gentsefeesten.stad.gent).

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `spot_id` | string | The id of the park and ride |
| `name` | string | The name of the park and ride |
| `parking_type` | string | The type of parking |
| `url` | string | The url with more information about the park and ride |
| `is_open` | boolean | Whether the park and ride is open or not |
| `free_parking` | boolean | Whether there is free parking or not |
| `temporary_closed` | boolean | Whether the park and ride is temporarily closed or not |
| `gentse_feesten` | boolean | Whether the park and ride is used for the [Gentse Feesten](https://gentsefeesten.stad.gent) |
| `free_space` | integer | The amount of free parking spaces |
| `total_capacity` | integer | The total capacity of the park and ride |
| `availability_pct` | float | The percentage of free parking spaces |
| `occupancy_pct` | integer | The percentage of occupied parking spaces |
| `longitude` | float | The longitude of the park and ride |
| `latitude` | float | The latitude of the park and ride |
| `updated_at` | datetime | The last time the data was updated |

### BlueBikes

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `spot_id` | string | The id of the bluebike location |
| `name` | string | Name of the bluebike location |
| `spot_type` | integer | The type of the bluebike location |
| `bikes_in_use` | integer | The amount of bikes in use |
| `bikes_available` | integer | The amount of bikes available |
| `last_update` | datetime | The last time the data was updated |
| `longitude` | float | The longitude of the bluebike location |
| `latitude` | float | The latitude of the bluebike location |
</details>
- [BlueBike rental locations][bluebike] (6 locations)
- [Partago vehicle locations][partago] (116 locations)

Find here [more information](examples) about the different variables and parameters per dataset with this python package.

## Example

Expand Down Expand Up @@ -218,9 +156,10 @@ SOFTWARE.
[api]: https://data.stad.gent/explore
[nipkaart]: https://www.nipkaart.nl

[garages]: https://data.stad.gent/explore/dataset/bezetting-parkeergarages-real-time/information/
[parkandride]: https://data.stad.gent/explore/dataset/real-time-bezetting-pr-gent/information/
[garages]: https://data.stad.gent/explore/dataset/bezetting-parkeergarages-real-time/information
[parkandride]: https://data.stad.gent/explore/dataset/real-time-bezetting-pr-gent/information
[bluebike]: https://data.stad.gent/explore/?disjunctive.keyword&disjunctive.theme&sort=modified&q=bluebike
[partago]: https://data.stad.gent/explore/dataset/real-time-locaties-deelwagen-partago/information

<!-- MARKDOWN LINKS & IMAGES -->
[build-shield]: https://github.com/klaasnicolaas/python-odp-gent/actions/workflows/tests.yaml/badge.svg
Expand Down
84 changes: 84 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
## Parking garages

With this you can read the occupancy of the parking garages in Ghent.

Parameters:

- **limit** (default: 10) - How many results you want to retrieve.

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `garage_id` | string | The id of the garage |
| `name` | string | The name of the garage |
| `parking_type` | string | The type of parking |
| `url` | string | The url with more information about the garage |
| `is_open` | boolean | Whether the garage is open or not |
| `free_parking` | boolean | Whether there is free parking or not |
| `temporary_closed` | boolean | Whether the garage is temporarily closed or not |
| `free_space` | integer | The amount of free parking spaces |
| `total_capacity` | integer | The total capacity of the garage |
| `availability_pct` | float | The percentage of free parking spaces |
| `occupancy_pct` | integer | The percentage of occupied parking spaces |
| `longitude` | float | The longitude of the garage |
| `latitude` | float | The latitude of the garage |
| `updated_at` | datetime | The last time the data was updated |

## Park and Ride

With this you can read the occupancy of the park and rides in Ghent.

Parameters:

- **limit** (default: 10) - How many results you want to retrieve.
- **gentse_feesten** - Whether a park and ride location is used for the [Gentse Feesten](https://gentsefeesten.stad.gent).

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `spot_id` | string | The id of the park and ride |
| `name` | string | The name of the park and ride |
| `parking_type` | string | The type of parking |
| `url` | string | The url with more information about the park and ride |
| `is_open` | boolean | Whether the park and ride is open or not |
| `free_parking` | boolean | Whether there is free parking or not |
| `temporary_closed` | boolean | Whether the park and ride is temporarily closed or not |
| `gentse_feesten` | boolean | Whether the park and ride is used for the [Gentse Feesten](https://gentsefeesten.stad.gent) |
| `free_space` | integer | The amount of free parking spaces |
| `total_capacity` | integer | The total capacity of the park and ride |
| `availability_pct` | float | The percentage of free parking spaces |
| `occupancy_pct` | integer | The percentage of occupied parking spaces |
| `longitude` | float | The longitude of the park and ride |
| `latitude` | float | The latitude of the park and ride |
| `updated_at` | datetime | The last time the data was updated |

## BlueBikes

This dataset consists of information about the rental locations of the [bluebikes](https://www.blue-bike.be).

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `spot_id` | string | The id of the bluebike location |
| `name` | string | Name of the bluebike location |
| `spot_type` | integer | The type of the bluebike location |
| `bikes_in_use` | integer | The amount of bikes in use |
| `bikes_available` | integer | The amount of bikes available |
| `last_update` | datetime | The last time the data was updated |
| `longitude` | float | The longitude of the bluebike location |
| `latitude` | float | The latitude of the bluebike location |

## Partago

This dataset consists of information about the [Partago](https://www.partago.be) vehicles and where they are located.

Parameters:

- **limit** (default: 10) - How many results you want to retrieve.

| Variable | Type | Description |
| :------- | :--- | :---------- |
| `name` | string | Name of the Partago vehicle |
| `vehicle_type` | Vehicle (dict) | The vehicle information (brand, mode, fuel etc.) |
| `picture_url` | string | The url of a picture of the vehicle |
| `station_type` | string | The type of station (free floating or fixed) |
| `last_update` | datetime | The last time the data was updated |
| `longitude` | float | The longitude of the vehicle |
| `latitude` | float | The latitude of the vehicle |
4 changes: 2 additions & 2 deletions examples/bluebike.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ async def main() -> None:
for index, item in enumerate(bluebiks, 1):
count = index
print(item)
print("________________________")
print(f"{count} bluebikes found")
print("__________________________")
print(f"{count} bluebike locations found")


if __name__ == "__main__":
Expand Down
23 changes: 23 additions & 0 deletions examples/partago.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# pylint: disable=W0621
"""Asynchronous Python client providing Open Data information of Gent."""

import asyncio

from odp_gent import ODPGent


async def main() -> None:
"""Fetch Partago data using the Gent API client."""
async with ODPGent() as client:
partago_vehicles = await client.partago_vehicles(limit=120)

count: int
for index, item in enumerate(partago_vehicles, 1):
count = index
print(item)
print("________________________")
print(f"{count} partago cars found")


if __name__ == "__main__":
asyncio.run(main())
3 changes: 2 additions & 1 deletion odp_gent/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Asynchronous Python client providing Open Data information of Gent."""

from .exceptions import ODPGentConnectionError, ODPGentError
from .models import BlueBike, Garage, ParkAndRide
from .models import BlueBike, Garage, ParkAndRide, Partago
from .odp_gent import ODPGent

__all__ = [
Expand All @@ -11,4 +11,5 @@
"Garage",
"ParkAndRide",
"BlueBike",
"Partago",
]
79 changes: 78 additions & 1 deletion odp_gent/models.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""Models for Open Data Platform of Gent."""
from __future__ import annotations

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

import pytz


@dataclass
class Garage:
Expand Down Expand Up @@ -177,7 +180,81 @@ def from_dict(cls: type[BlueBike], data: dict[str, Any]) -> BlueBike:
spot_type=int(attr.get("type")),
bikes_in_use=attr.get("bikes_in_use"),
bikes_available=attr.get("bikes_available"),
last_update=datetime.strptime(attr.get("last_seen"), "%Y-%m-%dT%H:%M:%S%z"),
last_update=datetime.strptime(
attr.get("last_seen"),
"%Y-%m-%dT%H:%M:%S%z",
).replace(tzinfo=pytz.timezone("Europe/Brussels")),
longitude=float(attr.get("longitude")),
latitude=float(attr.get("latitude")),
)


@dataclass
class Partago:
"""Object representing a Partago vehicle location."""

name: str
vehicle_type: Vehicle
picture_url: str | None
station_type: str
last_update: datetime

latitude: float
longitude: float

@classmethod
def from_dict(cls: type[Partago], data: dict[str, Any]) -> Partago:
"""Return a Partago vehicle object from a dictionary.
Args:
----
data: The data from the API.
Returns:
-------
A Partago object.
"""
attr = data["fields"]
geo = data["geometry"]["coordinates"]
return cls(
name=attr.get("displayname"),
vehicle_type=Vehicle.from_dict(json.loads(attr.get("vehicleinformation"))),
picture_url=None if attr.get("picture") == "null" else attr.get("picture"),
station_type=attr.get("stationtype"),
last_update=datetime.strptime(
data["record_timestamp"],
"%Y-%m-%dT%H:%M:%S.%fZ",
).replace(tzinfo=pytz.utc),
longitude=geo[0],
latitude=geo[1],
)


@dataclass
class Vehicle:
"""Object representing a general vehicle."""

brand: str
mode: str
fuel_type: str
transmission_type: str

@classmethod
def from_dict(cls: type[Vehicle], data: dict[str, Any]) -> Vehicle:
"""Return a Vehicle object from a dictionary.
Args:
----
data: The data from the API.
Returns:
-------
A Vehicle object.
"""
return cls(
brand=data["brand"],
mode=data["model"],
fuel_type=data["fuelType"],
transmission_type=data["transmissionType"],
)
19 changes: 18 additions & 1 deletion odp_gent/odp_gent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from yarl import URL

from .exceptions import ODPGentConnectionError, ODPGentError
from .models import BlueBike, Garage, ParkAndRide
from .models import BlueBike, Garage, ParkAndRide, Partago


@dataclass
Expand Down Expand Up @@ -177,6 +177,23 @@ async def bluebikes(self) -> list[BlueBike]:
results.append(BlueBike.from_dict(item))
return results

async def partago_vehicles(self, limit: int = 10) -> list[Partago]:
"""Get list of data from Partago vehicles.
Returns
-------
A list of Partago objects.
"""
results: list[Partago] = []
vehicles = await self._request(
"search/",
params={"dataset": "real-time-locaties-deelwagen-partago", "rows": limit},
)

for item in vehicles["records"]:
results.append(Partago.from_dict(item))
return results

async def close(self) -> None:
"""Close open client session."""
if self.session and self._close_session:
Expand Down
Loading

0 comments on commit 8a5e894

Please sign in to comment.