27 #include "../postgis_config.h"
35 #include "access/gist.h"
36 #include "access/itup.h"
39 #include "utils/elog.h"
40 #include "mb/pg_wchar.h"
41 #include "lib/stringinfo.h"
42 #include "utils/array.h"
43 #include "utils/builtins.h"
44 #include "utils/lsyscache.h"
48 #include "lwgeom_cache.h"
49 #include "lwgeom_pg.h"
51 #include "lwgeom_transform.h"
54 #include "access/htup_details.h"
87 char *input = PG_GETARG_CSTRING(0);
88 int32 geom_typmod = -1;
95 if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
96 geom_typmod = PG_GETARG_INT32(2);
102 if (
str[0] ==
'\0' ) {
103 ereport(ERROR,(errmsg(
"parse error - invalid geometry")));
108 if( strncasecmp(
str,
"SRID=",5) == 0 )
112 while ( tmp && *tmp !=
';' )
116 if ( tmp && *(tmp+1) ==
'0' )
132 size_t hexsize = strlen(
str);
137 if ( !lwgeom ) PG_RETURN_NULL();
143 ret = geometry_serialize(lwgeom);
146 else if (
str[0] ==
'{')
152 srid = GetSRIDCacheBySRS(fcinfo, srs);
156 ret = geometry_serialize(lwgeom);
164 PG_PARSER_ERROR(lwg_parser_result);
167 lwgeom = lwg_parser_result.
geom;
170 ret = geometry_serialize(lwgeom);
174 if ( geom_typmod >= 0 )
177 POSTGIS_DEBUG(3,
"typmod and geometry were consistent");
181 POSTGIS_DEBUG(3,
"typmod was -1");
187 PG_RETURN_POINTER(ret);
217 GSERIALIZED *pg_lwgeom = PG_GETARG_GSERIALIZED_P(0);
218 text *format_text = PG_GETARG_TEXT_P(1);
221 char *format_str = NULL;
223 char * formatted_str;
224 text * formatted_text;
231 lwpgerror(
"Only points are supported, you tried type %s.",
lwtype_name(geom_type));
236 if (format_text == NULL) {
237 lwpgerror(
"ST_AsLatLonText: invalid format string (null");
242 lwpgerror(
"ST_AsLatLonText: invalid coordinate");
246 format_str = text_to_cstring(format_text);
247 assert(format_str != NULL);
251 tmp = (
char *)pg_do_encoding_conversion(
252 (uint8_t *)format_str, strlen(format_str), GetDatabaseEncoding(), PG_UTF8);
254 if ( tmp != format_str ) {
261 assert(formatted_str != NULL);
265 tmp = (
char *)pg_do_encoding_conversion(
266 (uint8_t *)formatted_str, strlen(formatted_str),
267 PG_UTF8, GetDatabaseEncoding());
269 if ( tmp != formatted_str) {
270 pfree(formatted_str);
275 formatted_text = cstring_to_text(formatted_str);
276 pfree(formatted_str);
278 PG_RETURN_POINTER(formatted_text);
307 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
309 text *
type = PG_GETARG_TEXT_P(1);
311 if ( ! strncmp(VARDATA(
type),
"xdr", 3) ||
312 ! strncmp(VARDATA(
type),
"XDR", 3) )
353 bytea *bytea_wkb = PG_GETARG_BYTEA_P(0);
356 uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb);
360 lwpgerror(
"Unable to parse WKB");
362 if ((PG_NARGS() > 1) && (!PG_ARGISNULL(1)))
364 int32 srid = PG_GETARG_INT32(1);
371 geom = geometry_serialize(lwgeom);
373 PG_FREE_IF_COPY(bytea_wkb, 0);
374 PG_RETURN_POINTER(geom);
384 bytea *bytea_twkb = PG_GETARG_BYTEA_P(0);
387 uint8_t *twkb = (uint8_t*)VARDATA(bytea_twkb);
394 geom = geometry_serialize(lwgeom);
396 PG_FREE_IF_COPY(bytea_twkb, 0);
397 PG_RETURN_POINTER(geom);
412 if ( (PG_NARGS()>1) && (!PG_ARGISNULL(1)) )
414 text *
type = PG_GETARG_TEXT_P(1);
416 if ( ! strncmp(VARDATA(
type),
"xdr", 3) ||
417 ! strncmp(VARDATA(
type),
"XDR", 3) )
442 if ( PG_ARGISNULL(0) ) PG_RETURN_NULL();
444 geom = PG_GETARG_GSERIALIZED_P(0);
450 if ( PG_NARGS() > 1 && ! PG_ARGISNULL(1) )
451 sp.precision_xy = PG_GETARG_INT32(1);
454 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
455 sp.precision_z = PG_GETARG_INT32(2);
458 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
459 sp.precision_m = PG_GETARG_INT32(3);
465 if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) && PG_GETARG_BOOL(4) )
469 if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
481 ArrayType *arr_geoms = NULL;
482 ArrayType *arr_ids = NULL;
483 int num_geoms, num_ids, i = 0;
485 ArrayIterator iter_geoms, iter_ids;
486 bool null_geom, null_id;
487 Datum val_geom, val_id;
489 int is_homogeneous =
true;
490 uint32_t subtype = 0;
494 int64_t *idlist = NULL;
500 if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
503 arr_geoms = PG_GETARG_ARRAYTYPE_P(0);
504 arr_ids = PG_GETARG_ARRAYTYPE_P(1);
506 num_geoms = ArrayGetNItems(ARR_NDIM(arr_geoms), ARR_DIMS(arr_geoms));
507 num_ids = ArrayGetNItems(ARR_NDIM(arr_ids), ARR_DIMS(arr_ids));
509 if ( num_geoms != num_ids )
511 elog(ERROR,
"size of geometry[] and integer[] arrays must match");
518 iter_geoms = array_create_iterator(arr_geoms, 0, NULL);
519 iter_ids = array_create_iterator(arr_ids, 0, NULL);
521 while( array_iterate(iter_geoms, &val_geom, &null_geom) &&
522 array_iterate(iter_ids, &val_id, &null_id) )
527 if ( null_geom || null_id )
529 elog(NOTICE,
"ST_AsTWKB skipping NULL entry at position %d", i);
534 uid = DatumGetInt64(val_id);
544 idlist = palloc0(num_geoms *
sizeof(int64_t));
550 elog(ERROR,
"Geometries have different dimensionality");
551 PG_FREE_IF_COPY(arr_geoms, 0);
552 PG_FREE_IF_COPY(arr_ids, 1);
563 is_homogeneous =
false;
571 array_free_iterator(iter_geoms);
572 array_free_iterator(iter_ids);
576 elog(NOTICE,
"No valid geometry - id pairs found");
577 PG_FREE_IF_COPY(arr_geoms, 0);
578 PG_FREE_IF_COPY(arr_ids, 1);
581 if ( is_homogeneous )
590 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
591 sp.precision_xy = PG_GETARG_INT32(2);
594 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
595 sp.precision_z = PG_GETARG_INT32(3);
598 if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) )
599 sp.precision_m = PG_GETARG_INT32(4);
605 if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
609 if ( PG_NARGS() > 6 && ! PG_ARGISNULL(6) && PG_GETARG_BOOL(6) )
628 result = geometry_serialize(lwgeom);
630 PG_FREE_IF_COPY(geom, 0);
631 PG_RETURN_POINTER(
result);
642 PG_RETURN_POINTER(geom);
651 elog(ERROR,
"%s",
string);
663 text *wkt_text = PG_GETARG_TEXT_P(0);
668 wkt = text_to_cstring(wkt_text);
673 result = CallerFInfoFunctionCall1(
LWGEOM_in, fcinfo->flinfo, InvalidOid, CStringGetDatum(wkt));
693 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
694 int32 geom_typmod = -1;
698 if ( (PG_NARGS()>2) && (!PG_ARGISNULL(2)) ) {
699 geom_typmod = PG_GETARG_INT32(2);
705 ereport(ERROR,(errmsg(
"recv error - invalid geometry")));
713 buf->cursor = buf->len;
715 geom = geometry_serialize(lwgeom);
718 if ( geom_typmod >= 0 )
721 POSTGIS_DEBUG(3,
"typmod and geometry were consistent");
725 POSTGIS_DEBUG(3,
"typmod was -1");
729 PG_RETURN_POINTER(geom);
737 POSTGIS_DEBUG(2,
"LWGEOM_send called");
750 POSTGIS_DEBUG(2,
"LWGEOM_to_bytea called");
765 POSTGIS_DEBUG(2,
"LWGEOM_from_bytea start");
770 PG_RETURN_POINTER(
result);
char result[OUT_DOUBLE_BUFFER_SIZE]
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...
int gserialized_has_bbox(const GSERIALIZED *g)
Check if a GSERIALIZED has a bounding box without deserializing first.
int32_t gserialized_get_srid(const GSERIALIZED *g)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
uint32_t gserialized_get_type(const GSERIALIZED *g)
Extract the geometry type from the serialized form (it hides in the anonymous data area,...
GSERIALIZED * gserialized_drop_gbox(GSERIALIZED *g)
Remove the bounding box from a GSERIALIZED.
#define LW_PARSER_CHECK_ALL
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
LWGEOM * lwgeom_from_geojson(const char *geojson, char **srs)
Create an LWGEOM object from a GeoJSON representation.
void lwgeom_set_srid(LWGEOM *geom, int32_t srid)
Set the SRID on an LWGEOM For collections, only the parent gets an SRID, all the children get SRID_UN...
void lwgeom_free(LWGEOM *geom)
#define LW_PARSER_CHECK_NONE
LWGEOM * lwgeom_from_twkb(const 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)
int lwgeom_parse_wkt(LWGEOM_PARSER_RESULT *parser_result, char *wktstr, int parse_flags)
Parse a WKT geometry string into an LWGEOM structure.
char * lwgeom_to_hexwkb_buffer(const LWGEOM *geom, uint8_t variant)
lwvarlena_t * lwgeom_to_twkb(const LWGEOM *geom, uint8_t variant, int8_t precision_xy, int8_t precision_z, int8_t precision_m)
uint8_t * bytes_from_hexbytes(const char *hexbuf, size_t hexsize)
int lwgeom_needs_bbox(const LWGEOM *geom)
Check whether or not a lwgeom is big enough to warrant a bounding box.
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
int lwgeom_isfinite(const LWGEOM *lwgeom)
Check if a LWGEOM has any non-finite (NaN or Inf) coordinates.
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
#define TWKB_DEFAULT_PRECISION
lwvarlena_t * lwgeom_to_hexwkb_varlena(const LWGEOM *geom, uint8_t variant)
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
lwvarlena_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)
Convert LWGEOM to a char* in TWKB format.
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...
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
lwvarlena_t * lwgeom_to_wkb_varlena(const LWGEOM *geom, uint8_t variant)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
void lwgeom_parser_result_free(LWGEOM_PARSER_RESULT *parser_result)
This library is the generic geometry handling section of PostGIS.
Datum WKBFromLWGEOM(PG_FUNCTION_ARGS)
Datum parse_WKT_lwgeom(PG_FUNCTION_ARGS)
Datum TWKBFromLWGEOM(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_in)
Datum LWGEOM_in(PG_FUNCTION_ARGS)
Datum LWGEOM_to_latlon(PG_FUNCTION_ARGS)
Datum LWGEOM_out(PG_FUNCTION_ARGS)
Datum LWGEOM_to_bytea(PG_FUNCTION_ARGS)
Datum LWGEOMFromTWKB(PG_FUNCTION_ARGS)
Datum LWGEOM_from_bytea(PG_FUNCTION_ARGS)
Datum TWKBFromLWGEOMArray(PG_FUNCTION_ARGS)
Datum LWGEOM_send(PG_FUNCTION_ARGS)
Datum LWGEOM_dropBBOX(PG_FUNCTION_ARGS)
Datum LWGEOM_to_text(PG_FUNCTION_ARGS)
Datum LWGEOMFromEWKB(PG_FUNCTION_ARGS)
Datum LWGEOM_addBBOX(PG_FUNCTION_ARGS)
void elog_ERROR(const char *string)
Datum LWGEOM_asHEXEWKB(PG_FUNCTION_ARGS)
Datum LWGEOM_recv(PG_FUNCTION_ARGS)
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Parser result structure: returns the result of attempting to convert (E)WKT/(E)WKB to LWGEOM.