예제 #1
0
파일: tests.py 프로젝트: SEL-Columbia/NeXT
def _initTestData(session):
    """
    Initialize Test Scenario with 2 demand nodes and 2 supply_nodes
    Assumes:
    - Reference Data (i.e. NodeTypes) are loaded 
    - Database functions/triggers are loaded
    """ 
    from next.model.models import Scenario, Phase, Node, get_node_type, BASE_SRID
    scenario = Scenario("Test")
    session.add(scenario)
    phase1 = Phase(scenario)
    # add a 2nd phase to test against
    phase2 = Phase(scenario, phase1)
    session.add_all([phase1, phase2])

    supply1 = Node(
               from_shape(Point(0, 0), srid=BASE_SRID),
               1,
               get_node_type('supply', session), 
               phase1
           )

    supply2 = Node(
               from_shape(Point(-1, -1), srid=BASE_SRID),
               1,
               get_node_type('supply', session), 
               phase1
           )

    demand1  = Node(
               from_shape(Point(1, 1), srid=BASE_SRID),
               1,
               get_node_type('demand', session), 
               phase1
           )

    # demand2 is added to phase2
    demand2  = Node(
               from_shape(Point(-2, -2), srid=BASE_SRID),
               1,
               get_node_type('demand', session), 
               phase2
           )

    session.add(supply1)
    session.add(supply2)
    session.add(demand1)
    session.add(demand2)
    session.flush()
예제 #2
0
파일: views.py 프로젝트: SEL-Columbia/NeXT
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}
        )
예제 #3
0
파일: tests.py 프로젝트: SEL-Columbia/NeXT
def nodes_along_latitude(num_nodes, phase, session, node_type='demand', weight=1, y_val=0.0, x_origin=0.0, x_spacing=1.0):
    """
    Create num_node next.model.models.Node objects along a horizontal line
    at y_val spaced evenly at x_spacing intervals starting from x_origin
    """
    from next.model.models import Node, get_node_type, BASE_SRID
    nodes = []
    for i in range(num_nodes):
        x_val = x_origin + i*x_spacing
        node = Node(
                from_shape(Point(x_val, y_val), srid=BASE_SRID),
                weight, 
                get_node_type(node_type, session),
                phase
               )
        nodes.append(node)
                
    return nodes
예제 #4
0
파일: views.py 프로젝트: SEL-Columbia/NeXT
def show_nodes(request):
    """ 
    Returns nodes as geojson
    type parameter used as a filter
    If type=='demand', then add nearest-neighbor distance to output
    """

    session = DBSession()
    scenario = get_object_or_404(Scenario, request)
    nodes = []
    if (request.GET.has_key("type")):
        request_node_type = request.GET["type"]
        if(request_node_type == "demand"):
            return show_demand_json(scenario)
        else: 
            nodes = scenario.get_nodes().\
                filter_by(node_type=get_node_type(request_node_type, session))
    else:
        nodes = scenario.get_nodes()

    return to_geojson_feature_collection(nodes)
예제 #5
0
파일: views.py 프로젝트: SEL-Columbia/NeXT
def create_scenario(request):
    """
    Bulk load the nodes from the demand and supply csv's
    """
            
    if(request.method=='POST'):
        session = DBSession()
        dbapi_conn = session.connection().connection
        sc = phase = None
        try:
            demand_type = get_node_type('demand', session)
            supply_type = get_node_type('supply', session)
    
            name = request.POST['name']
            # make sure that we have a name
            assert len(name) != 0
    
            sc = Scenario(name)
            session.add(sc)
            session.flush()

            # add the root phase to the scenario
            phase = Phase(sc)
            session.add(phase)
            session.flush()

            demand_file = request.POST['demand-csv']
            supply_file = request.POST['supply-csv']

            tmp_demand_file = os.path.join(
                request.registry.settings['next.temporary_folder'],
                demand_file.filename
                )

            tmp_supply_file = os.path.join(
                request.registry.settings['next.temporary_folder'],
                supply_file.filename
                )

            write_tmp_file(demand_file, tmp_demand_file)
            write_tmp_file(supply_file, tmp_supply_file)

            in_demand_stream = open(tmp_demand_file, 'rU')
            in_supply_stream = open(tmp_supply_file, 'rU')

            import_nodes(dbapi_conn, in_demand_stream, 
                    demand_type.id, sc.id, phase.id)
            import_nodes(dbapi_conn, in_supply_stream, 
                    supply_type.id, sc.id, phase.id)
            
        except Exception as error:
            raise(error)    

            
        # send the user to the run scenario page right now
        # at this point, we should have the scenario/phase, 
        # so create the edges
        # session.commit()
        phase.create_edges()
        return HTTPFound(
            location=request.route_url('show-phase', id=sc.id, phase_id=phase.id))
        
    elif request.method == 'GET':
        return {}
    else:
        raise HTTPForbidden()