PostGIS  2.3.7dev-r@@SVN_REVISION@@
lwpoint.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 "../postgis_config.h"
30 /*#define POSTGIS_DEBUG_LEVEL 4*/
31 #include "liblwgeom_internal.h"
32 #include "lwgeom_log.h"
33 
34 
35 /*
36  * Convenience functions to hide the POINTARRAY
37  * TODO: obsolete this
38  */
39 int
41 {
42  return getPoint2d_p(point->point, 0, out);
43 }
44 
45 /* convenience functions to hide the POINTARRAY */
46 int
48 {
49  return getPoint3dz_p(point->point,0,out);
50 }
51 int
53 {
54  return getPoint3dm_p(point->point,0,out);
55 }
56 int
58 {
59  return getPoint4d_p(point->point,0,out);
60 }
61 
62 double
63 lwpoint_get_x(const LWPOINT *point)
64 {
65  POINT4D pt;
66  if ( lwpoint_is_empty(point) )
67  lwerror("lwpoint_get_x called with empty geometry");
68  getPoint4d_p(point->point, 0, &pt);
69  return pt.x;
70 }
71 
72 double
73 lwpoint_get_y(const LWPOINT *point)
74 {
75  POINT4D pt;
76  if ( lwpoint_is_empty(point) )
77  lwerror("lwpoint_get_y called with empty geometry");
78  getPoint4d_p(point->point, 0, &pt);
79  return pt.y;
80 }
81 
82 double
83 lwpoint_get_z(const LWPOINT *point)
84 {
85  POINT4D pt;
86  if ( lwpoint_is_empty(point) )
87  lwerror("lwpoint_get_z called with empty geometry");
88  if ( ! FLAGS_GET_Z(point->flags) )
89  lwerror("lwpoint_get_z called without z dimension");
90  getPoint4d_p(point->point, 0, &pt);
91  return pt.z;
92 }
93 
94 double
95 lwpoint_get_m(const LWPOINT *point)
96 {
97  POINT4D pt;
98  if ( lwpoint_is_empty(point) )
99  lwerror("lwpoint_get_m called with empty geometry");
100  if ( ! FLAGS_GET_M(point->flags) )
101  lwerror("lwpoint_get_m called without m dimension");
102  getPoint4d_p(point->point, 0, &pt);
103  return pt.m;
104 }
105 
106 /*
107  * Construct a new point. point will not be copied
108  * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
109  */
110 LWPOINT *
111 lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
112 {
113  LWPOINT *result;
114  uint8_t flags = 0;
115 
116  if (point == NULL)
117  return NULL; /* error */
118 
119  result = lwalloc(sizeof(LWPOINT));
120  result->type = POINTTYPE;
121  FLAGS_SET_Z(flags, FLAGS_GET_Z(point->flags));
122  FLAGS_SET_M(flags, FLAGS_GET_M(point->flags));
123  FLAGS_SET_BBOX(flags, bbox?1:0);
124  result->flags = flags;
125  result->srid = srid;
126  result->point = point;
127  result->bbox = bbox;
128 
129  return result;
130 }
131 
132 LWPOINT *
133 lwpoint_construct_empty(int srid, char hasz, char hasm)
134 {
135  LWPOINT *result = lwalloc(sizeof(LWPOINT));
136  result->type = POINTTYPE;
137  result->flags = gflags(hasz, hasm, 0);
138  result->srid = srid;
139  result->point = ptarray_construct(hasz, hasm, 0);
140  result->bbox = NULL;
141  return result;
142 }
143 
144 LWPOINT *
145 lwpoint_make2d(int srid, double x, double y)
146 {
147  POINT4D p = {x, y, 0.0, 0.0};
148  POINTARRAY *pa = ptarray_construct_empty(0, 0, 1);
149 
150  ptarray_append_point(pa, &p, LW_TRUE);
151  return lwpoint_construct(srid, NULL, pa);
152 }
153 
154 LWPOINT *
155 lwpoint_make3dz(int srid, double x, double y, double z)
156 {
157  POINT4D p = {x, y, z, 0.0};
158  POINTARRAY *pa = ptarray_construct_empty(1, 0, 1);
159 
160  ptarray_append_point(pa, &p, LW_TRUE);
161 
162  return lwpoint_construct(srid, NULL, pa);
163 }
164 
165 LWPOINT *
166 lwpoint_make3dm(int srid, double x, double y, double m)
167 {
168  POINT4D p = {x, y, 0.0, m};
169  POINTARRAY *pa = ptarray_construct_empty(0, 1, 1);
170 
171  ptarray_append_point(pa, &p, LW_TRUE);
172 
173  return lwpoint_construct(srid, NULL, pa);
174 }
175 
176 LWPOINT *
177 lwpoint_make4d(int srid, double x, double y, double z, double m)
178 {
179  POINT4D p = {x, y, z, m};
180  POINTARRAY *pa = ptarray_construct_empty(1, 1, 1);
181 
182  ptarray_append_point(pa, &p, LW_TRUE);
183 
184  return lwpoint_construct(srid, NULL, pa);
185 }
186 
187 LWPOINT *
188 lwpoint_make(int srid, int hasz, int hasm, const POINT4D *p)
189 {
190  POINTARRAY *pa = ptarray_construct_empty(hasz, hasm, 1);
192  return lwpoint_construct(srid, NULL, pa);
193 }
194 
196 {
197  if ( ! pt ) return;
198 
199  if ( pt->bbox )
200  lwfree(pt->bbox);
201  if ( pt->point )
202  ptarray_free(pt->point);
203  lwfree(pt);
204 }
205 
206 void printLWPOINT(LWPOINT *point)
207 {
208  lwnotice("LWPOINT {");
209  lwnotice(" ndims = %i", (int)FLAGS_NDIMS(point->flags));
210  lwnotice(" BBOX = %i", FLAGS_GET_BBOX(point->flags) ? 1 : 0 );
211  lwnotice(" SRID = %i", (int)point->srid);
212  printPA(point->point);
213  lwnotice("}");
214 }
215 
216 /* @brief Clone LWPOINT object. Serialized point lists are not copied.
217  *
218  * @see ptarray_clone
219  */
220 LWPOINT *
222 {
223  LWPOINT *ret = lwalloc(sizeof(LWPOINT));
224 
225  LWDEBUG(2, "lwpoint_clone called");
226 
227  memcpy(ret, g, sizeof(LWPOINT));
228 
229  ret->point = ptarray_clone(g->point);
230 
231  if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
232  return ret;
233 }
234 
235 
236 
237 void
239 {
241 }
242 
243 
244 /* check coordinate equality */
245 char
246 lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
247 {
248  return ptarray_same(p1->point, p2->point);
249 }
250 
251 
252 LWPOINT*
253 lwpoint_force_dims(const LWPOINT *point, int hasz, int hasm)
254 {
255  POINTARRAY *pdims = NULL;
256  LWPOINT *pointout;
257 
258  /* Return 2D empty */
259  if( lwpoint_is_empty(point) )
260  {
261  pointout = lwpoint_construct_empty(point->srid, hasz, hasm);
262  }
263  else
264  {
265  /* Always we duplicate the ptarray and return */
266  pdims = ptarray_force_dims(point->point, hasz, hasm);
267  pointout = lwpoint_construct(point->srid, NULL, pdims);
268  }
269  pointout->type = point->type;
270  return pointout;
271 }
272 
273 int lwpoint_is_empty(const LWPOINT *point)
274 {
275  if ( ! point->point || point->point->npoints < 1 )
276  return LW_TRUE;
277  return LW_FALSE;
278 }
279 
280 
281 LWPOINT *
282 lwpoint_grid(const LWPOINT *point, const gridspec *grid)
283 {
284  POINTARRAY *opa = ptarray_grid(point->point, grid);
285  return lwpoint_construct(point->srid, NULL, opa);
286 }
287 
double x
Definition: liblwgeom.h:351
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: g_box.c:438
double lwpoint_get_y(const LWPOINT *point)
Definition: lwpoint.c:73
void printLWPOINT(LWPOINT *point)
Definition: lwpoint.c:206
POINTARRAY * ptarray_construct(char hasz, char hasm, uint32_t npoints)
Construct an empty pointarray, allocating storage and setting the npoints, but not filling in any inf...
Definition: ptarray.c:62
LWPOINT * lwpoint_force_dims(const LWPOINT *point, int hasz, int hasm)
Definition: lwpoint.c:253
double m
Definition: liblwgeom.h:351
POINTARRAY * ptarray_clone(const POINTARRAY *ptarray)
Clone a POINTARRAY object.
Definition: ptarray.c:658
uint8_t type
Definition: liblwgeom.h:406
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:89
void lwfree(void *mem)
Definition: lwutil.c:242
LWPOINT * lwpoint_grid(const LWPOINT *point, const gridspec *grid)
Definition: lwpoint.c:282
int npoints
Definition: liblwgeom.h:370
char lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:246
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
int lwpoint_getPoint3dm_p(const LWPOINT *point, POINT3DM *out)
Definition: lwpoint.c:52
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.c:330
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:133
int lwpoint_getPoint2d_p(const LWPOINT *point, POINT2D *out)
Definition: lwpoint.c:40
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
int lwpoint_getPoint3dz_p(const LWPOINT *point, POINT3DZ *out)
Definition: lwpoint.c:47
int getPoint3dm_p(const POINTARRAY *pa, int n, POINT3DM *point)
Definition: lwgeom_api.c:382
POINTARRAY * point
Definition: liblwgeom.h:410
LWPOINT * lwpoint_make(int srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:188
LWPOINT * lwpoint_make3dm(int srid, double x, double y, double m)
Definition: lwpoint.c:166
int lwpoint_is_empty(const LWPOINT *point)
Definition: lwpoint.c:273
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:145
LWPOINT * lwpoint_make2d(int srid, double x, double y)
Definition: lwpoint.c:145
int getPoint3dz_p(const POINTARRAY *pa, int n, POINT3DZ *point)
Definition: lwgeom_api.c:332
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_FALSE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define FLAGS_GET_BBOX(flags)
Definition: liblwgeom.h:141
#define LW_FALSE
Definition: liblwgeom.h:76
uint8_t flags
Definition: liblwgeom.h:368
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:75
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:195
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:111
POINTARRAY * ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm)
Definition: ptarray.c:1035
LWPOINT * lwpoint_make4d(int srid, double x, double y, double z, double m)
Definition: lwpoint.c:177
int getPoint2d_p(const POINTARRAY *pa, int n, POINT2D *point)
Definition: lwgeom_api.c:461
void lwpoint_release(LWPOINT *lwpoint)
Definition: lwpoint.c:238
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
double z
Definition: liblwgeom.h:351
tuple x
Definition: pixval.py:53
int32_t srid
Definition: liblwgeom.h:409
uint8_t gflags(int hasz, int hasm, int geodetic)
Construct a new flags char.
Definition: g_util.c:145
uint8_t flags
Definition: liblwgeom.h:407
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:147
GBOX * bbox
Definition: liblwgeom.h:408
void printPA(POINTARRAY *pa)
Definition: lwgeom_api.c:611
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:484
double lwpoint_get_m(const LWPOINT *point)
Definition: lwpoint.c:95
POINTARRAY * ptarray_grid(const POINTARRAY *pa, const gridspec *grid)
Definition: ptarray.c:1843
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:385
double lwpoint_get_x(const LWPOINT *point)
Definition: lwpoint.c:63
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:267
LWPOINT * lwpoint_make3dz(int srid, double x, double y, double z)
Definition: lwpoint.c:155
double lwpoint_get_z(const LWPOINT *point)
Definition: lwpoint.c:83
void * lwalloc(size_t size)
Definition: lwutil.c:227
double y
Definition: liblwgeom.h:351
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
LWPOINT * lwpoint_clone(const LWPOINT *g)
Definition: lwpoint.c:221
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
tuple y
Definition: pixval.py:54
int getPoint4d_p(const POINTARRAY *pa, int n, POINT4D *point)
Definition: lwgeom_api.c:244
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:146
Snap to grid.