PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum TWKBFromLWGEOMArray ( PG_FUNCTION_ARGS  )

Definition at line 500 of file lwgeom_inout.c.

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

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

Here is the call graph for this function: