pyrosm icon indicating copy to clipboard operation
pyrosm copied to clipboard

Members of relations do not appear in the columns

Open leonefamily opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. I'm working with networks for traffic models, and it is often necessary to implement turn restrictions. It seems like getting members of relations as a part of a search using osm.get_data_by_custom_criteria is not possible.

Describe the solution you'd like It would be really convenient, if we had a column named members in the returned (Geo)DataFrame, when relations are involved.

Describe alternatives you've considered It is possible to get members info out of osm._relations, which has the members:

relations = pd.DataFrame(osm._relations[0])
members = relations['members'].apply(get_members)
members_strings = relations['members'].apply(lambda x: get_members(x, as_json_string=True))

This is a function, with which I'm currently handling members extraction, though it can probably be more efficient:

import json
import numpy as np
from typing import Dict, List, Union

def get_members(
        members_raw: Dict[str, np.ndarray],
        as_json_string: bool = False
        ) -> Union[str, List[Dict[str, Union[int, str]]]]:
    members = []
    for n, member_id in enumerate(members_raw['member_id']):
        members.append({
            # converting to int, because json module cannot serialize `np.int`s
            'member_id': int(member_id),
            # member_type is bytes for some reason, but member_role is not
            'member_type': members_raw['member_type'][n].decode('utf-8'),
            'member_role': members_raw['member_role'][n]
            })
    if as_json_string:
        return json.dumps(members)
    return members

leonefamily avatar Aug 27 '23 16:08 leonefamily

+1

timvahlbrock avatar Dec 01 '23 18:12 timvahlbrock