PostGIS  2.1.10dev-r@@SVN_REVISION@@
lwcurvepoly.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 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 /* basic LWCURVEPOLY manipulation */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "liblwgeom_internal.h"
19 #include "lwgeom_log.h"
20 
21 
23 lwcurvepoly_construct_empty(int srid, char hasz, char hasm)
24 {
25  LWCURVEPOLY *ret;
26 
27  ret = lwalloc(sizeof(LWCURVEPOLY));
28  ret->type = CURVEPOLYTYPE;
29  ret->flags = gflags(hasz, hasm, 0);
30  ret->srid = srid;
31  ret->nrings = 0;
32  ret->maxrings = 1; /* Allocate room for sub-members, just in case. */
33  ret->rings = lwalloc(ret->maxrings * sizeof(LWGEOM*));
34  ret->bbox = NULL;
35 
36  return ret;
37 }
38 
41 {
42  LWCURVEPOLY *ret;
43  int i;
44  ret = lwalloc(sizeof(LWCURVEPOLY));
45  ret->type = CURVEPOLYTYPE;
46  ret->flags = lwpoly->flags;
47  ret->srid = lwpoly->srid;
48  ret->nrings = lwpoly->nrings;
49  ret->maxrings = lwpoly->nrings; /* Allocate room for sub-members, just in case. */
50  ret->rings = lwalloc(ret->maxrings * sizeof(LWGEOM*));
51  ret->bbox = lwpoly->bbox ? gbox_clone(lwpoly->bbox) : NULL;
52  for ( i = 0; i < ret->nrings; i++ )
53  {
54  ret->rings[i] = lwline_as_lwgeom(lwline_construct(ret->srid, NULL, ptarray_clone_deep(lwpoly->rings[i])));
55  }
56  return ret;
57 }
58 
60 {
61  int i;
62 
63  /* Can't do anything with NULLs */
64  if( ! poly || ! ring )
65  {
66  LWDEBUG(4,"NULL inputs!!! quitting");
67  return LW_FAILURE;
68  }
69 
70  /* Check that we're not working with garbage */
71  if ( poly->rings == NULL && (poly->nrings || poly->maxrings) )
72  {
73  LWDEBUG(4,"mismatched nrings/maxrings");
74  lwerror("Curvepolygon is in inconsistent state. Null memory but non-zero collection counts.");
75  }
76 
77  /* Check that we're adding an allowed ring type */
78  if ( ! ( ring->type == LINETYPE || ring->type == CIRCSTRINGTYPE || ring->type == COMPOUNDTYPE ) )
79  {
80  LWDEBUGF(4,"got incorrect ring type: %s",lwtype_name(ring->type));
81  return LW_FAILURE;
82  }
83 
84 
85  /* In case this is a truly empty, make some initial space */
86  if ( poly->rings == NULL )
87  {
88  poly->maxrings = 2;
89  poly->nrings = 0;
90  poly->rings = lwalloc(poly->maxrings * sizeof(LWGEOM*));
91  }
92 
93  /* Allocate more space if we need it */
94  if ( poly->nrings == poly->maxrings )
95  {
96  poly->maxrings *= 2;
97  poly->rings = lwrealloc(poly->rings, sizeof(LWGEOM*) * poly->maxrings);
98  }
99 
100  /* Make sure we don't already have a reference to this geom */
101  for ( i = 0; i < poly->nrings; i++ )
102  {
103  if ( poly->rings[i] == ring )
104  {
105  LWDEBUGF(4, "Found duplicate geometry in collection %p == %p", poly->rings[i], ring);
106  return LW_SUCCESS;
107  }
108  }
109 
110  /* Add the ring and increment the ring count */
111  poly->rings[poly->nrings] = (LWGEOM*)ring;
112  poly->nrings++;
113  return LW_SUCCESS;
114 }
115 
119 double
120 lwcurvepoly_area(const LWCURVEPOLY *curvepoly)
121 {
122  double area = 0.0;
123  LWPOLY *poly;
124  if( lwgeom_is_empty((LWGEOM*)curvepoly) )
125  return 0.0;
126  poly = lwcurvepoly_segmentize(curvepoly, 32);
127  area = lwpoly_area(poly);
128  lwpoly_free(poly);
129  return area;
130 }
131 
132 
133 double
135 {
136  double result=0.0;
137  int i;
138 
139  for (i=0; i<poly->nrings; i++)
140  result += lwgeom_length(poly->rings[i]);
141 
142  return result;
143 }
144 
145 double
147 {
148  double result=0.0;
149  int i;
150 
151  for (i=0; i<poly->nrings; i++)
152  result += lwgeom_length_2d(poly->rings[i]);
153 
154  return result;
155 }
#define LINETYPE
Definition: liblwgeom.h:61
LWCURVEPOLY * lwcurvepoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwcurvepoly.c:23
LWGEOM ** rings
Definition: liblwgeom.h:491
double lwcurvepoly_perimeter_2d(const LWCURVEPOLY *poly)
Definition: lwcurvepoly.c:146
int maxrings
Definition: liblwgeom.h:490
Datum area(PG_FUNCTION_ARGS)
#define CURVEPOLYTYPE
Definition: liblwgeom.h:69
#define COMPOUNDTYPE
Definition: liblwgeom.h:68
#define LW_SUCCESS
Definition: liblwgeom.h:55
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
uint8_t type
Definition: liblwgeom.h:485
double lwcurvepoly_perimeter(const LWCURVEPOLY *poly)
Definition: lwcurvepoly.c:134
LWPOLY * lwcurvepoly_segmentize(const LWCURVEPOLY *curvepoly, uint32_t perQuad)
Definition: lwsegmentize.c:290
GBOX * bbox
Definition: liblwgeom.h:409
double lwgeom_length_2d(const LWGEOM *geom)
Definition: lwgeom.c:1588
char ** result
Definition: liblwgeom.h:218
#define LW_FAILURE
Definition: liblwgeom.h:54
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
int lwcurvepoly_add_ring(LWCURVEPOLY *poly, LWGEOM *ring)
Add a ring, allocating extra space if necessary.
Definition: lwcurvepoly.c:59
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:164
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
int32_t srid
Definition: liblwgeom.h:488
GBOX * bbox
Definition: liblwgeom.h:487
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:79
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
POINTARRAY ** rings
Definition: liblwgeom.h:413
POINTARRAY * ptarray_clone_deep(const POINTARRAY *ptarray)
Deep clone a pointarray (also clones serialized pointlist)
Definition: ptarray.c:619
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Definition: lwpoly.c:390
double lwcurvepoly_area(const LWCURVEPOLY *curvepoly)
This should be rewritten to make use of the curve itself.
Definition: lwcurvepoly.c:120
int nrings
Definition: liblwgeom.h:411
uint8_t flags
Definition: liblwgeom.h:486
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:131
int32_t srid
Definition: liblwgeom.h:410
double lwgeom_length(const LWGEOM *geom)
Definition: lwgeom.c:1566
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:183
uint8_t type
Definition: liblwgeom.h:352
GBOX * gbox_clone(const GBOX *gbox)
Definition: g_box.c:39
#define CIRCSTRINGTYPE
Definition: liblwgeom.h:67
uint8_t flags
Definition: liblwgeom.h:408
void * lwalloc(size_t size)
Definition: lwutil.c:175
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1229
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
LWCURVEPOLY * lwcurvepoly_construct_from_lwpoly(LWPOLY *lwpoly)
Construct an equivalent curve polygon from a polygon.
Definition: lwcurvepoly.c:40