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

feat: add support for extra_info parameter #251

Merged
merged 24 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9653fb8
feat: add extra_info to advanced parameters and add values
merydian Apr 26, 2024
b065691
feat: extra_info to request parameters with default route algorithm
merydian Apr 26, 2024
b01b782
feat: add extra_info values to request
merydian Apr 26, 2024
92f79bd
style: rename extra_info parameter variable
merydian Apr 26, 2024
729a0da
feat: add necessary fields to output layer
merydian Apr 26, 2024
e31e87a
style: run ruff
merydian Apr 26, 2024
61fcc00
feat: add extra info attributes to attribute table in points layer alg
merydian May 3, 2024
8aeb541
feat: add extra info attributes to attribute table in points layers a…
merydian May 3, 2024
f616734
feat: add support for csv_factor
merydian May 3, 2024
aa52090
feat: add support for csv_column
merydian May 3, 2024
15b4d0c
refactor: add typing hints, might lead to merge issues later?
koebi May 15, 2024
58b2848
feat: order extra_info according to fields order
koebi May 15, 2024
a0c050b
docs: add TODOs
koebi May 15, 2024
9e7dbde
style: ruff format
koebi May 15, 2024
1137c80
Merge branch 'main' into Add-support-for-extra-info-parameter
koebi May 15, 2024
13bd817
docs: Comment usage of Csv Factor/Column
koebi May 17, 2024
153449b
fix: correct type hint
koebi May 17, 2024
44532d6
feat: utility function to decode extra info
koebi May 17, 2024
866af09
feat: translate extra_info
koebi May 17, 2024
3da98fd
fix: set type according to extra info decoding
koebi May 17, 2024
5a7aa33
feat: decode extra info values
koebi May 17, 2024
5889c0e
fix: log, don't break on non-available extra info
koebi May 17, 2024
c98858e
docs: remove ToDos, available in #252
koebi May 17, 2024
94daede
style: ruff format
koebi May 17, 2024
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
20 changes: 20 additions & 0 deletions ORStools/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,26 @@
"INPUT_AVOID_COUNTRIES",
"INPUT_AVOID_POLYGONS",
"INPUT_SMOOTHING",
"EXTRA_INFO",
"CSV_FACTOR",
"CSV_COLUMN",
]

LOCATION_TYPES = ["start", "destination"]

EXTRA_INFOS = [
"steepness",
"suitability",
"surface",
"waytype",
"waycategory",
"tollways",
"traildifficulty",
"osmid",
"roadaccessrestrictions",
"countryinfo",
"green",
"noise",
"csv",
"shadow",
]
59 changes: 52 additions & 7 deletions ORStools/common/directions_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from PyQt5.QtCore import QVariant

from ORStools.utils import convert
from ORStools.utils import convert, logger


def get_request_point_features(route_dict: dict, row_by_row: str) -> Generator[List, Tuple, None]:
Expand Down Expand Up @@ -80,6 +80,7 @@ def get_fields(
from_name: str = "FROM_ID",
to_name: str = "TO_ID",
line: bool = False,
extra_info: list = [],
) -> QgsFields:
"""
Builds output fields for directions response layer.
Expand All @@ -104,14 +105,20 @@ def get_fields(
"""

fields = QgsFields()
fields.append(QgsField("DIST_KM", QVariant.Double))
fields.append(QgsField("DURATION_H", QVariant.Double))
fields.append(QgsField("PROFILE", QVariant.String))
fields.append(QgsField("PREF", QVariant.String))
fields.append(QgsField("OPTIONS", QVariant.String))
fields.append(QgsField(from_name, from_type))
if not extra_info:
fields.append(QgsField("DIST_KM", QVariant.Double))
fields.append(QgsField("DURATION_H", QVariant.Double))
fields.append(QgsField("PROFILE", QVariant.String))
fields.append(QgsField("PREF", QVariant.String))
fields.append(QgsField("OPTIONS", QVariant.String))
fields.append(QgsField(from_name, from_type))
if not line:
fields.append(QgsField(to_name, to_type))
for info in extra_info:
field_type = QVariant.Int
if info in ["waytype", "surface", "waycategory", "roadaccessrestrictions", "steepness"]:
field_type = QVariant.String
fields.append(QgsField(info.upper(), field_type))

return fields

Expand Down Expand Up @@ -215,6 +222,7 @@ def build_default_parameters(
point_list: Optional[List[QgsPointXY]] = None,
coordinates: Optional[list] = None,
options: Optional[dict] = None,
extra_info: Optional[list] = None,
) -> dict:
"""
Build default parameters for directions endpoint. Either uses a list of QgsPointXY to create the coordinates
Expand Down Expand Up @@ -246,6 +254,43 @@ def build_default_parameters(
"elevation": True,
"id": None,
"options": options,
"extra_info": extra_info,
}

return params


def get_extra_info_features_directions(response: dict, extra_info_order: list[str]):
extra_info_order = [
key if key != "waytype" else "waytypes" for key in extra_info_order
] # inconsistency in API
response_mini = response["features"][0]
coordinates = response_mini["geometry"]["coordinates"]
feats = list()
extra_info = response_mini["properties"]["extras"]
logger.log(str(extra_info))
extras_list = {i: [] for i in extra_info_order}
for key in extra_info_order:
try:
values = extra_info[key]["values"]
except KeyError:
logger.log(f"{key} is not available as extra_info.")
continue
for val in values:
for i in range(val[0], val[1]):
value = convert.decode_extrainfo(key, val[2])
extras_list[key].append(value)

for i in range(len(coordinates) - 1):
feat = QgsFeature()
qgis_coords = [QgsPoint(x, y, z) for x, y, z in coordinates[i : i + 2]]
feat.setGeometry(QgsGeometry.fromPolyline(qgis_coords))
attrs = list()
for j in extras_list:
extra = extras_list[j]
attr = extra[i]
attrs.append(attr)
feat.setAttributes(attrs)
feats.append(feat)

return feats
Loading
Loading