Exemplo n.º 1
0
class TestLatLonBbox:
    @pytest.mark.parametrize(
        "latlon_in, latlon_out, zoom",
        [
            ((28.304380682962783, -15.468750000000012), (28.3, -15.5), 0),
            ((28.304380682962783, -15.468750000000012), (28.30, -15.47), 5),
            ((28.304380682962783, -15.468750000000012), (28.3044, -15.4688), 12),
            ((0.0, 0.0), (0.0, 0.0), 42),
            ((-89.86367491884421, 75.43308649874739), (-89.8637, 75.4331), 12),
        ],
    )
    def test_truncate_precision(self, latlon_in, latlon_out, zoom):
        latlon_in = LatLon(*latlon_in)
        res = geo.truncate_latlon_precision(latlon_in, zoom)
        assert res == LatLon(*latlon_out)

    @pytest.mark.parametrize(
        "roundtrip",
        [False, True],
    )
    @pytest.mark.parametrize(
        "pixels, latlon, zoom, tile_size",
        [
            ((245, 153), (-33.1, 164.5), 0, 256),
            ((256, 173), (-53.3, 180.0), 0, 256),
            ((1, 149), (-28.3, -178.6), 0, 256),
            ((4, 164), (-45.1, -174.4), 0, 256),
            ((0, 0), (85.1, -180.0), 0, 256),
            ((128, 128), (0.0, 0.0), 0, 256),
            ((256, 256), (-85.1, 180.0), 0, 256),
            ((987, 808), (-71.5, 167.0), 0, 1024),
            ((525, 761), (41.9, -87.7), 3, 256),
        ],
    )
    def test_pixels_to_lat_lon(self, pixels, latlon, zoom, tile_size, roundtrip):
        pixels = Pixel(*pixels)
        latlon = LatLon(*latlon)
        print(pixels, latlon, zoom, tile_size, roundtrip)
        result_lat_lon = geo.pixels_to_lat_lon(pixels, zoom, tile_size)
        result_pixels = geo.lat_lon_to_pixels(result_lat_lon, zoom, tile_size)
        assert result_lat_lon == latlon
        if not roundtrip:
            assert result_pixels == pixels

    @pytest.mark.parametrize(
        "latlon, pixels, zoom",
        [
            (LatLon(lat=6.7, lon=109.2), Pixel(206, 123), 0),
        ],
    )
    def test_lat_lon_to_pixels(self, latlon, zoom, pixels):
        res = geo.lat_lon_to_pixels(latlon, zoom)
        assert res == pixels
        # res2 = geo.pixels_to_lat_lon(pixels, zoom)
        # assert res2 == latlon

    @pytest.mark.parametrize(
        "bbox, other_bbox, result",
        [
            (
                LatLonBBox(left=-180.0, right=180.0, top=90.0, bottom=-90.0),
                LatLonBBox(left=-180.0, right=180.0, top=90.0, bottom=-90.0),
                True,
            ),
            (
                LatLonBBox(left=-90.0, right=90.0, top=45.0, bottom=-45.0),
                LatLonBBox(left=-180.0, right=180.0, top=90.0, bottom=-90.0),
                False,
            ),
            (
                LatLonBBox(left=-90.0, right=90.0, top=45.0, bottom=-45.0),
                LatLonBBox(left=-90.0, right=90.0, top=44.99, bottom=-45.0),
                True,
            ),
        ],
    )
    def test_contains(self, bbox, other_bbox, result):
        assert bbox.contains(other_bbox) == result

    @pytest.mark.parametrize(
        "pixels_bbox, zoom, tile_size, truncate, expected",
        [
            (
                PixBbox(0, 0, 255, 255),
                0,
                256,
                True,
                LatLonBBox(bottom=-84.9, left=-180.0, top=85.1, right=178.6),
            ),
            # (PixBbox(1, 149, 256, 173), 0, 256, True, LatLonBBox(0, 0, 0, 0)),
        ],
    )
    def test_pixel_bbox_to_latlon_bbox(
        self, pixels_bbox, zoom, tile_size, truncate, expected
    ):
        res = geo.bounding_pixels_to_lat_lon(pixels_bbox, zoom, tile_size, truncate)
        assert res == expected

    @pytest.mark.parametrize(
        "latlon_tl, latlon_br",
        [
            (LatLon(lat=45, lon=90), LatLon(lat=-45, lon=-90)),
            (LatLon(lat=90, lon=180), LatLon(lat=-90, lon=-180)),
            (LatLon(lat=90, lon=180), LatLon(lat=0, lon=0)),
            (LatLon(lat=0, lon=0), LatLon(lat=-90, lon=-180)),
        ],
    )
    def test_bbox_lat_clamp(self, latlon_tl, latlon_br):
        test_bbox = LatLonBBox(
            left=latlon_tl.lon,
            top=latlon_tl.lat,
            right=latlon_br.lon,
            bottom=latlon_br.lat,
        )
        test_bbox.clamp_lat()
        print(test_bbox)
        assert test_bbox.top <= constants.max_latitude
        assert test_bbox.bottom >= -constants.max_latitude
