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