Skip to content

Commit cdec17d

Browse files
author
stepan-neretin7
committed
[ISSUE #22] Created spoint_deg, scircle_deg functions.
1 parent 612f06b commit cdec17d

12 files changed

+112
-8
lines changed

Diff for: expected/circle.out

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@
55
(1 row)
66

77
-- Input/Output ---
8+
SELECT scircle_deg(spoint(10,10), 90);
9+
scircle_deg
10+
------------------------------------------
11+
<(0.57522204 , -0.57522204) , 1.5707963>
12+
(1 row)
13+
14+
SELECT scircle_deg(spoint(10,10), 91);
15+
ERROR: radius must be not greater than 90 degrees or less than 0
16+
SELECT scircle_deg(spoint(0,0), 0);
17+
scircle_deg
18+
---------------
19+
<(0 , 0) , 0>
20+
(1 row)
21+
22+
SELECT scircle_deg(spoint(10,10), -1);
23+
ERROR: radius must be not greater than 90 degrees or less than 0
824
SELECT set_sphere_output( 'RAD' );
925
set_sphere_output
1026
-------------------

Diff for: expected/init_test.out.in

+7-7
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ psql:pg_sphere.test.sql:159: NOTICE: argument type spath is only a shell
2424
psql:pg_sphere.test.sql:178: NOTICE: type "sbox" is not yet defined
2525
DETAIL: Creating a shell type definition.
2626
psql:pg_sphere.test.sql:185: NOTICE: argument type sbox is only a shell
27-
psql:pg_sphere.test.sql:8540: NOTICE: type "spherekey" is not yet defined
27+
psql:pg_sphere.test.sql:8557: NOTICE: type "spherekey" is not yet defined
2828
DETAIL: Creating a shell type definition.
29-
psql:pg_sphere.test.sql:8547: NOTICE: argument type spherekey is only a shell
30-
psql:pg_sphere.test.sql:8561: NOTICE: type "pointkey" is not yet defined
29+
psql:pg_sphere.test.sql:8564: NOTICE: argument type spherekey is only a shell
30+
psql:pg_sphere.test.sql:8578: NOTICE: type "pointkey" is not yet defined
3131
DETAIL: Creating a shell type definition.
32-
psql:pg_sphere.test.sql:8568: NOTICE: argument type pointkey is only a shell
33-
psql:pg_sphere.test.sql:8574: NOTICE: argument type pointkey is only a shell
34-
psql:pg_sphere.test.sql:8580: NOTICE: argument type pointkey is only a shell
35-
psql:pg_sphere.test.sql:8586: NOTICE: argument type pointkey is only a shell
32+
psql:pg_sphere.test.sql:8585: NOTICE: argument type pointkey is only a shell
33+
psql:pg_sphere.test.sql:8591: NOTICE: argument type pointkey is only a shell
34+
psql:pg_sphere.test.sql:8597: NOTICE: argument type pointkey is only a shell
35+
psql:pg_sphere.test.sql:8603: NOTICE: argument type pointkey is only a shell

Diff for: expected/points.out

+18
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,24 @@ SELECT spoint(0.0109083078249646 , -0.000727220521664407);
239239
(0.625d , -0.041666667d)
240240
(1 row)
241241

242+
SELECT spoint_deg(57.2958, 57.2958);
243+
spoint_deg
244+
-----------------------
245+
(57.2958d , 57.2958d)
246+
(1 row)
247+
248+
SELECT spoint_deg(0, 0);
249+
spoint_deg
250+
------------
251+
(0d , 0d)
252+
(1 row)
253+
254+
SELECT spoint_deg(114.5916, 0);
255+
spoint_deg
256+
------------------
257+
(114.5916d , 0d)
258+
(1 row)
259+
242260
SELECT set_sphere_output( 'RAD' );
243261
set_sphere_output
244262
-------------------

Diff for: pgs_circle.sql.in

+8
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ CREATE FUNCTION scircle(spoint, float8)
3232
COMMENT ON FUNCTION scircle(spoint, float8) IS
3333
'spherical circle with spherical point as center and float8 as radius in radians';
3434

35+
CREATE FUNCTION scircle_deg(spoint, float8)
36+
RETURNS scircle
37+
AS 'MODULE_PATHNAME' , 'spherecircle_by_center_deg'
38+
LANGUAGE 'c'
39+
IMMUTABLE STRICT PARALLEL SAFE;
40+
41+
COMMENT ON FUNCTION scircle_deg(spoint, float8) IS
42+
'spherical circle with spherical point as center and float8 as radius in degrees';
3543

3644
--
3745
-- Casting point as circle

Diff for: pgs_point.sql.in

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ CREATE FUNCTION spoint(FLOAT8, FLOAT8)
1515
LANGUAGE 'c'
1616
IMMUTABLE STRICT PARALLEL SAFE;
1717

18+
CREATE FUNCTION spoint_deg(FLOAT8, FLOAT8)
19+
RETURNS spoint
20+
AS 'MODULE_PATHNAME', 'spherepoint_from_long_lat_deg'
21+
LANGUAGE 'c'
22+
IMMUTABLE STRICT PARALLEL SAFE;
23+
1824
CREATE FUNCTION set_sphere_output_precision(INT4)
1925
RETURNS CSTRING
2026
AS 'MODULE_PATHNAME', 'set_sphere_output_precision'
@@ -28,6 +34,9 @@ CREATE FUNCTION set_sphere_output(CSTRING)
2834
COMMENT ON FUNCTION spoint(FLOAT8, FLOAT8) IS
2935
'returns a spherical point from longitude (arg1), latitude (arg2)';
3036

37+
COMMENT ON FUNCTION spoint_deg(FLOAT8, FLOAT8) IS
38+
'returns a spherical point from longitude(in degrees) (arg1), latitude(in degrees) (arg2)';
39+
3140
CREATE FUNCTION long(spoint)
3241
RETURNS FLOAT8
3342
AS 'MODULE_PATHNAME', 'spherepoint_long'

Diff for: sql/circle.sql

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ SET extra_float_digits = 0;
55

66
-- Input/Output ---
77

8+
SELECT scircle_deg(spoint(10,10), 90);
9+
10+
SELECT scircle_deg(spoint(10,10), 91);
11+
12+
SELECT scircle_deg(spoint(0,0), 0);
13+
14+
SELECT scircle_deg(spoint(10,10), -1);
15+
816
SELECT set_sphere_output( 'RAD' );
917

1018
SELECT '< (1h 2m 30s , +1d 2m 30s), 1.0d >'::scircle;

Diff for: sql/points.sql

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ SELECT '(0.0109083078249646 , -0.000727220521664407)'::spoint;
8686

8787
SELECT spoint(0.0109083078249646 , -0.000727220521664407);
8888

89+
SELECT spoint_deg(57.2958, 57.2958);
90+
91+
SELECT spoint_deg(0, 0);
92+
93+
SELECT spoint_deg(114.5916, 0);
94+
8995
SELECT set_sphere_output( 'RAD' );
9096

9197
SELECT spoint(7.28318530717958623 , 0.00);

Diff for: src/circle.c

+13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "circle.h"
2+
#include "pgs_util.h"
23

34
/* Circle functions */
45

@@ -22,6 +23,7 @@ PG_FUNCTION_INFO_V1(spherecircle_center);
2223
PG_FUNCTION_INFO_V1(spherecircle_radius);
2324
PG_FUNCTION_INFO_V1(spherepoint_to_circle);
2425
PG_FUNCTION_INFO_V1(spherecircle_by_center);
26+
PG_FUNCTION_INFO_V1(spherecircle_by_center_deg);
2527
PG_FUNCTION_INFO_V1(spherecircle_area);
2628
PG_FUNCTION_INFO_V1(spherecircle_circ);
2729
PG_FUNCTION_INFO_V1(spheretrans_circle);
@@ -370,6 +372,17 @@ spherecircle_by_center(PG_FUNCTION_ARGS)
370372
PG_RETURN_POINTER(c);
371373
}
372374

