27 #include "../postgis_config.h" 36 #include "access/gist.h" 37 #include "access/itup.h" 40 #include "utils/elog.h" 41 #include "mb/pg_wchar.h" 42 # include "lib/stringinfo.h" 43 #include "utils/array.h" 44 #include "utils/lsyscache.h" 48 #include "lwgeom_pg.h" 50 #include "lwgeom_transform.h" 53 #include "access/htup_details.h" 86 char *input = PG_GETARG_CSTRING(0);
87 int32 geom_typmod = -1;
94 if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
95 geom_typmod = PG_GETARG_INT32(2);
101 if ( str[0] ==
'\0' ) {
102 ereport(ERROR,(errmsg(
"parse error - invalid geometry")));
107 if( strncasecmp(str,
"SRID=",5) == 0 )
111 while ( tmp && *tmp !=
';' )
115 if ( tmp && *(tmp+1) ==
'0' )
131 size_t hexsize = strlen(str);
148 PG_PARSER_ERROR(lwg_parser_result);
151 lwgeom = lwg_parser_result.
geom;
158 if ( geom_typmod >= 0 )
161 POSTGIS_DEBUG(3,
"typmod and geometry were consistent");
165 POSTGIS_DEBUG(3,
"typmod was -1");
171 PG_RETURN_POINTER(ret);
201 GSERIALIZED *pg_lwgeom = PG_GETARG_GSERIALIZED_P(0);
202 text *format_text = PG_GETARG_TEXT_P(1);
205 char *format_str = NULL;
207 char * formatted_str;
208 text * formatted_text;
215 lwpgerror(
"Only points are supported, you tried type %s.",
lwtype_name(geom_type));
220 if (format_text == NULL) {
221 lwpgerror(
"ST_AsLatLonText: invalid format string (null");
226 assert(format_str != NULL);
230 tmp = (
char *)pg_do_encoding_conversion(
231 (
uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
233 if ( tmp != format_str ) {
240 assert(formatted_str != NULL);
244 tmp = (
char *)pg_do_encoding_conversion(
245 (
uint8_t *)formatted_str, strlen(formatted_str),
246 PG_UTF8, GetDatabaseEncoding());
248 if ( tmp != formatted_str) {
249 pfree(formatted_str);
254 formatted_text = cstring2text(formatted_str);
255 pfree(formatted_str);
257 PG_RETURN_POINTER(formatted_text);
279 PG_RETURN_CSTRING(hexwkb);
298 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
300 type = PG_GETARG_TEXT_P(1);
302 if ( ! strncmp(VARDATA(type),
"xdr", 3) ||
303 ! strncmp(VARDATA(type),
"XDR", 3) )
319 text_size = hexwkb_size - 1 + VARHDRSZ;
320 result = palloc(text_size);
321 memcpy(VARDATA(result), hexwkb, hexwkb_size - 1);
322 SET_VARSIZE(result, text_size);
326 PG_FREE_IF_COPY(geom, 0);
327 PG_RETURN_TEXT_P(result);
353 result = cstring2text(hexwkb);
357 PG_FREE_IF_COPY(geom, 0);
358 PG_RETURN_TEXT_P(result);
371 bytea *bytea_wkb = (bytea*)PG_GETARG_BYTEA_P(0);
379 if ( ( PG_NARGS()>1) && ( ! PG_ARGISNULL(1) ))
381 srid = PG_GETARG_INT32(1);
390 PG_FREE_IF_COPY(bytea_wkb, 0);
391 PG_RETURN_POINTER(geom);
401 bytea *bytea_twkb = (bytea*)PG_GETARG_BYTEA_P(0);
413 PG_FREE_IF_COPY(bytea_twkb, 0);
414 PG_RETURN_POINTER(geom);
432 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
434 type = PG_GETARG_TEXT_P(1);
436 if ( ! strncmp(VARDATA(type),
"xdr", 3) ||
437 ! strncmp(VARDATA(type),
"XDR", 3) )
446 wkb_size= VARSIZE(geom) - VARHDRSZ;
454 result = palloc(wkb_size + VARHDRSZ);
455 memcpy(VARDATA(result), wkb, wkb_size);
456 SET_VARSIZE(result, wkb_size+VARHDRSZ);
460 PG_FREE_IF_COPY(geom, 0);
461 PG_RETURN_BYTEA_P(result);
477 if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
479 geom = PG_GETARG_GSERIALIZED_P(0);
485 if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
486 sp.precision_xy = PG_GETARG_INT32(1);
489 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
490 sp.precision_z = PG_GETARG_INT32(2);
493 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
494 sp.precision_m = PG_GETARG_INT32(3);
500 if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) && PG_GETARG_BOOL(4) )
504 if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
509 twkb =
lwgeom_to_twkb(lwgeom, variant, sp.precision_xy, sp.precision_z, sp.precision_m, &twkb_size);
512 result = palloc(twkb_size + VARHDRSZ);
513 memcpy(VARDATA(result), twkb, twkb_size);
514 SET_VARSIZE(result, twkb_size + VARHDRSZ);
516 PG_RETURN_BYTEA_P(result);
523 ArrayType *arr_geoms = NULL;
524 ArrayType *arr_ids = NULL;
525 int num_geoms, num_ids, i = 0;
527 ArrayIterator iter_geoms, iter_ids;
528 bool null_geom, null_id;
529 Datum val_geom, val_id;
531 int is_homogeneous =
true;
536 int64_t *idlist = NULL;
545 if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
548 arr_geoms = PG_GETARG_ARRAYTYPE_P(0);
549 arr_ids = PG_GETARG_ARRAYTYPE_P(1);
551 num_geoms = ArrayGetNItems(ARR_NDIM(arr_geoms), ARR_DIMS(arr_geoms));
552 num_ids = ArrayGetNItems(ARR_NDIM(arr_ids), ARR_DIMS(arr_ids));
554 if ( num_geoms != num_ids )
556 elog(ERROR,
"size of geometry[] and integer[] arrays must match");
563 #if POSTGIS_PGSQL_VERSION >= 95 564 iter_geoms = array_create_iterator(arr_geoms, 0, NULL);
565 iter_ids = array_create_iterator(arr_ids, 0, NULL);
567 iter_geoms = array_create_iterator(arr_geoms, 0);
568 iter_ids = array_create_iterator(arr_ids, 0);
571 while( array_iterate(iter_geoms, &val_geom, &null_geom) &&
572 array_iterate(iter_ids, &val_id, &null_id) )
577 if ( null_geom || null_id )
579 elog(NOTICE,
"ST_AsTWKB skipping NULL entry at position %d", i);
584 uid = DatumGetInt64(val_id);
594 idlist = palloc0(num_geoms *
sizeof(int64_t));
600 elog(ERROR,
"Geometries have differenct dimensionality");
601 PG_FREE_IF_COPY(arr_geoms, 0);
602 PG_FREE_IF_COPY(arr_ids, 1);
613 is_homogeneous =
false;
621 array_free_iterator(iter_geoms);
622 array_free_iterator(iter_ids);
626 elog(NOTICE,
"No valid geometry - id pairs found");
627 PG_FREE_IF_COPY(arr_geoms, 0);
628 PG_FREE_IF_COPY(arr_ids, 1);
631 if ( is_homogeneous )
640 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
641 sp.precision_xy = PG_GETARG_INT32(2);
644 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
645 sp.precision_z = PG_GETARG_INT32(3);
648 if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) )
649 sp.precision_m = PG_GETARG_INT32(4);
655 if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
659 if ( PG_NARGS() > 6 && ! PG_ARGISNULL(6) && PG_GETARG_BOOL(6) )
665 sp.precision_xy, sp.precision_z, sp.precision_m,
669 result = palloc(twkb_size + VARHDRSZ);
670 memcpy(VARDATA(result), twkb, twkb_size);
671 SET_VARSIZE(result, twkb_size + VARHDRSZ);
677 PG_FREE_IF_COPY(arr_geoms, 0);
678 PG_FREE_IF_COPY(arr_ids, 1);
680 PG_RETURN_BYTEA_P(result);
696 PG_FREE_IF_COPY(geom, 0);
697 PG_RETURN_POINTER(result);
708 PG_RETURN_POINTER(geom);
710 PG_RETURN_POINTER(gserialized_drop_gidx(geom));
717 elog(ERROR,
"%s",
string);
729 text *wkt_text = PG_GETARG_TEXT_P(0);
737 result = DirectFunctionCall1(
LWGEOM_in, CStringGetDatum(wkt));
743 PG_RETURN_DATUM(result);
757 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
758 int32 geom_typmod = -1;
762 if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
763 geom_typmod = PG_GETARG_INT32(2);
772 buf->cursor = buf->len;
777 if ( geom_typmod >= 0 )
780 POSTGIS_DEBUG(3,
"typmod and geometry were consistent");
784 POSTGIS_DEBUG(3,
"typmod was -1");
788 PG_RETURN_POINTER(geom);
796 POSTGIS_DEBUG(2,
"LWGEOM_send called");
809 POSTGIS_DEBUG(2,
"LWGEOM_to_bytea called");
824 POSTGIS_DEBUG(2,
"LWGEOM_from_bytea start");
826 result = (
GSERIALIZED *)DatumGetPointer(DirectFunctionCall1(
829 PG_RETURN_POINTER(result);
uint32_t gserialized_get_type(const GSERIALIZED *s)
Extract the geometry type from the serialized form (it hides in the anonymous data area...
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS)
Datum LWGEOM_to_latlon(PG_FUNCTION_ARGS)
GSERIALIZED * postgis_valid_typmod(GSERIALIZED *gser, int32_t typmod)
Check the consistency of the metadata we want to enforce in the typmod: srid, type and dimensionality...
Datum LWGEOM_recv(PG_FUNCTION_ARGS)
int lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
uint8_t * lwgeom_to_twkb_with_idlist(const LWGEOM *geom, int64_t *idlist, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
Convert LWGEOM to a char* in TWKB format.
uint8_t * bytes_from_hexbytes(const char *hexbuf, size_t hexsize)
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS)
Datum TWKBFromLWGEOMArray(PG_FUNCTION_ARGS)
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
void lwgeom_free(LWGEOM *geom)
Datum LWGEOM_to_text(PG_FUNCTION_ARGS)
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
int gserialized_has_bbox(const GSERIALIZED *gser)
Check if a GSERIALIZED has a bounding box without deserializing first.
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Datum LWGEOMFromEWKB(PG_FUNCTION_ARGS)
#define LW_PARSER_CHECK_NONE
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
uint8_t * lwgeom_to_wkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Convert LWGEOM to a char* in WKB format.
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
Datum LWGEOM_out(PG_FUNCTION_ARGS)
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM...
char * text2cstring(const text *textptr)
Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
#define TWKB_DEFAULT_PRECISION
Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS)
Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_in)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
LWGEOM * lwgeom_from_twkb(uint8_t *twkb, size_t twkb_size, char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
void lwgeom_parser_result_init(LWGEOM_PARSER_RESULT *parser_result)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
void elog_ERROR(const char *string)
Datum LWGEOM_in(PG_FUNCTION_ARGS)
void lwcollection_free(LWCOLLECTION *col)
Datum LWGEOMFromTWKB(PG_FUNCTION_ARGS)
void lwgeom_set_srid(LWGEOM *geom, int srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
#define LW_PARSER_CHECK_ALL
LWGEOM * lwgeom_from_wkb(const uint8_t *wkb, const size_t wkb_size, const char check)
WKB inputs must have a declared size, to prevent malformed WKB from reading off the end of the memory...
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS)
uint8_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m, size_t *twkb_size)
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
Datum LWGEOM_dropBBOX(PG_FUNCTION_ARGS)
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Datum LWGEOM_send(PG_FUNCTION_ARGS)
This library is the generic geometry handling section of PostGIS.
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)