PostGIS  2.5.0dev-r@@SVN_REVISION@@
postgis/lwgeom_transform.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * PostGIS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * PostGIS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  **********************************************************************
20  *
21  * Copyright (C) 2001-2003 Refractions Research Inc.
22  *
23  **********************************************************************/
24 
25 
26 #include "postgres.h"
27 #include "fmgr.h"
28 
29 #include "../postgis_config.h"
30 #include "liblwgeom.h"
31 #include "lwgeom_transform.h"
32 
33 
34 Datum transform(PG_FUNCTION_ARGS);
35 Datum transform_geom(PG_FUNCTION_ARGS);
36 Datum postgis_proj_version(PG_FUNCTION_ARGS);
37 
38 
39 
47 Datum transform(PG_FUNCTION_ARGS)
48 {
49  GSERIALIZED *geom;
50  GSERIALIZED *result=NULL;
51  LWGEOM *lwgeom;
52  projPJ input_pj, output_pj;
53  int32 output_srid, input_srid;
54 
55  output_srid = PG_GETARG_INT32(1);
56  if (output_srid == SRID_UNKNOWN)
57  {
58  elog(ERROR, "%d is an invalid target SRID", SRID_UNKNOWN);
59  PG_RETURN_NULL();
60  }
61 
62  geom = PG_GETARG_GSERIALIZED_P_COPY(0);
63  input_srid = gserialized_get_srid(geom);
64 
65  if ( input_srid == SRID_UNKNOWN )
66  {
67  PG_FREE_IF_COPY(geom, 0);
68  elog(ERROR, "Input geometry has unknown (%d) SRID", SRID_UNKNOWN);
69  PG_RETURN_NULL();
70  }
71 
72  /* Input SRID and output SRID are equal, noop */
73  if ( input_srid == output_srid )
74  PG_RETURN_POINTER(geom);
75 
76  if ( GetProjectionsUsingFCInfo(fcinfo, input_srid, output_srid, &input_pj, &output_pj) == LW_FAILURE )
77  {
78  PG_FREE_IF_COPY(geom, 0);
79  elog(ERROR, "Failure reading projections from spatial_ref_sys.");
80  PG_RETURN_NULL();
81  }
82 
83  /* now we have a geometry, and input/output PJ structs. */
84  lwgeom = lwgeom_from_gserialized(geom);
85  lwgeom_transform(lwgeom, input_pj, output_pj);
86  lwgeom->srid = output_srid;
87 
88  /* Re-compute bbox if input had one (COMPUTE_BBOX TAINTING) */
89  if ( lwgeom->bbox )
90  {
91  lwgeom_refresh_bbox(lwgeom);
92  }
93 
94  result = geometry_serialize(lwgeom);
95  lwgeom_free(lwgeom);
96  PG_FREE_IF_COPY(geom, 0);
97 
98  PG_RETURN_POINTER(result); /* new geometry */
99 }
100 
110 Datum transform_geom(PG_FUNCTION_ARGS)
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 }
196 
197 
199 Datum postgis_proj_version(PG_FUNCTION_ARGS)
200 {
201  const char *ver = pj_get_release();
202  text *result = cstring2text(ver);
203  PG_RETURN_POINTER(result);
204 }
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.
Datum transform(PG_FUNCTION_ARGS)
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
#define LW_FAILURE
Definition: liblwgeom.h:78
projPJ lwproj_from_string(const char *txt)
Get a projection from a string representation.
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
char * text2cstring(const text *textptr)
Datum postgis_proj_version(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(transform)
transform( GEOMETRY, INT (output srid) ) tmpPts - if there is a nadgrid error (-38), we re-try the transform on a copy of points.
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
Datum transform_geom(PG_FUNCTION_ARGS)
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:99
This library is the generic geometry handling section of PostGIS.