gralhix004 | Geolocating Random Islet Image Using Geometry & CUDA GPU Programming
16 – 08-2026 NOTE: this is a genuine human work, didnt use LLM generation.
16 – 08-2026
NOTE: this is a genuine human work, didnt use LLM generation.
I’m writing this page as a writeup for this challenge gralhix 004 made by Sofia Santos | Gralhix.
You can view, clone and locally try all code files and the final report with all instructions here at github.
You can view, clone and locally try all code files and the final report with all instructions here at github.
Task briefing:
This is a photo of a resort located on an island.
a) What is the name of the resort? b) What are the coordinates of the island? c) In which cardinal direction was the camera facing when the photo was taken?
a) What is the name of the resort? b) What are the coordinates of the island? c) In which cardinal direction was the camera facing when the photo was taken?
In my opinion, solving this challenge with google lens is wasting a fun opportunity, so decided to solve it with math and programming.
a] Metadata
Of course, first thing u look for is the metadata. Ran that on my linux void:
> exiftool main.png
File Type : WEBP (lossless) MIME Type : image/webp Image Width : 736 Image Height : 515
As expected, nothing useful here. No EXIF, no GPS, no camera make or model.
b] Building the fingerprint
U can see from the img, there are 3 landmasses:
P0: the islet itself,
P1: the right island,
P2: the left front island ( having mountain peak )
I couldnt make a correct perspective model of birdview of this image, as clearly the image is taken by a drone and cant estimate the elevation at all (and not found in the metadata).
So I had to estimate that by intuition, I just want the relative distances between the 3 islands and angles of that triangle.
I built a small click GUI 01_triangle_gui.py that records pixel coordinates for each point in order and computes the triangle’s geometry.
Since clicking exact centers by eye isn’t perfectly precise, I added a ±20% tolerance band around both values when searching.
c] SEARCH
With the fingerprint locked in, the next step is checking every real landmass on Earth against it !
I used OpenStreetMap’s split land polygon set as the dataset land-polygons-split-4326, full global coastline vectors in WGS84 which has size of 882 MB.
I used OpenStreetMap’s split land polygon set as the dataset land-polygons-split-4326, full global coastline vectors in WGS84 which has size of 882 MB.
I created heuristic filters (all by just intuition and non tangible proofs), spent days (yea full days) tweaking values and tons of trial and error 😭 untill I got this working filters recipe.
01] Tropical latitude bounding box
$$ -30° \le latitude \le 30° $$
the islet in the photo reads as tropical, so I decided that anything outside the tropics is thrown out immediately, before doing any expensive geometry work.
Exactly 141,131 land polygons survive that band filter.
Exactly 141,131 land polygons survive that band filter.
02] Local density filter
$$ N_{5\text{km}}(p) \le 10 $$
$ N_{5\text{km}}(p) $ counts how many other centroids fall within 5km of point (p). Cap is 10: if an islet has more than 10 neighbors that close, it’s sitting in a dense reef field, a crowded coastline or a archipelago clutter, not a small isolated 3 – 4 island group like the photo shows.
This dropped candidates down to 51,576.
This dropped candidates down to 51,576.
03] Clustering
For every surviving point, find every other point within 20km (heuristic, by eye from the image). If it has at least 2 neighbors that close (3 points total), it’s a cluster. Points with no cluster of 3+ nearby are dropped, they can’t form a triangle at all.
tree = cKDTree(f_coords) neigh = tree.query_ball_point( f_coords, CLUSTER_RADIUS_KM / 111.0) clusters = set(tuple(sorted(n)) for n in neigh if len(n) >= 3)
$$ \left|\{q : \text{dist}(p,q) \le 20\,\text{km}\}\right| \ge 3 $$
That collapses down to 23,500 clusters.
That collapses down to 23,500 clusters.
04] Generating Triplets
For every cluster, every combination of 3 points inside it becomes a candidate triangle. That’s $ C(n, 3) $, which explodes fast for big clusters, for example: a cluster of 60 points already gives 34,220 triples on its own. So each cluster gets capped at 60 points first, sampled by size, not randomly.
$$ \binom{n}{3} = \frac{n(n-1)(n-2)}{6} $$
def stratified_sample(idx_arr, area_arr, cap): order = np.argsort(area_arr[idx_arr]) n_small = cap // 3 n_large = cap // 3 n_mid = cap - n_small - n_large mid_start = max(0, (len(idx_arr) - n_large - n_mid) // 2) keep = np.unique(np.concatenate([ order[:n_small], order[-n_large:], order[mid_start:mid_start + n_mid], ])) return idx_arr[keep]
def gen_cluster_triples(idx_arr): local = np.array(list( itertools.combinations(range(len(idx_arr)), 3)), dtype=np.int64) return idx_arr[local]
The sampling takes a third small islands, a third large, a third from the middle of the size distribution, instead of the full cluster or a random cut.
23,500 clusters produce 80,690,777 triples total !!
23,500 clusters produce 80,690,777 triples total !!
05] Matching, on the GPU
I gave every triple one CUDA thread. Each thread sorts its 3 points by land area to pick out P0 (smallest, the resort islet), then uses the winding direction of the other two to assign P1 and P2:
long long i = blockIdx.x * (long long)blockDim.x + threadIdx.x; if (i >= n_triples) return;
int pos[3] = {0, 1, 2}; for (int a1 = 1; a1 < 3; a1++) { int key = pos[a1]; double keyval = a[key]; int j = a1 – 1; while (j >= 0 && a[pos[j]] > keyval) { pos[j + 1] = pos[j]; j–; } pos[j + 1] = key; }
P1 vs P2 comes from a 2D cross product, no branching on which cluster the triple came from, just the sign:
$$ \text{cross} = x_a y_b - x_b y_a $$ $$ P1 = \begin{cases} a & \text{cross} > 0 \\ b & \text{cross} \le 0 \end{cases} $$
Walk from P0 to a, then to b. If cross > 0, that’s a left turn (counterclockwise). If cross < 0, it’s a right turn (clockwise). It’s the same sign trick used to tell if 3 points curve one way or the other.
then angle at P0 and the distance ratio, same formulas as the fingerprint step, computed independently by every thread:
$$ \theta_0 = \arccos\left(\frac{\vec{d_1} \cdot \vec{d_2}}{|\vec{d_1}||\vec{d_2}|}\right), \qquad r = \frac{|\vec{d_1}|}{|\vec{d_2}|} $$
A triple survives if angle, ratio, P0′s size, the separation between P0 and P1, and both side lengths all land inside the fingerprint’s tolerance windows. Threads that pass write their result into a shared output array using an atomic counter, so two threads finishing at the same time never overwrite each other:
if (hit) { unsigned long long slot = atomicAdd(out_count, 1ULL); out_p0[slot] = p0idx; out_p1[slot] = p1idx; out_p2[slot] = p2idx; }
Now printed in the CLI directly from the kernel:
gpu: NVIDIA GeForce RTX 3050 (sm_86) vram used: 5169 MB kernel time: 204.1 ms
80.7 million triples go in, one thread each, in parallel. 158,784 pass the mask.
80.7 million triples go in, one thread each, in parallel. 158,784 pass the mask.
06] Dedup
Since same physical triple can get hit by multiple GPU threads if it belonged to more than one overlapping cluster, so raw matches get collapsed by identity first:
seen = set() uniq = [] for i in range(len(p0_all)): key = (p0_all[i], p1_all[i], p2_all[i]) if key not in seen: seen.add(key) uniq.append(i)
8,915 unique triples after dedup.
8,915 unique triples after dedup.
07] The Open Rectangle
Every surviving triple gets one more test: is the space next to it actually open water, like the photo shows ? A rectangle gets built along the P0→P1 edge, on whichever side P2 is not on, then checked against the land dataset for anything else sitting inside it.
width = np.hypot(x1, y1) u = np.array([x1, y1]) / width v = np.array([-u[1], u[0]])
# p2 sits on the +v side by construction, # so the check goes on -v length = 2 * width corners_local = [ (0, 0), (x1, y1), (x1 - v[0]*length, y1 - v[1]*length), (-v[0]*length, -v[1]*length), ]
If anything other than the 3 candidate islands themselves intersects that rectangle, the candidate is dropped. Land sitting there means it’s not the open, unobstructed water the photo actually shows.
8,915 unique triples down to 948.
8,915 unique triples down to 948.
and below is the map of places of the 948 candidates.
d] Coral Cay Shape Check
In this stage, we look only at P0, the resort islet, and check whether its shape actually looks like a coral cay.
1] Compactness, how close to a circle the shape is:
Polsby Popper Score: $$ PP = \frac{4\pi \cdot \text{area}}{\text{perimeter}^2} $$
def compactness(row): return (4 * np.pi * row.area_km2) / (row.perim_km ** 2 + 1e-12)
1.0 is a perfect circle, lower means a more jagged or elongated outline. Coral cays tend to be round from wave deposition, so anything < 0.5 gets dropped.
2] Micro Cay Halo Check:
def micro_cay_count(gdf, sindex, lon, lat): dists_km = nearby.geometry.distance(pt) * 111.0 mask = (dists_km > 0) & (dists_km <= HALO_KM) & (nearby[“area_km2”].values < MICRO_KM2) return int(mask.sum())
We Count land fragments under 0.05 km² within 1.5km of P0 ( just heuristic ). Real reef systems scatter tiny sandbars around the main island, not just one isolated landmass (I knew that with the hardway 😭). So we need at least 1.
213/948 candidates survive both checks.
213/948 candidates survive both checks.
e] Oval Shape Check
Another geometric filter on P0′s own polygon. Fits the minimum rotated rectangle around it and measures two ratios from that box.
def aspect_and_fill(geom): mrr = geom.minimum_rotated_rectangle coords = list(mrr.exterior.coords) s1 = math.hypot(coords[1][0] - coords[0][0], coords[1][1] - coords[0][1]) s2 = math.hypot(coords[2][0] - coords[1][0], coords[2][1] - coords[1][1]) long_side, short_side = max(s1, s2), min(s1, s2) return long_side / short_side, geom.area / mrr.area
Aspect ratio is long side over short side of that box:
$$ \text{aspect} = \frac{\text{long side}}{\text{short side}} \in [1.05,\ 2.2] $$
Too close to 1.0 and it’s basically a perfect circle, not the slightly elongated shape in the photo. Too high are shapes too much elongated more than 2:1.