Ejemplo n.º 1
0
def get_routes(lang, city, limit=3):

    station_from = int(request.query.station_from) if request.query.station_from else None
    station_to = int(request.query.station_to) if request.query.station_to else None
    portal_from = int(request.query.portal_from) if request.query.portal_from else None
    portal_to = int(request.query.portal_to) if request.query.portal_to else None

    # Извлечение информации о станции
    def get_station_info(station_id):
        for station in STATIONS[city]:
            if station['id_station'] == str(station_id):
                return dict(
                    name=station.get('name_' + lang, station.get('name_en')),
                    line=int(station['id_line']),
                    coords=(float(station['lat']), float(station['lon'])),
                    node_id=station['id_node']
                )

    # Извлечение информации о линии
    def get_line_info(line_id):
        for line in LINES[city]:
            if line['id_line'] == str(line_id):
                return dict(
                    name=line.get('name_' + lang, line.get('name_en')),
                    color=line['color']
                )

    # Проверка на принадлежность станций к одной линии
    def check_the_same_line(node1, node2):
        node1_line = get_station_info(node1)['line']
        node2_line = get_station_info(node2)['line']
        return node1_line == node2_line

    # Заполнение информации о препятствиях на входах и выходах
    def portal_barriers(portal_id):
        for portal in PORTALS[city]:
            if int(portal['id_entrance']) == portal_id:
                return get_barriers(portal)

    # Заполнение информации о препятствиях на переходах
    def interchange_barriers(station_from, station_to):
        for interchange in INTERCHANGES[city]:
            if (int(interchange['station_from']) == station_from) and (int(interchange['station_to']) == station_to):
                return get_barriers(interchange)

    if ((station_from is not None) and (station_to is not None)):

        all_shortest_paths = nx.all_shortest_paths(
            GRAPH[city],
            station_from,
            station_to,
            weight='weight'
        )

        routes = []
        for index, path in enumerate(all_shortest_paths):
            if index == limit:
                break

            route = []
            for station in path:
                station_info = get_station_info(station)
                line_id = station_info['line']
                same_line = check_the_same_line(station, get_next_item(path, station))

                station_type = "regular" if same_line else "interchange"
                node_id=station_info['node_id']

                unit = dict(
                    id=station,
                    station_type=station_type,
                    station_name=station_info['name'],
                    coordinates=station_info['coords'],
                    station_line=dict(
                        id=line_id,
                        name=get_line_info(line_id)['name'],
                        color=get_line_info(line_id)['color']
                    ),
                    schema=SCHEMAS[city][str(node_id)] if str(node_id) in SCHEMAS[city] else None
                )

                if station_type == "interchange":
                    unit['barriers'] = None
                    unit['barriers'] = interchange_barriers(station, get_next_item(path, station))

                elif station_type == "regular":
                    unit['barriers'] = None

                route.append(unit)

            # Заполняем информацию о входах
            if portal_from is not None:
                portal_from_obj = filter(
                    lambda portal: portal['id_entrance'] == str(portal_from),
                    PORTALS[city]
                )[0]

            if portal_to is not None:
                portal_to_obj = filter(
                    lambda portal: portal['id_entrance'] == str(portal_to),
                    PORTALS[city]
                )[0]

            portals = dict(
                portal_from=dict(
                    barriers=portal_barriers(portal_from),
                    meetcode='#%s' % portal_from_obj['meetcode'],
                    name='%s' % portal_from_obj.get('name_' + lang, portal_from_obj.get('name_en'))
                ) if portal_from else None,
                portal_to=dict(
                    barriers=portal_barriers(portal_to),
                    meetcode='#%s' % portal_to_obj['meetcode'],
                    name='%s' % portal_to_obj.get('name_' + lang, portal_to_obj.get('name_en'))
                ) if portal_to else None
            )

            routes.append(dict(route=route, portals=portals))

        response.content_type = 'application/json'
        return dumps(dict(result=routes))

    else:
        return HTTPResponse(status=400)
