Skip to content

Commit 042f177

Browse files
author
stepan-neretin7
committed
add base logick + 1 test
1 parent 2c4cd20 commit 042f177

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

expected/points.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,17 @@ SELECT long('(24h 2m 30s ,-90d 0m 0s)'::spoint);
385385
0.01090830782496
386386
(1 row)
387387

388+
-- get_gravity_center(array(spoint))
389+
SELECT get_gravity_center(ARRAY[
390+
spoint(40.7128, -74.0060),
391+
spoint(34.0522, -118.2437),
392+
spoint(37.7749, -122.4194)
393+
]);
394+
get_gravity_center
395+
--------------------------
396+
(3.0436698 , 0.85893807)
397+
(1 row)
398+
388399
-- lat(spoint)
389400
SELECT lat('( 0h 2m 30s , 0d 0m 0s)'::spoint);
390401
lat

pgs_point.sql.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,8 @@ CREATE OPERATOR <-> (
167167
COMMENT ON OPERATOR <-> (spoint, spoint) IS
168168
'distance between spherical points';
169169

170+
CREATE FUNCTION get_gravity_center(dots_vector SPoint[])
171+
RETURNS spoint
172+
AS 'MODULE_PATHNAME', 'get_gravity_center'
173+
LANGUAGE 'c'
174+
IMMUTABLE STRICT PARALLEL SAFE;

sql/points.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,14 @@ SELECT long('(24h 2m 30s ,90d 0m 0s)'::spoint);
138138

139139
SELECT long('(24h 2m 30s ,-90d 0m 0s)'::spoint);
140140

141+
-- get_gravity_center(array(spoint))
142+
SELECT get_gravity_center(ARRAY[
143+
spoint(40.7128, -74.0060),
144+
spoint(34.0522, -118.2437),
145+
spoint(37.7749, -122.4194)
146+
]);
147+
148+
141149
-- lat(spoint)
142150

143151
SELECT lat('( 0h 2m 30s , 0d 0m 0s)'::spoint);

src/point.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ PG_FUNCTION_INFO_V1(spherepoint_y);
1212
PG_FUNCTION_INFO_V1(spherepoint_z);
1313
PG_FUNCTION_INFO_V1(spherepoint_xyz);
1414
PG_FUNCTION_INFO_V1(spherepoint_equal);
15+
PG_FUNCTION_INFO_V1(get_gravity_center);
1516

1617
bool
1718
spoint_eq(const SPoint *p1, const SPoint *p2)
@@ -263,3 +264,40 @@ spherepoint_equal(PG_FUNCTION_ARGS)
263264

264265
PG_RETURN_BOOL(spoint_eq(p1, p2));
265266
}
267+
268+
static SPoint * spherepoint_from_vector3d(Vector3D v){
269+
SPoint* p = (SPoint *) palloc(sizeof(SPoint));
270+
p->lat = asin(v.z / sqrt(pow(v.x, 2) + pow(v.y, 2) + pow(v.z, 2)));
271+
p->lng = atan2(v.y, v.x);
272+
return p;
273+
}
274+
275+
Datum get_gravity_center(PG_FUNCTION_ARGS) {
276+
SPoint * p;
277+
SPoint current_point;
278+
Vector3D v;
279+
Vector3D res;
280+
ArrayType *dots_vector = PG_GETARG_ARRAYTYPE_P(0);
281+
int num_elements = ArrayGetNItems(ARR_NDIM(dots_vector), ARR_DIMS(dots_vector));
282+
SPoint *array_data = (SPoint *) ARR_DATA_PTR(dots_vector);
283+
284+
for (int i = 0; i < num_elements; i++) {
285+
current_point = array_data[i];
286+
//elog(LOG, "%lf\n", current_point.lat);
287+
spoint_vector3d(&v, &current_point);
288+
elog(LOG, "%lf\n", v.x);
289+
res.x+=v.x;
290+
res.y+=v.y;
291+
res.z+=v.z;
292+
}
293+
294+
res.x /= num_elements;
295+
res.y /= num_elements;
296+
res.z /= num_elements;
297+
298+
p = spherepoint_from_vector3d(res);
299+
300+
spoint_check(p);
301+
302+
PG_RETURN_POINTER(p);
303+
}

src/point.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ Datum spherepoint_xyz(PG_FUNCTION_ARGS);
9090
*/
9191
Datum spherepoint_equal(PG_FUNCTION_ARGS);
9292

93+
/*
94+
* Return gravity center from array of spherical points
95+
*/
96+
Datum get_gravity_center(PG_FUNCTION_ARGS);
97+
9398
#endif

0 commit comments

Comments
 (0)