Skip to content

Commit 1ef3f30

Browse files
author
Markus Nullmeier
committed
use Vincenty's formula in spoint_dist(): this vindicates a fixed ./expected/circle_extended.out
1 parent e290269 commit 1ef3f30

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

expected/circle.out

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ SELECT 180.0*dist('<( 23h 30m 00s , 0d 0m 0s), 1.0d>'::scircle,'<( 1h 0m 00s , 0
165165
(1 row)
166166

167167
SELECT 180.0*dist('<( 0h 40m 00s , 0d 0m 0s), 1.0d>'::scircle,'<( 0h 50m 00s , 0d 0m 0s),1.0d>'::scircle)/pi();
168-
?column?
169-
-------------------
170-
0.499999999999953
168+
?column?
169+
----------
170+
0.5
171171
(1 row)
172172

173173
SELECT 180.0*dist('<( 0h 40m 00s , 0d 0m 0s), 1.5d>'::scircle,'<( 0h 50m 00s , 0d 0m 0s),1.5d>'::scircle)/pi();
@@ -407,9 +407,9 @@ SELECT 180.0*('<( 23h 30m 00s , 0d 0m 0s), 1.0d>'::scircle<->'<( 1h 0m 00s , 0d
407407
(1 row)
408408

409409
SELECT 180.0*('<( 0h 40m 00s , 0d 0m 0s), 1.0d>'::scircle<->'<( 0h 50m 00s , 0d 0m 0s),1.0d>'::scircle)/pi();
410-
?column?
411-
-------------------
412-
0.499999999999953
410+
?column?
411+
----------
412+
0.5
413413
(1 row)
414414

415415
SELECT 180.0*('<( 0h 40m 00s , 0d 0m 0s), 1.5d>'::scircle<->'<( 0h 50m 00s , 0d 0m 0s),1.5d>'::scircle)/pi();

expected/circle_extended.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,6 @@ select count(sc) from scircle_data where '<(0d,0d),2.1d>'::scircle ~ sc;
305305
select count(spoint_data.sp) from spoint_data,scircle_data where spoint_data.sp @ scircle_data.sc;
306306
count
307307
-------
308-
76682
308+
78842
309309
(1 row)
310310

expected/points.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ SELECT spoint(0.0 , 2.141592653589793116);
261261
SELECT dist('( 0h 2m 30s , 0d 0m 0s)'::spoint,'( 0h 0m 30s , 0d 0m 0s)'::spoint);
262262
dist
263263
---------------------
264-
0.00872664625996925
264+
0.00872664625997165
265265
(1 row)
266266

267267
SELECT dist('( 0h 2m 30s , 0d 0m 0s)'::spoint,'( 0h 2m 30s , 10d 0m 0s)'::spoint);
@@ -609,7 +609,7 @@ SELECT '( 0h 2m 30s , 0d 0m 0s)'::spoint<>'( 12h 2m 30s , 45d 0m 0s)'::spoint;
609609
SELECT '( 0h 2m 30s , 0d 0m 0s)'::spoint<->'( 0h 0m 30s , 0d 0m 0s)'::spoint;
610610
?column?
611611
---------------------
612-
0.00872664625996925
612+
0.00872664625997165
613613
(1 row)
614614

615615
SELECT '( 0h 2m 30s , 0d 0m 0s)'::spoint<->'( 0h 2m 30s , 10d 0m 0s)'::spoint;

point.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -158,27 +158,22 @@ spherepoint_from_long_lat(PG_FUNCTION_ARGS)
158158
PG_RETURN_POINTER(p);
159159
}
160160

161+
static double
162+
norm2(double a, double b)
163+
{
164+
return sqrt(a * a + b * b);
165+
}
166+
161167
float8
162168
spoint_dist(const SPoint *p1, const SPoint *p2)
163169
{
164170
float8 dl = p1->lng - p2->lng;
165-
float8 f = ((sin(p1->lat) * sin(p2->lat) +
166-
cos(p1->lat) * cos(p2->lat) * cos(dl)));
167-
168-
if (FPeq(f, 1.0))
169-
{
170-
/* for small distances */
171-
Vector3D v1, v2, v3;
172-
173-
spoint_vector3d(&v1, p1);
174-
spoint_vector3d(&v2, p2);
175-
vector3d_cross(&v3, &v1, &v2);
176-
f = vector3d_length(&v3);
177-
}
178-
else
179-
{
180-
f = acos(f);
181-
}
171+
/* use Vincenty's formula for the inverse geodesic problem on the sphere */
172+
float8 f = atan2(norm2(cos(p2->lat) * sin(dl),
173+
cos(p1->lat) * sin(p2->lat)
174+
- sin(p1->lat) * cos(p2->lat) * cos(dl)),
175+
sin(p1->lat) * sin(p2->lat)
176+
+ cos(p1->lat) * cos(p2->lat) * cos(dl));
182177
if (FPzero(f))
183178
{
184179
return 0.0;

0 commit comments

Comments
 (0)