Skip to content

Commit

Permalink
[ISSUE postgrespro#22] Created spoint_deg, scircle_deg functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
dura0ok committed Jul 31, 2023
1 parent 612f06b commit 15ca67f
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 8 deletions.
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"files.associations": {
"circle.h": "c",
"euler.h": "c",
"point.h": "c",
"pgs_util.h": "c"
}
}
16 changes: 16 additions & 0 deletions expected/circle.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@
(1 row)

-- Input/Output ---
SELECT scircle_deg(spoint(10,10), 90);
scircle_deg
------------------------------------------
<(0.57522204 , -0.57522204) , 1.5707963>
(1 row)

SELECT scircle_deg(spoint(10,10), 91);
ERROR: radius must be not greater than 90 degrees or less than 0
SELECT scircle_deg(spoint(0,0), 0);
scircle_deg
---------------
<(0 , 0) , 0>
(1 row)

SELECT scircle_deg(spoint(10,10), -1);
ERROR: radius must be not greater than 90 degrees or less than 0
SELECT set_sphere_output( 'RAD' );
set_sphere_output
-------------------
Expand Down
14 changes: 7 additions & 7 deletions expected/init_test.out.in
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ psql:pg_sphere.test.sql:159: NOTICE: argument type spath is only a shell
psql:pg_sphere.test.sql:178: NOTICE: type "sbox" is not yet defined
DETAIL: Creating a shell type definition.
psql:pg_sphere.test.sql:185: NOTICE: argument type sbox is only a shell
psql:pg_sphere.test.sql:8540: NOTICE: type "spherekey" is not yet defined
psql:pg_sphere.test.sql:8557: NOTICE: type "spherekey" is not yet defined
DETAIL: Creating a shell type definition.
psql:pg_sphere.test.sql:8547: NOTICE: argument type spherekey is only a shell
psql:pg_sphere.test.sql:8561: NOTICE: type "pointkey" is not yet defined
psql:pg_sphere.test.sql:8564: NOTICE: argument type spherekey is only a shell
psql:pg_sphere.test.sql:8578: NOTICE: type "pointkey" is not yet defined
DETAIL: Creating a shell type definition.
psql:pg_sphere.test.sql:8568: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8574: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8580: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8586: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8585: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8591: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8597: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8603: NOTICE: argument type pointkey is only a shell
18 changes: 18 additions & 0 deletions expected/points.out
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,24 @@ SELECT spoint(0.0109083078249646 , -0.000727220521664407);
(0.625d , -0.041666667d)
(1 row)

SELECT spoint_deg(57.2958, 57.2958);
spoint_deg
-----------------------
(57.2958d , 57.2958d)
(1 row)

SELECT spoint_deg(0, 0);
spoint_deg
------------
(0d , 0d)
(1 row)

SELECT spoint_deg(114.5916, 0);
spoint_deg
------------------
(114.5916d , 0d)
(1 row)

SELECT set_sphere_output( 'RAD' );
set_sphere_output
-------------------
Expand Down
8 changes: 8 additions & 0 deletions pgs_circle.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ CREATE FUNCTION scircle(spoint, float8)
COMMENT ON FUNCTION scircle(spoint, float8) IS
'spherical circle with spherical point as center and float8 as radius in radians';

CREATE FUNCTION scircle_deg(spoint, float8)
RETURNS scircle
AS 'MODULE_PATHNAME' , 'spherecircle_by_center_deg'
LANGUAGE 'c'
IMMUTABLE STRICT PARALLEL SAFE;

COMMENT ON FUNCTION scircle_deg(spoint, float8) IS
'spherical circle with spherical point as center and float8 as radius in degrees';

--
-- Casting point as circle
Expand Down
9 changes: 9 additions & 0 deletions pgs_point.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ CREATE FUNCTION spoint(FLOAT8, FLOAT8)
LANGUAGE 'c'
IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION spoint_deg(FLOAT8, FLOAT8)
RETURNS spoint
AS 'MODULE_PATHNAME', 'spherepoint_from_long_lat_deg'
LANGUAGE 'c'
IMMUTABLE STRICT PARALLEL SAFE;

CREATE FUNCTION set_sphere_output_precision(INT4)
RETURNS CSTRING
AS 'MODULE_PATHNAME', 'set_sphere_output_precision'
Expand All @@ -28,6 +34,9 @@ CREATE FUNCTION set_sphere_output(CSTRING)
COMMENT ON FUNCTION spoint(FLOAT8, FLOAT8) IS
'returns a spherical point from longitude (arg1), latitude (arg2)';

COMMENT ON FUNCTION spoint_deg(FLOAT8, FLOAT8) IS
'returns a spherical point from longitude(in degrees) (arg1), latitude(in degrees) (arg2)';

CREATE FUNCTION long(spoint)
RETURNS FLOAT8
AS 'MODULE_PATHNAME', 'spherepoint_long'
Expand Down
8 changes: 8 additions & 0 deletions sql/circle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ SET extra_float_digits = 0;

-- Input/Output ---

SELECT scircle_deg(spoint(10,10), 90);

SELECT scircle_deg(spoint(10,10), 91);

SELECT scircle_deg(spoint(0,0), 0);

