480{
481 ArrayType *arr_geoms = NULL;
482 ArrayType *arr_ids = NULL;
483 int num_geoms, num_ids, i = 0;
484
485 ArrayIterator iter_geoms, iter_ids;
486 bool null_geom, null_id;
487 Datum val_geom, val_id;
488
489 int is_homogeneous = true;
490 uint32_t subtype = 0;
491 int has_z = 0;
492 int has_m = 0;
494 int64_t *idlist = NULL;
496
497 srs_precision sp;
498
499
500 if ( PG_NARGS() < 2 || PG_ARGISNULL(0) || PG_ARGISNULL(1) )
501 PG_RETURN_NULL();
502
503 arr_geoms = PG_GETARG_ARRAYTYPE_P(0);
504 arr_ids = PG_GETARG_ARRAYTYPE_P(1);
505
506 num_geoms = ArrayGetNItems(ARR_NDIM(arr_geoms), ARR_DIMS(arr_geoms));
507 num_ids = ArrayGetNItems(ARR_NDIM(arr_ids), ARR_DIMS(arr_ids));
508
509 if ( num_geoms != num_ids )
510 {
511 elog(ERROR, "size of geometry[] and integer[] arrays must match");
512 PG_RETURN_NULL();
513 }
514
515
516
517
518 iter_geoms = array_create_iterator(arr_geoms, 0, NULL);
519 iter_ids = array_create_iterator(arr_ids, 0, NULL);
520
521 while( array_iterate(iter_geoms, &val_geom, &null_geom) &&
522 array_iterate(iter_ids, &val_id, &null_id) )
523 {
525 int32_t uid;
526
527 if ( null_geom || null_id )
528 {
529 elog(NOTICE, "ST_AsTWKB skipping NULL entry at position %d", i);
530 continue;
531 }
532
534 uid = DatumGetInt64(val_id);
535
536
537 if ( ! col )
538 {
542 }
543 if ( ! idlist )
544 idlist = palloc0(num_geoms * sizeof(int64_t));
545
546
547
549 {
550 elog(ERROR, "Geometries have different dimensionality");
551 PG_FREE_IF_COPY(arr_geoms, 0);
552 PG_FREE_IF_COPY(arr_ids, 1);
553 PG_RETURN_NULL();
554 }
555
557 idlist[i++] = uid;
558
559
560
562 {
563 is_homogeneous = false;
564 }
565 else
566 {
568 }
569
570 }
571 array_free_iterator(iter_geoms);
572 array_free_iterator(iter_ids);
573
574 if(i==0)
575 {
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);
579 PG_RETURN_NULL();
580 }
581 if ( is_homogeneous )
582 {
584 }
585
586
588
589
590 if ( PG_NARGS() > 2 && ! PG_ARGISNULL(2) )
591 sp.precision_xy = PG_GETARG_INT32(2);
592
593
594 if ( PG_NARGS() > 3 && ! PG_ARGISNULL(3) )
595 sp.precision_z = PG_GETARG_INT32(3);
596
597
598 if ( PG_NARGS() > 4 && ! PG_ARGISNULL(4) )
599 sp.precision_m = PG_GETARG_INT32(4);
600
601
603
604
605 if ( PG_NARGS() > 5 && ! PG_ARGISNULL(5) && PG_GETARG_BOOL(5) )
607
608
609 if ( PG_NARGS() > 6 && ! PG_ARGISNULL(6) && PG_GETARG_BOOL(6) )
611
612
615}
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
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.
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
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.
#define TWKB_DEFAULT_PRECISION
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int32_t srid, char hasz, char hasm)
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
static uint32_t lwgeom_get_type(const LWGEOM *geom)
Return LWTYPE number.