Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
81 lines
4.5 KiB
Python
81 lines
4.5 KiB
Python
"""Compose observed terminal junctions from independently admitted strips.
|
|
|
|
This does not admit raw proposals. It consumes the immutable admitted bank;
|
|
the exact union is one physical object, never two overlapping output objects.
|
|
Alternative axes, parallel tracks, interior crossings and gaps stay conflicts.
|
|
"""
|
|
import hashlib,json
|
|
import numpy as np
|
|
import shapely
|
|
from shapely.geometry import shape,mapping
|
|
from beta_profiles import describe
|
|
from marking_rules import POLICY,VERSION
|
|
from layer_common import feature
|
|
|
|
|
|
def geometry(feature):
|
|
return shapely.force_2d(shape(feature['geometry']))
|
|
|
|
|
|
def identity(transaction):
|
|
value=dict(sources=sorted(transaction['source_ids']),
|
|
after=sorted(f['id']for f in transaction['after']),kind=transaction['kind'])
|
|
return hashlib.sha256(json.dumps(value,sort_keys=True).encode()).hexdigest()
|
|
|
|
|
|
def junction(a,b):
|
|
"""A measured overlap must occupy a terminal cap of each distinct axis."""
|
|
da,db=describe(a),describe(b)
|
|
if da is None or db is None:return None
|
|
if abs(da['axis']@db['axis'])>=np.cos(np.deg2rad(POLICY['max_join_angle_deg'])):return None
|
|
shared=a.intersection(b)
|
|
if shared.area<=POLICY['max_overlap_m2']:return None
|
|
for d in [da,db]:
|
|
t=(shapely.get_coordinates(shared)-d['origin'])@d['axis']
|
|
cap=d['width']+POLICY['retained_paint_tolerance_m']
|
|
# End caps must be disjoint: a short near-square patch has no
|
|
# identifiable terminal junction, even if its fitted axes differ.
|
|
if 2*cap>=d['length']:return None
|
|
if not(t.min()>=d['length']/2-cap or t.max()<=-d['length']/2+cap):return None
|
|
merged=a.union(b)
|
|
if merged.geom_type!='Polygon' or not merged.is_valid:return None
|
|
return merged
|
|
|
|
|
|
def compose(configurations,lift):
|
|
"""One composition per measured neighboring pair; no recursive closure."""
|
|
allowed={'observed-finite-stroke','whole-observed-finite-stroke',
|
|
'observed-continuous-addition','whole-observed-continuous-stroke','observed-continuous-extension'}
|
|
unique={identity(c):c for c in configurations if c['kind']in allowed and len(c['after'])==1
|
|
and not c.get('atomic_family') and c['after'][0]['properties'].get('subtype')=='linear_marking'}
|
|
keys=sorted(unique);records=[unique[k]for k in keys]
|
|
polygons=[geometry(c['after'][0])for c in records];tree=shapely.STRtree(polygons)
|
|
output=[];checked=0
|
|
for i,(a,pa) in enumerate(zip(records,polygons)):
|
|
for j in sorted(int(k)for k in tree.query(pa,predicate='intersects')if k>i):
|
|
b=records[j]
|
|
if set(a['source_ids'])&set(b['source_ids']):continue
|
|
checked+=1;merged=junction(pa,polygons[j])
|
|
if merged is None:continue
|
|
members=[keys[i],keys[j]];family=hashlib.sha256(json.dumps(members).encode()).hexdigest()[:20]
|
|
sources=sorted(set(a['source_ids'])|set(b['source_ids']))
|
|
tiles=sorted(set(a['source_tiles'])|set(b['source_tiles']))
|
|
before={f['id']:f for c in [a,b]for f in c['before']}
|
|
# Existing evidence is retained by exact set union, with no hull,
|
|
# padding, snapping, endpoint extension or reused historical map.
|
|
f=feature(lift(merged),'road_marking',subtype='linear_marking',line_kind='observed-connected-junction',
|
|
method=VERSION,source_ids=sources,source_tiles=tiles,family_id=family,observed_paint=True,
|
|
standard_compliance='not_assessed',endpoint_authority='admitted-member-strips',
|
|
member_ids=[c['after'][0]['id']for c in [a,b]],
|
|
member_widths_m=[describe(p)['width']for p in [pa,polygons[j]]])
|
|
output.append(dict(kind='whole-observed-terminal-junction',source_ids=sources,output_ids=[f['id']],
|
|
before=[before[k]for k in sorted(before)],after=[f],source_tiles=tiles,
|
|
source_views=[a['source_views'],b['source_views']],profile_mode='admitted-terminal-union',
|
|
family_id=family,bounds_xy=merged.bounds,domain=None,atomic_family=False,
|
|
seam_members=[a['after'][0],b['after'][0]],
|
|
evidence=dict(member_configurations=members,member_admission=[a['evidence'],b['evidence']],
|
|
exact_union=True,added_area_m2=0.,removed_area_m2=0.,junction_area_m2=pa.intersection(polygons[j]).area)))
|
|
return output,dict(input_configurations=len(configurations),eligible_strips=len(records),
|
|
intersecting_pairs=checked,junctions=len(output),recursive_combinations=False,additional_image_reads=0,
|
|
overlap_threshold_unchanged=POLICY['max_overlap_m2'])
|