PostGIS  2.1.10dev-r@@SVN_REVISION@@
lwpoly.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  *
6  * Copyright (C) 2012 Sandro Santilli <strk@keybit.net>
7  * Copyright (C) 2001-2006 Refractions Research Inc.
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU General Public Licence. See the COPYING file.
11  *
12  **********************************************************************/
13 
14 /* basic LWPOLY manipulation */
15 
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "liblwgeom_internal.h"
20 #include "lwgeom_log.h"
21 
22 
23 #define CHECK_POLY_RINGS_ZM 1
24 
25 /* construct a new LWPOLY. arrays (points/points per ring) will NOT be copied
26  * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
27  */
28 LWPOLY*
29 lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
30 {
31  LWPOLY *result;
32  int hasz, hasm;
33 #ifdef CHECK_POLY_RINGS_ZM
34  char zm;
35  uint32_t i;
36 #endif
37 
38  if ( nrings < 1 ) lwerror("lwpoly_construct: need at least 1 ring");
39 
40  hasz = FLAGS_GET_Z(points[0]->flags);
41  hasm = FLAGS_GET_M(points[0]->flags);
42 
43 #ifdef CHECK_POLY_RINGS_ZM
44  zm = FLAGS_GET_ZM(points[0]->flags);
45  for (i=1; i<nrings; i++)
46  {
47  if ( zm != FLAGS_GET_ZM(points[i]->flags) )
48  lwerror("lwpoly_construct: mixed dimensioned rings");
49  }
50 #endif
51 
52  result = (LWPOLY*) lwalloc(sizeof(LWPOLY));
53  result->type = POLYGONTYPE;
54  result->flags = gflags(hasz, hasm, 0);
55  FLAGS_SET_BBOX(result->flags, bbox?1:0);
56  result->srid = srid;
57  result->nrings = nrings;
58  result->maxrings = nrings;
59  result->rings = points;
60  result->bbox = bbox;
61 
62  return result;
63 }
64 
65 LWPOLY*
66 lwpoly_construct_empty(int srid, char hasz, char hasm)
67 {
68  LWPOLY *result = lwalloc(sizeof(LWPOLY));
69  result->type = POLYGONTYPE;
70  result->flags = gflags(hasz,hasm,0);
71  result->srid = srid;
72  result->nrings = 0;
73  result->maxrings = 1; /* Allocate room for ring, just in case. */
74  result->rings = lwalloc(result->maxrings * sizeof(POINTARRAY*));
75  result->bbox = NULL;
76  return result;
77 }
78 
79 void lwpoly_free(LWPOLY *poly)
80 {
81  int t;
82 
83  if( ! poly ) return;
84 
85  if ( poly->bbox )
86  lwfree(poly->bbox);
87 
88  for (t=0; t<poly->nrings; t++)
89  {
90  if ( poly->rings[t] )
91  ptarray_free(poly->rings[t]);
92  }
93 
94  if ( poly->rings )
95  lwfree(poly->rings);
96 
97  lwfree(poly);
98 }
99 
100 void printLWPOLY(LWPOLY *poly)
101 {
102  int t;
103  lwnotice("LWPOLY {");
104  lwnotice(" ndims = %i", (int)FLAGS_NDIMS(poly->flags));
105  lwnotice(" SRID = %i", (int)poly->srid);
106  lwnotice(" nrings = %i", (int)poly->nrings);
107  for (t=0; t<poly->nrings; t++)
108  {
109  lwnotice(" RING # %i :",t);
110  printPA(poly->rings[t]);
111  }
112  lwnotice("}");
113 }
114 
115 /* @brief Clone LWLINE object. Serialized point lists are not copied.
116  *
117  * @see ptarray_clone
118  */
119 LWPOLY *
121 {
122  int i;
123  LWPOLY *ret = lwalloc(sizeof(LWPOLY));
124  memcpy(ret, g, sizeof(LWPOLY));
125  ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
126  for ( i = 0; i < g->nrings; i++ ) {
127  ret->rings[i] = ptarray_clone(g->rings[i]);
128  }
129  if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
130  return ret;
131 }
132 
133 /* Deep clone LWPOLY object. POINTARRAY are copied, as is ring array */
134 LWPOLY *
136 {
137  int i;
138  LWPOLY *ret = lwalloc(sizeof(LWPOLY));
139  memcpy(ret, g, sizeof(LWPOLY));
140  if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
141  ret->rings = lwalloc(sizeof(POINTARRAY *)*g->nrings);
142  for ( i = 0; i < ret->nrings; i++ )
143  {
144  ret->rings[i] = ptarray_clone_deep(g->rings[i]);
145  }
146  FLAGS_SET_READONLY(ret->flags,0);
147  return ret;
148 }
149 
153 int
155 {
156  if( ! poly || ! pa )
157  return LW_FAILURE;
158 
159  /* We have used up our storage, add some more. */
160  if( poly->nrings >= poly->maxrings )
161  {
162  int new_maxrings = 2 * (poly->nrings + 1);
163  poly->rings = lwrealloc(poly->rings, new_maxrings * sizeof(POINTARRAY*));
164  poly->maxrings = new_maxrings;
165  }
166 
167  /* Add the new ring entry. */
168  poly->rings[poly->nrings] = pa;
169  poly->nrings++;
170 
171  return LW_SUCCESS;
172 }
173 
174 void
176 {
177  int i;
178 
179  /* No-op empties */
180  if ( lwpoly_is_empty(poly) )
181  return;
182 
183  /* External ring */
184  if ( ptarray_isccw(poly->rings[0]) )
185  ptarray_reverse(poly->rings[0]);
186 
187  /* Internal rings */
188  for (i=1; i<poly->nrings; i++)
189  if ( ! ptarray_isccw(poly->rings[i]) )
190  ptarray_reverse(poly->rings[i]);
191 
192 }
193 
194 void
196 {
198 }
199 
200 void
202 {
203  int i;
204  if ( lwpoly_is_empty(poly) ) return;
205  for (i=0; i<poly->nrings; i++)
206  ptarray_reverse(poly->rings[i]);
207 }
208 
209 LWPOLY *
210 lwpoly_segmentize2d(LWPOLY *poly, double dist)
211 {
212  POINTARRAY **newrings;
213  uint32_t i;
214 
215  newrings = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
216  for (i=0; i<poly->nrings; i++)
217  {
218  newrings[i] = ptarray_segmentize2d(poly->rings[i], dist);
219  }
220  return lwpoly_construct(poly->srid, NULL,
221  poly->nrings, newrings);
222 }
223 
224 /*
225  * check coordinate equality
226  * ring and coordinate order is considered
227  */
228 char
229 lwpoly_same(const LWPOLY *p1, const LWPOLY *p2)
230 {
231  uint32_t i;
232 
233  if ( p1->nrings != p2->nrings ) return 0;
234  for (i=0; i<p1->nrings; i++)
235  {
236  if ( ! ptarray_same(p1->rings[i], p2->rings[i]) )
237  return 0;
238  }
239  return 1;
240 }
241 
242 /*
243  * Construct a polygon from a LWLINE being
244  * the shell and an array of LWLINE (possibly NULL) being holes.
245  * Pointarrays from intput geoms are cloned.
246  * SRID must be the same for each input line.
247  * Input lines must have at least 4 points, and be closed.
248  */
249 LWPOLY *
251  uint32_t nholes, const LWLINE **holes)
252 {
253  uint32_t nrings;
254  POINTARRAY **rings = lwalloc((nholes+1)*sizeof(POINTARRAY *));
255  int srid = shell->srid;
256  LWPOLY *ret;
257 
258  if ( shell->points->npoints < 4 )
259  lwerror("lwpoly_from_lwlines: shell must have at least 4 points");
260  if ( ! ptarray_is_closed_2d(shell->points) )
261  lwerror("lwpoly_from_lwlines: shell must be closed");
262  rings[0] = ptarray_clone_deep(shell->points);
263 
264  for (nrings=1; nrings<=nholes; nrings++)
265  {
266  const LWLINE *hole = holes[nrings-1];
267 
268  if ( hole->srid != srid )
269  lwerror("lwpoly_from_lwlines: mixed SRIDs in input lines");
270 
271  if ( hole->points->npoints < 4 )
272  lwerror("lwpoly_from_lwlines: holes must have at least 4 points");
273  if ( ! ptarray_is_closed_2d(hole->points) )
274  lwerror("lwpoly_from_lwlines: holes must be closed");
275 
276  rings[nrings] = ptarray_clone_deep(hole->points);
277  }
278 
279  ret = lwpoly_construct(srid, NULL, nrings, rings);
280  return ret;
281 }
282 
283 LWGEOM*
285 {
286  uint32_t i;
287  POINTARRAY **newrings;
288 
289  newrings = lwalloc(sizeof(POINTARRAY *)*poly->nrings);
290  for (i=0; i<poly->nrings; i++)
291  {
292  newrings[i] = ptarray_remove_repeated_points(poly->rings[i]);
293  }
294 
295  return (LWGEOM*)lwpoly_construct(poly->srid,
296  poly->bbox ? gbox_copy(poly->bbox) : NULL,
297  poly->nrings, newrings);
298 
299 }
300 
301 
302 LWPOLY*
303 lwpoly_force_dims(const LWPOLY *poly, int hasz, int hasm)
304 {
305  LWPOLY *polyout;
306 
307  /* Return 2D empty */
308  if( lwpoly_is_empty(poly) )
309  {
310  polyout = lwpoly_construct_empty(poly->srid, hasz, hasm);
311  }
312  else
313  {
314  POINTARRAY **rings = NULL;
315  int i;
316  rings = lwalloc(sizeof(POINTARRAY*) * poly->nrings);
317  for( i = 0; i < poly->nrings; i++ )
318  {
319  rings[i] = ptarray_force_dims(poly->rings[i], hasz, hasm);
320  }
321  polyout = lwpoly_construct(poly->srid, NULL, poly->nrings, rings);
322  }
323  polyout->type = poly->type;
324  return polyout;
325 }
326 
327 int lwpoly_is_empty(const LWPOLY *poly)
328 {
329  if ( (poly->nrings < 1) || (!poly->rings) || (!poly->rings[0]) || (poly->rings[0]->npoints < 1) )
330  return LW_TRUE;
331  return LW_FALSE;
332 }
333 
335 {
336  int i = 0;
337  int v = 0; /* vertices */
338  assert(poly);
339  for ( i = 0; i < poly->nrings; i ++ )
340  {
341  v += poly->rings[i]->npoints;
342  }
343  return v;
344 }
345 
346 LWPOLY* lwpoly_simplify(const LWPOLY *ipoly, double dist)
347 {
348  int i;
349  LWPOLY *opoly = lwpoly_construct_empty(ipoly->srid, FLAGS_GET_Z(ipoly->flags), FLAGS_GET_M(ipoly->flags));
350 
351  LWDEBUGF(2, "simplify_polygon3d: simplifying polygon with %d rings", ipoly->nrings);
352 
353  if( lwpoly_is_empty(ipoly) )
354  return opoly; /* should we return NULL instead ? */
355 
356  for (i = 0; i < ipoly->nrings; i++)
357  {
358  static const int minvertices = 0; /* TODO: allow setting this */
359  POINTARRAY *opts = ptarray_simplify(ipoly->rings[i], dist, minvertices);
360 
361  LWDEBUGF(3, "ring%d simplified from %d to %d points", i, ipoly->rings[i]->npoints, opts->npoints);
362 
363  /* Less points than are needed to form a closed ring, we can't use this */
364  if ( opts->npoints < 4 )
365  {
366  LWDEBUGF(3, "ring%d skipped (% pts)", i, opts->npoints);
367  ptarray_free(opts);
368  if ( i ) continue;
369  else break; /* Don't scan holes if shell is collapsed */
370  }
371 
372  /* Add ring to simplified polygon */
373  if( lwpoly_add_ring(opoly, opts) == LW_FAILURE )
374  return NULL;
375  }
376 
377  LWDEBUGF(3, "simplified polygon with %d rings", ipoly->nrings);
378  opoly->type = ipoly->type;
379 
380  if( lwpoly_is_empty(opoly) )
381  return NULL;
382 
383  return opoly;
384 }
385 
389 double
390 lwpoly_area(const LWPOLY *poly)
391 {
392  double poly_area = 0.0;
393  int i;
394 
395  if ( ! poly )
396  lwerror("lwpoly_area called with null polygon pointer!");
397 
398  for ( i=0; i < poly->nrings; i++ )
399  {
400  POINTARRAY *ring = poly->rings[i];
401  double ringarea = 0.0;
402 
403  /* Empty or messed-up ring. */
404  if ( ring->npoints < 3 )
405  continue;
406 
407  ringarea = fabs(ptarray_signed_area(ring));
408  if ( i == 0 ) /* Outer ring, positive area! */
409  poly_area += ringarea;
410  else /* Inner ring, negative area! */
411  poly_area -= ringarea;
412  }
413 
414  return poly_area;
415 }
416 
417 
422 double
424 {
425  double result=0.0;
426  int i;
427 
428  LWDEBUGF(2, "in lwgeom_polygon_perimeter (%d rings)", poly->nrings);
429 
430  for (i=0; i<poly->nrings; i++)
431  result += ptarray_length(poly->rings[i]);
432 
433  return result;
434 }
435 
440 double
442 {
443  double result=0.0;
444  int i;
445 
446  LWDEBUGF(2, "in lwgeom_polygon_perimeter (%d rings)", poly->nrings);
447 
448  for (i=0; i<poly->nrings; i++)
449  result += ptarray_length_2d(poly->rings[i]);
450 
451  return result;
452 }
453 
454 int
456 {
457  int i = 0;
458 
459  if ( poly->nrings == 0 )
460  return LW_TRUE;
461 
462  for ( i = 0; i < poly->nrings; i++ )
463  {
464  if (FLAGS_GET_Z(poly->flags))
465  {
466  if ( ! ptarray_is_closed_3d(poly->rings[i]) )
467  return LW_FALSE;
468  }
469  else
470  {
471  if ( ! ptarray_is_closed_2d(poly->rings[i]) )
472  return LW_FALSE;
473  }
474  }
475 
476  return LW_TRUE;
477 }
478 
479 int
481 {
482  if ( poly->nrings < 1 )
483  return LW_FAILURE;
484  return ptarray_startpoint(poly->rings[0], pt);
485 }
486 
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:362
POINTARRAY * ptarray_clone(const POINTARRAY *ptarray)
Clone a POINTARRAY object.
Definition: ptarray.c:643
double lwpoly_area(const LWPOLY *poly)
Find the area of the outer ring - sum (area of inner rings).
Definition: lwpoly.c:390
int ptarray_isccw(const POINTARRAY *pa)
Definition: ptarray.c:990
void lwfree(void *mem)
Definition: lwutil.c:190
void lwpoly_release(LWPOLY *lwpoly)
Definition: lwpoly.c:195
int npoints
Definition: liblwgeom.h:327
int ptarray_is_closed_3d(const POINTARRAY *pa)
Definition: ptarray.c:678
#define POLYGONTYPE
Definition: liblwgeom.h:62
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:315
double lwpoly_perimeter(const LWPOLY *poly)
Compute the sum of polygon rings length.
Definition: lwpoly.c:423
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1586
#define LW_SUCCESS
Definition: liblwgeom.h:55
POINTARRAY * ptarray_segmentize2d(const POINTARRAY *ipa, double dist)
Returns a modified POINTARRAY so that no segment is longer than the given distance (computed using 2d...
Definition: ptarray.c:381
void printLWPOLY(LWPOLY *poly)
Definition: lwpoly.c:100
#define FLAGS_GET_ZM(flags)
Definition: liblwgeom.h:119
LWGEOM * lwpoly_remove_repeated_points(LWPOLY *poly)
Definition: lwpoly.c:284
int lwpoly_add_ring(LWPOLY *poly, POINTARRAY *pa)
Add a ring to a polygon.
Definition: lwpoly.c:154
GBOX * bbox
Definition: liblwgeom.h:409
int32_t srid
Definition: liblwgeom.h:377
double lwpoly_perimeter_2d(const LWPOLY *poly)
Compute the sum of polygon rings length (forcing 2d computation).
Definition: lwpoly.c:441
LWPOLY * lwpoly_clone(const LWPOLY *g)
Definition: lwpoly.c:120
double ptarray_signed_area(const POINTARRAY *pa)
Returns the area in cartesian units.
Definition: ptarray.c:959
int ptarray_is_closed_2d(const POINTARRAY *pa)
Definition: ptarray.c:672
char ** result
Definition: liblwgeom.h:218
LWGEOM * lwpoly_as_lwgeom(const LWPOLY *obj)
Definition: lwgeom.c:239
#define LW_FAILURE
Definition: liblwgeom.h:54
int lwpoly_is_closed(const LWPOLY *poly)
Definition: lwpoly.c:455
char lwpoly_same(const LWPOLY *p1, const LWPOLY *p2)
Definition: lwpoly.c:229
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
LWPOLY * lwpoly_construct(int srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:29
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:54
#define LW_FALSE
Definition: liblwgeom.h:52
uint8_t type
Definition: liblwgeom.h:407
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:51
LWPOLY * lwpoly_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoly.c:66
LWPOLY * lwpoly_simplify(const LWPOLY *ipoly, double dist)
Definition: lwpoly.c:346
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
POINTARRAY * ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm)
Definition: ptarray.c:999
int nrings
Definition: liblwgeom.h:411
LWPOLY * lwpoly_from_lwlines(const LWLINE *shell, uint32_t nholes, const LWLINE **holes)
Definition: lwpoly.c:250
LWPOLY * lwpoly_clone_deep(const LWPOLY *g)
Definition: lwpoly.c:135
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:106
void lwpoly_reverse(LWPOLY *poly)
Definition: lwpoly.c:201
LWPOLY * lwpoly_force_dims(const LWPOLY *poly, int hasz, int hasm)
Definition: lwpoly.c:303
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:131
int lwpoly_startpoint(const LWPOLY *poly, POINT4D *pt)
Definition: lwpoly.c:480
LWPOLY * lwpoly_segmentize2d(LWPOLY *poly, double dist)
Definition: lwpoly.c:210
int ptarray_startpoint(const POINTARRAY *pa, POINT4D *pt)
Definition: ptarray.c:1740
double ptarray_length(const POINTARRAY *pts)
Find the 3d/2d length of the given POINTARRAY (depending on its dimensionality)
Definition: ptarray.c:1614
POINTARRAY * ptarray_simplify(POINTARRAY *inpts, double epsilon, unsigned int minpts)
Definition: ptarray.c:1495
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:114
int maxrings
Definition: liblwgeom.h:412
void printPA(POINTARRAY *pa)
Definition: lwgeom_api.c:563
int32_t srid
Definition: liblwgeom.h:410
int lwpoly_count_vertices(LWPOLY *poly)
Definition: lwpoly.c:334
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:443
void lwpoly_free(LWPOLY *poly)
Definition: lwpoly.c:79
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:183
void ptarray_reverse(POINTARRAY *pa)
Definition: ptarray.c:328
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:107
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:328
void lwpoly_force_clockwise(LWPOLY *poly)
Definition: lwpoly.c:175
uint8_t flags
Definition: liblwgeom.h:408
void * lwalloc(size_t size)
Definition: lwutil.c:175
POINTARRAY * ptarray_remove_repeated_points(POINTARRAY *in)
Definition: ptarray.c:1401
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:118
int lwpoly_is_empty(const LWPOLY *poly)
Definition: lwpoly.c:327
#define FLAGS_SET_READONLY(flags, value)
Definition: liblwgeom.h:116
POINTARRAY * points
Definition: liblwgeom.h:378