PostGIS  2.4.9dev-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  LWGEOM *lwpt1, *lwpt2;
98 
99  newgeoms = lwalloc(sizeof(LWGEOM *)*mpoint->ngeoms);
100  nnewgeoms = 0;
101  for (i=0; i<mpoint->ngeoms; ++i)
102  {
103  lwpt1 = (LWGEOM*)mpoint->geoms[i];
104  /* Brute force, may be optimized by building an index */
105  int seen=0;
106  for (j=0; j<nnewgeoms; ++j)
107  {
108  lwpt2 = (LWGEOM*)newgeoms[j];
109  if ( lwgeom_mindistance2d(lwpt1, lwpt2) <= tolerance )
110  {
111  seen=1;
112  break;
113  }
114  }
115  if ( seen ) continue;
116  newgeoms[nnewgeoms++] = lwgeom_clone_deep(lwpt1);
117  }
118 
119  return (LWGEOM*)lwcollection_construct(mpoint->type,
120  mpoint->srid,
121  mpoint->bbox ? gbox_copy(mpoint->bbox) : NULL,
122  nnewgeoms, newgeoms);
123 
124 }
125 
126 LWMPOINT*
128 {
130  int has_z = lwgeom_has_z(g);
131  int has_m = lwgeom_has_m(g);
132  LWMPOINT* result = lwmpoint_construct_empty(g->srid, has_z, has_m);
133  POINT4D p;
134 
135  while(lwpointiterator_next(it, &p)) {
136  LWPOINT* lwp = lwpoint_make(g->srid, has_z, has_m, &p);
137  lwmpoint_add_lwpoint(result, lwp);
138  }
139 
141  return result;
142 }
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
GBOX * bbox
Definition: liblwgeom.h:466
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:244
int npoints
Definition: liblwgeom.h:371
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep clone an LWGEOM, everything is copied.
Definition: lwgeom.c:482
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:885
LWPOINTITERATOR * lwpointiterator_create(const LWGEOM *g)
Create a new LWPOINTITERATOR over supplied LWGEOM*.
Definition: lwiterator.c:244
int32_t srid
Definition: liblwgeom.h:399
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
unsigned int uint32_t
Definition: uthash.h:78
void lwpointiterator_destroy(LWPOINTITERATOR *s)
Free all memory associated with the iterator.
Definition: lwiterator.c:269
uint8_t type
Definition: liblwgeom.h:464
LWPOINT * lwpoint_make(int srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:206
LWPOINT ** geoms
Definition: liblwgeom.h:470
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
double lwgeom_mindistance2d(const LWGEOM *lw1, const LWGEOM *lw2)
Function initialazing min distance calculation.
Definition: measures.c:202
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:421
int ptarray_has_z(const POINTARRAY *pa)
Definition: ptarray.c:36
void * lwalloc(size_t size)
Definition: lwutil.c:229
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
int ngeoms
Definition: liblwgeom.h:468
int32_t srid
Definition: liblwgeom.h:467
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:892
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:122
LWGEOM * lwmpoint_as_lwgeom(const LWMPOINT *obj)
Definition: lwgeom.c:263
LWMPOINT * lwmpoint_from_lwgeom(const LWGEOM *g)
Definition: lwmpoint.c:127