PGROUTING  3.2
alphaShape.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 FILE: alphaShape.c
3 
4 Copyright (c) 2018 Vicky Vergara
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 #include <stdbool.h>
26 #include <float.h>
27 
29 
30 #include "c_common/debug_macro.h"
31 #include "c_common/e_report.h"
32 #include "c_common/time_msg.h"
33 
34 
35 #include "c_common/edges_input.h"
36 #include "c_types/geom_text_rt.h"
37 
39 
40 PGDLLEXPORT Datum _pgr_alphashape(PG_FUNCTION_ARGS);
41 
42 static void process(
43  char* edges_sql,
44  double alpha,
45 
46  GeomText_t **res,
47  size_t *result_count) {
49 
50  Pgr_edge_xy_t *edgesArr = NULL;
51  size_t edgesSize = 0;
52 
53  pgr_get_edges_xy(edges_sql, &edgesArr, &edgesSize);
54 
55  PGR_DBG("total edges %ld", edgesSize);
56  PGR_DBG("alpha %f", alpha);
57 #if 0
58  for (size_t i = 0; i < edgesSize; ++i) {
59  PGR_DBG("x1=%f y1=%f", edgesArr[i].x1, edgesArr[i].y1);
60  PGR_DBG("x2=%f y2=%f", edgesArr[i].x2, edgesArr[i].y2);
61  }
62 #endif
63 
64  if (edgesSize < 3) {
65  if (edgesArr) pfree(edgesArr);
66  elog(ERROR, "Less than 3 vertices."
67  " pgr_alphaShape needs at least 3 vertices.");
69  return;
70  }
71 
72  PGR_DBG("Calling alpha-shape driver\n");
73 
74  char *err_msg = NULL;
75  char* log_msg = NULL;
76  char* notice_msg = NULL;
77 
79  edgesArr, edgesSize,
80  alpha,
81 
82  res,
83  result_count,
84  &log_msg,
85  &notice_msg,
86  &err_msg);
87 
88  if (err_msg && (*res)) {
89  pfree(*res);
90  (*res) = NULL;
91  (*result_count) = 0;
92  }
93 
94  pgr_global_report(log_msg, notice_msg, err_msg);
95 
96  if (log_msg) pfree(log_msg);
97  if (notice_msg) pfree(notice_msg);
98  if (err_msg) pfree(err_msg);
99  if (edgesArr) pfree(edgesArr);
100  pgr_SPI_finish();
101 }
102 
104 
105 PGDLLEXPORT
106 Datum _pgr_alphashape(PG_FUNCTION_ARGS) {
107  FuncCallContext *funcctx;
108  TupleDesc tuple_desc;
109 
110  /**********************************************************************/
111  GeomText_t *result_tuples = NULL;
112  size_t result_count = 0;
113  /**********************************************************************/
114 
115  if (SRF_IS_FIRSTCALL()) {
116  MemoryContext oldcontext;
117  funcctx = SRF_FIRSTCALL_INIT();
118  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
119 
120  /******************************************************************/
121 
122  process(
123  text_to_cstring(PG_GETARG_TEXT_P(0)),
124  PG_GETARG_FLOAT8(1),
125  &result_tuples,
126  &result_count);
127 
128  /******************************************************************/
129 
130 #if PGSQL_VERSION > 95
131  funcctx->max_calls = result_count;
132 #else
133  funcctx->max_calls = (uint32_t)result_count;
134 #endif
135 
136  funcctx->user_fctx = result_tuples;
137  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
138  ereport(ERROR,
139  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
140  errmsg("function returning record called in context "
141  "that cannot accept type record")));
142 
143  funcctx->tuple_desc = tuple_desc;
144  MemoryContextSwitchTo(oldcontext);
145  }
146 
147  funcctx = SRF_PERCALL_SETUP();
148  tuple_desc = funcctx->tuple_desc;
149  result_tuples = (GeomText_t*) funcctx->user_fctx;
150 
151  if (funcctx->call_cntr < funcctx->max_calls) {
152  HeapTuple tuple;
153  Datum result;
154  Datum *values;
155  bool *nulls;
156  size_t call_cntr = funcctx->call_cntr;
157 
158  size_t numb = 4;
159 
160  values = palloc(numb * sizeof(Datum));
161  nulls = palloc(numb * sizeof(bool));
162 
163  size_t i;
164  for (i = 0; i < numb; ++i) {
165  nulls[i] = false;
166  }
167 
168  values[0] = Int64GetDatum(call_cntr + 1);
169  values[1] = CStringGetTextDatum(result_tuples[call_cntr].geom);
170 
171  tuple = heap_form_tuple(tuple_desc, values, nulls);
172  result = HeapTupleGetDatum(tuple);
173  SRF_RETURN_NEXT(funcctx, result);
174  } else {
175  SRF_RETURN_DONE(funcctx);
176  }
177 }
time_msg.h
postgres_connection.h
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_alphashape)
pgr_get_edges_xy
void pgr_get_edges_xy(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
Edges with x, y vertices values.
Definition: edges_input.c:744
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
process
static void process(char *edges_sql, double alpha, GeomText_t **res, size_t *result_count)
Definition: alphaShape.c:42
debug_macro.h
_pgr_alphashape
PGDLLEXPORT Datum _pgr_alphashape(PG_FUNCTION_ARGS)
Definition: alphaShape.c:106
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
alphaShape_driver.h
do_alphaShape
void do_alphaShape(Pgr_edge_xy_t *edgesArr, size_t edgesSize, double alpha, GeomText_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: alphaShape_driver.cpp:59
edges_input.h
GeomText_t
Definition: geom_text_rt.h:30
Pgr_edge_xy_t
Definition: pgr_edge_xy_t.h:38
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
geom_text_rt.h