Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #16] Implemented a function and tests to extract vertices from spoly by index #37

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)}' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate to nitpick whitespace, but I recommend removing one of the two spaces after the SELECTs on lines 599, 601, 603, and 605.

Otherwise, it looks good to me! But if you're adding a function, shouldn't there be an upgrade script and version number bump? Or will that be done in a separate PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, but which version should we put? @vitcpp

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed before, the master branch is the development branch. Once the API is changed I propose to assign a new version 1.3.0. Upgrade script should be named as pg_sphere--1.2.3--1.3.0.sql.in. The same as I proposed in #22.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerning the use of blank lines for queries separation. I propose to follow the same coding style. There are no blank lines in sql/path.sql but there are blank lines in sql/poly.sql. I propose to remove these redundant blank lines.

Copy link
Contributor

@esabol esabol Aug 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except PR #38 already bumps the version to 1.3.0 and adds an upgrade script. I propose merging PR #38 first and then git rebase this PR on the new master to add this functionality to the future 1.3.0.

27 changes: 27 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,30 @@ 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++)
dura0ok marked this conversation as resolved.
Show resolved Hide resolved
{
if (!spath_get_point(&p[i], path, i))
{
pfree(p);
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
12 changes: 12 additions & 0 deletions src/point.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "point.h"
#include "pgs_util.h"
#include <catalog/namespace.h>

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

Expand All @@ -15,6 +16,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
2 changes: 2 additions & 0 deletions src/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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