Exemple #1
0
 def setUp(self):
     """Setup test fixture for each model test method."""
     try:
         new_attrs = {}
         new_attrs.update(self.attrs)
         new_attrs.update(self.do_get_dependencies())
         self.obj = self.klass(**new_attrs)
         DBSession.add(self.obj)
         DBSession.flush()
         return self.obj
     except:
         DBSession.rollback()
         raise
Exemple #2
0
 def test_query_obj(self):
     """Model objects can be queried"""
     obj = DBSession.query(self.klass).one()
     for key, value in self.attrs.items():
         eq_(getattr(obj, key), value)
Exemple #3
0
 def tearDown(self):
     """Tear down test fixture for each model test method."""
     DBSession.rollback()
Exemple #4
0
    def network(self, network_id, scenario_id):
        """This method returns a network page, with a list of networks."""
        user_id = request.identity['user'].user_id
        net = HydraServer.lib.network.get_network(int(network_id), scenario_ids=[int(scenario_id)], **{'user_id':user_id})
        
        if net.projection is not None and net.projection.strip() != "":
            try:
                if net.projection == 'WGS84':
                    net_proj = 4326
                else:
                    net_proj = net.projection.split(':')[1]

                source_proj = osr.SpatialReference()
                source_proj.ImportFromEPSG(int(net_proj))
                target_proj = osr.SpatialReference()
                target_proj.ImportFromEPSG(4326)
                transform = osr.CoordinateTransformation(source_proj, target_proj)
                for n in net.nodes:
                    x = n.node_x
                    y = n.node_y
                    source_point = ogr.CreateGeometryFromWkt("POINT (%s %s)" % (x, y))
                    source_point.Transform(transform)
                    n.node_x = source_point.GetX()
                    n.node_y = source_point.GetY()
            except:
                log.critical("Unable to recognise projection %s"%net.projection)
                net.projection = None


        def get_layout_property(resource, prop, default):
            layout = {}
            if resource.layout is not None:
                layout = eval(resource.layout)
            elif resource.types:
                if resource.types[0].templatetype.layout is not None:
                    layout = eval(resource.types[0].templatetype.layout)
           
            prop_value = default
            if layout.get(prop) is not None:
                prop_value = layout[prop]

            return prop_value
            
        json_net = {'nodes':[], 'edges':[]}
        for node in net.nodes:
            colour = get_layout_property(node, 'colour', 'red')
            size = get_layout_property(node, 'size', 1)
            node_dict = {
                'id' : str(node.node_id),
                'label': node.node_name,
                'x'    : float(node.node_x),
                'y'    : float(node.node_y),
                'size' : size,
                'color': colour,
            }
            json_net['nodes'].append(node_dict)

        for link in net.links:
            colour = get_layout_property(link, 'colour', 'red')
            width = get_layout_property(link, 'line_weight', 5)
            link_dict = {
                'id' : str(link.link_id),
                'source': str(link.node_1_id),
                'target' : str(link.node_2_id),
                'color': colour,
                'size':width,
                'type':'curve',
                'hover_color':'#ccc',
            }
            json_net['edges'].append(link_dict)

        node_coords = {}
        node_name_map = {}
        for node in net.nodes:
            node_coords[node.node_id] = [node.node_y, node.node_x];
            node_name_map[node.node_id] = node.node_name
        link_coords = {}
        for link in net.links:
            link_coords[link.link_id] = [node_coords[link.node_1_id], node_coords[link.node_2_id]]
        log.info(node_coords)
        
        attributes = []
        rs_dict = {}
        
        if scenario_id is not None:
            network_data = HydraServer.lib.scenario.get_resource_data('NETWORK', network_id, scenario_id, None, **{'user_id':user_id})
            for rs in network_data:
                rs_dict[rs.resource_attr_id] = rs.dataset
        
        for a in net.attributes:
            attr = {}
            attr['attr_id'] = a.attr_id
            attr['resource_attr_id']   = a.resource_attr_id
            attr['is_var'] = a.attr_is_var
            attr['name']   = a.attr_name
            attr['value'] = rs_dict.get(a.resource_attr_id)
            attributes.append(attr)

        DBSession.expunge_all() 
        
        return dict(network=net,
                    scenario_id=scenario_id,
                    environment=request.environ,
                    attributes=attributes,
                    link_coords=link_coords,
                    node_coords=node_coords,
                    node_name_map=node_name_map,
                    json_net = json.dumps(json_net),
                    projection = net.projection)