Exemplo n.º 2
0
 def test_equal(self, in_bbox, comp):
     print(PixBbox(*in_bbox))
     assert PixBbox(*in_bbox) == comp
Exemplo n.º 3
0
class TestLatLonBbox:
    @pytest.mark.parametrize(
        "in_bbox",
        [
            (-54.75, -68.25, -54.85, -68.35),
        ],
    )
    def test_creation(self, in_bbox):
        res_bbox = LatLonBBox(*in_bbox)
        assert res_bbox == in_bbox

    @pytest.mark.parametrize(
        "not_eq",
        [
            True,
            False,
        ],
    )
    @pytest.mark.parametrize(
        "in_bbox, comp",
        [
            (
                (
                    -90.0,
                    -45.0,
                    90.0,
                    45.0,
                ),
                (-90.0, -45.0, 90.0, 45.0),
            ),
            (
                (
                    1,
                    2,
                    3,
                    4,
                ),
                [1, 2, 3, 4],
            ),
            (
                (
                    1,
                    2,
                    3,
                    4,
                ),
                LatLonBBox(1, 2, 3, 4),
            ),
        ],
    )
    def test_comparison(self, in_bbox, comp, not_eq):
        res_bbox = LatLonBBox(*in_bbox)
        if not_eq:
            assert res_bbox != tuple(reversed(in_bbox))
        else:
            assert res_bbox == comp

    @pytest.mark.parametrize(
        "in_bbox, srs",
        [
            ((-54.75, -68.25, -54.85, -68.35), "EPSG:4326"),
        ],
    )
    def test_comparison_srs(self, in_bbox, srs):
        assert LatLonBBox(*in_bbox) != LatLonBBox(*in_bbox, srs=srs)

    @pytest.mark.parametrize(
        "in_bbox",
        [
            (1, 2, 3, 4),
        ],
    )
    def test_get_set_alt(self, in_bbox):
        res_bbox = LatLonBBox(*in_bbox)
        assert res_bbox.maxlat == in_bbox[1]
        res_bbox.left = 7
        assert res_bbox.left == 7
        assert res_bbox.west == 7

        res_bbox.minx = 3
        assert res_bbox.left == 3
        assert res_bbox.west == 3

    def test_property_aliases(self):
        bbx = LatLonBBox(-110.39, 24.06, -110.25, 24.17)
        assert bbx.xy_dims == bbx.range

    def test_property_direct(self):
        a = LatLonBBox(20.0, 40.0, 40.0, -40.0).center
        assert a == LatLon(30.0, 0.0)
        assert type(a) == LatLon

    @pytest.mark.parametrize(
        "in_bbox, prop, res",
        [
            (LatLonBBox(-180.0, 90.0, 180.0, -90.0), "tl", LatLon(90.0, -180.0)),
            (LatLonBBox(-180.0, 90.0, 180.0, -90.0), "br", LatLon(-90.0, 180)),
            (LatLonBBox(-54.75, -68.25, -54.85, -68.35), "tl", LatLon(-68.25, -54.75)),
            (LatLonBBox(-54.75, -68.25, -54.85, -68.35), "br", LatLon(-68.35, -54.85)),
            (LatLonBBox(-54.75, -68.25, -54.85, -68.35), "xy_dims", (0.1, 0.1)),
            (LatLonBBox(20.0, 40.0, 40.0, -40.0), "xy_dims", (20.0, 80.0)),
            (LatLonBBox(1.0, 2.0, 1.0, 4.0), "xy_dims", (0.0, 2.0)),
            (
                LatLonBBox(-54.75, -68.25, -54.85, -68.35),
                "center",
                LatLon(-54.80, -68.30),
            ),
        ],
    )
    def test_properties(self, in_bbox, prop, res):
        a = object.__getattribute__(in_bbox, prop)
        assert pytest.approx(a) == res
        assert type(a) == type(res)

    def test_area_ni(self):
        with pytest.raises(NotImplementedError):
            _ = LatLonBBox(12, 45, 39, 124).area

    @pytest.mark.parametrize(
        "latlon, zoom, pixels",
        [
            (
                LatLonBBox(west=-180.0, north=85.0, east=180.0, south=-85.0),
                0,
                PixBbox(left=0, top=0, right=256, bottom=256),
            ),
            (
                LatLonBBox(-54.75, -68.25, -54.85, -68.35),
                4,
                PixBbox(1425, 3123, 1424, 3126),
            ),
            (
                LatLonBBox(-54.75, 68.25, 54.85, -68.35),
                5,
                PixBbox(2850, 1945, 5344, 6253),
            ),
            (
                LatLonBBox(20.0, 40.0, 40.0, -40.0),
                0,
                PixBbox(142, 97, 156, 159),
            ),
            (
                LatLonBBox(-54.75, -68.25, -54.85, -68.35),
                15,
                PixBbox(2918537, 6396732, 2916206, 6403034),
            ),
        ],
    )
    def test_latlon_to_pixels(self, latlon, zoom, pixels):
        res = geo.bounding_lat_lon_to_pixels(latlon, zoom)
        print("res", res)
        assert [a for a in res] == [a for a in pixels]
        # coordinate origin is lower left corner.
        # make sure the results are in this order.
        assert res.left >= res.left
        assert res.top <= res.bottom

    @pytest.mark.parametrize(
        "latlon, result",
        [
            (
                LatLonBBox(north=45.0, south=-45.0, west=-90.0, east=90.0),
                None,
            ),
            (
                LatLonBBox(north=45.0, south=-45.0, west=90.0, east=-90.0),
                (
                    LatLonBBox(north=45.0, south=-45.0, west=90.0, east=180.0),
                    LatLonBBox(north=45.0, south=-45.0, west=-180.0, east=-90.0),
                ),
            ),
            (
                LatLonBBox(left=165, top=-29, right=185, bottom=-53),
                (
                    LatLonBBox(left=165, top=-29, right=180.0, bottom=-53),
                    LatLonBBox(left=-180.0, top=-29, right=-175, bottom=-53),
                ),
            ),
            (
                LatLonBBox(left=-185, top=-29, right=-165, bottom=-53),
                (
                    LatLonBBox(left=175.0, top=-29, right=180, bottom=-53),
                    LatLonBBox(left=-180, top=-29, right=-165, bottom=-53),
                ),
            ),
            (
                LatLonBBox(left=99, top=72, right=379, bottom=9),
                (
                    LatLonBBox(left=99, top=72, right=180, bottom=9),
                    LatLonBBox(left=-180, top=72, right=19, bottom=9),
                ),
            ),
        ],
    )
    def test_antimeridian_split(self, latlon, result):
        res = latlon.am_split()
        if res is None:
            assert res == result
        else:
            assert res[0] == result[0]
            assert res[1] == result[1]
