Пример #1
0
def add_nodes(request):
    """
    Add nodes to a new child phase
    """
    session = DBSession()
    phase = get_object_or_404(Phase, request, ('phase_id', 'id'))
    child_phase = Phase(phase.scenario, phase)
    new_nodes = []
    for feature in request.json_body['features']:
        # assumes point geom
        coords = feature['geometry']['coordinates']
        shp_pt = shapely.geometry.Point(coords[0], coords[1])
        wkb_geom = from_shape(shp_pt, srid=BASE_SRID)
        type_property = feature['properties']['type']
        weight = feature['properties']['weight']
        node_type = get_node_type(type_property, session)
        node = Node(wkb_geom, weight, node_type, child_phase)
        new_nodes.append(node)
    session.add_all(new_nodes)
    session.flush()
    child_phase.create_edges()
    return json_response(
        {'scenario_id': child_phase.scenario_id,
         'phase_id': child_phase.id}
        )
Пример #2
0
def create_supply_nodes(request):
    """
    Create new supply based on distance and re-create the nearest neighbor edges.  
    Create a new child phase of the phase passed in
    Display the new output
    """
    session = DBSession()
    phase = get_object_or_404(Phase, request, ('phase_id', 'id'))
    child_phase = Phase(phase.scenario, phase)
    session.add(child_phase)
    session.flush() #flush this so object has all id's
    distance = float(request.json_body.get('d', 1000))
    num_supply_nodes = int(request.json_body.get('n', 1))

    centroids = child_phase.locate_supply_nodes(distance, num_supply_nodes, session)
    session.add_all(centroids)

    # need to flush so that create_edges knows about new nodes
    session.flush()
    child_phase.create_edges()
    return json_response(
        {'scenario_id': child_phase.scenario_id,
         'phase_id': child_phase.id}
        )