fix: 加快竞对信号和采样点生成
This commit is contained in:
+11
-19
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
import math
|
||||
import sqlite3
|
||||
|
||||
from shapely import Point, centroid, to_wkb
|
||||
from shapely import Point
|
||||
import shapely
|
||||
|
||||
from . import config
|
||||
@@ -87,11 +87,6 @@ def merge_sample_records(five, four, latitude, meters):
|
||||
return merged
|
||||
|
||||
|
||||
def _meters(lon1, lat1, lon2, lat2):
|
||||
scale_x = 111_320 * math.cos(math.radians((lat1 + lat2) / 2))
|
||||
return math.hypot((lon1 - lon2) * scale_x, (lat1 - lat2) * 110_540)
|
||||
|
||||
|
||||
def _bin(x, y, meters):
|
||||
return math.floor(x / meters), math.floor(y / meters)
|
||||
|
||||
@@ -104,8 +99,10 @@ def _collect(conn, layer, rtree, market):
|
||||
pk = quote_identifier(layer.pk_col)
|
||||
geom = quote_identifier(layer.geom_col)
|
||||
selected = [geom, *(quote_identifier(name) for name in columns)]
|
||||
# 栅格是轴对齐矩形,空间索引的外接框就是图形范围,不必把每个图形拆成 Shapely 再写回。
|
||||
query = f"""
|
||||
select b.{pk}, {", ".join(f"b.{name}" for name in selected)}
|
||||
select b.{pk}, {", ".join(f"b.{name}" for name in selected)},
|
||||
r."minx", r."maxx", r."miny", r."maxy"
|
||||
from {quote_identifier(rtree)} r
|
||||
join {table} b on b.{pk} = r."id"
|
||||
where r."minx" <= ? and r."maxx" >= ? and r."miny" <= ? and r."maxy" >= ?
|
||||
@@ -117,32 +114,27 @@ def _collect(conn, layer, rtree, market):
|
||||
cursor = conn.execute(query, (maxx, minx, maxy, miny))
|
||||
for batch, geometries, valid in iter_geometry_batches(cursor, 1):
|
||||
hits = valid & shapely.intersects(market.geometry, geometries)
|
||||
centers = centroid(geometries)
|
||||
for index, row in enumerate(batch):
|
||||
if not valid[index] or not hits[index]:
|
||||
if not hits[index] or row[1] is None:
|
||||
continue
|
||||
center_lon, center_lat = float(centers[index].x), float(centers[index].y)
|
||||
bounds = (row[7], row[9], row[8], row[10])
|
||||
lon, lat = to_float(row[2]), to_float(row[3])
|
||||
# 属性里的中心点若明显不在该栅格上,改用图形中心,避免空值或 0 把不相邻的栅格拼到一起。
|
||||
if lon is None or lat is None or _meters(lon, lat, center_lon, center_lat) > 20:
|
||||
lon, lat = center_lon, center_lat
|
||||
# 属性里的中心点若落在格子外面,改用外接框中心,避免空值或 0 把不相邻的栅格拼到一起。
|
||||
if lon is None or lat is None or not (bounds[0] <= lon <= bounds[2] and bounds[1] <= lat <= bounds[3]):
|
||||
lon = (bounds[0] + bounds[2]) / 2
|
||||
lat = (bounds[1] + bounds[3]) / 2
|
||||
if config.CLIP_MODE == "center" and not market.geometry.covers(Point(lon, lat)):
|
||||
continue
|
||||
values = [to_float(row[offset]) or 0.0 for offset in range(4, 7)]
|
||||
bounds = geometries[index].bounds
|
||||
rows.append({
|
||||
"lon": lon, "lat": lat, "dl": values[0], "gd": values[1], "yd": values[2],
|
||||
"blob": _gp_blob(geometries[index]), "bounds": bounds,
|
||||
"blob": row[1], "bounds": bounds,
|
||||
})
|
||||
finally:
|
||||
shapely.destroy_prepared(market.geometry)
|
||||
return rows
|
||||
|
||||
|
||||
def _gp_blob(geometry):
|
||||
return b"GP" + bytes((0, 1)) + int(SRS_ID).to_bytes(4, "little", signed=True) + to_wkb(geometry)
|
||||
|
||||
|
||||
def _extent(records, fallback):
|
||||
if not records:
|
||||
return fallback
|
||||
|
||||
Reference in New Issue
Block a user