def get_asset_locations(self, asset):
     """Mock function."""
     print(f'id: {asset.id}')
     feature_collection = FeatureCollection([self.feature])
     # add the asset to the location history as the "source"
     feature_collection.update({"source_id": asset.id})
     feature_collection.update({"source_type": asset.type})
     return feature_collection
def get_asset_locations(connection: extensions.connection,
                        asset: Asset) -> FeatureCollection:
    """
    Get an asset's location history in GEOJson format.

    :param connection: The database connection.
    :param asset: The asset.
    :return: The asset's location history.
    """
    sql = '''
        select
            is_asset_location.nam_locn_id,
            is_asset_location.install_date,
            is_asset_location.remove_date,
            nam_locn.nam_locn_name,
            type.type_name
        from
            is_asset_location, nam_locn, type
        where
            is_asset_location.asset_uid = %s
        and
            nam_locn.nam_locn_id = is_asset_location.nam_locn_id
        and
            type.type_id = nam_locn.type_id
    '''
    features: List[Feature] = []
    with closing(connection.cursor()) as cursor:
        cursor.execute(sql, [asset.id])
        rows = cursor.fetchall()
        for row in rows:
            key = row[0]
            install_date = row[1]
            remove_date = row[2]
            name = row[3]
            locations: FeatureCollection = get_named_location_locations(
                connection, key)
            properties: List[Property] = get_named_location_properties(
                connection, key)
            parents: Dict[str,
                          str] = get_named_location_parents(connection, key)
            domain: str = parents['domain'] if parents else None
            site: str = parents['site'] if parents else None
            context: List[str] = get_named_location_context(connection, key)
            asset_location = AssetLocation(name=name,
                                           domain=domain,
                                           site=site,
                                           install_date=install_date,
                                           remove_date=remove_date,
                                           context=context,
                                           properties=properties,
                                           locations=locations)
            features.append(
                geojson_converter.convert_asset_location(asset_location))
    feature_collection = FeatureCollection(features)
    # add the asset as the source
    feature_collection.update(source_id=asset.id)
    feature_collection.update(source_type=asset.type)
    return feature_collection
Example #3
0
def run_route(start_node_id, end_node_id, route_type):
    """

    :param start_node_id:
    :param end_node_id:
    :param route_type:
    :param route_options: a dictionary
    :return:
    """
    # TODO add route options dictionary
    # TODO add parameter to function   route_options=None

    # sample dictionary of options
    # route_options = {'route_types': {
    #     'standard_route': 1,
    #     'barrierfree route': 2,
    #     'indoor_only_prefered': 3,
    #     'fastest': 4
    # },
    #     'route_logic': {
    #         'force_route_through_location': True
    #     }
    # }

    cur = connection.cursor()
    # base_route_q = """SELECT id, source, target,
    #                  total_cost:: DOUBLE PRECISION AS cost,
    #                  floor, network_type
    #                  FROM geodata.networklines_3857"""
    base_route_q = """SELECT id, source, target, cost, reverse_cost FROM geodata.networklines_3857"""

    # set default query
    barrierfree_q = "WHERE 1=1"
    if route_type == "1":
        # exclude all networklines of type stairs
        barrierfree_q = "WHERE network_type not in (1,3)"


    # routing_query = '''
    #  SELECT seq, id, node, edge, ST_Length(geom) AS cost, floor, network_type, ST_AsGeoJSON(geom) AS geoj
    #   FROM pgr_dijkstra( '{normal} {type}', %s, %s, FALSE) AS dij_route
    #   LEFT JOIN geodata.networklines_3857 AS input_network
    #   ON dij_route.edge = input_network.id
    #   ORDER BY dij_route.seq;
    # '''.format(normal=base_route_q,type=barrierfree_q)
    route_query = "SELECT id, source, target, cost, reverse_cost FROM geodata.networklines_3857"

    routing_query = """
        SELECT seq, id, node, edge,
            ST_Length(geom) AS cost, agg_cost, floor, network_type,
            ST_AsGeoJSON(geom) AS geoj
        FROM pgr_dijkstra( '{normal} {type}',{start_node}, {end_node}) AS dij_route

          JOIN geodata.networklines_3857 AS input_network
          ON dij_route.edge = input_network.id ;""".format(normal=route_query, type=barrierfree_q, start_node=start_node_id, end_node=end_node_id)

    print(routing_query)

    # routing_query = '''
    #     SELECT seq,
    #     id1 AS node,
    #     id2 AS edge,
    #       ST_Length(geom) AS cost,
    #        floor,
    #       network_type,
    #       ST_AsGeoJSON(geom) AS geoj
    #       FROM pgr_dijkstra('
    #         {normal} {type}', %s, %s, FALSE, FALSE
    #       ) AS dij_route
    #       JOIN  geodata.networklines_3857 AS input_network
    #       ON dij_route.id2 = input_network.id ;
    #   '''.format(normal=base_route_q, type=barrierfree_q)

    # run our shortest path query
    if start_node_id or end_node_id:
        if start_node_id != end_node_id:
            # cur.execute(routing_query, (start_node_id, end_node_id))
            cur.execute(routing_query)
    else:
        logger.error("start or end node is None or is the same node " + str(start_node_id))
        return HttpResponseNotFound('<h1>Sorry NO start or end node'
                                    ' found within 200m</h1>')

    # get entire query results to work with
    route_segments = cur.fetchall()

    route_info = calc_distance_walktime(route_segments)

    # empty list to hold each segment for our GeoJSON output
    route_result = []

    # loop over each segment in the result route segments
    # create the list of our new GeoJSON
    for segment in route_segments:
        seg_length = segment[4]  # length of segment
        layer_level = segment[6]  # floor number
        seg_type = segment[7]
        seg_node_id = segment[2]
        seq_sequence = segment[0]
        geojs = segment[8]  # geojson coordinates
        geojs_geom = loads(geojs)  # load string to geom
        geojs_feat = Feature(geometry=geojs_geom,
                             properties={'floor': layer_level,
                                         'segment_length': seg_length,
                                         'network_type': seg_type,
                                         'seg_node_id': seg_node_id,
                                         'sequence': seq_sequence}
                             )
        route_result.append(geojs_feat)

    # using the geojson module to create our GeoJSON Feature Collection
    geojs_fc = FeatureCollection(route_result)
    geojs_fc.update(route_info)
    return geojs_fc
