Skip to content

Commit

Permalink
[ISSUE #16] Implemented a function and tests to extract vertices from…
Browse files Browse the repository at this point in the history
… spoly by index
  • Loading branch information
dura0ok committed Aug 7, 2023
1 parent 04456c7 commit f5b83ef
Show file tree
Hide file tree
Showing 16 changed files with 328 additions and 11 deletions.
89 changes: 87 additions & 2 deletions doc/functions.sgm
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
Positions at a path
</title>
<para>
<application>pgSphere</application> provides two functions to
<application>pgSphere</application> provides three functions to
get points at a path.
</para>
<funcsynopsis>
Expand All @@ -477,6 +477,10 @@
<paramdef>spath <parameter>path</parameter></paramdef>
<paramdef>float8 <parameter>f</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef><function>spath_as_array</function></funcdef>
<paramdef>spath <parameter>path</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<para>
The first function returns the <parameter>i</parameter>-th
Expand All @@ -495,6 +499,36 @@
<![CDATA[ spoint]]>
<![CDATA[------------]]>
<![CDATA[ (15d , 0d)]]>
<![CDATA[(1 row)]]>
</programlisting>
</example>
<example>
<title>
Get i-th point of a path
</title>
<programlisting>
<![CDATA[sql> SELECT spoint( spath '{(0, 0),(1, 1)}', 1 );]]>
<![CDATA[ spoint ]]>
<![CDATA[------------]]>
<![CDATA[ (0 , 0) ]]>
<![CDATA[(1 row)]]>
<![CDATA[]]>
<![CDATA[sql> SELECT spoint( spath '{(0, 0),(1, 1)}', 2 );]]>
<![CDATA[ spoint ]]>
<![CDATA[------------]]>
<![CDATA[ (1 , 1) ]]>
<![CDATA[(1 row)]]>
</programlisting>
</example>
<example>
<title>
Get array representation of points
</title>
<programlisting>
<![CDATA[sql> SELECT spath_as_array( spath '{(0, 0),(1, 1)}');]]>
<![CDATA[ spath_as_array ]]>
<![CDATA[-----------------------]]>
<![CDATA[ {"(0 , 0)","(1 , 1)"}]]>
<![CDATA[(1 row)]]>
</programlisting>
</example>
Expand Down Expand Up @@ -532,7 +566,58 @@
</example>

</sect3>

<sect3 id="funcs.spoly.pos">
<title>
Positions at a polygon
</title>
<para>
<application>pgSphere</application> provides two functions to
get points at a path.
</para>
<funcsynopsis>
<funcprototype>
<funcdef><function>spoint</function></funcdef>
<paramdef>spoly <parameter>path</parameter></paramdef>
<paramdef>int4 <parameter>i</parameter></paramdef>
</funcprototype>
<funcprototype>
<funcdef><function>spoly_as_array</function></funcdef>
<paramdef>spath <parameter>path</parameter></paramdef>
</funcprototype>
</funcsynopsis>
<example>
<title>Get by index</title>
<programlisting>
<![CDATA[sql> SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 1 );]]>
<![CDATA[ spoint ]]>
<![CDATA[---------]]>
<![CDATA[ (0 , 0)]]>
<![CDATA[ (1 row)]]>
<![CDATA[]]>
<![CDATA[sql> SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 2 );]]>
<![CDATA[ spoint ]]>
<![CDATA[---------]]>
<![CDATA[ (1 , 0)]]>
<![CDATA[ (1 row)]]>
<![CDATA[]]>
<![CDATA[sql> SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 3 );]]>
<![CDATA[ spoint ]]>
<![CDATA[---------]]>
<![CDATA[ (1 , 1)]]>
<![CDATA[ (1 row)]]>
</programlisting>
</example>
<example>
<title>Represent points as array</title>
<programlisting>
<![CDATA[sql> SELECT spoly_as_array( spoly '{(0,0),(1,0),(1,1)}' );]]>
<![CDATA[ spoly_as_array ]]>
<![CDATA[---------------------------------]]>
<![CDATA[ {"(0 , 0)","(1 , 0)","(1 , 1)"}]]>
<![CDATA[(1 row)]]>
</programlisting>
</example>
</sect3>
</sect2>

