PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ transform_geom()

Datum transform_geom ( PG_FUNCTION_ARGS  )

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

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 }
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_refresh_bbox(LWGEOM *lwgeom)
Drop current bbox and calculate a fresh one.
Definition: lwgeom.c:698
projPJ lwproj_from_string(const char *txt)
Get a projection from a string representation.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
int lwgeom_transform(LWGEOM *geom, projPJ inpj, projPJ outpj)
Transform (reproject) a geometry in-place.
char * text_to_cstring(const text *textptr)
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition: shpopen.c:273
GBOX * bbox
Definition: liblwgeom.h:401
int32_t srid
Definition: liblwgeom.h:402

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

Here is the call graph for this function: