PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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
40Datum LWGEOM_has_arc(PG_FUNCTION_ARGS);
41Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS);
42Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS);
43
44
45
46/*******************************************************************************
47 * Begin PG_FUNCTIONs
48 ******************************************************************************/
49
51Datum 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 */
69Datum 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
100Datum 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
127Datum 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
152Datum ST_NumCurves(PG_FUNCTION_ARGS)
153{
154 GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
156 if (!lwcmp)
157 PG_RETURN_NULL();
158
159 PG_RETURN_INT32(lwcompound_num_curves(lwcmp));
160}
161
163Datum ST_CurveN(PG_FUNCTION_ARGS)
164{
165 LWGEOM *subgeom;
166 GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
167 GSERIALIZED *ret = NULL;
168 int index = PG_GETARG_INT32(1);
170 if (!lwcmp)
171 PG_RETURN_NULL();
172 if (index < 1 || (uint32_t)index > lwcmp->ngeoms)
173 PG_RETURN_NULL();
174 subgeom = lwgeom_clone(lwcollection_getsubcurve(lwcmp, index-1));
175 ret = geometry_serialize(subgeom);
176 PG_FREE_IF_COPY(geom, 0);
177 PG_RETURN_POINTER(ret);
178}
179
180
181/*******************************************************************************
182 * End PG_FUNCTIONs
183 ******************************************************************************/
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition cu_print.c:267
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
const LWGEOM * lwcollection_getsubcurve(const LWCOMPOUND *compound, uint32_t gnum)
Definition lwcompound.c:42
void lwgeom_free(LWGEOM *geom)
Definition lwgeom.c:1246
uint32_t lwcompound_num_curves(const LWCOMPOUND *compound)
Definition lwcompound.c:34
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Convert linearized type into arc type, de-linearizing the strokes where possible.
Definition lwstroke.c:1271
LWGEOM * lwgeom_clone(const LWGEOM *lwgeom)
Clone LWGEOM object.
Definition lwgeom.c:519
LWCOMPOUND * lwgeom_as_lwcompound(const LWGEOM *lwgeom)
Definition lwgeom.c:225
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Convert type with arcs into equivalent linearized type.
Definition lwstroke.c:871
LWGEOM * lwcurve_linearize(const LWGEOM *geom, double tol, LW_LINEARIZE_TOLERANCE_TYPE type, int flags)
Definition lwstroke.c:838
int lwgeom_has_arc(const LWGEOM *geom)
Geometry includes at least one actual circular arc.
Definition lwstroke.c:55
This library is the generic geometry handling section of PostGIS.
Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(LWGEOM_has_arc)
Datum ST_CurveN(PG_FUNCTION_ARGS)
Datum ST_CurveToLine(PG_FUNCTION_ARGS)
Datum ST_NumCurves(PG_FUNCTION_ARGS)
Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
unsigned int int32
Definition shpopen.c:54
uint32_t ngeoms
Definition liblwgeom.h:594