import json
def pip(pt, ring):
    x,y = pt; inside=False; n=len(ring)
    for i in range(n):
        x1,y1 = ring[i]; x2,y2 = ring[(i+1)%n]
        if (y1>y) != (y2>y):
            xi = x1 + (y-y1)*(x2-x1)/(y2-y1)
            if x < xi: inside = not inside
    return inside
def hit(pt, f):
    d = json.load(open(f))
    return any(pip(pt,p['ring']) for p in d['polygons'])
T = [
 ("haz_liquefaction.json","Marina",(-122.4370,37.8035),True),
 ("haz_liquefaction.json","Mission Bay",(-122.3930,37.7700),True),
 ("haz_liquefaction.json","Treasure Island",(-122.3700,37.8230),True),
 ("haz_liquefaction.json","Twin Peaks",(-122.4470,37.7530),False),
 ("haz_liquefaction.json","Noe Valley",(-122.4330,37.7513),False),
 ("haz_tsunami.json","Ocean Beach",(-122.5107,37.7594),True),
 ("haz_tsunami.json","Embarcadero",(-122.3937,37.7955),True),
 ("haz_tsunami.json","Twin Peaks",(-122.4470,37.7530),False),
]
allpass=True
for f,name,pt,exp in T:
    got = hit(pt,f)
    ok = (got==exp); allpass &= ok
    print(f"{'PASS' if ok else 'FAIL'}  {f.replace('haz_','').replace('.json',''):13s} {name:16s} expected={'IN ' if exp else 'OUT'} got={'IN ' if got else 'OUT'}")
print("ALL PASS" if allpass else "SOME FAILED")
