import json,hashlib from pathlib import Path import numpy as np from shapely.geometry import shape from shapely.ops import unary_union root=Path('/final');candidate=json.loads((root/'whole-map.json').read_text());source=json.loads(Path('/continuity/whole.json').read_text());audit=json.loads((root/'scene-assembly.json').read_text()) curbs=[f for f in candidate['features']if f['properties']['class']=='road_boundary'];geoms=[shape(f['geometry'])for f in curbs] assert all(g.is_simple and not g.is_ring for g in geoms) assert all(np.isfinite(np.asarray(g.coords)).all()for g in geoms) contacts=[] for i,a in enumerate(geoms): for j,b in enumerate(geoms[i+1:],i+1): intersection=a.intersection(b) if not intersection.is_empty:contacts.append(dict(ids=[curbs[i]['id'],curbs[j]['id']],geometry_type=intersection.geom_type,length_m=intersection.length)) assert not contacts,contacts fragments=json.loads((root/'global-quarantine.json').read_text())['features'] removed=set(audit['quarantined_parent_ids']);removed.update(s['id']for f in fragments for c in f['properties']['construction']for s in c['parents']) intervals={} for f in curbs: for part in f['properties']['construction']: for s in part['parents']:intervals.setdefault(s['id'],[]).append(s['interval_m']) accounting=[] for f in source['features']: if f['properties']['class']!='road_boundary':continue identity=f['id'];length=shape(f['geometry']).length if identity in removed: assert identity not in intervals,identity accounting.append(dict(id=identity,source_m=length,quarantined=True));continue values=sorted(intervals.get(identity,[]));cursor=0.;overlap=0.;gap=0. for a,b in values: gap+=max(0.,a-cursor);overlap+=max(0.,cursor-a);cursor=max(cursor,b) gap+=max(0.,length-cursor) assert gap<1e-4 and overlap<1e-4,(identity,gap,overlap) accounting.append(dict(id=identity,source_m=length,intervals=len(values),unaccounted_m=gap,duplicate_m=overlap)) assert candidate['coordinate_system']==source['coordinate_system'] assert [f for f in source['features']if f['properties']['class']!='road_boundary']==[f for f in candidate['features']if f['properties']['class']!='road_boundary'] graph=json.loads((root/'boundary-graph.json').read_text());nodes={n['id']:n for n in graph['nodes']};assert len(graph['edges'])==len(curbs) for edge in graph['edges']: assert nodes[edge['start_node']]['coordinates']==edge['geometry']['coordinates'][0] assert nodes[edge['end_node']]['coordinates']==edge['geometry']['coordinates'][-1] spans=0 for f in curbs: line=shape(f['geometry']);xy=line for s in f['properties']['inferred_spans']: spans+=1;g=shape(dict(type='LineString',coordinates=s['coordinates'])) assert g.difference(xy.buffer(1e-6)).length<1e-5,(f['id'],s['method']) result=dict(status='passed',scene_version='1d64edcfeeb7466b',curbs=len(curbs),all_pair_contacts=contacts,source_accounting=accounting,source_chains_accounted=len(accounting),non_curbs_preserved=723,graph_nodes=len(nodes),graph_edges=len(graph['edges']),inferred_spans_on_final_geometry=spans,world_map_sha256=hashlib.sha256((root/'whole-map.json').read_bytes()).hexdigest()) Path('/out/independent-audit.json').write_text(json.dumps(result,indent=2));print(json.dumps({k:v for k,v in result.items()if k!='source_accounting'}))