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