<sect2 id="funcs.sbox">
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:158: NOTICE: argument type spath is only a shell
psql:pg_sphere.test.sql:177: NOTICE: type "sbox" is not yet defined
DETAIL: Creating a shell type definition.
psql:pg_sphere.test.sql:184: NOTICE: argument type sbox is only a shell
psql:pg_sphere.test.sql:8568: NOTICE: type "spherekey" is not yet defined
psql:pg_sphere.test.sql:8594: NOTICE: type "spherekey" is not yet defined
DETAIL: Creating a shell type definition.
psql:pg_sphere.test.sql:8575: NOTICE: argument type spherekey is only a shell
psql:pg_sphere.test.sql:8589: NOTICE: type "pointkey" is not yet defined
psql:pg_sphere.test.sql:8601: NOTICE: argument type spherekey is only a shell
psql:pg_sphere.test.sql:8615: NOTICE: type "pointkey" is not yet defined
DETAIL: Creating a shell type definition.
psql:pg_sphere.test.sql:8596: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8602: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8608: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8614: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8622: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8628: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8634: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8640: NOTICE: argument type pointkey is only a shell
4 changes: 2 additions & 2 deletions expected/init_test_healpix.out.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
psql:pg_sphere.test.sql:9181: NOTICE: return type smoc is only a shell
psql:pg_sphere.test.sql:9187: NOTICE: argument type smoc is only a shell
psql:pg_sphere.test.sql:9207: NOTICE: return type smoc is only a shell
psql:pg_sphere.test.sql:9213: NOTICE: argument type smoc is only a shell
25 changes: 25 additions & 0 deletions expected/path.out
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,28 @@ SELECT spoint(p,2) FROM spheretmp6 WHERE id=2;
(1d , -5d)
(1 row)

SELECT set_sphere_output( 'RAD' );
set_sphere_output
-------------------
SET RAD
(1 row)

-- get n-th point and array representation path points tests
SELECT spoint( spath '{(0, 0),(1, 1)}', 1 );
spoint
---------
(0 , 0)
(1 row)

SELECT spoint( spath '{(0, 0),(1, 1)}', 2 );
spoint
---------
(1 , 1)
(1 row)

SELECT spath_as_array( spath '{(0, 0),(1, 1)}');
spath_as_array
-----------------------
{"(0 , 0)","(1 , 1)"}
(1 row)

30 changes: 30 additions & 0 deletions expected/poly.out
Original file line number Diff line number Diff line change
Expand Up @@ -1760,3 +1760,33 @@ SELECT npoints( spoly '{
4
(1 row)

SELECT set_sphere_output( 'RAD' );
set_sphere_output
-------------------
SET RAD
(1 row)

SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 1 );
spoint
---------
(0 , 0)
(1 row)

SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 2 );
spoint
---------
(1 , 0)
(1 row)

SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 3 );
spoint
---------
(1 , 1)
(1 row)

SELECT spoly_as_array( spoly '{(0,0),(1,0),(1,1)}' );
spoly_as_array
---------------------------------
{"(0 , 0)","(1 , 0)","(1 , 1)"}
(1 row)

8 changes: 8 additions & 0 deletions pgs_path.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ CREATE FUNCTION spoint(spath, float8)
COMMENT ON FUNCTION spoint(spath, float8) IS
'returns n-th point of spherical path using linear interpolation';

CREATE FUNCTION spath_as_array(spath)
RETURNS spoint[]
AS 'MODULE_PATHNAME', 'spherepath_get_array'
LANGUAGE 'c'
IMMUTABLE STRICT PARALLEL SAFE;

COMMENT ON FUNCTION spath_as_array(spath) IS
'returns spath as array of points';

-- ******************************
--
Expand Down
18 changes: 18 additions & 0 deletions pgs_polygon.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ CREATE FUNCTION npoints(spoly)
COMMENT ON FUNCTION npoints(spoly) IS
'returns number of points of spherical polygon';

CREATE FUNCTION spoint(spoly, int4)
RETURNS spoint
AS 'MODULE_PATHNAME', 'spherepoly_get_point'
LANGUAGE 'c'
IMMUTABLE STRICT PARALLEL SAFE;

COMMENT ON FUNCTION spoint(spoly, int4) IS
'returns n-th point of spherical polygon';

CREATE FUNCTION spoly_as_array(spoly)
RETURNS spoint[]
AS 'MODULE_PATHNAME', 'spherepoly_get_array'
LANGUAGE 'c'
IMMUTABLE STRICT PARALLEL SAFE;

COMMENT ON FUNCTION spoly_as_array(spoly) IS
'returns spoly as array of points';

CREATE FUNCTION area(spoly)
RETURNS FLOAT8
AS 'MODULE_PATHNAME', 'spherepoly_area'
Expand Down
6 changes: 6 additions & 0 deletions sql/path.sql
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,9 @@ SELECT set_sphere_output( 'DEG' );
-- test stored data
SELECT spoint(p,2) FROM spheretmp6 WHERE id=2;

SELECT set_sphere_output( 'RAD' );

-- get n-th point and array representation path points tests
SELECT spoint( spath '{(0, 0),(1, 1)}', 1 );
SELECT spoint( spath '{(0, 0),(1, 1)}', 2 );
SELECT spath_as_array( spath '{(0, 0),(1, 1)}');
7 changes: 7 additions & 0 deletions sql/poly.sql
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,10 @@ SELECT npoints( spoly '{
(1.5121581120647 , -1.93925472462553e-05),
(1.51214841579108 , -1.93925472462553e-05)
}');

SELECT set_sphere_output( 'RAD' );

SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 1 );
SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 2 );
SELECT spoint( spoly '{(0,0),(1,0),(1,1)}', 3 );
SELECT spoly_as_array( spoly '{(0,0),(1,0),(1,1)}' );
29 changes: 29 additions & 0 deletions src/path.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "path.h"
#include "point.h"
#include <catalog/namespace.h>

/*
* Path functions
Expand Down Expand Up @@ -50,6 +52,7 @@ PG_FUNCTION_INFO_V1(spheretrans_path);
PG_FUNCTION_INFO_V1(spheretrans_path_inverse);
PG_FUNCTION_INFO_V1(spherepath_add_point);
PG_FUNCTION_INFO_V1(spherepath_add_points_finalize);
PG_FUNCTION_INFO_V1(spherepath_get_array);


/*
Expand Down Expand Up @@ -555,6 +558,32 @@ spherepath_get_point(PG_FUNCTION_ARGS)
PG_RETURN_NULL();
}

Datum
spherepath_get_array(PG_FUNCTION_ARGS)
{
SPATH *path = PG_GETARG_SPATH(0);
Datum *datum_arr = (Datum *) palloc(sizeof(Datum) * path->npts);
ArrayType *res;
SPoint *p = (SPoint *) palloc(sizeof(SPoint) * path->npts);

for (size_t i = 0; i < path->npts; i++)
{
if (!spath_get_point(&p[i], path, i))
{
// Clean up and return NULL
for (size_t j = 0; j < i; j++)
pfree(DatumGetPointer(datum_arr[j]));
pfree(datum_arr);
PG_RETURN_NULL();
}
datum_arr[i] = PointerGetDatum(&p[i]);
}

res = construct_array(datum_arr, path->npts, get_spoint_type_oid(), sizeof(SPoint), false, 'd');

PG_RETURN_ARRAYTYPE_P(res);
}

Datum
spherepath_point(PG_FUNCTION_ARGS)
{
Expand Down
5 changes: 5 additions & 0 deletions src/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Datum spherepath_in(PG_FUNCTION_ARGS);
*/
Datum spherepath_get_point(PG_FUNCTION_ARGS);

/*
* Returns spath as array of points
*/
Datum spherepath_get_array(PG_FUNCTION_ARGS);

/*
* This function interpolates between points of path. Returns the
* n-th point of a path where n is a float.
Expand Down
11 changes: 11 additions & 0 deletions src/point.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ PG_FUNCTION_INFO_V1(spherepoint_z);
PG_FUNCTION_INFO_V1(spherepoint_xyz);
PG_FUNCTION_INFO_V1(spherepoint_equal);

static Oid point_id = InvalidOid;

Oid get_spoint_type_oid(void)
{
if (point_id == InvalidOid)
{
point_id = TypenameGetTypid("spoint");
}
return point_id;
}

bool
spoint_eq(const SPoint *p1, const SPoint *p2)
{
Expand Down
3 changes: 3 additions & 0 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "vector3d.h"
#include "sbuffer.h"
#include <catalog/namespace.h>

/* This file contains declarations for spherical point and functions. */

Expand All @@ -15,6 +16,8 @@ typedef struct
float8 lat; /* latitude value in radians */
} SPoint;

Oid get_spoint_type_oid(void);

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

0 comments on commit f5b83ef

Please sign in to comment.