PostGIS  2.4.9dev-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_line_desegmentize(PG_FUNCTION_ARGS);
42 
43 
44 
45 /*******************************************************************************
46  * Begin PG_FUNCTIONs
47  ******************************************************************************/
48 
50 Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
51 {
52  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
53  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
54  uint32 result = lwgeom_has_arc(lwgeom);
55  lwgeom_free(lwgeom);
56  PG_RETURN_BOOL(result == 1);
57 }
58 
59 /*
60  * Converts any curve segments of the geometry into a linear approximation.
61  * Curve centers are determined by projecting the defining points into the 2d
62  * plane. Z and M values are assigned by linear interpolation between
63  * defining points.
64  *
65  * TODO: drop, use ST_CurveToLine instead
66  */
67 Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS);
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 
99 Datum ST_CurveToLine(PG_FUNCTION_ARGS);
101 Datum ST_CurveToLine(PG_FUNCTION_ARGS)
102 {
103  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
104  double tol = PG_GETARG_FLOAT8(1);
105  int toltype = PG_GETARG_INT32(2);
106  int flags = PG_GETARG_INT32(3);
107  GSERIALIZED *ret;
108  LWGEOM *igeom = NULL, *ogeom = NULL;
109 
110  POSTGIS_DEBUG(2, "ST_CurveToLine called.");
111 
112  POSTGIS_DEBUGF(3, "tol = %g, typ = %d, flg = %d", tol, toltype, flags);
113 
114  igeom = lwgeom_from_gserialized(geom);
115  ogeom = lwcurve_linearize(igeom, tol, toltype, flags);
116  lwgeom_free(igeom);
117 
118  if (ogeom == NULL)
119  PG_RETURN_NULL();
120 
121  ret = geometry_serialize(ogeom);
122  lwgeom_free(ogeom);
123  PG_FREE_IF_COPY(geom, 0);
124  PG_RETURN_POINTER(ret);
125 }
126 
128 Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
129 {
130  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
131  GSERIALIZED *ret;
132  LWGEOM *igeom = NULL, *ogeom = NULL;
133 
134  POSTGIS_DEBUG(2, "LWGEOM_line_desegmentize.");
135 
136  igeom = lwgeom_from_gserialized(geom);
137  ogeom = lwgeom_unstroke(igeom);
138  lwgeom_free(igeom);
139 
140  if (ogeom == NULL)
141  {
142  PG_FREE_IF_COPY(geom, 0);
143  PG_RETURN_NULL();
144  }
145 
146  ret = geometry_serialize(ogeom);
147  lwgeom_free(ogeom);
148  PG_FREE_IF_COPY(geom, 0);
149  PG_RETURN_POINTER(ret);
150 }
151 
152 /*******************************************************************************
153  * End PG_FUNCTIONs
154  ******************************************************************************/
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:767
unsigned int int32
Definition: shpopen.c:273
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1099
Datum ST_CurveToLine(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:101
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:54
Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:50
Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:69
Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:128
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
PG_FUNCTION_INFO_V1(LWGEOM_has_arc)
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Definition: lwstroke.c:1139
LWGEOM * lwcurve_linearize(const LWGEOM *geom, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition: lwstroke.c:734
This library is the generic geometry handling section of PostGIS.