"""Prepare permitted public Noksan JSON. Never reads private fire/casualty coordinates."""
from pathlib import Path
import argparse,json,hashlib
from shapely.geometry import Polygon
R=Path(__file__).resolve().parents[1]
p=argparse.ArgumentParser();p.add_argument('--source-root',type=Path,default=R.parent);args=p.parse_args()
source=args.source_root/'works/industrial-fire/data/noksan.json';raw=source.read_bytes();d=json.loads(raw)
bs=[]
for b in d['buildings']:
 poly=Polygon(b['p'][0],b['p'][1:]);center=poly.centroid
 bs.append({'p':b['p'],'center':[center.x,center.y],'h':b['h'],'y':b['y'],'z':b['z']})
out={'source':d['source'],'asof':d['asof'],'crs':d['crs'],'license':'User-authorized reuse of the public anonymised Noksan JSON; source building data MOLIT / KOGL Type 1 (see G1 ledger).','zonesMeaning':'9 basic postal-zone labels, not the paper’s 20 blocks; no boundary polygons','buildings':bs}
folder=R/'spatial-statistics/data';folder.mkdir(parents=True,exist_ok=True)
(folder/'noksan.json').write_text(json.dumps(out,ensure_ascii=False,separators=(',',':')))
audit={'source':'works/industrial-fire/data/noksan.json','sha256':hashlib.sha256(raw).hexdigest(),'buildings':len(bs),'missingYear':sum(b['y'] is None for b in bs),'minimumYear':min(b['y'] for b in bs if b['y']),'knownYears':sum(bool(b['y']) for b in bs),'asof':'2025-07-25','geometry':'original local metre polygon rings; centroids independently computed with Shapely','analysisDefinition':'centroid counts / all occupied cells or count>=5 for autocorrelation; equal grid area; unknown years excluded from share denominator','deferred':['paper 20-block boundaries','administrative-dong boundaries']}
(folder/'source-audit.json').write_text(json.dumps(audit,ensure_ascii=False,indent=2));print(audit)
