banner

For a list of BASHing data 2 blog posts see the index page.    RSS


Find all data points "X" km or less from a given point

I have a table with about 10000 records of millipede occurrences in Tasmania. Almost every record has the latitude and longitude of the point where a particular species of millipede was recorded, although in a few cases the lat/lon entries are blank (inadequately georeferenced occurrences, like locality = "Tasmania").

Occasionally I need to identify any records that are 5 (or 10) km or less from a particular point in the Tasmanian landscape. Previously, finding those records meant importing the latest occurrences table into a GIS program, adding the point of interest, buffering the point with a 5 (or 10) km circle, then intersecting the records layer with the circle; i.e. "cookie-cutting" out the millipede records found within the circle. As a final step I'd export the cookie-cut records to a new text file.

To save time and avoid opening a GUI GIS program I wrote a simple shell script ("circle") with a YAD entry dialog. In the screenshot below, I'm looking for records 5 km or less from the locality "Loyetea", at -41.2889 145.9550:

YAD

After I've pressed Enter or clicked "OK" on the YAD box, the script runs through my millipede records table and selects the records I want from all the records with non-blank lat/lon entries. It prints to stdout an explanatory line ("Records...") and a header line, then for each record the record ID, millipede scientific name, date of the record and distance from the selected point (rounded to 0.1 km). In the screenshot below I've found the 15 records that meet my conditions:

records

The core of the script is an AWK command, and the core of the command is the formula for a circle in plane geometry:

x2 + y2 = r2

If I treat the point of interest as the center of a circle with latitude "ctrlat" and longitude "ctrlon", then a point record with latitude "reclat" and longitude "reclon" is this far away from the point of interest:

square root of [((reclat-ctrlat)*111.32)2 +
((reclon-ctrlon)*111.32*cos(ctrlat*π/180))2]

The calculated distance is approximate because the test assumes Euclidean rather than great-circle distances between points, and because I've used the central point latitude to calculate the distance between longitudes. Both these simplifications are OK for small distances. Note also that 111.32 is the number of km in a degree of latitude, and that I've multiplied "ctrlat" by pi/180 to get the angle in radians.

So the script simply tests whether the calculated distance for a millipede record is equal to or less than the specified radius. Interested readers can email me for the script, or try the following demo: save the CSV at the end of this post as "abels1300". This is a list of the 64 discrete mountain peaks (not plateaus) with elevation 1300 m or greater in Tasmania. It's based on the Abels website, which covers all discrete peaks in Tasmania over 1100 m and explains why these peaks are called "Abels".

Somewhat obsessive bushwalkers aim to climb all the Abels. Our aim here is less exhausting: which are the Abels in the 1300 m+ class that are equal to or less than (say) 2.5 km apart? Finding these might reveal a cluster of mighty peaks.

As a first step, use the "combinations without repetition" command demonstrated in last week's BASHing data 2 post to build a table ("abels1300pairs") containing unique pairs of every peak on the list with every other peak:

awk -F"," '{a[c++]=$0} END {for (i=0;i<c;i++) for (j=i+1;j<c;j++) print a[i]","a[j]}' abels1300 > abels1300pairs

abelspairs

There are 2016 such pairs in "abels1300pairs". AWK will now run through the list and find all pairs that are 2.5 km or less apart, and print the peak names and the calculated distance:

awk -F"," 'BEGIN {cosf=(3.14159/180)} {x=((($3-$7)*111.32)^2 + (($4-$8)*111.32*cos($3*cosf))^2); if (x<=6.25) printf("%s,%s,%0.1f\n",$1,$5,sqrt(x))}'

2.5km

The Turrana Bluff-Mersey Crag pair is in the Walls of Jerusalem National Park, while the other eight pairs are in the Cradle Mountain - Lake St Clair National Park. For a description of a bushwalk to the 3-peak Mt Achilles-Mt Thetis-Perrins Bluff cluster, see this post.


Mt Ossa,1617,-41.8710,146.0333
Legges Tor,1575,-41.5331,147.6589
Mt Pelion West,1560,-41.8316,145.9774
Barn Bluff,1559,-41.7240,145.9231
Cradle Mountain,1545,-41.6845,145.9514
Stacks Bluff,1528,-41.6261,147.6883
Du Cane Range,1520+,-41.9194,146.0509
Mt Massif,1514,-41.9059,146.0643
King Davids Peak,1499,-41.8164,146.2882
Mt Gould,1485,-41.9746,146.0504
Falling Mountain,1482,-41.9105,146.0967
Mt Thetis,1482,-41.8617,145.9998
The Acropolis,1481,-41.9353,146.0614
Mt Hyperion,1480,-41.9152,146.0360
Mt Olympus,1472,-42.0376,146.0938
Mt Pelion East,1461,-41.8572,146.0676
Mt Jerusalem,1459,-41.8204,146.3243
Clumner Bluff,1458,-41.7613,146.2567
Turrana Bluff,1454,-41.7587,146.3521
Mt Gell,1447,-42.1530,146.0250
Frenchmans Cap,1446,-42.2698,145.8304
Ironstone Mountain,1444,-41.7115,146.4771
Eldon Peak,1440,-41.9739,145.7324
Mt Field West,1435,-42.6563,146.5149
Walled Mountain,1431,-41.9396,146.0197
Goulds Sugarloaf,1425,-42.0669,145.9997
Mt Anne,1423,-42.9429,146.4239
Mt Barrow,1420+,-41.3763,147.4180
Mersey Crag,1420+,-41.7713,146.3335
Perrins Bluff,1420+,-41.8765,145.9682
Western Bluff,1420,-41.6171,146.2825
Mt Rufus,1416,-42.1247,146.1000
Macs Mountain,1413,-41.9361,145.9837
Mt Emmett,1410,-41.7101,145.9824
Twin Spires,1406,-41.8806,146.1180
Mt Hugel,1403,-42.1046,146.0856
Sandbanks Tier,1401,-41.8441,146.8671
Snowy South,1398,-42.9426,146.6583
The Guardians,1394,-41.9733,146.0232
Wild Dog Tier,1394,-41.7832,146.5792
Rats Castle,1393,-41.8394,146.6506
Nevada Peak,1390,-42.9198,146.6397
Mt Manfred,1382,-42.0078,146.0216
Mt Cuvier,1378,-42.0252,146.0246
Rodway Range,1377,-42.6771,146.5664
Florentine Peak,1376,-42.6890,146.5342
Mt Byron,1375,-42.0298,146.0597
Ragged Jack,1369,-41.5548,147.5856
Ben Nevis,1368,-41.4098,147.6261
High Dome,1366,-41.9877,145.9192
Mensa Moor,1365,-41.5757,147.6382
Mt Achilles,1363,-41.8592,145.9776
Mt King William II,1363,-42.3039,146.1423
Eldon Bluff,1361,-41.9764,145.8260
Mt Rogoona,1350,-41.8905,146.1974
Mt Weld,1344,-43.0043,146.5773
Black Bluff Range,1340+,-41.4574,145.9386
Drys Bluff,1340,-41.7147,146.8056
Wylds Craig,1339,-42.4740,146.3904
Mt Picton,1327,-43.1573,146.6072
Mountains of Jupiter,1326,-41.9413,146.1811
Mt King William I,1324,-42.2242,146.1389
Eldon Crag,1321,-41.9672,145.8040
Slatters Peak,1300,-42.2792,146.1548


Next post:
2025-03-21   New code for my translation box


Last update: 2025-03-14
The blog posts on this website are licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License