feat: 楼宇查询改为按轮廓 WKT 计算内部点查询

This commit is contained in:
nixevol
2026-09-17 09:22:53 +08:00
parent df69b0192c
commit 48e68469ad
7 changed files with 116 additions and 41 deletions
+45 -3
View File
@@ -11,9 +11,50 @@ from unittest.mock import patch
from aiohttp import web
from aiohttp.test_utils import TestServer
from shapely import wkt
from shapely.geometry import Point
import GetNRBuildingsData as script
def square(x, y, half=0.001):
return (f"MULTIPOLYGON ((({x - half} {y - half},{x + half} {y - half},"
f"{x + half} {y + half},{x - half} {y + half},{x - half} {y - half})))")
class InteriorPointTest(unittest.TestCase):
def test_point_lies_inside_concave_shapes_where_centroid_and_bbox_center_do_not(self):
# U 形:外包框中心与质心都落在缺口里。
u_shape = ("POLYGON ((0 0, 10 0, 10 10, 7 10, 7 3, 3 3, 3 10, 0 10, 0 0))")
# L 形:外包框中心落在外面。
l_shape = ("POLYGON ((0 0, 10 0, 10 2, 2 2, 2 10, 0 10, 0 0))")
for text in (u_shape, l_shape):
geometry = wkt.loads(text)
x1, y1, x2, y2 = geometry.bounds
self.assertFalse(Point((x1 + x2) / 2, (y1 + y2) / 2).within(geometry), text)
lon, lat = script.interior_point(text)
self.assertTrue(Point(float(lon), float(lat)).within(geometry), text)
def test_multipolygon_uses_largest_part_and_invalid_input_is_rejected(self):
text = ("MULTIPOLYGON (((0 0, 1 0, 1 1, 0 1, 0 0)), ((5 5, 9 5, 9 9, 5 9, 5 5)))")
lon, lat = script.interior_point(text)
self.assertTrue(Point(float(lon), float(lat)).within(wkt.loads("POLYGON ((5 5, 9 5, 9 9, 5 9, 5 5))")))
for bad in ("", "not wkt", "POINT (1 1)", "POLYGON EMPTY", None):
with self.assertRaises(ValueError):
script.interior_point(bad)
def test_real_sample_rows_from_source_file(self):
path = Path(script.INPUT_CSV)
if not path.is_file():
self.skipTest("源文件不存在")
with path.open(encoding=script.INPUT_ENCODING, newline="") as source:
reader = csv.DictReader(source)
for _, row in zip(range(200), reader):
geometry = wkt.loads(row["WKT"])
lon, lat = script.interior_point(row["WKT"])
self.assertTrue(Point(float(lon), float(lat)).within(geometry), row["building_id"])
class AsyncBuildingsTest(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
self.directory = tempfile.TemporaryDirectory()
@@ -22,8 +63,9 @@ class AsyncBuildingsTest(unittest.IsolatedAsyncioTestCase):
self.output = Path(self.directory.name) / "results.csv"
with self.input.open("w", encoding="utf-8-sig", newline="") as destination:
writer = csv.writer(destination)
writer.writerow(["序号", "经度", "维度"])
writer.writerows([[str(i), str(i), "22"] for i in range(6)])
writer.writerow(["WKT", "building_id"])
# 以 (i, 22) 为中心的正方形,内部点应为 POINT(i 22)。
writer.writerows([[square(i, 22), str(i)] for i in range(6)])
settings = patch.multiple(script, INPUT_CSV=self.input, OUTPUT_CSV=self.output,
INPUT_ENCODING="utf-8-sig", URL="http://127.0.0.1/wfs", COOKIES="test-session",
START_ROW=1, LIMIT=6, CONCURRENCY=3, REQUEST_INTERVAL=0, MAX_RETRIES=0)
@@ -50,7 +92,7 @@ class AsyncBuildingsTest(unittest.IsolatedAsyncioTestCase):
async def handler(request):
nonlocal active, maximum
identifier = int(re.search(r"POINT\((\d+) 22\)", request.query["CQL_FILTER"]).group(1))
identifier = round(float(re.search(r"POINT\(([-\d.]+) ([-\d.]+)\)", request.query["CQL_FILTER"]).group(1)))
active += 1
maximum = max(maximum, active)
try:
+3 -3
View File
@@ -50,11 +50,11 @@ class TaskIntegrationTest(unittest.TestCase):
self.patch.start()
self.addCleanup(self.patch.stop)
self.addCleanup(self.temp.cleanup)
self.input = self.root / "坐标.csv"
self.input = self.root / "楼宇轮廓.csv"
with self.input.open("w", encoding="utf-8-sig", newline="") as output:
writer = csv.writer(output)
writer.writerow(["序号", "经度", "维度"])
writer.writerows((i, 112+i/1000, 22) for i in range(30))
writer.writerow(["WKT", "building_id"])
writer.writerows((box(112 + i / 1000, 22, 112 + i / 1000 + 0.0005, 22.0005).wkt, f"JM{i}") for i in range(30))
self.params = {"INPUT_CSV": str(self.input), "INPUT_ENCODING": "utf-8-sig", "START_ROW": 1,
"LIMIT": 30, "CONCURRENCY": 2, "TIMEOUT": 5, "MAX_RETRIES": 0, "REQUEST_INTERVAL": 0}