Exemplo n.º 4
0
 def test_corners(self, in_bbox):
     res_bbox = PixBbox(*in_bbox)
     assert res_bbox.left == in_bbox[0]
     assert res_bbox.top == in_bbox[1]
     assert res_bbox.right == in_bbox[2]
     assert res_bbox.bottom == in_bbox[3]
Exemplo n.º 5
0
 def test_creation(self, in_bbox):
     res_bbox = PixBbox(*in_bbox)
     assert res_bbox == in_bbox
Exemplo n.º 6
0
class TestImage:
    test_img_path = Path("./static_maps/tests/images")

    # @staticmethod
    def open_image(self, fn):
        p = self.test_img_path / Path(fn)
        with open(p, "rb") as f:
            return Image.open(f).copy()

    @staticmethod
    def compare_images(a, b):
        return list(a.getdata()) == list(b.getdata())

    @pytest.mark.parametrize(
        "transparency",
        [0, 64, 128, 200],
    )
    def test_composite_transparency(self, transparency):
        """
        Tests compositing a mixed transparency image on a background and then changing the transparency of that image to a baseline.
        """
        foreground = self.open_image("transparency-test-paw_RGBA.png")
        background_rgb = self.open_image("background_RGB.png")
        background_rgba = self.open_image("background_RGBA.png")

        res1 = imager.transparency_composite(a=background_rgb,
                                             b=foreground,
                                             t=transparency)
        res2 = imager.transparency_composite(a=background_rgba,
                                             b=foreground,
                                             t=transparency)

        exp1 = self.open_image(f"result-RGB_comp-trans_{transparency}.png")
        exp2 = self.open_image(f"result-RGBA_comp-trans_{transparency}.png")

        assert self.compare_images(res1, exp1)
        assert self.compare_images(res2, exp2)

    def test_monkeypatch_getbbox(self):
        res = Image.new("RGBA", (256, 256), (255, 255, 255, 255))
        bb = res.getbbox()
        print(bb, type(bb))
        print("image", Image)
        assert bb.left == 0
        assert bb.right == 256
        assert bb.top == 0
        assert bb.bottom == 256

    def test_swap(self):
        img = self.open_image("test_image_RGBA.png")
        swap = imager.swap_left_right(img)
        exp = self.open_image("result_swap_lr.png")
        assert self.compare_images(swap, exp)
        back = imager.swap_left_right(swap)
        assert self.compare_images(back, img)

    @pytest.mark.parametrize(
        "images, result",
        [
            (
                (
                    "test_comp_2x2-0_0011_768.png",
                    "test_comp_2x2-1_1021_768.png",
                    "test_comp_2x2-2_0112_768.png",
                    "test_comp_2x2-3_1122_768.png",
                ),
                "result-composite_2x2-1536.png",
            ),
        ],
    )
    def test_composite_split_2x2(self, images, result):
        """
        Tests compositing 4 images together into a 2x2 grid and then splitting them back again.
        """
        expected = self.open_image(result)
        imgs = [self.open_image(fn) for fn in images]
        idxs = [(x + 1, y + 2) for x, y in ((0, 0), (1, 0), (0, 1), (1, 1))]
        idx_imgs = {k: v for k, v in zip(idxs, imgs)}
        res = imager.composite_mxn(idx_imgs)
        assert self.compare_images(res, expected)

        # And back again.
        sp = imager.quad_split(res)
        assert all([self.compare_images(*a) for a in zip(sp, imgs)])

    @pytest.mark.parametrize(
        "left_image, right_image, result, error",
        [
            (
                "test_paste_halves_left.png",
                "test_paste_halves_right.png",
                "result_paste_halves.png",
                None,
            ),
            (
                "test_paste_halves_left.png",
                "test_paste_halves_right-2x.png",
                "result_paste_halves-2x.png",
                None,
            ),
            (
                "test_paste_halves_left.png",
                "test_paste_halves_right.png",
                "",
                imager.MixedImageModesError,
            ),
            (
                "test_paste_halves_left.png",
                "test_paste_halves_right.png",
                "",
                imager.CompositingError,
            ),
        ],
    )
    def test_paste_halves(self, left_image, right_image, result, error):
        left = self.open_image(left_image)
        right = self.open_image(right_image)
        if error is imager.MixedImageModesError:
            left.mode = "RGBA"
            right.mode = "RGB"
        if error is imager.CompositingError:
            left = left.crop((0, 0, left.size[0], left.size[1] // 2))
        if not error:
            result = self.open_image(result)
            res = imager.paste_halves(left, right)
            res.save("imph.png")
            assert self.compare_images(result, res)
        else:
            with pytest.raises(error):
                _ = imager.paste_halves(left, right)

    @pytest.mark.parametrize(
        "image_fn, crop_size, expected_bounds",
        [
            (
                "test_crop_bounds_512-max.png",
                512,
                PixBbox(256, 256, 768, 768),
            ),
            (
                "test_crop_bounds_512-normal.png",
                512,
                PixBbox(109, 132, 621, 644),
            ),
            (
                "test_crop_bounds_512-am.png",
                512,
                PixBbox(128, 128, 640, 640),
            ),
        ],
    )
    def test_find_crop_bounds(self, image_fn, crop_size, expected_bounds):
        image = self.open_image(image_fn)
        fitted_crop, center_crop = imager.find_crop_bounds(image, crop_size)
        assert fitted_crop == expected_bounds
Exemplo n.º 7
0
class TestBbox:
    @pytest.mark.parametrize(
        "in_bbox",
        [
            (
                1,
                2,
                3,
                4,
            ),
        ],
    )
    def test_creation(self, in_bbox):
        res_bbox = PixBbox(*in_bbox)
        assert res_bbox == in_bbox

    @pytest.mark.parametrize(
        "in_bbox",
        [
            (
                1,
                2,
                3,
                4,
            ),
        ],
    )
    def test_corners(self, in_bbox):
        res_bbox = PixBbox(*in_bbox)
        assert res_bbox.left == in_bbox[0]
        assert res_bbox.top == in_bbox[1]
        assert res_bbox.right == in_bbox[2]
        assert res_bbox.bottom == in_bbox[3]

    @pytest.mark.parametrize(
        "in_bbox, comp",
        [
            (
                (
                    1,
                    2,
                    3,
                    4,
                ),
                (1, 2, 3, 4),
            ),
            (
                (
                    1,
                    2,
                    3,
                    4,
                ),
                [1, 2, 3, 4],
            ),
            (
                (
                    1,
                    2,
                    3,
                    4,
                ),
                PixBbox(1, 2, 3, 4),
            ),
        ],
    )
    def test_equal(self, in_bbox, comp):
        print(PixBbox(*in_bbox))
        assert PixBbox(*in_bbox) == comp

    @pytest.mark.parametrize(
        "in_bbox, comp",
        [
            (
                (
                    1,
                    2,
                    3,
                    0,
                ),
                (1, 2, 3, 4),
            ),
            (
                (
                    1,
                    2,
                    3,
                    0,
                ),
                [1, 2, 3, 4],
            ),
            (
                (
                    1,
                    2,
                    3,
                    0,
                ),
                PixBbox(1, 2, 3, 4),
            ),
        ],
    )
    def test_not_equal(self, in_bbox, comp):
        print(PixBbox(*in_bbox))
        assert PixBbox(*in_bbox) != comp

    @pytest.mark.parametrize(
        "in_bbox, center",
        [
            (
                (
                    0,
                    0,
                    0,
                    0,
                ),
                (0, 0),
            ),
            (
                (
                    0,
                    0,
                    128,
                    128,
                ),
                (64, 64),
            ),
            ((12, 45, 39, 124), (26, 84)),
        ],
    )
    def test_center(self, in_bbox, center):
        r = PixBbox(*in_bbox)
        assert r.center == Pixel(*center)
        assert type(r.center) == Pixel

    @pytest.mark.parametrize(
        "in_bbox, result",
        [
            (
                PixBbox(left=0, top=256, right=256, bottom=0),
                (0, 0, 256, 256),
            ),
            (
                PixBbox(left=216, top=512, right=728, bottom=1024),
                (216, 512, 728, 1024),
            ),
        ],
    )
    def test_pillow(self, in_bbox, result):
        assert in_bbox.pillow == result
Exemplo n.º 8
0
 def test_center(self, in_bbox, center):
     r = PixBbox(*in_bbox)
     assert r.center == Pixel(*center)
     assert type(r.center) == Pixel