Skip to content

Commit

Permalink
Implemented a function and tests to extract vertices from spoly by index
Browse files Browse the repository at this point in the history
  • Loading branch information
dura0ok committed Aug 1, 2023
1 parent 612f06b commit d77d4d8
Show file tree
Hide file tree
Showing 17 changed files with 333 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ DATA_built = $(RELEASE_SQL) \
pg_sphere--1.1.5beta4gavo--1.2.0.sql \
pg_sphere--1.2.0--1.2.1.sql \
pg_sphere--1.2.1--1.2.2.sql \
pg_sphere--1.2.2--1.2.3.sql
pg_sphere--1.2.2--1.2.3.sql \
pg_sphere--1.2.3--1.3.0.sql

DOCS = README.pg_sphere COPYRIGHT.pg_sphere
REGRESS = init tables points euler circle line ellipse poly path box index \
Expand Down Expand Up @@ -260,6 +261,9 @@ endif
pg_sphere--1.2.2--1.2.3.sql:
cat upgrade_scripts/$@.in > $@

pg_sphere--1.2.3--1.3.0.sql:
cat upgrade_scripts/$@.in > $@

# end of local stuff

src/sscan.o : src/sparse.c
Expand Down
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: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:8566: 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:8573: NOTICE: argument type spherekey is only a shell
psql:pg_sphere.test.sql:8587: 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:8594: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8600: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8606: NOTICE: argument type pointkey is only a shell
psql:pg_sphere.test.sql:8612: 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:9153: NOTICE: return type smoc is only a shell
psql:pg_sphere.test.sql:9159: NOTICE: argument type smoc is only a shell
psql:pg_sphere.test.sql:9179: NOTICE: return type smoc is only a shell
psql:pg_sphere.test.sql:9185: 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 @@ -1738,3 +1738,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 @@ -593,3 +593,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)}' );
28 changes: 28 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,31 @@ 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_point_id(), 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
7 changes: 7 additions & 0 deletions src/point.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ PG_FUNCTION_INFO_V1(spherepoint_z);
PG_FUNCTION_INFO_V1(spherepoint_xyz);
PG_FUNCTION_INFO_V1(spherepoint_equal);

Oid get_point_id(void){
if(POINT_ID == InvalidOid){
POINT_ID = TypenameGetTypid("spoint");
}
return POINT_ID;
}

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

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

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

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

__attribute__((unused))
static Oid POINT_ID = InvalidOid;

Oid get_point_id(void);

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

0 comments on commit d77d4d8

Please sign in to comment.