PostGIS  2.1.10dev-r@@SVN_REVISION@@
lwgeom_sqlmm.c
Go to the documentation of this file.
1 /**********************************************************************
2  * $Id: lwgeom_sqlmm.c 13134 2014-12-01 08:47:21Z strk $
3  *
4  * PostGIS - Spatial Types for PostgreSQL
5  * http://postgis.net
6  * Copyright 2001-2006 Refractions Research Inc.
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence. See the COPYING file.
10  *
11  **********************************************************************/
12 
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdarg.h>
16 #include <string.h>
17 #include <math.h>
18 
19 #include "postgres.h"
20 #include "fmgr.h"
21 
22 #include "../postgis_config.h"
23 #include "liblwgeom.h"
24 #include "lwgeom_pg.h"
25 
26 
27 Datum LWGEOM_has_arc(PG_FUNCTION_ARGS);
28 Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS);
29 Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS);
30 
31 
32 
33 /*******************************************************************************
34  * Begin PG_FUNCTIONs
35  ******************************************************************************/
36 
38 Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
39 {
40  GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
41  LWGEOM *lwgeom = lwgeom_from_gserialized(geom);
42  uint32 result = lwgeom_has_arc(lwgeom);
43  lwgeom_free(lwgeom);
44  PG_RETURN_BOOL(result == 1);
45 }
46 
47 /*
48  * Converts any curve segments of the geometry into a linear approximation.
49  * Curve centers are determined by projecting the defining points into the 2d
50  * plane. Z and M values are assigned by linear interpolation between
51  * defining points.
52  */
54 Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
55 {
56  GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
57  int32 perQuad = PG_GETARG_INT32(1);
58  GSERIALIZED *ret;
59  LWGEOM *igeom = NULL, *ogeom = NULL;
60 
61  POSTGIS_DEBUG(2, "LWGEOM_curve_segmentize called.");
62 
63  if (perQuad < 0)
64  {
65  elog(ERROR, "2nd argument must be positive.");
66  PG_RETURN_NULL();
67  }
68 
69  POSTGIS_DEBUGF(3, "perQuad = %d", perQuad);
70 
71  igeom = lwgeom_from_gserialized(geom);
72  ogeom = lwgeom_segmentize(igeom, perQuad);
73  lwgeom_free(igeom);
74 
75  if (ogeom == NULL)
76  PG_RETURN_NULL();
77 
78  ret = geometry_serialize(ogeom);
79  lwgeom_free(ogeom);
80  PG_FREE_IF_COPY(geom, 0);
81  PG_RETURN_POINTER(ret);
82 }
83 
85 Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
86 {
87  GSERIALIZED *geom = (GSERIALIZED *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
88  GSERIALIZED *ret;
89  LWGEOM *igeom = NULL, *ogeom = NULL;
90 
91  POSTGIS_DEBUG(2, "LWGEOM_line_desegmentize.");
92 
93  igeom = lwgeom_from_gserialized(geom);
94  ogeom = lwgeom_desegmentize(igeom);
95  lwgeom_free(igeom);
96 
97  if (ogeom == NULL)
98  {
99  PG_FREE_IF_COPY(geom, 0);
100  PG_RETURN_NULL();
101  }
102 
103  ret = geometry_serialize(ogeom);
104  lwgeom_free(ogeom);
105  PG_FREE_IF_COPY(geom, 0);
106  PG_RETURN_POINTER(ret);
107 }
108 
109 /*******************************************************************************
110  * End PG_FUNCTIONs
111  ******************************************************************************/
LWGEOM * lwgeom_desegmentize(LWGEOM *geom)
Definition: lwsegmentize.c:841
LWGEOM * lwgeom_from_gserialized(const GSERIALIZED *g)
Allocate a new LWGEOM from a GSERIALIZED.
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
char ** result
Definition: liblwgeom.h:218
LWGEOM * lwgeom_segmentize(LWGEOM *geom, uint32_t perQuad)
Definition: lwsegmentize.c:446
unsigned int uint32
Definition: shpopen.c:274
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwsegmentize.c:42
Datum LWGEOM_has_arc(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:38
Datum LWGEOM_curve_segmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:54
Datum LWGEOM_line_desegmentize(PG_FUNCTION_ARGS)
Definition: lwgeom_sqlmm.c:85
GSERIALIZED * geometry_serialize(LWGEOM *lwgeom)
PG_FUNCTION_INFO_V1(LWGEOM_has_arc)
This library is the generic geometry handling section of PostGIS.