Ejemplo n.º 2
0
def get_routes(lang, city, delta=5, limit=3):

    station_from = int(request.query.station_from) if request.query.station_from else None
    station_to = int(request.query.station_to) if request.query.station_to else None
    portal_from = int(request.query.portal_from) if request.query.portal_from else None
    portal_to = int(request.query.portal_to) if request.query.portal_to else None

    # Извлечение информации о станции
    def get_station_info(station_id):
        for station in STATIONS[city]:
            if station['id_station'] == str(station_id):
                return dict(
                    name=station.get('name_' + lang, station.get('name_en')),
                    line=int(station['id_line']),
                    coords=(float(station['lat']), float(station['lon'])),
                    node_id=station['id_node']
                )

    # Извлечение информации о линии
    def get_line_info(line_id):
        for line in LINES[city]:
            if line['id_line'] == str(line_id):
                return dict(
                    name=line.get('name_' + lang, line.get('name_en')),
                    color=line['color']
                )

    # Проверка на принадлежность станций к одной линии
    def check_the_same_line(node1, node2):
        node1_line = get_station_info(node1)['line']
        node2_line = get_station_info(node2)['line']
        return node1_line == node2_line

    # Заполнение информации о препятствиях на входах и выходах
    def portal_barriers(portal_id):
        for portal in PORTALS[city]:
            if int(portal['id_entrance']) == portal_id:
                return get_barriers(portal)

    # Заполнение информации о препятствиях на переходах
    def interchange_barriers(station_from, station_to):
        for interchange in INTERCHANGES[city]:
            if (int(interchange['station_from']) == station_from) and (int(interchange['station_to']) == station_to):
                return get_barriers(interchange)

    if ((station_from is not None) and (station_to is not None)):

        minlength = nx.shortest_path_length(GRAPH[city], station_from, station_to)
        # generator
        simple_paths = nx.all_simple_paths(GRAPH[city], station_from, station_to, minlength+delta)
        simple_paths_list = [p for p in simple_paths]
        simple_paths_list_sorted = sorted(simple_paths_list, key=lambda path: len(path))

        routes = []
        for index in range(min(limit, len(simple_paths_list))):
            route = []
            for station in simple_paths_list_sorted[index]:
                station_info = get_station_info(station)
                line_id = station_info['line']
                same_line = check_the_same_line(station, get_next_item(simple_paths_list_sorted[index], station))

                station_type = "regular" if same_line else "interchange"
                node_id=station_info['node_id']

                unit = dict(
                    station_type=station_type,
                    station_id=station,
                    station_name=station_info['name'],
                    coordinates=station_info['coords'],
                    station_line=dict(
                        id=line_id,
                        name=get_line_info(line_id)['name'],
                        color=get_line_info(line_id)['color']
                    ),
                    schema=SCHEMAS[city][str(node_id)] if str(node_id) in SCHEMAS[city] else None
                )

                if station_type == "interchange":
                    unit['barriers'] = None
                    unit['barriers'] = interchange_barriers(station, get_next_item(simple_paths_list_sorted[index], station))

                elif station_type == "regular":
                    unit['barriers'] = None

                route.append(unit)

            # Заполняем информацию о входах
            portals = dict(
                portal_from=dict(barriers=portal_barriers(portal_from)) if portal_from else None,
                portal_to=dict(barriers=portal_barriers(portal_to)) if portal_to else None
            )

            routes.append(dict(route=route, portals=portals))

        response.content_type = 'application/json'
        return dumps(dict(result=routes))

    else:
        return HTTPResponse(status=400)
Ejemplo n.º 3
0
def get_routes(lang, city, delta=5, limit=3):

    station_from = int(
        request.query.station_from) if request.query.station_from else None
    station_to = int(
        request.query.station_to) if request.query.station_to else None
    portal_from = int(
        request.query.portal_from) if request.query.portal_from else None
    portal_to = int(
        request.query.portal_to) if request.query.portal_to else None

    # Извлечение информации о станции
    def get_station_info(station_id):
        for station in STATIONS[city]:
            if station['id_station'] == str(station_id):
                return dict(name=station['name_' + lang],
                            line=int(station['id_line']),
                            coords=(float(station['lat']),
                                    float(station['lon'])),
                            node_id=station['id_node'])

    # Извлечение информации о линии
    def get_line_info(line_id):
        for line in LINES[city]:
            if line['id_line'] == str(line_id):
                return dict(name=line['name_' + lang], color=line['color'])

    # Проверка на принадлежность станций к одной линии
    def check_the_same_line(node1, node2):
        node1_line = get_station_info(node1)['line']
        node2_line = get_station_info(node2)['line']
        return node1_line == node2_line

    # Заполнение информации о препятствиях на входах и выходах
    def portal_barriers(portal_id):
        for portal in PORTALS[city]:
            if int(portal['id_entrance']) == portal_id:
                return get_barriers(portal)

    # Заполнение информации о препятствиях на переходах
    def interchange_barriers(station_from, station_to):
        for interchange in INTERCHANGES[city]:
            if (int(interchange['station_from']) == station_from) and (int(
                    interchange['station_to']) == station_to):
                return get_barriers(interchange)

    if ((station_from is not None) and (station_to is not None)):

        minlength = nx.shortest_path_length(GRAPH[city], station_from,
                                            station_to)
        # generator
        simple_paths = nx.all_simple_paths(GRAPH[city], station_from,
                                           station_to, minlength + delta)
        simple_paths_list = [p for p in simple_paths]
        simple_paths_list_sorted = sorted(simple_paths_list,
                                          key=lambda path: len(path))

        routes = []
        for index in range(min(limit, len(simple_paths_list))):
            route = []
            for station in simple_paths_list_sorted[index]:
                station_info = get_station_info(station)
                line_id = station_info['line']
                same_line = check_the_same_line(
                    station,
                    get_next_item(simple_paths_list_sorted[index], station))

                station_type = "regular" if same_line else "interchange"
                node_id = station_info['node_id']

                unit = dict(station_type=station_type,
                            station_id=station,
                            station_name=station_info['name'],
                            coordinates=station_info['coords'],
                            station_line=dict(
                                id=line_id,
                                name=get_line_info(line_id)['name'],
                                color=get_line_info(line_id)['color']),
                            schema=SCHEMAS[city][str(node_id)]
                            if str(node_id) in SCHEMAS[city] else None)

                if station_type == "interchange":
                    unit['barriers'] = None
                    unit['barriers'] = interchange_barriers(
                        station,
                        get_next_item(simple_paths_list_sorted[index],
                                      station))

                elif station_type == "regular":
                    unit['barriers'] = None

                route.append(unit)

            # Заполняем информацию о входах
            portals = dict(
                portal_from=dict(barriers=portal_barriers(portal_from))
                if portal_from else None,
                portal_to=dict(barriers=portal_barriers(portal_to))
                if portal_to else None)

            routes.append(dict(route=route, portals=portals))

        response.content_type = 'application/json'
        return dumps(dict(result=routes))

    else:
        return HTTPResponse(status=400)
Ejemplo n.º 4
0
def get_routes(lang, city, limit=3):

    station_from = int(
        request.query.station_from) if request.query.station_from else None
    station_to = int(
        request.query.station_to) if request.query.station_to else None
    portal_from = int(
        request.query.portal_from) if request.query.portal_from else None
    portal_to = int(
        request.query.portal_to) if request.query.portal_to else None

    # Извлечение информации о станции
    def get_station_info(station_id):
        for station in STATIONS[city]:
            if station['id_station'] == str(station_id):
                return dict(name=station.get('name_' + lang,
                                             station.get('name_en')),
                            line=int(station['id_line']),
                            coords=(float(station['lat']),
                                    float(station['lon'])),
                            node_id=station['id_node'])

    # Извлечение информации о линии
    def get_line_info(line_id):
        for line in LINES[city]:
            if line['id_line'] == str(line_id):
                return dict(name=line.get('name_' + lang, line.get('name_en')),
                            color=line['color'])

    # Проверка на принадлежность станций к одной линии
    def check_the_same_line(node1, node2):
        node1_line = get_station_info(node1)['line']
        node2_line = get_station_info(node2)['line']
        return node1_line == node2_line

    # Заполнение информации о препятствиях на входах и выходах
    def portal_barriers(portal_id):
        for portal in PORTALS[city]:
            if int(portal['id_entrance']) == portal_id:
                return get_barriers(portal)

    # Заполнение информации о препятствиях на переходах
    def interchange_barriers(station_from, station_to):
        for interchange in INTERCHANGES[city]:
            if (int(interchange['station_from']) == station_from) and (int(
                    interchange['station_to']) == station_to):
                return get_barriers(interchange)

    if ((station_from is not None) and (station_to is not None)):

        all_shortest_paths = nx.all_shortest_paths(GRAPH[city],
                                                   station_from,
                                                   station_to,
                                                   weight='weight')

        routes = []
        for index, path in enumerate(all_shortest_paths):
            if index == limit:
                break

            route = []
            for station in path:
                station_info = get_station_info(station)
                line_id = station_info['line']
                same_line = check_the_same_line(station,
                                                get_next_item(path, station))

                station_type = "regular" if same_line else "interchange"
                node_id = station_info['node_id']

                unit = dict(id=station,
                            station_type=station_type,
                            station_name=station_info['name'],
                            coordinates=station_info['coords'],
                            station_line=dict(
                                id=line_id,
                                name=get_line_info(line_id)['name'],
                                color=get_line_info(line_id)['color']),
                            schema=SCHEMAS[city][str(node_id)]
                            if str(node_id) in SCHEMAS[city] else None)

                if station_type == "interchange":
                    unit['barriers'] = None
                    unit['barriers'] = interchange_barriers(
                        station, get_next_item(path, station))

                elif station_type == "regular":
                    unit['barriers'] = None

                route.append(unit)

            # Заполняем информацию о входах
            if portal_from is not None:
                portal_from_obj = filter(
                    lambda portal: portal['id_entrance'] == str(portal_from),
                    PORTALS[city])[0]

            if portal_to is not None:
                portal_to_obj = filter(
                    lambda portal: portal['id_entrance'] == str(portal_to),
                    PORTALS[city])[0]

            portals = dict(
                portal_from=dict(
                    barriers=portal_barriers(portal_from),
                    meetcode='#%s' % portal_from_obj['meetcode'],
                    name='%s' %
                    portal_from_obj.get('name_' +
                                        lang, portal_from_obj.get('name_en')))
                if portal_from else None,
                portal_to=dict(barriers=portal_barriers(portal_to),
                               meetcode='#%s' % portal_to_obj['meetcode'],
                               name='%s' %
                               portal_to_obj.get('name_' + lang,
                                                 portal_to_obj.get('name_en')))
                if portal_to else None)

            routes.append(dict(route=route, portals=portals))

        response.content_type = 'application/json'
        return dumps(dict(result=routes))

    else:
        return HTTPResponse(status=400)