<jupyter_start><jupyter_text>The haversine formula is$$\sin^2 \left(\frac{d}{2R}\right) = \sin^2\left(\frac{\varphi_2-\varphi_1}{2}\right) + \cos \varphi_1 \cos \varphi_2 \sin^2\left(\frac{\lambda_2-\lambda_1}{2}\right)$$or equivalently$$\cos \left(\frac{d}{R}\right) = \sin \varphi_1 \sin \varphi_2  + \cos (\lambda_1 - \lambda_2)\cos \varphi_1 \cos \varphi_2 $$where the (lat, lon) of two points are$$p_1 = (\varphi_1,\lambda_1)\,,\quad p_2 = (\varphi_2,\lambda_2)$$and are bounded by$$ -\frac{\pi}{2} \leq \varphi \leq \frac{\pi}{2}$$$$ -\pi \leq \lambda \leq \pi$$Note that the python package haversine takes degrees as the default arguments If two latitudes are equal, $\varphi_1=\varphi_2=\varphi$ then$$d = 2 R \, \sin^{-1} \cos \varphi \sin \left(\frac{\lambda_2-\lambda_1}{2}\right)$$$$\Rightarrow \quad |\lambda_2-\lambda_1| = 2\left|\sin^{-1}\left[ \frac{\sin\left(\frac{d}{2R}\right)}{\cos(\varphi)}\right]\right|$$ When two longitudes are equal we have$$\varphi_2-\varphi_1 = \frac{d}{R}$$ We want a bounding box centered on $(\varphi_0, \lambda_0)$ of edge-size 2L.$$\lambda_{east} = \lambda_0 + 2\left|\sin^{-1}\left[ \frac{\sin\left(\frac{L}{2R}\right)}{\cos(\varphi_0)}\right]\right|$$$$\lambda_{west} = \lambda_0 - 2\left|\sin^{-1}\left[ \frac{\sin\left(\frac{L}{2R}\right)}{\cos(\varphi_0)}\right]\right|$$$$\varphi_{north} = \varphi_0 + \frac{L}{R}$$$$\varphi_{north} = \varphi_0 - \frac{L}{R}$$<jupyter_code>1==1 and not 1==2
N=1
bbox_length = make_bounding_box_length(suva, places_suva, N)


boundingbox = BoundingBox(suva, 100)
print(boundingbox.bbox)
print(bbox_length)
get_closest_N_points(suva, places_suva, N, 100)

2/3
0==0.0
d = {}
d['FRONT']={'north':1, 'south':3}
d['REVERSE']={'north':2, 'south':4}
for k,v in d.items():
    print(k,v)
ll=[]
l2 = np.array([[1,2],[3,4]])
ll+l2<jupyter_output><empty_output><jupyter_text>alert<jupyter_code>norlisk = (69.3558, 88.1893)
alert = (82.5018, 62.3481)
barneo = (89.5250, -30.4500)
bronlundhus = (82.17166598, -30.667663996)
nagurskoye = (80.8107, 47.6499)
russkoye = (71.1333, 149.2667)
sachsharbour = (71.9851, 125.2465)
places_alert = np.array([norlisk, barneo, bronlundhus, nagurskoye, russkoye, sachsharbour])

distances_alert = [haversine(alert, place) for place in places_alert]
distances_alert
N=3
boundingbox = BoundingBox(alert, 100)
print(boundingbox.bbox)
get_closest_points(alert, places_alert, N, 1300)

print(places_alert)
bbox = {'reverse': {'east': -90.0, 'west': 90.0, 'north': 90.0, 'south': 81.00422457211346}, 'front': {'east': 90.0, 'west': -90.0, 'north': 90.0, 'south': 70.8106191230565}}
def target_in_bounding_box_front(target, bbox):
    return (target[0] >= bbox[SOUTH]) & (target[0] <= bbox[NORTH]) & (target[1] >= bbox[WEST]) & (target[1] <= bbox[EAST])

def target_in_bounding_box_reverse(target, bbox):
    return 1 == (target[0] >= bbox[SOUTH]) & (target[0] <= bbox[NORTH]) & ~((target[1] >= bbox[EAST]) & (target[1] <= bbox[WEST]))

    
print(target_in_bounding_box_front(np.transpose(places_alert), bbox[FRONT]))
print(target_in_bounding_box_reverse(np.transpose(places_alert), bbox[REVERSE]))

