PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ composite_to_geojson()

static void composite_to_geojson ( FunctionCallInfo  fcinfo,
Datum  composite,
char *  geom_column_name,
int32  maxdecimaldigits,
StringInfo  result,
bool  use_line_feeds,
Oid  geom_oid,
Oid  geog_oid 
)
static

Definition at line 118 of file lwgeom_out_geojson.c.

126 {
127  HeapTupleHeader td;
128  Oid tupType;
129  int32 tupTypmod;
130  TupleDesc tupdesc;
131  HeapTupleData tmptup,
132  *tuple;
133  int i;
134  bool needsep = false;
135  const char *sep;
136  StringInfo props = makeStringInfo();
137  bool geom_column_found = false;
138 
139  sep = use_line_feeds ? ",\n " : ", ";
140 
141  td = DatumGetHeapTupleHeader(composite);
142 
143  /* Extract rowtype info and find a tupdesc */
144  tupType = HeapTupleHeaderGetTypeId(td);
145  tupTypmod = HeapTupleHeaderGetTypMod(td);
146  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
147 
148  /* Build a temporary HeapTuple control structure */
149  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
150  tmptup.t_data = td;
151  tuple = &tmptup;
152 
153  appendStringInfoString(result, "{\"type\": \"Feature\", \"geometry\": ");
154 
155  for (i = 0; i < tupdesc->natts; i++)
156  {
157  Datum val;
158  bool isnull;
159  char *attname;
160  JsonTypeCategory tcategory;
161  Oid outfuncoid;
162  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
163  bool is_geom_column = false;
164 
165  if (att->attisdropped)
166  continue;
167 
168  attname = NameStr(att->attname);
169  /* Use the column name if provided, use the first geometry column otherwise */
170  if (geom_column_name)
171  is_geom_column = (strcmp(attname, geom_column_name) == 0);
172  else
173  is_geom_column = (att->atttypid == geom_oid || att->atttypid == geog_oid);
174 
175  if ((!geom_column_found) && is_geom_column)
176  {
177  /* this is our geom column */
178  geom_column_found = true;
179 
180  val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
181  if (!isnull)
182  {
183  appendStringInfo(
184  result,
185  "%s",
186  TextDatumGetCString(CallerFInfoFunctionCall2(LWGEOM_asGeoJson,
187  fcinfo->flinfo,
188  InvalidOid,
189  val,
190  Int32GetDatum(maxdecimaldigits))));
191  }
192  else
193  {
194  appendStringInfoString(result, "{\"type\": null}");
195  }
196  }
197  else
198  {
199  if (needsep)
200  appendStringInfoString(props, sep);
201  needsep = true;
202 
203  escape_json(props, attname);
204  appendStringInfoString(props, ": ");
205 
206  val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
207 
208  if (isnull)
209  {
210  tcategory = JSONTYPE_NULL;
211  outfuncoid = InvalidOid;
212  }
213  else
214  json_categorize_type(att->atttypid, &tcategory, &outfuncoid);
215 
216  datum_to_json(val, isnull, props, tcategory, outfuncoid, false);
217  }
218  }
219 
220  if (!geom_column_found)
221  ereport(ERROR,
222  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
223  errmsg("geometry column is missing")));
224 
225  appendStringInfoString(result, ", \"properties\": {");
226  appendStringInfo(result, "%s", props->data);
227 
228  appendStringInfoString(result, "}}");
229  ReleaseTupleDesc(tupdesc);
230 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)
JsonTypeCategory
@ JSONTYPE_NULL
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)
unsigned int int32
Definition: shpopen.c:54

References datum_to_json(), json_categorize_type(), JSONTYPE_NULL, LWGEOM_asGeoJson(), and result.

Referenced by ST_AsGeoJsonRow().

Here is the call graph for this function:
Here is the caller graph for this function: