PostGIS  2.5.7dev-r@@SVN_REVISION@@
lwgeom_sqlmm.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 2001-2006 Refractions Research Inc.
22  *
23  **********************************************************************/
24 
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <math.h>
31 
32 #include "postgres.h"
33 #include "fmgr.h"
34 
35 #include "../postgis_config.h"
36 #include "liblwgeom.h"
37 #include "lwgeom_pg.h"
38 
39 
40 Datum LWGEOM_has_arc(PG_FUNCTION_ARGS);
41 Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS);
42 Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS);
43 
44 
45 
46 /*******************************************************************************
47  * Begin PG_FUNCTIONs
48  ******************************************************************************/
49 
51 Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
52 {
53  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
54  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
55  uint32 result = lwgeom_has_arc(lwgeom);
56  lwgeom_free(lwgeom);
57  PG_RETURN_BOOL(result == 1);
58 }
59 
60 /*
61  * Converts any curve segments of the geometry into a linear approximation.
62  * Curve centers are determined by projecting the defining points into the 2d
63  * plane. Z and M values are assigned by linear interpolation between
64  * defining points.
65  *
66  * TODO: drop, use ST_CurveToLine instead
67  */
69 Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
70 {
71  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
72  int32 perQuad = PG_GETARG_INT32(1);
73  GSERIALIZED *ret;
74  LWGEOM *igeom = NULL, *ogeom = NULL;
75 
76  POSTGIS_DEBUG(2, "LWGEOM_curve_segmentize called.");
77 
78  if (perQuad < 0)
79  {
80  elog(ERROR, "2nd argument must be positive.");
81  PG_RETURN_NULL();
82  }
83 
84  POSTGIS_DEBUGF(3, "perQuad = %d", perQuad);
85 
86  igeom = lwgeom_from_gserialized(geom);
87  ogeom = lwgeom_stroke(igeom, perQuad);
88  lwgeom_free(igeom);
89 
90  if (ogeom == NULL)
91  PG_RETURN_NULL();
92 
93  ret = geometry_serialize(ogeom);
94  lwgeom_free(ogeom);
95  PG_FREE_IF_COPY(geom, 0);
96  PG_RETURN_POINTER(ret);
97 }
98 
100 Datum ST_CurveToLine(PG_FUNCTION_ARGS)
101 {
102  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
103  double tol = PG_GETARG_FLOAT8(1);
104  int toltype = PG_GETARG_INT32(2);
105  int flags = PG_GETARG_INT32(3);
106  GSERIALIZED *ret;
107  LWGEOM *igeom = NULL, *ogeom = NULL;
108 
109  POSTGIS_DEBUG(2, "ST_CurveToLine called.");
110 
111  POSTGIS_DEBUGF(3, "tol = %g, typ = %d, flg = %d", tol, toltype, flags);
112 
113  igeom = lwgeom_from_gserialized(geom);
114  ogeom = lwcurve_linearize(igeom, tol, toltype, flags);
115  lwgeom_free(igeom);
116 
117  if (ogeom == NULL)
118  PG_RETURN_NULL();
119 
120  ret = geometry_serialize(ogeom);
121  lwgeom_free(ogeom);
122  PG_FREE_IF_COPY(geom, 0);
123  PG_RETURN_POINTER(ret);
124 }
125 
127 Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
128 {
129  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
130  GSERIALIZED *ret;
131  LWGEOM *igeom = NULL, *ogeom = NULL;
132 
133  POSTGIS_DEBUG(2, "LWGEOM_line_desegmentize.");
134 
135  igeom = lwgeom_from_gserialized(geom);
136  ogeom = lwgeom_unstroke(igeom);
137  lwgeom_free(igeom);
138 
139  if (ogeom == NULL)
140  {
141  PG_FREE_IF_COPY(geom, 0);
142  PG_RETURN_NULL();
143  }
144 
145  ret = geometry_serialize(ogeom);
146  lwgeom_free(ogeom);
147  PG_FREE_IF_COPY(geom, 0);
148  PG_RETURN_POINTER(ret);
149 }
150 
151 /*******************************************************************************
152  * End PG_FUNCTIONs
153  ******************************************************************************/
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1144
LWGEOM * lwcurve_linearize(const LWGEOM *geom, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:735
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Definition: lwstroke.c:1168
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:768
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:55
This library is the generic geometry handling section of PostGIS.
Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:51
PG_FUNCTION_INFO_V1(LWGEOM_has_arc)
Datum ST_CurveToLine(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:100
Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:127
Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:69
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
unsigned int int32
Definition: shpopen.c:273