PostGIS  2.5.1dev-r@@SVN_REVISION@@

◆ TWKBFromLWGEOMArray()

Datum TWKBFromLWGEOMArray ( PG_FUNCTION_ARGS  )

Definition at line 522 of file lwgeom_inout.c.

References COLLECTIONTYPE, lwcollection_add_lwgeom(), lwcollection_as_lwgeom(), lwcollection_construct_empty(), lwcollection_free(), LWGEOM_addBBOX(), lwgeom_from_gserialized(), lwgeom_get_srid(), lwgeom_get_type(), lwgeom_has_m(), lwgeom_has_z(), lwgeom_to_twkb_with_idlist(), lwtype_get_collectiontype(), PG_FUNCTION_INFO_V1(), TWKB_BBOX, TWKB_DEFAULT_PRECISION, TWKB_ID, TWKB_SIZE, LWCOLLECTION::type, and variant.

Referenced by TWKBFromLWGEOM().

523 {
524  ArrayType *arr_geoms = NULL;
525  ArrayType *arr_ids = NULL;
526  int num_geoms, num_ids, i = 0;
527 
528  ArrayIterator iter_geoms, iter_ids;
529  bool null_geom, null_id;
530  Datum val_geom, val_id;
531 
532  int is_homogeneous = true;
533  uint32_t subtype = 0;
534  int has_z = 0;
535  int has_m = 0;
536  LWCOLLECTION *col = NULL;
537  int64_t *idlist = NULL;
538  uint8_t variant = 0;
539 
540  srs_precision sp;
541  uint8_t *twkb;
542  size_t twkb_size;
543  bytea *result;
544 
545  /* The first two arguments are required */
546  if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
547  PG_RETURN_NULL();
548 
549  arr_geoms = PG_GETARG_ARRAYTYPE_P(0);
550  arr_ids = PG_GETARG_ARRAYTYPE_P(1);
551 
552  num_geoms = ArrayGetNItems(ARR_NDIM(arr_geoms), ARR_DIMS(arr_geoms));
553  num_ids = ArrayGetNItems(ARR_NDIM(arr_ids), ARR_DIMS(arr_ids));
554 
555  if ( num_geoms != num_ids )
556  {
557  elog(ERROR, "size of geometry[] and integer[] arrays must match");
558  PG_RETURN_NULL();
559  }
560 
561  /* Loop through array and build a collection of geometry and */
562  /* a simple array of ids. If either side is NULL, skip it */
563 
564 #if POSTGIS_PGSQL_VERSION >= 95
565  iter_geoms = array_create_iterator(arr_geoms, 0, NULL);
566  iter_ids = array_create_iterator(arr_ids, 0, NULL);
567 #else
568  iter_geoms = array_create_iterator(arr_geoms, 0);
569  iter_ids = array_create_iterator(arr_ids, 0);
570 #endif
571 
572  while( array_iterate(iter_geoms, &val_geom, &null_geom) &&
573  array_iterate(iter_ids, &val_id, &null_id) )
574  {
575  LWGEOM *geom;
576  int32_t uid;
577 
578  if ( null_geom || null_id )
579  {
580  elog(NOTICE, "ST_AsTWKB skipping NULL entry at position %d", i);
581  continue;
582  }
583 
584  geom = lwgeom_from_gserialized((GSERIALIZED*)DatumGetPointer(val_geom));
585  uid = DatumGetInt64(val_id);
586 
587  /* Construct collection/idlist first time through */
588  if ( ! col )
589  {
590  has_z = lwgeom_has_z(geom);
591  has_m = lwgeom_has_m(geom);
592  col = lwcollection_construct_empty(COLLECTIONTYPE, lwgeom_get_srid(geom), has_z, has_m);
593  }
594  if ( ! idlist )
595  idlist = palloc0(num_geoms * sizeof(int64_t));
596 
597 
598  /* Check if there is differences in dimensionality*/
599  if( lwgeom_has_z(geom)!=has_z || lwgeom_has_m(geom)!=has_m)
600  {
601  elog(ERROR, "Geometries have different dimensionality");
602  PG_FREE_IF_COPY(arr_geoms, 0);
603  PG_FREE_IF_COPY(arr_ids, 1);
604  PG_RETURN_NULL();
605  }
606  /* Store the values */
607  lwcollection_add_lwgeom(col, geom);
608  idlist[i++] = uid;
609 
610  /* Grab the geometry type and note if all geometries share it */
611  /* If so, we can make this a homogeneous collection and save some space */
612  if ( lwgeom_get_type(geom) != subtype && subtype )
613  {
614  is_homogeneous = false;
615  }
616  else
617  {
618  subtype = lwgeom_get_type(geom);
619  }
620 
621  }
622  array_free_iterator(iter_geoms);
623  array_free_iterator(iter_ids);
624 
625  if(i==0)
626  {
627  elog(NOTICE, "No valid geometry - id pairs found");
628  PG_FREE_IF_COPY(arr_geoms, 0);
629  PG_FREE_IF_COPY(arr_ids, 1);
630  PG_RETURN_NULL();
631  }
632  if ( is_homogeneous )
633  {
634  col->type = lwtype_get_collectiontype(subtype);
635  }
636 
637  /* Read sensible precision defaults (about one meter) given the srs */
638  sp = srid_axis_precision(fcinfo, lwgeom_get_srid(lwcollection_as_lwgeom(col)), TWKB_DEFAULT_PRECISION);
639 
640  /* If user specified XY precision, use it */
641  if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
642  sp.precision_xy = PG_GETARG_INT32(2);
643 
644  /* If user specified Z precision, use it */
645  if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
646  sp.precision_z = PG_GETARG_INT32(3);
647 
648  /* If user specified M precision, use it */
649  if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) )
650  sp.precision_m = PG_GETARG_INT32(4);
651 
652  /* We are building an ID'ed output */
653  variant = TWKB_ID;
654 
655  /* If user wants registered twkb sizes */
656  if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
657  variant |= TWKB_SIZE;
658 
659  /* If user wants bounding boxes */
660  if ( PG_NARGS() > 6 && ! PG_ARGISNULL(6) && PG_GETARG_BOOL(6) )
661  variant |= TWKB_BBOX;
662 
663  /* Write out the TWKB */
665  idlist, variant,
666  sp.precision_xy, sp.precision_z, sp.precision_m,
667  &twkb_size);
668 
669  /* Convert to a bytea return type */
670  result = palloc(twkb_size + VARHDRSZ);
671  memcpy(VARDATA(result), twkb, twkb_size);
672  SET_VARSIZE(result, twkb_size + VARHDRSZ);
673 
674  /* Clean up */
675  pfree(twkb);
676  pfree(idlist);
677  lwcollection_free(col);
678  PG_FREE_IF_COPY(arr_geoms, 0);
679  PG_FREE_IF_COPY(arr_ids, 1);
680 
681  PG_RETURN_BYTEA_P(result);
682 }
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.
Definition: lwout_twkb.c:567
uint8_t variant
Definition: cu_in_twkb.c:26
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
uint8_t type
Definition: liblwgeom.h:505
uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.
Definition: lwgeom.c:923
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:916
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:930
unsigned int uint32_t
Definition: uthash.h:78
#define TWKB_ID
Definition: liblwgeom.h:2084
#define TWKB_DEFAULT_PRECISION
Definition: liblwgeom.h:2087
uint32_t lwtype_get_collectiontype(uint8_t type)
Given an lwtype number, what homogeneous collection can hold it?
Definition: lwgeom.c:1120
#define TWKB_BBOX
Definition: liblwgeom.h:2082
#define TWKB_SIZE
Definition: liblwgeom.h:2083
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:356
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
unsigned char uint8_t
Definition: uthash.h:79
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:937
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:300
Here is the call graph for this function:
Here is the caller graph for this function: