PostGIS  2.2.7dev-r@@SVN_REVISION@@
Datum transform_geom ( PG_FUNCTION_ARGS  )

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

References LWGEOM::bbox, geometry_serialize(), gserialized_get_srid(), lwgeom_add_bbox(), lwgeom_drop_bbox(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_transform(), lwproj_from_string(), LWGEOM::srid, SRID_UNKNOWN, and text2cstring().

101 {
102  GSERIALIZED *geom;
103  GSERIALIZED *result=NULL;
104  LWGEOM *lwgeom;
105  projPJ input_pj, output_pj;
106  char *input_proj4, *output_proj4;
107  text *input_proj4_text;
108  text *output_proj4_text;
109  int32 result_srid ;
110  char *pj_errstr;
111 
112 
113 
114  result_srid = PG_GETARG_INT32(3);
115  if (result_srid == SRID_UNKNOWN)
116  {
117  elog(ERROR,"transform: destination SRID = %d",SRID_UNKNOWN);
118  PG_RETURN_NULL();
119  }
120 
121  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
122  if (gserialized_get_srid(geom) == SRID_UNKNOWN)
123  {
124  pfree(geom);
125  elog(ERROR,"transform_geom: source SRID = %d",SRID_UNKNOWN);
126  PG_RETURN_NULL();
127  }
128 
129  /* Set the search path if we haven't already */
130  SetPROJ4LibPath();
131 
132  /* Read the arguments */
133  input_proj4_text = (PG_GETARG_TEXT_P(1));
134  output_proj4_text = (PG_GETARG_TEXT_P(2));
135 
136  /* Convert from text to cstring for libproj */
137  input_proj4 = text2cstring(input_proj4_text);
138  output_proj4 = text2cstring(output_proj4_text);
139 
140  /* make input and output projection objects */
141  input_pj = lwproj_from_string(input_proj4);
142  if ( input_pj == NULL )
143  {
144  pj_errstr = pj_strerrno(*pj_get_errno_ref());
145  if ( ! pj_errstr ) pj_errstr = "";
146 
147  /* we need this for error reporting */
148  /* pfree(input_proj4); */
149  pfree(output_proj4);
150  pfree(geom);
151 
152  elog(ERROR,
153  "transform_geom: could not parse proj4 string '%s' %s",
154  input_proj4, pj_errstr);
155  PG_RETURN_NULL();
156  }
157  pfree(input_proj4);
158 
159  output_pj = lwproj_from_string(output_proj4);
160 
161  if ( output_pj == NULL )
162  {
163  pj_errstr = pj_strerrno(*pj_get_errno_ref());
164  if ( ! pj_errstr ) pj_errstr = "";
165 
166  /* we need this for error reporting */
167  /* pfree(output_proj4); */
168  pj_free(input_pj);
169  pfree(geom);
170 
171  elog(ERROR,
172  "transform_geom: couldn't parse proj4 output string: '%s': %s",
173  output_proj4, pj_errstr);
174  PG_RETURN_NULL();
175  }
176  pfree(output_proj4);
177 
178  /* now we have a geometry, and input/output PJ structs. */
179  lwgeom = lwgeom_from_gserialized(geom);
180  lwgeom_transform(lwgeom, input_pj, output_pj);
181  lwgeom->srid = result_srid;
182 
183  /* clean up */
184  pj_free(input_pj);
185  pj_free(output_pj);
186 
187  /* Re-compute bbox if input had one (COMPUTE_BBOX TAINTING) */
188  if ( lwgeom->bbox )
189  {
190  lwgeom_drop_bbox(lwgeom);
191  lwgeom_add_bbox(lwgeom);
192  }
193 
194  result = geometry_serialize(lwgeom);
195 
196  lwgeom_free(lwgeom);
197  PG_FREE_IF_COPY(geom, 0);
198 
199  PG_RETURN_POINTER(result); /* new geometry */
200 }
unsigned int int32
Definition: shpopen.c:273
GBOX * bbox
Definition: liblwgeom.h:382
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
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:586
int32_t srid
Definition: liblwgeom.h:383
projPJ lwproj_from_string(const char *txt)
Get a projection from a string representation.
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:172
char * text2cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
void lwgeom_add_bbox(LWGEOM *lwgeom)
Compute a bbox if not already computed.
Definition: lwgeom.c:599
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:69

Here is the call graph for this function: