PostGIS  3.0.0dev-r@@SVN_REVISION@@

◆ transform()

Datum transform ( PG_FUNCTION_ARGS  )

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

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

46 {
47  GSERIALIZED *geom;
48  GSERIALIZED *result=NULL;
49  LWGEOM *lwgeom;
50  projPJ input_pj, output_pj;
51  int32 output_srid, input_srid;
52 
53  output_srid = PG_GETARG_INT32(1);
54  if (output_srid == SRID_UNKNOWN)
55  {
56  elog(ERROR, "%d is an invalid target SRID", SRID_UNKNOWN);
57  PG_RETURN_NULL();
58  }
59 
60  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
61  input_srid = gserialized_get_srid(geom);
62 
63  if ( input_srid == SRID_UNKNOWN )
64  {
65  PG_FREE_IF_COPY(geom, 0);
66  elog(ERROR, "Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
67  PG_RETURN_NULL();
68  }
69 
70  /* Input SRID and output SRID are equal, noop */
71  if ( input_srid == output_srid )
72  PG_RETURN_POINTER(geom);
73 
74  if ( GetProjectionsUsingFCInfo(fcinfo, input_srid, output_srid, &input_pj, &output_pj) == LW_FAILURE )
75  {
76  PG_FREE_IF_COPY(geom, 0);
77  elog(ERROR, "Failure reading projections from spatial_ref_sys.");
78  PG_RETURN_NULL();
79  }
80 
81  /* now we have a geometry, and input/output PJ structs. */
82  lwgeom = lwgeom_from_gserialized(geom);
83  lwgeom_transform(lwgeom, input_pj, output_pj);
84  lwgeom->srid = output_srid;
85 
86  /* Re-compute bbox if input had one (COMPUTE_BBOX TAINTING) */
87  if ( lwgeom->bbox )
88  {
89  lwgeom_refresh_bbox(lwgeom);
90  }
91 
92  result = geometry_serialize(lwgeom);
93  lwgeom_free(lwgeom);
94  PG_FREE_IF_COPY(geom, 0);
95 
96  PG_RETURN_POINTER(result); /* new geometry */
97 }
void lwgeom_refresh_bbox(LWGEOM *lwgeom)
Drop current bbox and calculate a fresh one.
Definition: lwgeom.c:689
unsigned int int32
Definition: shpopen.c:273
GBOX * bbox
Definition: liblwgeom.h:400
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1128
int lwgeom_transform(LWGEOM *geom, projPJ inpj, projPJ outpj)
Transform (reproject) a geometry in-place.
int32_t srid
Definition: liblwgeom.h:401
#define LW_FAILURE
Definition: liblwgeom.h:78
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
int32_t gserialized_get_srid(const GSERIALIZED *s)
Extract the SRID from the serialized form (it is packed into three bytes so this is a handy function)...
Definition: g_serialized.c:99
Here is the call graph for this function: