PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ transform()

Datum transform ( PG_FUNCTION_ARGS  )

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

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