118 HeapTupleData tmptup,
121 bool needsep =
false;
123 StringInfo props = makeStringInfo();
124 bool geom_column_found =
false;
126 sep = use_line_feeds ?
",\n " :
", ";
128 td = DatumGetHeapTupleHeader(composite);
131 tupType = HeapTupleHeaderGetTypeId(td);
132 tupTypmod = HeapTupleHeaderGetTypMod(td);
133 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
136 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
140 appendStringInfoString(result,
"{\"type\": \"Feature\", \"geometry\": ");
142 for (i = 0; i < tupdesc->natts; i++)
149 Form_pg_attribute att = TupleDescAttr(tupdesc, i);
150 bool is_geom_column =
false;
152 if (att->attisdropped)
155 attname = NameStr(att->attname);
157 if (geom_column_name)
158 is_geom_column = (strcmp(attname, geom_column_name) == 0);
160 is_geom_column = (att->atttypid == geom_oid || att->atttypid == geog_oid);
162 if ((!geom_column_found) && is_geom_column)
165 geom_column_found =
true;
167 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
170 appendStringInfo(result,
"%s",
171 TextDatumGetCString(DirectFunctionCall2(
LWGEOM_asGeoJson, val, Int32GetDatum(maxdecimaldigits))));
175 appendStringInfoString(result,
"{\"type\": null}");
181 appendStringInfoString(props, sep);
184 escape_json(props, attname);
185 appendStringInfoString(props,
": ");
187 val = heap_getattr(tuple, i + 1, tupdesc, &isnull);
192 outfuncoid = InvalidOid;
197 datum_to_json(val, isnull, props, tcategory, outfuncoid,
false);
201 if (!geom_column_found)
203 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
204 errmsg(
"geometry column is missing")));
206 appendStringInfoString(result,
", \"properties\": {");
207 appendStringInfo(result,
"%s", props->data);
209 appendStringInfoString(result,
"}}");
210 ReleaseTupleDesc(tupdesc);
static void json_categorize_type(Oid typoid, JsonTypeCategory *tcategory, Oid *outfuncoid)
Datum LWGEOM_asGeoJson(PG_FUNCTION_ARGS)
static void datum_to_json(Datum val, bool is_null, StringInfo result, JsonTypeCategory tcategory, Oid outfuncoid, bool key_scalar)