PostGIS  2.3.8dev-r@@SVN_REVISION@@
lwmpoint.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 (C) 2001-2006 Refractions Research Inc.
22  *
23  **********************************************************************/
24 
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "liblwgeom_internal.h"
30 #include "lwgeom_log.h"
31 
32 void
34 {
36 }
37 
38 LWMPOINT *
39 lwmpoint_construct_empty(int srid, char hasz, char hasm)
40 {
42  return ret;
43 }
44 
46 {
47  LWDEBUG(4, "Called");
48  return (LWMPOINT*)lwcollection_add_lwgeom((LWCOLLECTION*)mobj, (LWGEOM*)obj);
49 }
50 
51 LWMPOINT *
52 lwmpoint_construct(int srid, const POINTARRAY *pa)
53 {
54  int i;
55  int hasz = ptarray_has_z(pa);
56  int hasm = ptarray_has_m(pa);
58 
59  for ( i = 0; i < pa->npoints; i++ )
60  {
61  LWPOINT *lwp;
62  POINT4D p;
63  getPoint4d_p(pa, i, &p);
64  lwp = lwpoint_make(srid, hasz, hasm, &p);
65  lwmpoint_add_lwpoint(ret, lwp);
66  }
67 
68  return ret;
69 }
70 
71 
73 {
74  int i;
75 
76  if ( ! mpt ) return;
77 
78  if ( mpt->bbox )
79  lwfree(mpt->bbox);
80 
81  for ( i = 0; i < mpt->ngeoms; i++ )
82  if ( mpt->geoms && mpt->geoms[i] )
83  lwpoint_free(mpt->geoms[i]);
84 
85  if ( mpt->geoms )
86  lwfree(mpt->geoms);
87 
88  lwfree(mpt);
89 }
90 
91 LWGEOM*
92 lwmpoint_remove_repeated_points(const LWMPOINT *mpoint, double tolerance)
93 {
94  uint32_t nnewgeoms;
95  uint32_t i, j;
96  LWGEOM **newgeoms;
97 
98  newgeoms = lwalloc(sizeof(LWGEOM *)*mpoint->ngeoms);
99  nnewgeoms = 0;
100  for (i=0; i<mpoint->ngeoms; ++i)
101  {
102  /* Brute force, may be optimized by building an index */
103  int seen=0;
104  for (j=0; j<nnewgeoms; ++j)
105  {
106  if ( lwpoint_same((LWPOINT*)newgeoms[j],
107  (LWPOINT*)mpoint->geoms[i]) )
108  {
109  seen=1;
110  break;
111  }
112  }
113  if ( seen ) continue;
114  newgeoms[nnewgeoms++] = (LWGEOM*)lwpoint_clone(mpoint->geoms[i]);
115  }
116 
117  return (LWGEOM*)lwcollection_construct(mpoint->type,
118  mpoint->srid, mpoint->bbox ? gbox_copy(mpoint->bbox) : NULL,
119  nnewgeoms, newgeoms);
120 
121 }
122 
123 LWMPOINT*
125 {
127  int has_z = lwgeom_has_z(g);
128  int has_m = lwgeom_has_m(g);
129  LWMPOINT* result = lwmpoint_construct_empty(g->srid, has_z, has_m);
130  POINT4D p;
131 
132  while(lwpointiterator_next(it, &p)) {
133  LWPOINT* lwp = lwpoint_make(g->srid, has_z, has_m, &p);
134  lwmpoint_add_lwpoint(result, lwp);
135  }
136 
138  return result;
139 }
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:438
void lwmpoint_free(LWMPOINT *mpt)
Definition: lwmpoint.c:72
char lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:246
GBOX * bbox
Definition: liblwgeom.h:465
int lwpointiterator_next(LWPOINTITERATOR *s, POINT4D *p)
Attempts to assign the next point in the iterator to p, and advances the iterator to the next point...
Definition: lwiterator.c:212
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
void lwfree(void *mem)
Definition: lwutil.c:242
int npoints
Definition: liblwgeom.h:370
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:195
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:849
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
Definition: lwiterator.c:244
int32_t srid
Definition: liblwgeom.h:398
LWGEOM * lwmpoint_remove_repeated_points(const LWMPOINT *mpoint, double tolerance)
Definition: lwmpoint.c:92
LWMPOINT * lwmpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwmpoint.c:39
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:269
uint8_t type
Definition: liblwgeom.h:463
LWPOINT * lwpoint_clone(const LWPOINT *lwgeom)
Definition: lwpoint.c:221
LWPOINT * lwpoint_make(int srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:188
LWPOINT ** geoms
Definition: liblwgeom.h:469
LWMPOINT * lwmpoint_construct(int srid, const POINTARRAY *pa)
Definition: lwmpoint.c:52
int ptarray_has_m(const POINTARRAY *pa)
Definition: ptarray.c:43
void lwmpoint_release(LWMPOINT *lwmpoint)
Definition: lwmpoint.c:33
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:385
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:36
void * lwalloc(size_t size)
Definition: lwutil.c:227
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
int ngeoms
Definition: liblwgeom.h:467
int32_t srid
Definition: liblwgeom.h:466
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:856
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:244
LWGEOM * lwmpoint_as_lwgeom(const LWMPOINT *obj)
Definition: lwgeom.c:227
LWMPOINT * lwmpoint_from_lwgeom(const LWGEOM *g)
Definition: lwmpoint.c:124