places_alert
# distance from alert to the north pole
haversine(alert, (90, 0))
alert
haversine(alert, (90, 0))
haversine((85.02036896289921, 0), (90, 0))
N=2

boundingbox = BoundingBox(alert, 1000)

print(boundingbox.bbox)
get_closest_N_points(alert, places_alert, N, 1000)

{'reverse': {'east': -90.0, 'west': 90.0, 'north': 90.0, 'south': 73.6554533217098}, 'front': {'east': 90.0, 'west': -90.0, 'north': 90.0, 'south': 64.5621212895553}}

bbox_1 = {'east': -90.0, 'west': 90.0, 'north': 90.0, 'south': 73.6554533217098}
bbox_2 = {'east': 90.0, 'west': -90.0, 'north': 90.0, 'south': 64.5621212895553}

def func(latlon, minlat, maxlat, minlon, maxlon):
    return (latlon[0] >= minlat) & (latlon[0] <= maxlat) & (latlon[1] >= minlon) & (latlon[1] <= maxlon) 
bbox = bbox_2
targets = np.transpose(np.array([nagurskoye, nagurskoye, nagurskoye]))
func(targets, bbox[SOUTH], bbox[NORTH], bbox[WEST], bbox[EAST])
def target_in_bounding_box_front(target, bbox):
    return (target[0] >= bbox[SOUTH]) & (target[0] <= bbox[NORTH]) & \
            (target[1] >= bbox[WEST]) & (target[1] <= bbox[EAST])

targets = np.array([nagurskoye, nagurskoye, nagurskoye])
print(targets)
targets[target_in_bounding_box_front(np.transpose(targets), bbox)]
targets.reshape(2,3)
(1>0) & ~((2>1) & (2>3))
True & (False & True)<jupyter_output><empty_output><jupyter_text>Large Random Target set<jupyter_code>N = 10**6
lats = (180 * (random.random(N)-1/2)).reshape(N,1)
lons = (360 * (random.random(N)-1/2)).reshape(N,1)
targets = np.concatenate([lats, lons], axis=1)
N=4
bbox_length = make_bounding_box_length(paris, targets, N)
print(bbox_length)
start = time.time()
pts = get_closest_points(paris, targets, N, 30)
print(time.time()-start)
print(pts)
bbox = {'reverse': {'east': -90.0, 'west': 90.0, 'north': 90.0, 'south': 39.156748256370705}, 'front': {'east': 90.0, 'west': -90.0, 'north': 90.0, 'south': -12.750277357138522}}

def target_in_bounding_box_front(target, bbox):
    return (target[0] >= bbox[SOUTH]) & (target[0] <= bbox[NORTH]) & (target[1] >= bbox[WEST]) & (target[1] <= bbox[EAST])

def target_in_bounding_box_reverse(target, bbox):
    return 1 == (target[0] >= bbox[SOUTH]) & (target[0] <= bbox[NORTH]) & ~((target[1] >= bbox[WEST]) & (target[1] <= bbox[EAST]))


print(target_in_bounding_box_front(paris, bbox[FRONT]))
print(target_in_bounding_box_front(paris, bbox[REVERSE]))
print(target_in_bounding_box_reverse(paris, bbox[FRONT]))
print(target_in_bounding_box_reverse(paris, bbox[REVERSE]))

N=4
bbox_length = make_bounding_box_length(paris, targets, N)
print(bbox_length)
start = time.time()
pts = get_closest_points(paris, targets, N, bbox_length)
print(time.time()-start)
print(pts)
targets_dist = pts
targets_dist[1] > 10
arr1 = np.array([1,2,3,4,5]).reshape(5,1)
arr2 = np.array([6,7,8,9,10]).reshape(5,1)
arr3 = np.transpose(np.concatenate([arr1, arr2], axis=1))
arr3[1] > 7
targets_dist_tr = np.transpose(targets_dist)
targets_dist[targets_dist_tr[1]>10]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.21, 'fraction_numerical': 0.08245901639344262, 'mean_word_length': 6.004592422502871, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '593582', 'license': 'MIT', 'n_tokens_mistral': 2866, 'n_tokens_neox': 2572, 'n_words': 461, 'path': 'notebooks/bbox.ipynb', 'repo_name': 'nickhalmagyi/boundingbox', 'size': '6100'}