SELECT scircle_deg(spoint(10,10), -1);

SELECT set_sphere_output( 'RAD' );

SELECT '< (1h 2m 30s , +1d 2m 30s), 1.0d >'::scircle;
Expand Down
6 changes: 6 additions & 0 deletions sql/points.sql
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ SELECT '(0.0109083078249646 , -0.000727220521664407)'::spoint;

SELECT spoint(0.0109083078249646 , -0.000727220521664407);

SELECT spoint_deg(57.2958, 57.2958);

SELECT spoint_deg(0, 0);

SELECT spoint_deg(114.5916, 0);

SELECT set_sphere_output( 'RAD' );

SELECT spoint(7.28318530717958623 , 0.00);
Expand Down
13 changes: 13 additions & 0 deletions src/circle.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "circle.h"
#include "pgs_util.h"

/* Circle functions */

Expand All @@ -22,6 +23,7 @@ PG_FUNCTION_INFO_V1(spherecircle_center);
PG_FUNCTION_INFO_V1(spherecircle_radius);
PG_FUNCTION_INFO_V1(spherepoint_to_circle);
PG_FUNCTION_INFO_V1(spherecircle_by_center);
PG_FUNCTION_INFO_V1(spherecircle_by_center_deg);
PG_FUNCTION_INFO_V1(spherecircle_area);
PG_FUNCTION_INFO_V1(spherecircle_circ);
PG_FUNCTION_INFO_V1(spheretrans_circle);
Expand Down Expand Up @@ -370,6 +372,17 @@ spherecircle_by_center(PG_FUNCTION_ARGS)
PG_RETURN_POINTER(c);
}

Datum
spherecircle_by_center_deg(PG_FUNCTION_ARGS)
{
Datum res;
SPoint *p = (SPoint *) PG_GETARG_POINTER(0);
float8 rad = PG_GETARG_FLOAT8(1);
rad = deg_to_rad(rad);
res = DirectFunctionCall2(spherecircle_by_center, PointerGetDatum(p), Float8GetDatum(rad));
PG_RETURN_DATUM(res);
}

Datum
spherecircle_area(PG_FUNCTION_ARGS)
{
Expand Down
5 changes: 5 additions & 0 deletions src/circle.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ Datum spherepoint_to_circle(PG_FUNCTION_ARGS);
*/
Datum spherecircle_by_center(PG_FUNCTION_ARGS);

/*
* Creates a circle from center and radius(in degrees).
*/
Datum spherecircle_by_center_deg(PG_FUNCTION_ARGS);

/*
* Calculates the area of a circle in square radians.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/pgs_util.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef __PGS_UTIL_H__
#define __PGS_UTIL_H__
#include <postgres.h>

#define PI 3.14159265358979323846 /* pi */
#define PIH 1.57079632679489661923 /* pi/2 */
Expand All @@ -26,4 +27,8 @@ conv_theta(double x)
return y;
}

static inline float8 deg_to_rad(float8 in){
return in * PI / 180;
}

#endif
16 changes: 15 additions & 1 deletion src/point.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "point.h"
#include "pgs_util.h"

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

PG_FUNCTION_INFO_V1(spherepoint_in);
PG_FUNCTION_INFO_V1(spherepoint_from_long_lat);
PG_FUNCTION_INFO_V1(spherepoint_from_long_lat_deg);
PG_FUNCTION_INFO_V1(spherepoint_distance);
PG_FUNCTION_INFO_V1(spherepoint_long);
PG_FUNCTION_INFO_V1(spherepoint_lat);
Expand Down Expand Up @@ -141,18 +143,30 @@ spherepoint_in(PG_FUNCTION_ARGS)
PG_RETURN_POINTER(sp);
}


Datum
spherepoint_from_long_lat(PG_FUNCTION_ARGS)
{
SPoint *p = (SPoint *) palloc(sizeof(SPoint));

p->lng = PG_GETARG_FLOAT8(0);
p->lat = PG_GETARG_FLOAT8(1);
elog(LOG, "INPUT ASD %lf %lf", p->lng, p->lat);
spoint_check(p);
elog(LOG, "INPUT ASD after %lf %lf", p->lng, p->lat);
PG_RETURN_POINTER(p);
}

Datum
spherepoint_from_long_lat_deg(PG_FUNCTION_ARGS)
{
Datum res;
float lng = deg_to_rad(PG_GETARG_FLOAT8(0));
float lat = deg_to_rad(PG_GETARG_FLOAT8(1));
elog(LOG, "CONVERTED %lf %lf", lng, lat);
res = DirectFunctionCall2(spherepoint_from_long_lat, Float8GetDatum(lng), Float8GetDatum(lat));
PG_RETURN_DATUM(res);
}

static double
norm2(double a, double b)
{
Expand Down
5 changes: 5 additions & 0 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ Datum spherepoint_in(PG_FUNCTION_ARGS);
*/
Datum spherepoint_from_long_lat(PG_FUNCTION_ARGS);

/*
* Create a spherical point from longitude and latitude both in degrees.
*/
Datum spherepoint_from_long_lat_deg(PG_FUNCTION_ARGS);

/*
* Calculate the distance between two spherical points.
*/
Expand Down

0 comments on commit 15ca67f

Please sign in to comment.