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} )
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} )
def test_delete_phase(self): """ This test is pretty comprehensive (may get promoted to top level test) 1. Add 1 new phase with 100 nodes 2. To this add another phase via "create_supply_nodes" 3. delete the phase created in step 1 4. Test whether it and its child are deleted """ from next.model.models import Scenario, Phase, Node, Edge, PhaseAncestor from next.views import create_edges, create_supply_nodes, remove_phase scen1 = self.session.query(Scenario).filter(Scenario.name == "Test").first() phase1 = self.session.query(Phase).filter(Phase.scenario_id == scen1.id).first() parent_phase = Phase(scen1, phase1) self.session.add_all([scen1, parent_phase]) demand_nodes = nodes_along_latitude(100, parent_phase, self.session, node_type='demand', y_val=0.0, x_origin=0.0, x_spacing=0.1) supply_nodes = nodes_along_latitude(5, parent_phase, self.session, node_type='supply', y_val=0.0, x_origin=0.0, x_spacing=2.0) self.session.add_all(demand_nodes) self.session.add_all(supply_nodes) # flush to assign ids self.session.flush() # add the edges for the parent phase request = testing.DummyRequest() params = {'id': scen1.id, 'phase_id': parent_phase.id} request.matchdict = params #create the edges create_edges(request) # build the request to auto create_supply_nodes # NOTE: this uses the previously set matchdict with # scenario_id/phase_id params request.json_body = {'d': 1000, 'n': 10} response = create_supply_nodes(request) # Get the new phase (to test that it was created) # TODO: QUERY HERE child_phase = parent_phase.get_descendents_query().filter( Phase.id != parent_phase.id).order_by(Phase.id).first() descendent_ids = [p.id for p in parent_phase.get_descendents_query()] self.assertTrue(len(descendent_ids) == 2) self.assertTrue(child_phase.id > parent_phase.id) self.assertTrue(child_phase.get_nodes_query().count() == 10) # remove the parent phase (params remain the same) response = remove_phase(request) # Check whether all nodes, edges, phases, ancestors associated with # both phases have been deleted self.assertTrue(self.session.query(Node).filter((Node.scenario_id == scen1.id) & (Node.phase_id.in_(descendent_ids))).count() == 0) self.assertTrue(self.session.query(Edge).filter((Edge.scenario_id == scen1.id) & (Edge.phase_id.in_(descendent_ids))).count() == 0) self.assertTrue(self.session.query(Phase).filter((Phase.scenario_id == scen1.id) & (Phase.id.in_(descendent_ids))).count() == 0) self.assertTrue(self.session.query(PhaseAncestor).filter((PhaseAncestor.scenario_id == scen1.id) & (PhaseAncestor.phase_id.in_(descendent_ids))).count() == 0) # ensure that no phases reference these deleted phases too self.assertTrue(self.session.query(PhaseAncestor).filter((PhaseAncestor.scenario_id == scen1.id) & (PhaseAncestor.ancestor_phase_id.in_(descendent_ids))).count() == 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()