From a9194210a24a785c15afcad2543ffc174320589d Mon Sep 17 00:00:00 2001 From: wangdapeng <345731923@qq.com> Date: Tue, 7 Apr 2026 13:16:54 +0800 Subject: [PATCH] doc: add examples section to earthdistance documentation --- doc/src/sgml/earthdistance.sgml | 103 ++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/doc/src/sgml/earthdistance.sgml b/doc/src/sgml/earthdistance.sgml index 5a709e3..89e6059 100644 --- a/doc/src/sgml/earthdistance.sgml +++ b/doc/src/sgml/earthdistance.sgml @@ -262,4 +262,107 @@ + + Examples + + + The following examples demonstrate the use of the + earthdistance module with locations in + Tokyo (latitude 35.6762, longitude 139.6503) and + Osaka (latitude 34.6937, longitude 135.5023). + + + + First, install the required extensions: + +CREATE EXTENSION cube; +CREATE EXTENSION earthdistance CASCADE; + + + + + Retrieve the assumed radius of the Earth in meters: + +SELECT earth(); + earth +--------- + 6378168 +(1 row) + + + + + Convert latitude and longitude to an earth point + (a 3D coordinate): + +SELECT ll_to_earth(35.6762, 139.6503); + ll_to_earth +------------------------------------------------------------- + (-3948591.2574923225, 3354541.741384729, 3719772.012205412) +(1 row) + + + + + Calculate the great circle distance in meters between Tokyo and Osaka: + +SELECT earth_distance( + ll_to_earth(35.6762, 139.6503), + ll_to_earth(34.6937, 135.5023) +); + earth_distance +------------------- + 392882.7648340743 +(1 row) + + + + + Extract latitude and longitude back from an earth point: + +SELECT latitude(ll_to_earth(35.6762, 139.6503)); + latitude +-------------------- + 35.676199999999994 +(1 row) + +SELECT longitude(ll_to_earth(35.6762, 139.6503)); + longitude +----------- + 139.6503 +(1 row) + + + + + Using the point-based operator to calculate distance in statute miles: + +SELECT point(139.6503, 35.6762) <@> point(135.5023, 34.6937); + ?column? +------------------- + 243.8511729296967 +(1 row) + + Note that point takes (longitude, latitude) order, + not (latitude, longitude). + + + + Use earth_box to find points within 50 km of + Tokyo. This creates a bounding box suitable for an indexed search: + +SELECT earth_box(ll_to_earth(35.6762, 139.6503), 50000); + + Since some points inside the box may be farther than 50 km, + combine it with earth_distance for exact filtering: + +SELECT name, location +FROM cities +WHERE earth_box(ll_to_earth(35.6762, 139.6503), 50000) @> location + AND earth_distance(location, ll_to_earth(35.6762, 139.6503)) < 50000; + + + + + -- 2.46.2.windows.1