PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ transform_geom()

Datum transform_geom ( PG_FUNCTION_ARGS  )

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

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

Referenced by transform().

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