Пример #1
0
def show_cumulative_phase_demand_json(phase):
    session = DBSession()
    conn = session.connection()
    sql = text('''
    select nodes.id,
    nodetypes.name,
    nodes.weight,
    st_asgeojson(nodes.point),
    edges.distance 
    from nodes, edges, nodetypes, phase_ancestors
    where nodes.scenario_id = :sc_id and
    nodes.phase_id = phase_ancestors.ancestor_phase_id and
    phase_ancestors.phase_id = :ph_id and
    edges.scenario_id = phase_ancestors.scenario_id and
    edges.phase_id = phase_ancestors.phase_id and
    nodes.id = edges.from_node_id and
    nodes.node_type_id = nodetypes.id and
    nodetypes.name='demand'
    ''')
    rset = conn.execute(sql, sc_id=phase.scenario_id, ph_id=phase.id).fetchall()
    feats = [
        {
        'type': 'Feature',
        'geometry': simplejson.loads(feat[3]),
        'properties': {
            'id':feat[0],
            'type':feat[1],
            'weight':feat[2],
            'distance': feat[4]
            }
        } for feat in rset
    ]
    return json_response({'type': 'FeatureCollection', 'features': feats })
Пример #2
0
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()