import tempfile import unittest import xml.etree.ElementTree as ET import zipfile from pathlib import Path import numpy as np from shapely import STRtree from shapely.geometry import box import main class SignalRulesTest(unittest.TestCase): def test_signal_level_boundaries(self): self.assertEqual(main.signal_level(None), "无采集信息") self.assertEqual(main.signal_level(-105), "较差") self.assertEqual(main.signal_level(-95), "一般") self.assertEqual(main.signal_level(-94.9), "正常") def test_signal_raster_colors(self): values = np.array([[-106.0, -105.0, -100.0, -95.0, -94.0]], dtype=np.float32) colors = main.heatmap_rgb_for_values(values) self.assertEqual(colors.tolist(), [[[255, 0, 0], [255, 0, 0], [255, 255, 0], [255, 255, 0], [0, 176, 80]]]) class RankedHeatmapTest(unittest.TestCase): def test_equal_values_are_not_split_between_colors(self): values = np.array([[10.0, 0.0, 0.0], [0.0, 0.0, 0.0]], dtype=np.float32) mask = np.ones(values.shape, dtype=bool) result, thresholds, counts = main.ranked_pixel_rgb_for_values( values, mask, main.WEAK_COVER_COLORS, main.WEAK_COVER_PERCENT_STEPS, ) self.assertEqual(thresholds, [0.0, 0.0]) self.assertEqual(counts, [1, 0, 5]) self.assertTrue(np.all(result[values == 0] == main.WEAK_COVER_COLORS[-1])) self.assertTrue(np.all(result[values == 10] == main.WEAK_COVER_COLORS[0])) def test_invalid_color_threshold_configuration_fails(self): values = np.array([[1.0]], dtype=np.float32) mask = np.ones(values.shape, dtype=bool) with self.assertRaises(ValueError): main.ranked_pixel_rgb_for_values(values, mask, ((255, 0, 0),), (0.5,)) class SpatialNameIndexTest(unittest.TestCase): def test_largest_intersection_wins(self): geometries = [box(0, 0, 1, 1), box(0, 0, 2, 2)] index = (STRtree(geometries), geometries, ["小建筑", "大建筑"]) self.assertEqual(main.match_building_info_name(index, box(0, 0, 1.5, 1.5)), "大建筑") def test_batch_matching_matches_scalar_results(self): info_geometries = [box(0, 0, 1, 1), box(0, 0, 2, 2), box(5, 5, 6, 6)] index = (STRtree(info_geometries), info_geometries, ["小建筑", "大建筑", "远端建筑"]) buildings = [box(0, 0, 1.5, 1.5), box(5.2, 5.2, 5.8, 5.8), box(10, 10, 11, 11)] expected = [main.match_building_info_name(index, geometry) for geometry in buildings] self.assertEqual(main.match_building_info_names(index, buildings), expected) class GaussianBlurTest(unittest.TestCase): def test_batch_convolution_matches_scalar_reference(self): array = np.arange(30, dtype=np.float32).reshape(5, 6) kernel = main.gaussian_kernel(2) radius = kernel.size // 2 padded = np.pad(array, ((radius, radius), (0, 0)), mode="edge") moved = np.moveaxis(padded, 0, 0) expected = np.empty_like(array) for index in range(expected.shape[0]): expected[index] = np.tensordot(kernel, moved[index:index + kernel.size], axes=(0, 0)) actual = main.convolve_axis(array, kernel, axis=0) np.testing.assert_allclose(actual, expected, rtol=1e-6, atol=1e-6) padded = np.pad(array, ((0, 0), (radius, radius)), mode="edge") moved = np.moveaxis(padded, 1, 0) expected = np.empty_like(np.moveaxis(array, 1, 0)) for index in range(expected.shape[0]): expected[index] = np.tensordot(kernel, moved[index:index + kernel.size], axes=(0, 0)) actual = main.convolve_axis(array, kernel, axis=1) np.testing.assert_allclose(actual, np.moveaxis(expected, 0, 1), rtol=1e-6, atol=1e-6) class OutputSafetyTest(unittest.TestCase): def test_raster_layout_respects_pixel_limit(self): original_limit = main.MAX_RASTER_PIXELS main.MAX_RASTER_PIXELS = 100 try: layout = main.market_raster_layout((0, 0, 10, 10), 0.1, 0.1) finally: main.MAX_RASTER_PIXELS = original_limit self.assertLessEqual(layout[6] * layout[7], 100) self.assertGreater(layout[8], 1.0) def test_zip_output_is_complete(self): with tempfile.TemporaryDirectory() as temp_dir: root = Path(temp_dir) workspace = root / "workspace" workspace.mkdir() (workspace / "a.txt").write_text("a", encoding="utf-8") (workspace / "b.txt").write_text("b", encoding="utf-8") zip_path = root / "market.zip" market = main.MarketFeature(1, "1-test", "test", box(0, 0, 1, 1)) result = main.write_market_zip(workspace, zip_path, market) with zipfile.ZipFile(zip_path) as archive: self.assertEqual(sorted(archive.namelist()), ["a.txt", "b.txt"]) self.assertEqual(result["entries"], ["a.txt", "b.txt"]) self.assertFalse((root / ".market.zip.tmp").exists()) class QgisProjectTest(unittest.TestCase): def test_project_extent_tracks_building_info(self): project_xml = b'''\xef\xbb\xbf 0011 100100200200 BuildingInfo./BuildingInfo.gpkg|layername=BuildingInfo0011 ''' with tempfile.TemporaryDirectory() as temp_dir: root_dir = Path(temp_dir) source_path = root_dir / "source.qgz" output_path = root_dir / "GIS.qgz" with zipfile.ZipFile(source_path, "w", compression=zipfile.ZIP_DEFLATED) as archive: archive.writestr("GIS.qgs", project_xml) archive.writestr("styles.db", b"style-data") market = main.MarketFeature(1, "1-test", "test", box(0, 0, 1, 1)) result = main.copy_gis_project_to_market(source_path, output_path, market, (10, 20, 30, 40)) with zipfile.ZipFile(output_path) as archive: updated_xml = archive.read("GIS.qgs") self.assertTrue(updated_xml.startswith(b"\xef\xbb\xbf