PostGIS  2.5.0beta2dev-r@@SVN_REVISION@@

◆ transform_geom()

Datum transform_geom ( PG_FUNCTION_ARGS  )

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

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

Referenced by transform().

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