PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ transform()

Datum transform ( PG_FUNCTION_ARGS  )

Definition at line 51 of file postgis/lwgeom_transform.c.

52 {
53  GSERIALIZED* geom;
54  GSERIALIZED* result=NULL;
55  LWGEOM* lwgeom;
56  LWPROJ *pj;
57  int32 srid_to, srid_from;
58 
59  srid_to = PG_GETARG_INT32(1);
60  if (srid_to == SRID_UNKNOWN)
61  {
62  elog(ERROR, "ST_Transform: %d is an invalid target SRID", SRID_UNKNOWN);
63  PG_RETURN_NULL();
64  }
65 
66  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
67  srid_from = gserialized_get_srid(geom);
68 
69  if ( srid_from == SRID_UNKNOWN )
70  {
71  PG_FREE_IF_COPY(geom, 0);
72  elog(ERROR, "ST_Transform: Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
73  PG_RETURN_NULL();
74  }
75 
76  /* Input SRID and output SRID are equal, noop */
77  if ( srid_from == srid_to )
78  PG_RETURN_POINTER(geom);
79 
80  postgis_initialize_cache();
81  if ( lwproj_lookup(srid_from, srid_to, &pj) == LW_FAILURE )
82  {
83  PG_FREE_IF_COPY(geom, 0);
84  elog(ERROR, "ST_Transform: Failure reading projections from spatial_ref_sys.");
85  PG_RETURN_NULL();
86  }
87 
88  /* now we have a geometry, and input/output PJ structs. */
89  lwgeom = lwgeom_from_gserialized(geom);
90  lwgeom_transform(lwgeom, pj);
91  lwgeom->srid = srid_to;
92 
93  /* Re-compute bbox if input had one (COMPUTE_BBOX TAINTING) */
94  if ( lwgeom->bbox )
95  {
96  lwgeom_refresh_bbox(lwgeom);
97  }
98 
99  result = geometry_serialize(lwgeom);
100  lwgeom_free(lwgeom);
101  PG_FREE_IF_COPY(geom, 0);
102 
103  PG_RETURN_POINTER(result); /* new geometry */
104 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
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)...
Definition: gserialized.c:126
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
Definition: gserialized.c:239
void lwgeom_refresh_bbox(LWGEOM *lwgeom)
Drop current bbox and calculate a fresh one.
Definition: lwgeom.c:707
#define LW_FAILURE
Definition: liblwgeom.h:96
int lwgeom_transform(LWGEOM *geom, LWPROJ *pj)
Transform (reproject) a geometry in-place.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:215
unsigned int int32
Definition: shpopen.c:54
GBOX * bbox
Definition: liblwgeom.h:458
int32_t srid
Definition: liblwgeom.h:460

References LWGEOM::bbox, gserialized_get_srid(), LW_FAILURE, lwgeom_free(), lwgeom_from_gserialized(), lwgeom_refresh_bbox(), lwgeom_transform(), result, LWGEOM::srid, and SRID_UNKNOWN.

Here is the call graph for this function: