Esempio n. 1
0
    def test_tile_background_generic_fetch_zoom(self):

        width = 240
        height = 200

        bbox_3857 = [
            -8250483.072013094, -8221819.186406153, 4961221.562116772,
            4985108.133455889, "EPSG:3857"
        ]

        zooms = range(6, 13)
        expected_image_paths = [
            EXPECTED_RESULTS_DIR +
            f"background/wikimedia_zoom_{zoom}_outcome.png" for zoom in zooms
        ]

        background = GenericXYZBackground(thumbnail_width=width,
                                          thumbnail_height=height)

        for zoom, expected_image_path in zip(zooms, expected_image_paths):
            try:
                image = background.fetch(bbox_3857, zoom)
                expected_image = Image.open(expected_image_path)
                diff = Image.new("RGB", image.size)

                mismatch = pixelmatch(image, expected_image, diff)
                self.assertTrue(
                    mismatch < width * height * 0.01,
                    "Expected test and pre-generated backgrounds to differ up to 1%"
                )
            except UnidentifiedImageError as e:
                logger.error(
                    f"It was not possible to fetch the background: {e}")
Esempio n. 2
0
    def test_tile_background_bbox_zoom_calculation(self):
        bboxes_3857 = [
            [
                -8252241.123663656, -8223577.238056716, 4967814.255806367,
                4983101.661463401
            ],
            [
                -8972128.86948389, -7211019.73779343, 4554411.364647665,
                5532805.32669792
            ],
            [
                -1915008.1381942185, 5129428.388567626, -1125203.1711428363,
                2788372.6770581882
            ],
            [
                9669176.372480828, 16713612.899242673, 4035824.978672272,
                7949400.826873297
            ],
        ]

        expected_zooms = [11, 4, 2, 2]

        background = GenericXYZBackground(thumbnail_width=1,
                                          thumbnail_height=1)

        for expected_zoom, bbox in zip(expected_zooms, bboxes_3857):
            bbox4326 = background.bbox3857to4326(*bbox)
            background._mercantile_bbox = [
                bbox4326[0], bbox4326[2], bbox4326[1], bbox4326[3]
            ]

            zoom = background.calculate_zoom()

            self.assertEqual(zoom, expected_zoom,
                             "Calculated zooms should be equal expected")
Esempio n. 3
0
    def test_tile_background_bbox_conversions(self):
        bboxes_3857 = [
            [
                -8252241.123663656, -8223577.238056716, 4967814.255806367,
                4983101.661463401
            ],
            [
                -8972128.86948389, -7211019.73779343, 4554411.364647665,
                5532805.32669792
            ],
            [
                -1915008.1381942185, 5129428.388567626, -1125203.1711428363,
                2788372.6770581882
            ],
            [
                9669176.372480828, 16713612.899242673, 4035824.978672272,
                7949400.826873297
            ],
        ]

        for bbox_3857 in bboxes_3857:
            background = GenericXYZBackground(thumbnail_width=1,
                                              thumbnail_height=1)
            bbox4326 = background.bbox3857to4326(*bbox_3857)
            new_bbox_3857 = background.bbox4326to3857(*bbox4326)

            self.assertEqual(
                [round(coord, 4) for coord in bbox_3857],
                [round(coord, 4) for coord in new_bbox_3857],
                "Expected converted BBOXes to be equal",
            )
Esempio n. 4
0
    def test_tile_background_retries(self, request_mock):
        request_mock.return_value = (None, None)

        width = 240
        height = 200
        max_retries = 3
        retry_delay = 1

        start = datetime.now()

        with self.assertRaises(UnidentifiedImageError):
            GenericXYZBackground(thumbnail_width=width,
                                 thumbnail_height=height,
                                 max_retries=max_retries,
                                 retry_delay=retry_delay).fetch([
                                     623869.6556559108, 2458358.334500141,
                                     4291621.974352865, 5270015.93640312,
                                     "EPSG:3857"
                                 ])

        end = datetime.now()

        if request_mock.call_count:
            self.assertEqual(
                request_mock.call_count, max_retries,
                f"Expected to {max_retries} number of failing fetches")
            self.assertGreaterEqual(
                (end - start).seconds, max_retries * retry_delay - 1,
                "Expected delay between consecutive failing fetches")
Esempio n. 5
0
    def test_tile_background_generic_fetch(self):

        width = 240
        height = 200

        bbox_3857 = [-8250483.072013094, -8221819.186406153, 4961221.562116772, 4985108.133455889, "EPSG:3857"]
        expected_image_path = f"{EXPECTED_RESULTS_DIR}background/wikimedia_outcome1.png"

        background = GenericXYZBackground(thumbnail_width=width, thumbnail_height=height)
        self._fetch_and_compare_background(background, bbox_3857, expected_image_path)
Esempio n. 6
0
    def test_tile_background_tms_fetch(self):

        width = 240
        height = 200

        bboxes_3857 = [
            [
                -8250483.072013094, -8221819.186406153, 4961221.562116772,
                4985108.133455889, "EPSG:3857"
            ],
            [
                -9990526.32372507, -6321548.96603661, 3335075.3607465066,
                6392556.492153557, "EPSG:3857"
            ],
            [
                -107776710.17911679, 9630565.26691392, -50681609.070756994,
                47157787.134268604, "EPSG:3857"
            ],
            [
                39681312.13711384, 43350289.494802296, 3596795.7455949546,
                6654276.877002003, "EPSG:3857"
            ],
        ]

        expected_results_dir = f"{EXPECTED_RESULTS_DIR}background/"
        expected_images_paths = [
            f"{expected_results_dir}tms_outcome1.png",
            f"{expected_results_dir}tms_outcome2.png",
            f"{expected_results_dir}tms_outcome3.png",
            f"{expected_results_dir}tms_outcome4.png",
        ]

        background = GenericXYZBackground(thumbnail_width=width,
                                          thumbnail_height=height)

        for bbox, expected_image_path in zip(bboxes_3857,
                                             expected_images_paths):
            self._fetch_and_compare_background(background, bbox,
                                               expected_image_path)