Example #4
0
def run_route(start_node_id, end_node_id, route_type):
    """

    :param start_node_id:
    :param end_node_id:
    :param route_type:
    :param route_options: a dictionary
    :return:
    """
    # TODO add route options dictionary
    # TODO add parameter to function   route_options=None

    # sample dictionary of options
    # route_options = {'route_types': {
    #     'standard_route': 1,
    #     'barrierfree route': 2,
    #     'indoor_only_prefered': 3,
    #     'fastest': 4
    # },
    #     'route_logic': {
    #         'force_route_through_location': True
    #     }
    # }

    cur = connection.cursor()
    base_route_q = """SELECT id, source, target,
                     total_cost:: DOUBLE PRECISION AS cost,
                     floor, network_type
                     FROM geodata.networklines_3857"""

    # set default query
    barrierfree_q = "WHERE 1=1"
    if route_type == "1":
        # exclude all networklines of type stairs
        barrierfree_q = "WHERE network_type not in (1,3)"

    routing_query = '''
        SELECT seq,
        id1 AS node,
        id2 AS edge,
          ST_Length(geom) AS cost,
           floor,
          network_type,
          ST_AsGeoJSON(geom) AS geoj
          FROM pgr_dijkstra('
            {normal} {type}', %s, %s, FALSE, FALSE
          ) AS dij_route
          JOIN  geodata.networklines_3857 AS input_network
          ON dij_route.id2 = input_network.id ;
      '''.format(normal=base_route_q, type=barrierfree_q)

    # run our shortest path query
    if start_node_id or end_node_id:
        if start_node_id != end_node_id:
            cur.execute(routing_query, (start_node_id, end_node_id))
    else:
        logger.error("start or end node is None or is the same node " + str(start_node_id))
        return HttpResponseNotFound('<h1>Sorry NO start or end node'
                                    ' found within 200m</h1>')

    # get entire query results to work with
    route_segments = cur.fetchall()

    route_info = calc_distance_walktime(route_segments)

    # empty list to hold each segment for our GeoJSON output
    route_result = []

    # loop over each segment in the result route segments
    # create the list of our new GeoJSON
    for segment in route_segments:
        seg_length = segment[3]  # length of segment
        layer_level = segment[4]  # floor number
        seg_type = segment[5]
        seg_node_id = segment[1]
        seq_sequence = segment[0]
        geojs = segment[6]  # geojson coordinates
        geojs_geom = loads(geojs)  # load string to geom
        geojs_feat = Feature(geometry=geojs_geom,
                             properties={'floor': layer_level,
                                         'length': seg_length,
                                         'network_type': seg_type,
                                         'seg_node_id': seg_node_id,
                                         'sequence': seq_sequence}
                             )
        route_result.append(geojs_feat)

    # using the geojson module to create our GeoJSON Feature Collection
    geojs_fc = FeatureCollection(route_result)
    geojs_fc.update(route_info)
    return geojs_fc