PostGIS  2.3.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  */
67 Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
68 {
69  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
70  int32 perQuad = PG_GETARG_INT32(1);
71  GSERIALIZED *ret;
72  LWGEOM *igeom = NULL, *ogeom = NULL;
73 
74  POSTGIS_DEBUG(2, "LWGEOM_curve_segmentize called.");
75 
76  if (perQuad < 0)
77  {
78  elog(ERROR, "2nd argument must be positive.");
79  PG_RETURN_NULL();
80  }
81 
82  POSTGIS_DEBUGF(3, "perQuad = %d", perQuad);
83 
84  igeom = lwgeom_from_gserialized(geom);
85  ogeom = lwgeom_stroke(igeom, perQuad);
86  lwgeom_free(igeom);
87 
88  if (ogeom == NULL)
89  PG_RETURN_NULL();
90 
91  ret = geometry_serialize(ogeom);
92  lwgeom_free(ogeom);
93  PG_FREE_IF_COPY(geom, 0);
94  PG_RETURN_POINTER(ret);
95 }
96 
98 Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
99 {
100  GSERIALIZED *geom = PG_GETARG_GSERIALIZED_P(0);
101  GSERIALIZED *ret;
102  LWGEOM *igeom = NULL, *ogeom = NULL;
103 
104  POSTGIS_DEBUG(2, "LWGEOM_line_desegmentize.");
105 
106  igeom = lwgeom_from_gserialized(geom);
107  ogeom = lwgeom_unstroke(igeom);
108  lwgeom_free(igeom);
109 
110  if (ogeom == NULL)
111  {
112  PG_FREE_IF_COPY(geom, 0);
113  PG_RETURN_NULL();
114  }
115 
116  ret = geometry_serialize(ogeom);
117  lwgeom_free(ogeom);
118  PG_FREE_IF_COPY(geom, 0);
119  PG_RETURN_POINTER(ret);
120 }
121 
122 /*******************************************************************************
123  * End PG_FUNCTIONs
124  ******************************************************************************/
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:462
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:1063
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:55
Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:51
Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:67
Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:98
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
PG_FUNCTION_INFO_V1(LWGEOM_has_arc)
LWGEOM * lwgeom_unstroke(const LWGEOM *geom)
Definition: lwstroke.c:858
This library is the generic geometry handling section of PostGIS.