from pathlib import Path from collections import Counter import json,os,time,hashlib import numpy as np,cv2,shapely from shapely.geometry import shape,Polygon,LineString,mapping from shapely.ops import transform from scipy.ndimage import distance_transform_edt from layer_common import O,S,B,feature,parts,sample from fit_scene import world from plan_primitives import regularize_paint from frontage_chains import join_planes from shared_surface_edges import reconcile_features from paint_templates import fit_symbol from crosswalk_family import rectangle_info,joint_rectangles cv2.setNumThreads(3);start=time.monotonic();old=json.loads(Path('/fullplan/full-map.json').read_text());base=json.loads((O/'whole-map.json').read_text());tiles=[t for t in json.loads((S/'tile-index.json').read_text())['tiles']if t['status']=='surveyed'] with np.load('/fullplan/fields.npz')as f:labels=f['labels'];height=f['z'];low,high=f['bounds'];cell=float(f['cell']) road=labels==1 # Reconcile the accepted physical edges with the fresh coverage polygons. # Original labels remain evidence for downstream checks, never edited to fit. areas=[f for f in old['features']if f['properties']['class']=='pedestrian_area'] boundaries=[f for f in base['features']if f['properties']['class']=='road_boundary'] def classify(xy): return sample(labels,xy,low,high,cell=cell) areas,topology=reconcile_features(areas,boundaries,classify) walk=areas+[f for f in old['features']if f['properties']['class']=='pedestrian_boundary'] (O/'surface-topology.json').write_text(json.dumps(topology,allow_nan=False)) def lift(g): def project(x,y,z=None):return x,y,sample(height,np.c_[x,y],low,high) return transform(project,g) allfacades=[f for f in old['features']if f['properties']['class']=='building_facade'];rawfacades=[];rejected_facades=[] with np.load(O/'occluder-field.npz')as occ:vehicle_field=occ['vehicle'] for item in allfacades: line=shapely.force_2d(shape(item['geometry']));p=np.array([line.interpolate(t).coords[0]for t in np.linspace(0,line.length,max(3,int(line.length/.2)))]) fraction=float(np.mean(sample(vehicle_field,p,low,high)>.5)) if line.length<12 and fraction>.35:rejected_facades.append(dict(source_id=item['id'],reason='vehicle-supported-vertical-plane',vehicle_fraction=fraction));continue rawfacades.append(item) (O/'rejected-facades.json').write_text(json.dumps(rejected_facades,indent=2)) planes=[shapely.force_2d(shape(f['geometry']))for f in rawfacades] def facade_bridge_allowed(line,sources): novel=line.difference(shapely.union_all(sources).buffer(.2)) for segment in parts(novel,'LineString'): if segment.length<.4:continue p=np.array([segment.interpolate(t).coords[0]for t in np.linspace(0,segment.length,max(3,int(segment.length/.2)))]) if np.mean(sample(labels,p,low,high)==1)>.2:return False return True joined=join_planes(planes,offset=.45,gap=8.,bridge_validator=facade_bridge_allowed);facades=[] for p,ids in joined: facades.append(feature(lift(LineString(p)),'building_facade',method='huber-frontage-chain-with-bounded-infill',source_ids=[rawfacades[i]['id']for i in ids],closed_footprint=False,hidden_backs_invented=False,schematic_join=True)) result={**base,'schema_version':'missioncore.systematic-plan/v1','features':base['features']+walk+facades,'logical_layers':['roads','pedestrian_zones','buildings','road_markings'],'review_status':'unreviewed','surface_topology':topology} assert all(shape(f['geometry']).is_valid and np.isfinite(shapely.get_coordinates(shape(f['geometry']),include_z=True)).all() for f in result['features']) (O/'full-map.json').write_text(json.dumps(result,allow_nan=False)) (O/'structure-report.json').write_text(json.dumps(dict(facades=len(facades),manual_rois=[],paint_search=False,shared_edges=len(topology['edges']),shared_length_m=sum(shape(e['geometry']).length for e in topology['edges']),surface_assembly='shared-observed-edges',original_evidence_unchanged=True,seconds=time.monotonic()-start)))