PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ transform()

Datum transform ( PG_FUNCTION_ARGS  )

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

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

48 {
49  GSERIALIZED *geom;
50  GSERIALIZED *result=NULL;
51  LWGEOM *lwgeom;
52  projPJ input_pj, output_pj;
53  int32 output_srid, input_srid;
54 
55  output_srid = PG_GETARG_INT32(1);
56  if (output_srid == SRID_UNKNOWN)
57  {
58  elog(ERROR,"%d is an invalid target SRID",SRID_UNKNOWN);
59  PG_RETURN_NULL();
60  }
61 
62  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
63  input_srid = gserialized_get_srid(geom);
64  if ( input_srid == SRID_UNKNOWN )
65  {
66  PG_FREE_IF_COPY(geom, 0);
67  elog(ERROR,"Input geometry has unknown (%d) SRID",SRID_UNKNOWN);
68  PG_RETURN_NULL();
69  }
70 
71  /*
72  * If input SRID and output SRID are equal, return geometry
73  * without transform it
74  */
75  if ( input_srid == output_srid )
76  PG_RETURN_POINTER(PG_GETARG_DATUM(0));
77 
78  if ( GetProjectionsUsingFCInfo(fcinfo, input_srid, output_srid, &input_pj, &output_pj) == LW_FAILURE )
79  {
80  PG_FREE_IF_COPY(geom, 0);
81  elog(ERROR,"Failure reading projections from spatial_ref_sys.");
82  PG_RETURN_NULL();
83  }
84 
85  /* now we have a geometry, and input/output PJ structs. */
86  lwgeom = lwgeom_from_gserialized(geom);
87  lwgeom_transform(lwgeom, input_pj, output_pj);
88  lwgeom->srid = output_srid;
89 
90  /* Re-compute bbox if input had one (COMPUTE_BBOX TAINTING) */
91  if ( lwgeom->bbox )
92  {
93  lwgeom_drop_bbox(lwgeom);
94  lwgeom_add_bbox(lwgeom);
95  }
96 
97  result = geometry_serialize(lwgeom);
98  lwgeom_free(lwgeom);
99  PG_FREE_IF_COPY(geom, 0);
100 
101  PG_RETURN_POINTER(result); /* new geometry */
102 }
unsigned int int32
Definition: shpopen.c:273
GBOX * bbox
Definition: liblwgeom.h:398
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
int lwgeom_transform(LWGEOM *geom, projPJ inpj, projPJ outpj)
Transform (reproject) a geometry in-place.
void lwgeom_drop_bbox(LWGEOM *lwgeom)
Call this function to drop BBOX and SRID from LWGEOM.
Definition: lwgeom.c:635
int32_t srid
Definition: liblwgeom.h:399
#define LW_FAILURE
Definition: liblwgeom.h:79
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:188
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:648
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:100
Here is the call graph for this function: