Ejemplo n.º 1
0
def trigger_page():
    """
    Renders the trigger page. This page contains an instance of a TriggerForm.
    The TriggerForm contains input fields for the simulation. If the submit
    button is pressed, a simulation is triggered and the results are generated
    using the Visualiser class. If all succeeds, forward to endpoint "visualise"
    """
    form = TriggerForm()

    # if the form is valid and the submit button was pressed
    if form.validate_on_submit():

        # get the static path from the current application to store the visualisations
        static_path = os.path.join(current_app.root_path,
                                   current_app.static_folder)

        # clean up the previous simulation results
        Cleaner().remove_previous_simulation_results(static_path=static_path)

        # retrieve number of requests from the form
        number_of_requests = form.number_of_requests_field.data

        # Create an instance of the bounding box class, using the form data
        bounding_box = BoundingBox((form.x1_field.data, form.y1_field.data,
                                    form.x2_field.data, form.y2_field.data))

        # Create an instance of the StaticDataReader class.
        static_data = StaticDataReader(
            berlin_bounds_file=current_app.config['BERLIN_BOUNDS_FILE'],
            berlin_stops_file=current_app.config['BERLIN_STOPS_FILE'])

        # Create an instance of the Simulator class.
        simulator = Simulator(
            bounding_box=bounding_box.bounding_box,
            path_to_stops=current_app.config['BERLIN_STOPS_FILE'])
        # Run a simulation
        simulation_results = simulator.simulate(number_of_requests)

        # Create an instance of the Visualiser class.
        visualiser = Visualiser(bounding_box=bounding_box,
                                simulation_results=simulation_results,
                                static_path=static_path,
                                static_data=static_data)

        # Generate visualisations
        visualiser.generate_overview_figure()
        visualiser.generate_closeup_figure()
        visualiser.generate_gmap()

        # redirect to the visualise endpoint
        return redirect(
            url_for('routes.visualise', visualiser_id=visualiser.id))

    # render a template for the trigger page.
    return render_template('trigger_page.html',
                           title="MI Code Challenge",
                           form=form)
class TestVisualiser(unittest.TestCase):
    """
    Testcase for the Visualiser Class
    """
    @classmethod
    def setUpClass(self):
        """
        Sets up the class. The data only has to be read in once, so this is done in the class setup method.
        """
        # TODO: Remove warn ngs filter when migrating to geopandas > 0.7.0 !
        warnings.simplefilter('ignore', category=FutureWarning)
        warnings.simplefilter('ignore', category=DeprecationWarning)

        static_data = StaticDataReader('data/berlin_bounds.poly',
                                       'data/berlin_stops.geojson')
        bounding_box_tuple = (13.34014892578125, 52.52791908000258,
                              13.506317138671875, 52.562995039558004)
        simulator = Simulator(bounding_box=bounding_box_tuple)
        simulation_results = simulator.simulate(number_of_requests=6)

        self.visualiser = Visualiser(
            bounding_box=BoundingBox(bounding_box_tuple),
            simulation_results=simulation_results,
            static_data=static_data,
            static_path='webapp/static')

    def setUp(self):
        pass

    def test_generate_overview_figure(self):
        """
        Asserts a overview figure is generated after calling the function.
        """
        self.visualiser.generate_overview_figure()

        self.assertTrue(
            isfile(
                f"{self.visualiser.static_path}/{self.visualiser.id}_overview_plot.png"
            ))

    def test_generate_closeup_figure(self):
        """
        Asserts a closeup figure is generated after calling the function.
        """
        self.visualiser.generate_closeup_figure()

        self.assertTrue(
            isfile(
                f"{self.visualiser.static_path}/{self.visualiser.id}_closeup_plot.png"
            ))

    def test_generate_gmap(self):
        """
        Asserts a html map is generated after calling the function.
        """
        self.visualiser.generate_gmap()

        self.assertTrue(
            isfile(
                f"{self.visualiser.static_path}/{self.visualiser.id}_map.html")
        )

    def tearDown(self):
        pass

    if __name__ == "__main__":
        unittest.main()