PGROUTING  3.2
alphaShape.c File Reference
#include <stdbool.h>
#include <float.h>
#include "c_common/postgres_connection.h"
#include "c_common/debug_macro.h"
#include "c_common/e_report.h"
#include "c_common/time_msg.h"
#include "c_common/edges_input.h"
#include "c_types/geom_text_rt.h"
#include "drivers/alpha_shape/alphaShape_driver.h"
Include dependency graph for alphaShape.c:

Go to the source code of this file.

Functions

PGDLLEXPORT Datum _pgr_alphashape (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (_pgr_alphashape)
 
static void process (char *edges_sql, double alpha, GeomText_t **res, size_t *result_count)
 

Function Documentation

◆ _pgr_alphashape()

PGDLLEXPORT Datum _pgr_alphashape ( PG_FUNCTION_ARGS  )

Definition at line 106 of file alphaShape.c.

106  {
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 }

References if(), and process().

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( _pgr_alphashape  )

◆ process()

static void process ( char *  edges_sql,
double  alpha,
GeomText_t **  res,
size_t *  result_count 
)
static

Definition at line 42 of file alphaShape.c.

47  {
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 }

References do_alphaShape(), PGR_DBG, pgr_get_edges_xy(), pgr_global_report(), pgr_SPI_connect(), and pgr_SPI_finish().

Referenced by _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
process
static void process(char *edges_sql, double alpha, GeomText_t **res, size_t *result_count)
Definition: alphaShape.c:42
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
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
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