src/analysis.py
1"""Streaming synthetic emergency extraction and auditable spatial fusion."""
2import io,json,math,tarfile
3from datetime import datetime,timedelta
4import numpy as np
5from rasterio.io import MemoryFile
6from pyproj import Transformer
7from shapely.geometry import Point,box,shape,mapping
8from shapely.ops import transform,unary_union
9from polygon_region import selection
10
11CRS='EPSG:32648';STRIDE=50
12
13class ChunkReader(io.RawIOBase):
14 def __init__(self,chunks):self.chunks=iter(chunks);self.pending=memoryview(b'')
15 def readable(self):return True
16 def readinto(self,target):
17 while not self.pending:
18 try:self.pending=memoryview(next(self.chunks))
19 except StopIteration:return 0
20 count=min(len(target),len(self.pending));target[:count]=self.pending[:count];self.pending=self.pending[count:];return count
21
22def bundle(chunks):
23 with io.BufferedReader(ChunkReader(chunks),buffer_size=65536) as stream:
24 with tarfile.open(fileobj=stream,mode='r|') as archive:
25 for member in archive:
26 if not member.isfile() or member.size>48*1024*1024 or '..' in member.name.split('/'):raise ValueError('Invalid emergency bundle member')
27 yield member.name,archive.extractfile(member).read()
28 while stream.read(65536):pass
29
30def read_json(chunks):
31 data=bytearray()
32 for part in chunks:
33 data.extend(part)
34 if len(data)>8*1024*1024:raise ValueError('Emergency JSON exceeds 8 MB limit')
35 return json.loads(data)
36
37def source_status(kind,params):
38 observed={'terrain':'2026-08-01T00:00:00Z','imagery':'2026-08-14T12:00:00Z','hydrology':'2026-08-14T08:00:00Z','exposure':'2026-08-01T00:00:00Z'}[kind]
39 maximum={'terrain':8760,'imagery':72,'hydrology':24,'exposure':8760}[kind]
40 event=datetime.fromisoformat(params['event']['time'].replace('Z','+00:00'));assessment=event+timedelta(hours=12);date=datetime.fromisoformat(observed.replace('Z','+00:00'))
41 simulation=params.get('simulation','none');status='available';reason='在演示时效范围内'
42 if simulation=='missing_imagery' and kind=='imagery':status='missing';reason='用户选择的缺测演示'
43 elif simulation=='stale_hydrology' and kind=='hydrology':status='stale';reason='用户选择的过期观测演示'
44 elif date>assessment:status='unavailable';reason='观测晚于本次研判参考时刻'
45 elif (assessment-date).total_seconds()/3600>maximum:status='stale';reason='超过该类数据的演示时效阈值'
46 elif kind=='imagery' and date<event:status='unavailable';reason='未提供事件后的遥感观测'
47 return {'source':kind,'status':status,'observed_at':observed,'assessment_at':assessment.isoformat(),'maximum_age_hours':maximum,'reason':reason,'synthetic':True}
48
49def extract(chunks,params,kind):
50 region=selection(params['region']);cells=[];metadata=None;before=None;scanned=0;sampled=0;status=source_status(kind,params);to_geo=Transformer.from_crs(CRS,4326,always_xy=True)
51 for name,data in bundle(chunks):
52 if name.endswith('/metadata.json'):metadata=json.loads(data);before=None;continue
53 if not metadata:raise ValueError('Missing raster tile metadata')
54 if not name.endswith('.tif'):continue
55 scanned+=1
56 if status['status']!='available' or not region.intersects(box(*metadata['bbox_wgs84'])):continue
57 with MemoryFile(data) as memory,memory.open() as raster:
58 if str(raster.crs)!=CRS or raster.count!=2 or raster.width*raster.height>3_000_000:raise ValueError('Unexpected emergency raster contract')
59 values=raster.read();affine=raster.transform
60 if not np.isfinite(values).all():raise ValueError('Nonfinite emergency raster values')
61 if kind=='imagery' and name.endswith('/before.tif'):before=values;continue
62 if kind=='imagery':
63 if before is None or before.shape!=values.shape:raise ValueError('Missing or inconsistent before/after pair')
64 difference=np.mean(np.abs(values-before),axis=0);before=None
65 for row in range(0,raster.height,STRIDE):
66 for col in range(0,raster.width,STRIDE):
67 rr=min(row+STRIDE,raster.height);cc=min(col+STRIDE,raster.width)
68 west,north=affine*(col,row);east,south=affine*(cc,rr)
69 projected=box(west,south,east,north);geographic=transform(to_geo.transform,projected)
70 if not region.intersects(geographic):continue
71 item={'id':f"{metadata['tile']}/{row}/{col}",'bounds':[west,south,east,north]}
72 view=values[:,row:rr,col:cc]
73 if kind=='terrain':item.update(elevation_m=round(float(view[0].mean()),2),slope_deg=round(float(view[1].mean()),2))
74 elif kind=='imagery':item.update(change_fraction=round(float((difference[row:rr,col:cc]>.08).mean()),4),mean_spectral_change=round(float(difference[row:rr,col:cc].mean()),4))
75 else:item.update(rainfall_mm=round(float(view[0].mean())*params['event']['rainfall_mm']/180,2),river_distance_m=round(float(view[1].mean()),2))
76 cells.append(item);sampled+=1
77 return {'region':params['region'],'event':params['event'],'source':status,'cells':cells,'raster_members_scanned':scanned,'sample_cells':sampled,'analysis_cell_m':500,'synthetic':True}
78
79def fuse(terrain,imagery,hydrology,exposure,params):
80 region=selection(params['region']);event=params['event'];kind=event['type']
81 for value in [terrain,imagery,hydrology]:
82 if value['region']!=params['region'] or value['event']!=event:raise ValueError('Emergency selection/event mismatch')
83 sources=[v['source'] for v in [terrain,imagery,hydrology]]+[source_status('exposure',params)]
84 required={'terrain','imagery','exposure'}|({'hydrology'} if kind=='mudslide' else set())
85 complete=all(s['status']=='available' for s in sources if s['source'] in required)
86 warnings=[f"{s['source']}: {s['reason']}" for s in sources if s['status']!='available']
87 to_metric=Transformer.from_crs(4326,CRS,always_xy=True);to_geo=Transformer.from_crs(CRS,4326,always_xy=True);project=lambda geometry:transform(to_metric.transform,geometry);unproject=lambda geometry:transform(to_geo.transform,geometry)
88 region_metric=project(region);event_x,event_y=to_metric.transform(event['longitude'],event['latitude']);changes={c['id']:c for c in imagery['cells']};rain={c['id']:c for c in hydrology['cells']};polygons=[];evidence=[]
89 if complete:
90 for cell in terrain['cells']:
91 image=changes.get(cell['id']);water=rain.get(cell['id'])
92 if image is None:continue
93 w,s,e,n=cell['bounds'];distance=math.hypot((w+e)/2-event_x,(s+n)/2-event_y)/1000
94 if distance>event['radius_km']:continue
95 change=image['change_fraction'];slope=cell['slope_deg']
96 if kind=='earthquake':
97 score=.55*change+.35*max(0,1-distance/event['radius_km'])*min(1,event['magnitude']/7)+.1*min(1,slope/40)
98 candidate=change>=params.get('change_threshold',.15) and score>=.35
99 else:
100 if water is None:continue
101 score=.4*change+.35*min(1,water['rainfall_mm']/200)+.25*min(1,slope/35)
102 candidate=water['rainfall_mm']>=120 and 12<=slope<=45 and water['river_distance_m']<=2000 and change>=params.get('change_threshold',.15) and score>=.45
103 if candidate:
104 clipped=box(w,s,e,n).intersection(region_metric)
105 if clipped.area>0:polygons.append(clipped);evidence.append({'cell':cell['id'],'score':round(score,3),'distance_km':round(distance,2),'slope_deg':slope,'change_fraction':change,'rainfall_mm':water['rainfall_mm'] if water else None})
106 affected=unary_union(polygons) if polygons else __import__('shapely').geometry.GeometryCollection();features=[];road_km=0;settlements=0;people=0;facilities=0
107 if not affected.is_empty:
108 parts=list(affected.geoms) if hasattr(affected,'geoms') else [affected]
109 for i,part in enumerate(parts):features.append({'type':'Feature','geometry':mapping(unproject(part).intersection(region)),'properties':{'id':f'impact-{i+1:03}','kind':'suspected_area','assessment':'suspected','name':f'疑似影响区 {i+1}','area_km2':round(part.area/1e6,3),'basis':'合成影像变化与事件、地形约束;泥石流另使用降雨及沟道距离'}})
110 for feature in exposure['features']:
111 geometry=shape(feature['geometry']);clipped=geometry.intersection(region)
112 if clipped.is_empty:continue
113 properties=dict(feature['properties']);metric=project(clipped);hit=complete and metric.intersects(affected);properties['assessment']='unknown' if not complete else 'suspected' if hit else 'not_detected'
114 properties['assessment_label']={'unknown':'数据不足,无法评估','suspected':'疑似受影响','not_detected':'当前数据未检出影响(非安全判定)'}[properties['assessment']]
115 if properties['kind']=='road':
116 length=metric.intersection(affected).length if complete else None;properties['affected_length_m']=None if length is None else round(length,1)
117 if length is not None:road_km+=length/1000
118 elif hit and properties['kind']=='settlement':settlements+=1;people+=properties.get('population',0)
119 elif hit:facilities+=1
120 features.append({'type':'Feature','geometry':mapping(clipped),'properties':properties})
121 return {'synthetic':True,'event':event,'region':params['region'],'region_bbox':list(region.bounds),'complete':complete,'warnings':warnings,'sources':sources,'geojson':{'type':'FeatureCollection','features':features},'summary':{'suspected_area_km2':round(affected.area/1e6,3) if complete else None,'affected_road_km':round(road_km,2) if complete else None,'affected_settlements':settlements if complete else None,'exposed_population':people if complete else None,'affected_facilities':facilities if complete else None},'evidence':evidence[:2000],'analysis_cell_m':500,'method':{'name':'Synthetic spectral-change and terrain/hydrology screening','imagery_change':'fraction of pixels with mean absolute green/NIR reflectance change > 0.08','earthquake':'change >= threshold; event radius; heuristic score >= 0.35 (not a shaking or damage model)','mudslide':'rain >= 120 mm; slope 12–45 deg; channel distance <= 2000 m; change >= threshold; heuristic score >= 0.45','context':'Whole source tiles are streamed sequentially; analysis uses 500 m cells and exact Polygon clipping. Outputs are suspected areas, not verified disaster observations.','reference_time':'event time + 12 hours','simulation':params.get('simulation','none')},'data_status':'complete' if complete else 'insufficient'}
122
123def process(ctx):
124 params=dict(ctx.params);selection(params['region']);operation=params['operation']
125 if operation in ('terrain','imagery','hydrology'):result=extract(ctx.input('source').bytes(),params,operation)
126 elif operation=='fusion':
127 # The streaming SDK consumes named inputs once in lexical order.
128 inputs={name:read_json(ctx.input(name).bytes()) for name in ('exposure','hydrology','imagery','terrain')}
129 result=fuse(inputs['terrain'],inputs['imagery'],inputs['hydrology'],inputs['exposure'],params)
130 else:raise ValueError('Unknown emergency operation')
131 out=ctx.bytes_output('result','json');out.write(json.dumps(result,allow_nan=False).encode())
132
133def infer_outputs(inputs,params):
134 from xstream_operator import descriptor,result
135 return result({'result':descriptor(['bytes'],['json'],None,certainty='known')})
136
137def preview_outputs(inputs,params,limits):
138 from xstream_operator import preview_process
139 return preview_process(process,inputs,params,limits)
140
141if __name__=='__main__':
142 from xstream_operator import run_operator
143 run_operator(process,infer_outputs,preview_outputs=preview_outputs)