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

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

References LWGEOM::bbox, geometry_serialize(), lwgeom_free(), lwgeom_from_gserialized(), lwgeom_refresh_bbox(), lwgeom_transform(), lwproj_from_string(), LWGEOM::srid, and text2cstring().

111 {
112  GSERIALIZED *geom;
113  GSERIALIZED *result=NULL;
114  LWGEOM *lwgeom;
115  projPJ input_pj, output_pj;
116  char *input_proj4, *output_proj4;
117  text *input_proj4_text;
118  text *output_proj4_text;
119  int32 result_srid ;
120  char *pj_errstr;
121 
122  result_srid = PG_GETARG_INT32(3);
123  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
124 
125  /* Set the search path if we haven't already */
126  SetPROJ4LibPath();
127 
128  /* Read the arguments */
129  input_proj4_text = (PG_GETARG_TEXT_P(1));
130  output_proj4_text = (PG_GETARG_TEXT_P(2));
131 
132  /* Convert from text to cstring for libproj */
133  input_proj4 = text2cstring(input_proj4_text);
134  output_proj4 = text2cstring(output_proj4_text);
135 
136  /* make input and output projection objects */
137  input_pj = lwproj_from_string(input_proj4);
138  if ( input_pj == NULL )
139  {
140  pj_errstr = pj_strerrno(*pj_get_errno_ref());
141  if ( ! pj_errstr ) pj_errstr = "";
142 
143  /* we need this for error reporting */
144  /* pfree(input_proj4); */
145  pfree(output_proj4);
146  pfree(geom);
147 
148  elog(ERROR,
149  "transform_geom: could not parse proj4 string '%s' %s",
150  input_proj4, pj_errstr);
151  PG_RETURN_NULL();
152  }
153  pfree(input_proj4);
154 
155  output_pj = lwproj_from_string(output_proj4);
156 
157  if ( output_pj == NULL )
158  {
159  pj_errstr = pj_strerrno(*pj_get_errno_ref());
160  if ( ! pj_errstr ) pj_errstr = "";
161 
162  /* we need this for error reporting */
163  /* pfree(output_proj4); */
164  pj_free(input_pj);
165  pfree(geom);
166 
167  elog(ERROR,
168  "transform_geom: couldn't parse proj4 output string: '%s': %s",
169  output_proj4, pj_errstr);
170  PG_RETURN_NULL();
171  }
172  pfree(output_proj4);
173 
174  /* now we have a geometry, and input/output PJ structs. */
175  lwgeom = lwgeom_from_gserialized(geom);
176  lwgeom_transform(lwgeom, input_pj, output_pj);
177  lwgeom->srid = result_srid;
178 
179  /* clean up */
180  pj_free(input_pj);
181  pj_free(output_pj);
182 
183  /* Re-compute bbox if input had one (COMPUTE_BBOX TAINTING) */
184  if ( lwgeom->bbox )
185  {
186  lwgeom_refresh_bbox(lwgeom);
187  }
188 
189  result = geometry_serialize(lwgeom);
190 
191  lwgeom_free(lwgeom);
192  PG_FREE_IF_COPY(geom, 0);
193 
194  PG_RETURN_POINTER(result); /* new geometry */
195 }
void lwgeom_refresh_bbox(LWGEOM *lwgeom)
Drop current bbox and calculate a fresh one.
Definition: lwgeom.c:691
unsigned int int32
Definition: shpopen.c:273
GBOX * bbox
Definition: liblwgeom.h:397
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
int lwgeom_transform(LWGEOM *geom, projPJ inpj, projPJ outpj)
Transform (reproject) a geometry in-place.
int32_t srid
Definition: liblwgeom.h:398
projPJ lwproj_from_string(const char *txt)
Get a projection from a string representation.
char * text2cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)

Here is the call graph for this function: