feat: 楼宇查询改为按轮廓 WKT 计算内部点查询
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user