Esempio n. 1
0
def do_the_thing(args):
    graph = Greengraph(args.start, args.end)
    if args.save_png:
        points = graph.location_sequence(graph.geolocate(graph.start),
                                         graph.geolocate(graph.end),
                                         args.steps)
        green = []
        for n, point in enumerate(points):
            _m = Map(*point)
            green.append(_m.count_green())
            with open(args.filename+'_'+str(n)+'.png','w') as f:
                f.write(_m.show_green())
    else:
        green = graph.green_between(args.steps)
    plt.plot(graph.green_between(args.steps))
    plt.savefig(args.filename+'.png')
    plt.show()
Esempio n. 2
0
class test_map:
    def setup(self):
        with open(
            "C://Users//Ross//Documents//UCL//Modules//RSE_Python//greengraph//greengraph//test//fixtures//london.raw",
            "rb",
        ) as f:
            data = f.read()
        self.test_pixels = img.imread(StringIO(data))
        returndata = type("", (object,), {"content": data})()
        requests.get = MagicMock(return_value=returndata)

        self.latitude = "51.5073509"
        self.longitude = "-0.1277583"
        self.mymap = Map(self.latitude, self.longitude)

    @with_setup(setup)
    def test_map_init_defaults_passed_correct(self):
        center = self.latitude + "," + self.longitude
        self.mymap = Map(self.latitude, self.longitude)
        params_used = requests.get.mock_calls[0][2]["params"]
        assert_equal(10, params_used["zoom"])
        assert_equal(center, params_used["center"])
        assert_equal("satellite", params_used["maptype"])
        assert_equal("false", params_used["sensor"])
        assert_equal("x".join(map(str, (400, 400))), params_used["size"])

    @with_setup(setup)
    def test_map_init_params_passed_correct(self):
        zoom = 12
        self.center = self.latitude + "," + self.longitude
        satellite = True
        sensor = True
        size = (450, 450)
        self.mymap = Map(self.latitude, self.longitude, satellite, zoom, size, sensor)
        params_used = requests.get.mock_calls[1][2]["params"]
        assert_equal(zoom, params_used["zoom"])
        assert_equal(self.center, params_used["center"])
        assert_equal("satellite", params_used["maptype"])
        assert_equal("true", params_used["sensor"])
        assert_equal("x".join(map(str, size)), params_used["size"])

    @with_setup(setup)
    def test_map_init_params_satellite_correct(self):
        satellite = False
        self.mymap = Map(self.latitude, self.longitude, satellite)
        params_used = requests.get.mock_calls[1][2]["params"]
        assert_equal(satellite, "maptype" in params_used)

    @with_setup(setup)
    def test_map_init_pixel_array_generated(self):
        # self.mymap = Map(self.latitude,longitude);
        assert_allclose(self.test_pixels, self.mymap.pixels)

    @with_setup(setup)
    def test_count_green_no_green_pixels(self):
        self.mymap.green = MagicMock(return_value=np.zeros((2, 2), dtype=bool))
        result = self.mymap.count_green()
        assert_equal(0, result)

    @with_setup(setup)
    def test_count_green_half_green_pixels(self):
        self.mymap.green = MagicMock(return_value=np.append(np.ones((2, 1), dtype=bool), np.zeros((2, 1), dtype=bool)))
        result = self.mymap.count_green()
        assert_equal(2, result)

    @with_setup(setup)
    def test_count_green_all_green_pixels(self):
        self.mymap.green = MagicMock(return_value=np.ones((2, 2), dtype=bool))
        result = self.mymap.count_green()
        assert_equal(4, result)

    @with_setup(setup)
    def test_count_green_calls_green_with_threshold(self):
        threshold = 1.6
        self.mymap.green = MagicMock()
        self.mymap.count_green(threshold)
        self.mymap.green.assert_called_with(threshold)

    @with_setup(setup)
    def test_show_green_all_green(self):
        self.mymap.green = MagicMock(return_value=np.ones((3, 3), dtype=bool), dtype=bool)
        pixels = img.imread(StringIO(self.mymap.show_green(1.1)))
        greener_than_red = pixels[:, :, 1] > 1 * pixels[:, :, 0]
        greener_than_blue = pixels[:, :, 1] > 1 * pixels[:, :, 2]
        green = np.logical_and(greener_than_red, greener_than_blue)
        assert_equal(9, np.sum(green))

    @with_setup(setup)
    def test_show_green_no_green(self):
        self.mymap.green = MagicMock(return_value=np.zeros((3, 3), dtype=bool), dtype=bool)
        pixels = img.imread(StringIO(self.mymap.show_green(1.1)))
        greener_than_red = pixels[:, :, 1] > 1 * pixels[:, :, 0]
        greener_than_blue = pixels[:, :, 1] > 1 * pixels[:, :, 2]
        green = np.logical_and(greener_than_red, greener_than_blue)
        assert_equal(0, np.sum(green))

    @with_setup(setup)
    def test_show_green_half_green(self):
        return_value_array = np.ones((3, 3), dtype=bool)
        return_value_array[:, 2] = False
        self.mymap.green = MagicMock(return_value=return_value_array)
        pixels = img.imread(StringIO(self.mymap.show_green(1.1)))
        greener_than_red = pixels[:, :, 1] > 1 * pixels[:, :, 0]
        greener_than_blue = pixels[:, :, 1] > 1 * pixels[:, :, 2]
        green = np.logical_and(greener_than_red, greener_than_blue)
        assert_equal(6, np.sum(green))

    @with_setup(setup)
    def test_green_all_green_pixels(self):
        self.mymap.pixels = np.zeros((3, 3, 3))
        self.mymap.pixels[:, :, 1] = 1
        bools = self.mymap.green(1.1)
        assert_equal(9, np.sum(bools))

    @with_setup(setup)
    def test_green_no_green_pixels(self):
        self.mymap.pixels = np.zeros((3, 3, 3))
        bools = self.mymap.green(1.1)
        assert_equal(0, np.sum(bools))

    @with_setup(setup)
    def test_green_all_red_pixels(self):
        self.mymap.pixels = np.zeros((3, 3, 3))
        self.mymap.pixels[:, :, 1] = 1
        self.mymap.pixels[:, :, 0] = 2
        bools = self.mymap.green(1.1)
        assert_equal(0, np.sum(bools))

    @with_setup(setup)
    def test_green_all_blue_pixels(self):
        self.mymap.pixels = np.zeros((3, 3, 3))
        self.mymap.pixels[:, :, 1] = 1
        self.mymap.pixels[:, :, 2] = 2
        bools = self.mymap.green(1.1)
        assert_equal(0, np.sum(bools))

    @with_setup(setup)
    def test_green_zero_threshold(self):
        self.mymap.pixels = np.zeros((3, 3, 3))
        self.mymap.pixels[:, :, 1] = 1
        self.mymap.pixels[:, :, 2] = 2
        bools = self.mymap.green(0)
        assert_equal(9, np.sum(bools))

    @with_setup(setup)
    def test_green_100_threshold(self):
        self.mymap.pixels = np.zeros((3, 3, 3))
        self.mymap.pixels[:, :, 1] = 1
        self.mymap.pixels[:, :, 0] = 0.15
        bools = self.mymap.green(10)
        assert_equal(0, np.sum(bools))