375+
Datum
376+
spherecircle_by_center_deg(PG_FUNCTION_ARGS)
377+
{
378+
Datum res;
379+
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
380+
float8 rad = PG_GETARG_FLOAT8(1);
381+
rad = deg_to_rad(rad);
382+
res = DirectFunctionCall2(spherecircle_by_center, PointerGetDatum(p), Float8GetDatum(rad));
383+
PG_RETURN_DATUM(res);
384+
}
385+
373386
Datum
374387
spherecircle_area(PG_FUNCTION_ARGS)
375388
{

Diff for: src/circle.h

+5
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ Datum spherepoint_to_circle(PG_FUNCTION_ARGS);
132132
*/
133133
Datum spherecircle_by_center(PG_FUNCTION_ARGS);
134134

135+
/*
136+
* Creates a circle from center and radius(in degrees).
137+
*/
138+
Datum spherecircle_by_center_deg(PG_FUNCTION_ARGS);
139+
135140
/*
136141
* Calculates the area of a circle in square radians.
137142
*/

Diff for: src/pgs_util.h

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef __PGS_UTIL_H__
22
#define __PGS_UTIL_H__
3+
#include <postgres.h>
34

45
#define PI 3.14159265358979323846 /* pi */
56
#define PIH 1.57079632679489661923 /* pi/2 */
@@ -26,4 +27,8 @@ conv_theta(double x)
2627
return y;
2728
}
2829

