PostGIS  3.1.6dev-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 115 of file lwgeom_out_geojson.c.

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