corridors.ts
7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* 海上合法走廊(offshore legal corridors)—— 纯几何解析与命中判定,无 IO。
*
* 这是「坐标落水 / 域外」的第二道防线:**第一道**用陆地掩膜把所有海上点标出来
* (见 offshoreVerdict),**这一道**再把其中「落在已知合法跨海构筑物上」的点豁免掉 ——
* 不让深中通道、深圳湾大桥、盐田港、大铲湾码头这类点混进「疑似漂移」清单里让导入人逐条复核。
* 距离阈值救不了它们(深中通道西人工岛离岸 6km、孖洲岛 2.3km,1000m 拐点只会把它们全判成远海),
* 必须按几何命中。
*
* 数据是一份离线 GeoJSON(Polygon / LineString / Point),走廊以 **WGS-84** 存储
* (与陆地掩膜同一套系)。命中判定与全项目米↔度换算同一套口径(等距圆柱近似)。
*
* 容灾:解析失败由调用方处理(`parseCorridors` 抛错),命中判定对 null 直接返回未命中,
* 退化成「只用陆地掩膜」,不会误杀也不会卡住。
*/
/** 1° 经线米数(与全项目米↔度换算同一口径) */
const M_PER_EQUATOR = 111_195;
/** 1° 纬线米数 */
const M_PER_DEG_LAT = 110_700;
import { pointInPolygon, type LngLat, type ShapePolygon } from './polygon';
/** 一条海上合法走廊:线(path)或面(polygon,用于岛/港区整体),外加缓冲半径 */
export interface CorridorFeature {
/** 可读名,写进报告给人审计(深中通道 / 盐田国际集装箱码头 …) */
name: string;
/** 缓冲半径(米):点到线 / 面边界 ≤ 此值即视为落在合法构筑物上 */
bufferM: number;
/** 折线集合,每条折线是一串 WGS-84 [lng,lat]。单点(Point)就是一条退化成点的折线 */
paths: LngLat[][];
/** 可选面(Polygon 的环数组),整片视为合法(岛 / 港区陆域)。点在面内或距边界 ≤ bufferM 即命中 */
polygon?: LngLat[][];
}
export interface CorridorMatch {
hit: boolean;
name: string;
}
/* ------------------------------- 解析 GeoJSON ------------------------------- */
/**
* 把离线 GeoJSON 解析成 CorridorFeature[]。
*
* 支持三种几何:LineString / MultiLineString(缓冲沿线)、Point(退化为单点 + 缓冲)、
* Polygon(整片合法,点在面内或距边界 ≤ bufferM)。每条 feature 用 properties.name 命名、
* properties.bufferM 覆盖默认缓冲(缺省 200m)。
*/
export function parseCorridors(raw: unknown): CorridorFeature[] {
const fc = raw as { type?: string; features?: unknown[] };
if (!fc || fc.type !== 'FeatureCollection' || !Array.isArray(fc.features)) {
throw new Error('走廊数据不是 FeatureCollection');
}
const out: CorridorFeature[] = [];
for (const f of fc.features) {
const feature = f as {
properties?: Record<string, unknown>;
geometry?: { type?: string; coordinates?: unknown };
};
const props = feature.properties ?? {};
const name = typeof props.name === 'string' && props.name ? props.name : '海上合法构筑物';
const bufferM = Number.isFinite(Number(props.bufferM)) ? Number(props.bufferM) : 200;
const geom = feature.geometry;
if (!geom || !geom.type || !Array.isArray(geom.coordinates)) continue;
const paths: LngLat[][] = [];
let polygon: LngLat[][] | undefined;
if (geom.type === 'LineString') {
paths.push(coordsToLngLat(geom.coordinates));
} else if (geom.type === 'MultiLineString') {
for (const line of geom.coordinates as unknown[]) paths.push(coordsToLngLat(line));
} else if (geom.type === 'Point') {
// Point 的 coordinates 是单个位置 [lng,lat](不是位置数组),单独处理
const c = geom.coordinates as unknown[];
if (Array.isArray(c) && c.length >= 2) {
const lng = Number(c[0]);
const lat = Number(c[1]);
if (Number.isFinite(lng) && Number.isFinite(lat)) paths.push([[lng, lat]]);
}
} else if (geom.type === 'Polygon') {
polygon = (geom.coordinates as unknown[]).map((ring) => coordsToLngLat(ring));
} else {
continue; // 其余类型(MultiPolygon 等)暂时不支持,跳过这条不影响其它
}
if (paths.length === 0 && !polygon) continue;
out.push({ name, bufferM, paths, polygon });
}
return out;
}
function coordsToLngLat(raw: unknown): LngLat[] {
if (!Array.isArray(raw)) return [];
const out: LngLat[] = [];
for (const pos of raw) {
if (!Array.isArray(pos) || pos.length < 2) continue;
const lng = Number(pos[0]);
const lat = Number(pos[1]);
if (!Number.isFinite(lng) || !Number.isFinite(lat)) continue;
out.push([lng, lat]);
}
return out;
}
/* ------------------------------- 命中判定 ------------------------------- */
/**
* 一个 WGS-84 点是否落在任一合法走廊内(含缓冲)。
*
* 距离用本地等距圆柱近似:把经纬度按「该点纬度」投影成米,再算点到折线 / 面边界的欧氏距离。
* 与全项目米↔度换算同一套口径。
*
* @param lng 已归一化到 WGS-84 的经度(案件点请先 gcj02ToWgs84)
* @param lat 已归一化到 WGS-84 的纬度
* @param corridors null 时(数据未就绪)直接返回未命中,交回上层按陆地掩膜处理
*/
export function pointInLegalCorridor(
lng: number,
lat: number,
corridors: CorridorFeature[] | null,
): CorridorMatch {
if (!corridors) return { hit: false, name: '' };
// 该点所在纬度下的经线米数(随纬度收缩)
const kx = M_PER_EQUATOR * Math.cos((lat * Math.PI) / 180);
const ky = M_PER_DEG_LAT;
const px = lng * kx;
const py = lat * ky;
for (const c of corridors) {
// 面:点在面内即命中(整片合法),否则看距边界多近
if (c.polygon) {
const poly: ShapePolygon = { rings: c.polygon };
if (pointInPolygon(lng, lat, poly)) return { hit: true, name: c.name };
if (distToPolygonBoundaryM(px, py, c.polygon, kx, ky) <= c.bufferM) {
return { hit: true, name: c.name };
}
}
// 线(含 Point 退化成的单点):距任一折线段 ≤ bufferM 即命中
for (const path of c.paths) {
if (distToPathM(px, py, path, kx, ky) <= c.bufferM) return { hit: true, name: c.name };
}
}
return { hit: false, name: '' };
}
/** 点到一条折线的距离(米),投影后逐段取最短 */
function distToPathM(
px: number,
py: number,
path: LngLat[],
kx: number,
ky: number,
): number {
if (path.length === 0 || !path[0]) return Infinity;
if (path.length === 1) {
const ax = path[0][0] * kx;
const ay = path[0][1] * ky;
return Math.hypot(px - ax, py - ay);
}
let best = Infinity;
for (let i = 1; i < path.length; i += 1) {
const ax = path[i - 1][0] * kx;
const ay = path[i - 1][1] * ky;
const bx = path[i][0] * kx;
const by = path[i][1] * ky;
const d = distToSegmentM(px, py, ax, ay, bx, by);
if (d < best) best = d;
}
return best;
}
/** 点到一个 Polygon(所有环的线段)的距离(米) */
function distToPolygonBoundaryM(
px: number,
py: number,
rings: LngLat[][],
kx: number,
ky: number,
): number {
let best = Infinity;
for (const ring of rings) {
const d = distToPathM(px, py, ring, kx, ky);
if (d < best) best = d;
}
return best;
}
/** 点到线段的最短距离(投影空间,米) */
function distToSegmentM(
px: number,
py: number,
ax: number,
ay: number,
bx: number,
by: number,
): number {
const dx = bx - ax;
const dy = by - ay;
const len2 = dx * dx + dy * dy;
let t = len2 > 0 ? ((px - ax) * dx + (py - ay) * dy) / len2 : 0;
if (t < 0) t = 0;
else if (t > 1) t = 1;
const cx = ax + t * dx;
const cy = ay + t * dy;
return Math.hypot(px - cx, py - cy);
}