30+
static inline float8 deg_to_rad(float8 in){
31+
return in * PI / 180;
32+
}
33+
2934
#endif

Diff for: src/point.c

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include "point.h"
2+
#include "pgs_util.h"
23

34
/* This file contains definitions for spherical point functions. */
45

56
PG_FUNCTION_INFO_V1(spherepoint_in);
67
PG_FUNCTION_INFO_V1(spherepoint_from_long_lat);
8+
PG_FUNCTION_INFO_V1(spherepoint_from_long_lat_deg);
79
PG_FUNCTION_INFO_V1(spherepoint_distance);
810
PG_FUNCTION_INFO_V1(spherepoint_long);
911
PG_FUNCTION_INFO_V1(spherepoint_lat);
@@ -141,7 +143,6 @@ spherepoint_in(PG_FUNCTION_ARGS)
141143
PG_RETURN_POINTER(sp);
142144
}
143145

144-
145146
Datum
146147
spherepoint_from_long_lat(PG_FUNCTION_ARGS)
147148
{
@@ -153,6 +154,16 @@ spherepoint_from_long_lat(PG_FUNCTION_ARGS)
153154
PG_RETURN_POINTER(p);
154155
}
155156

157+
Datum
158+
spherepoint_from_long_lat_deg(PG_FUNCTION_ARGS)
159+
{
160+
Datum res;
161+
float lng = deg_to_rad(PG_GETARG_FLOAT8(0));
162+
float lat = deg_to_rad(PG_GETARG_FLOAT8(1));
163+
res = DirectFunctionCall2(spherepoint_from_long_lat, Float8GetDatum(lng), Float8GetDatum(lat));
164+
PG_RETURN_DATUM(res);
165+
}
166+
156167
static double
157168
norm2(double a, double b)
158169
{

Diff for: src/point.h

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ Datum spherepoint_in(PG_FUNCTION_ARGS);
5050
*/
5151
Datum spherepoint_from_long_lat(PG_FUNCTION_ARGS);
5252

53+
/*
54+
* Create a spherical point from longitude and latitude both in degrees.
55+
*/
56+
Datum spherepoint_from_long_lat_deg(PG_FUNCTION_ARGS);
57+
5358
/*
5459
* Calculate the distance between two spherical points.
5560
*/

0 commit comments

Comments
 (0)