|
| 1 | +# Project: MapCache |
| 2 | +# Purpose: Test MapCache disk based storage backend |
| 3 | +# Author: Maris Nartiss |
| 4 | +# |
| 5 | +# ***************************************************************************** |
| 6 | +# Copyright (c) 2025 Regents of the University of Minnesota. |
| 7 | +# |
| 8 | +# Permission is hereby granted, free of charge, to any person obtaining a |
| 9 | +# copy of this software and associated documentation files (the "Software"), |
| 10 | +# to deal in the Software without restriction, including without limitation |
| 11 | +# the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 12 | +# and/or sell copies of the Software, and to permit persons to whom the |
| 13 | +# Software is furnished to do so, subject to the following conditions: |
| 14 | +# |
| 15 | +# The above copyright notice and this permission notice shall be included in |
| 16 | +# all copies of this Software or works derived from this Software. |
| 17 | +# |
| 18 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 19 | +# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 21 | +# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 23 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 24 | +# DEALINGS IN THE SOFTWARE. |
| 25 | +# ****************************************************************************/ |
| 26 | + |
| 27 | +import os |
| 28 | +import pytest |
| 29 | +import numpy as np |
| 30 | +import logging |
| 31 | + |
| 32 | +from osgeo import gdal |
| 33 | + |
| 34 | +# Import the GeoTIFF generation function |
| 35 | +from generate_synthetic_geotiff import generate_synthetic_geotiff |
| 36 | + |
| 37 | +# Import generic verification functions and constants |
| 38 | +from verification_core import ( |
| 39 | + TILE_SIZE, |
| 40 | + TILE_CACHE_BASE_DIR, |
| 41 | + TEMP_MAPCACHE_CONFIG_DIR, |
| 42 | + calculate_expected_tile_data, |
| 43 | + compare_tile_arrays, |
| 44 | + cleanup, |
| 45 | + run_seeder, |
| 46 | + create_temp_mapcache_config, |
| 47 | +) |
| 48 | + |
| 49 | +# --- Configuration --- # |
| 50 | +SYNTHETIC_GEOTIFF_FILENAME = os.path.join( |
| 51 | + TEMP_MAPCACHE_CONFIG_DIR, "synthetic_test_data.tif" |
| 52 | +) |
| 53 | +GEOTIFF_WIDTH = 512 |
| 54 | +GEOTIFF_HEIGHT = 512 |
| 55 | +MAPCACHE_TEMPLATE_CONFIG = "../data/mapcache_backend_template.xml" |
| 56 | + |
| 57 | +# --- Grid Parameters --- # |
| 58 | +INITIAL_RESOLUTION = 1000 |
| 59 | +ORIGIN_X = -500000 |
| 60 | +ORIGIN_Y = 500000 |
| 61 | + |
| 62 | + |
| 63 | +def read_tile(tile_path, tile_size=TILE_SIZE): |
| 64 | + if not os.path.exists(tile_path): |
| 65 | + logging.error(f"Error: Actual tile not found at {tile_path}") |
| 66 | + return None |
| 67 | + |
| 68 | + actual_ds = gdal.Open(tile_path, gdal.GA_ReadOnly) |
| 69 | + if actual_ds is None: |
| 70 | + logging.error(f"Error: Could not open actual tile {tile_path}") |
| 71 | + return None |
| 72 | + |
| 73 | + # Read all bands from the actual tile |
| 74 | + actual_tile_data = np.zeros( |
| 75 | + (tile_size, tile_size, actual_ds.RasterCount), dtype=np.uint8 |
| 76 | + ) |
| 77 | + for i in range(actual_ds.RasterCount): |
| 78 | + actual_tile_data[:, :, i] = actual_ds.GetRasterBand(i + 1).ReadAsArray() |
| 79 | + |
| 80 | + actual_ds = None # Close the dataset |
| 81 | + |
| 82 | + # Mapcache might output 4 bands (RGBA) even if source is 3 bands. Handle this. |
| 83 | + # If actual_tile_data has 4 bands, ignore the alpha band for comparison. |
| 84 | + if actual_tile_data.shape[2] == 4: |
| 85 | + actual_tile_data_rgb = actual_tile_data[:, :, :3] # Take only RGB bands |
| 86 | + elif actual_tile_data.shape[2] == 3: |
| 87 | + actual_tile_data_rgb = actual_tile_data |
| 88 | + else: |
| 89 | + logging.error( |
| 90 | + f"Error: Unexpected number of bands in actual tile: {actual_tile_data.shape[2]}" |
| 91 | + ) |
| 92 | + return None |
| 93 | + |
| 94 | + return actual_tile_data_rgb |
| 95 | + |
| 96 | + |
| 97 | +def run_mapcache_test(zoom, x, y, geotiff_path, initial_resolution, origin_x, origin_y): |
| 98 | + logging.info(f"Running MapCache test for tile Z{zoom}-X{x}-Y{y}...") |
| 99 | + |
| 100 | + # Calculate expected tile data using generic function |
| 101 | + expected_tile_data = calculate_expected_tile_data( |
| 102 | + zoom, |
| 103 | + x, |
| 104 | + y, |
| 105 | + geotiff_path, |
| 106 | + initial_resolution, |
| 107 | + origin_x, |
| 108 | + origin_y, |
| 109 | + ) |
| 110 | + if expected_tile_data is None: |
| 111 | + return False |
| 112 | + |
| 113 | + # --- Read Actual Tile Data --- |
| 114 | + actual_tile_path = os.path.join( |
| 115 | + TILE_CACHE_BASE_DIR, |
| 116 | + "disk", |
| 117 | + "disk-tileset", |
| 118 | + "synthetic_grid", |
| 119 | + f"{zoom:02d}", |
| 120 | + f"{x // 1000000:03d}", |
| 121 | + f"{(x // 1000) % 1000:03d}", |
| 122 | + f"{x % 1000:03d}", |
| 123 | + f"{y // 1000000:03d}", |
| 124 | + f"{(y // 1000) % 1000:03d}", |
| 125 | + f"{y % 1000:03d}.png", |
| 126 | + ) |
| 127 | + |
| 128 | + logging.info(f"Reading tile {actual_tile_path}") |
| 129 | + actual_tile_data_rgb = read_tile(actual_tile_path, TILE_SIZE) |
| 130 | + if actual_tile_data_rgb is None: |
| 131 | + return False |
| 132 | + |
| 133 | + # --- Compare --- |
| 134 | + return compare_tile_arrays(expected_tile_data, actual_tile_data_rgb, zoom, x, y) |
| 135 | + |
| 136 | + |
| 137 | +@pytest.fixture(scope="module") |
| 138 | +def setup_test_environment(request): |
| 139 | + cleanup() |
| 140 | + logging.info("Testing disk storage backend...") |
| 141 | + os.makedirs(TEMP_MAPCACHE_CONFIG_DIR, exist_ok=True) |
| 142 | + generate_synthetic_geotiff( |
| 143 | + output_filename=SYNTHETIC_GEOTIFF_FILENAME, |
| 144 | + width=GEOTIFF_WIDTH, |
| 145 | + height=GEOTIFF_HEIGHT, |
| 146 | + ) |
| 147 | + create_temp_mapcache_config( |
| 148 | + SYNTHETIC_GEOTIFF_FILENAME, |
| 149 | + MAPCACHE_TEMPLATE_CONFIG, |
| 150 | + ) |
| 151 | + run_seeder("disk-tileset", "0,1") |
| 152 | + |
| 153 | + def teardown(): |
| 154 | + cleanup() |
| 155 | + logging.info("Cleanup complete.") |
| 156 | + |
| 157 | + request.addfinalizer(teardown) |
| 158 | + |
| 159 | + |
| 160 | +def test_disk_tiles(setup_test_environment): |
| 161 | + ok0 = run_mapcache_test( |
| 162 | + 0, |
| 163 | + 0, |
| 164 | + 0, |
| 165 | + SYNTHETIC_GEOTIFF_FILENAME, |
| 166 | + INITIAL_RESOLUTION, |
| 167 | + ORIGIN_X, |
| 168 | + ORIGIN_Y, |
| 169 | + ) |
| 170 | + ok1 = run_mapcache_test( |
| 171 | + 1, |
| 172 | + 1, |
| 173 | + 2, |
| 174 | + SYNTHETIC_GEOTIFF_FILENAME, |
| 175 | + INITIAL_RESOLUTION, |
| 176 | + ORIGIN_X, |
| 177 | + ORIGIN_Y, |
| 178 | + ) |
| 179 | + assert ok0 |
| 180 | + assert ok1 |
0 commit comments