PostGIS  3.4.0dev-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 lwpoint_is_empty(point) ? 0 : getPoint2d_p(point->point, 0, out);
43 }
44 
45 /* convenience functions to hide the POINTARRAY */
46 int
48 {
49  return lwpoint_is_empty(point) ? 0 : getPoint3dz_p(point->point,0,out);
50 }
51 int
53 {
54  return lwpoint_is_empty(point) ? 0 : getPoint3dm_p(point->point,0,out);
55 }
56 int
58 {
59  return lwpoint_is_empty(point) ? 0 : 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  {
68  lwerror("lwpoint_get_x called with empty geometry");
69  return 0;
70  }
71  getPoint4d_p(point->point, 0, &pt);
72  return pt.x;
73 }
74 
75 double
76 lwpoint_get_y(const LWPOINT *point)
77 {
78  POINT4D pt;
79  if ( lwpoint_is_empty(point) )
80  {
81  lwerror("lwpoint_get_y called with empty geometry");
82  return 0;
83  }
84  getPoint4d_p(point->point, 0, &pt);
85  return pt.y;
86 }
87 
88 double
89 lwpoint_get_z(const LWPOINT *point)
90 {
91  POINT4D pt;
92  if ( lwpoint_is_empty(point) )
93  {
94  lwerror("lwpoint_get_z called with empty geometry");
95  return 0;
96  }
97  if ( ! FLAGS_GET_Z(point->flags) )
98  {
99  lwerror("lwpoint_get_z called without z dimension");
100  return 0;
101  }
102  getPoint4d_p(point->point, 0, &pt);
103  return pt.z;
104 }
105 
106 double
107 lwpoint_get_m(const LWPOINT *point)
108 {
109  POINT4D pt;
110  if ( lwpoint_is_empty(point) )
111  {
112  lwerror("lwpoint_get_m called with empty geometry");
113  return 0;
114  }
115  if ( ! FLAGS_GET_M(point->flags) )
116  {
117  lwerror("lwpoint_get_m called without m dimension");
118  return 0;
119  }
120  getPoint4d_p(point->point, 0, &pt);
121  return pt.m;
122 }
123 
124 /*
125  * Construct a new point. point will not be copied
126  * use SRID=SRID_UNKNOWN for unknown SRID (will have 8bit type's S = 0)
127  */
128 LWPOINT *
129 lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
130 {
131  LWPOINT *result;
132  lwflags_t flags = 0;
133 
134  if (point == NULL)
135  return NULL; /* error */
136 
137  result = lwalloc(sizeof(LWPOINT));
138  result->type = POINTTYPE;
139  FLAGS_SET_Z(flags, FLAGS_GET_Z(point->flags));
140  FLAGS_SET_M(flags, FLAGS_GET_M(point->flags));
141  FLAGS_SET_BBOX(flags, bbox?1:0);
142  result->flags = flags;
143  result->srid = srid;
144  result->point = point;
145  result->bbox = bbox;
146 
147  return result;
148 }
149 
150 LWPOINT *
151 lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
152 {
153  LWPOINT *result = lwalloc(sizeof(LWPOINT));
154  result->type = POINTTYPE;
155  result->flags = lwflags(hasz, hasm, 0);
156  result->srid = srid;
157  result->point = ptarray_construct(hasz, hasm, 0);
158  result->bbox = NULL;
159  return result;
160 }
161 
162 LWPOINT *
163 lwpoint_make2d(int32_t srid, double x, double y)
164 {
165  POINT4D p = {x, y, 0.0, 0.0};
166  POINTARRAY *pa = ptarray_construct_empty(0, 0, 1);
167 
168  ptarray_append_point(pa, &p, LW_TRUE);
169  return lwpoint_construct(srid, NULL, pa);
170 }
171 
172 LWPOINT *
173 lwpoint_make3dz(int32_t srid, double x, double y, double z)
174 {
175  POINT4D p = {x, y, z, 0.0};
176  POINTARRAY *pa = ptarray_construct_empty(1, 0, 1);
177 
178  ptarray_append_point(pa, &p, LW_TRUE);
179 
180  return lwpoint_construct(srid, NULL, pa);
181 }
182 
183 LWPOINT *
184 lwpoint_make3dm(int32_t srid, double x, double y, double m)
185 {
186  POINT4D p = {x, y, 0.0, m};
187  POINTARRAY *pa = ptarray_construct_empty(0, 1, 1);
188 
189  ptarray_append_point(pa, &p, LW_TRUE);
190 
191  return lwpoint_construct(srid, NULL, pa);
192 }
193 
194 LWPOINT *
195 lwpoint_make4d(int32_t srid, double x, double y, double z, double m)
196 {
197  POINT4D p = {x, y, z, m};
198  POINTARRAY *pa = ptarray_construct_empty(1, 1, 1);
199 
200  ptarray_append_point(pa, &p, LW_TRUE);
201 
202  return lwpoint_construct(srid, NULL, pa);
203 }
204 
205 LWPOINT *
206 lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
207 {
208  POINTARRAY *pa = ptarray_construct_empty(hasz, hasm, 1);
210  return lwpoint_construct(srid, NULL, pa);
211 }
212 
214 {
215  if ( ! pt ) return;
216 
217  if ( pt->bbox )
218  lwfree(pt->bbox);
219  if ( pt->point )
220  ptarray_free(pt->point);
221  lwfree(pt);
222 }
223 
224 void printLWPOINT(LWPOINT *point)
225 {
226  lwnotice("LWPOINT {");
227  lwnotice(" ndims = %i", (int)FLAGS_NDIMS(point->flags));
228  lwnotice(" BBOX = %i", FLAGS_GET_BBOX(point->flags) ? 1 : 0 );
229  lwnotice(" SRID = %i", (int)point->srid);
230  printPA(point->point);
231  lwnotice("}");
232 }
233 
234 /* @brief Clone LWPOINT object. Serialized point lists are not copied.
235  *
236  * @see ptarray_clone
237  */
238 LWPOINT *
240 {
241  LWPOINT *ret = lwalloc(sizeof(LWPOINT));
242 
243  LWDEBUG(2, "lwpoint_clone called");
244 
245  memcpy(ret, g, sizeof(LWPOINT));
246 
247  ret->point = ptarray_clone(g->point);
248 
249  if ( g->bbox ) ret->bbox = gbox_copy(g->bbox);
250  return ret;
251 }
252 
253 
254 
255 void
257 {
259 }
260 
261 
262 /* check coordinate equality */
263 char
264 lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
265 {
266  return ptarray_same(p1->point, p2->point);
267 }
268 
269 /* check 2d coordinate equality */
270 char
271 lwpoint_same2d(const LWPOINT *p1, const LWPOINT *p2)
272 {
273  return ptarray_same2d(p1->point, p2->point);
274 }
275 
276 LWPOINT *
277 lwpoint_project_lwpoint(const LWPOINT* lwpoint1, const LWPOINT* lwpoint2, double distance)
278 {
279  POINT4D p1, p2, p3;
280  int srid = lwgeom_get_srid((const LWGEOM*)lwpoint1);
281  int hasz = lwgeom_has_z((const LWGEOM*)lwpoint1);
282  int hasm = lwgeom_has_m((const LWGEOM*)lwpoint1);
283  lwpoint_getPoint4d_p(lwpoint1, &p1);
284  lwpoint_getPoint4d_p(lwpoint2, &p2);
285  project_pt_pt(&p1, &p2, distance, &p3);
286  return lwpoint_make(srid, hasz, hasm, &p3);
287 }
288 
289 LWPOINT *
290 lwpoint_project(const LWPOINT* lwpoint1, double distance, double azimuth)
291 {
292  POINT4D p1, p2;
293  int srid = lwgeom_get_srid((const LWGEOM*)lwpoint1);
294  int hasz = lwgeom_has_z((const LWGEOM*)lwpoint1);
295  int hasm = lwgeom_has_m((const LWGEOM*)lwpoint1);
296  lwpoint_getPoint4d_p(lwpoint1, &p1);
297  lwpoint_getPoint4d_p(lwpoint1, &p2);
298  project_pt((POINT2D*)&p1, distance, azimuth, (POINT2D*)&p2);
299  return lwpoint_make(srid, hasz, hasm, &p2);
300 }
301 
302 
303 LWPOINT*
304 lwpoint_force_dims(const LWPOINT *point, int hasz, int hasm, double zval, double mval)
305 {
306  POINTARRAY *pdims = NULL;
307  LWPOINT *pointout;
308 
309  /* Return 2D empty */
310  if( lwpoint_is_empty(point) )
311  {
312  pointout = lwpoint_construct_empty(point->srid, hasz, hasm);
313  }
314  else
315  {
316  /* Always we duplicate the ptarray and return */
317  pdims = ptarray_force_dims(point->point, hasz, hasm, zval, mval);
318  pointout = lwpoint_construct(point->srid, NULL, pdims);
319  }
320  pointout->type = point->type;
321  return pointout;
322 }
323 
324 
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition: gbox.c:426
int32_t lwgeom_get_srid(const LWGEOM *geom)
Return SRID number.
Definition: lwgeom.c:927
uint16_t lwflags_t
Definition: liblwgeom.h:299
#define FLAGS_GET_BBOX(flags)
Definition: liblwgeom.h:167
#define FLAGS_SET_BBOX(flags, value)
Definition: liblwgeom.h:174
void printPA(POINTARRAY *pa)
Definition: lwgeom_api.c:440
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition: lwgeom_api.c:342
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:51
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
#define FLAGS_GET_Z(flags)
Definition: liblwgeom.h:165
int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point)
Definition: lwgeom_api.c:215
void lwfree(void *mem)
Definition: lwutil.c:242
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:344
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:179
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:59
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:166
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition: lwgeom_api.c:125
void ptarray_free(POINTARRAY *pa)
Definition: ptarray.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_FALSE,...
Definition: ptarray.c:147
void * lwalloc(size_t size)
Definition: lwutil.c:227
lwflags_t lwflags(int hasz, int hasm, int geodetic)
Construct a new flags bitmask.
Definition: lwutil.c:471
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
int getPoint3dm_p(const POINTARRAY *pa, uint32_t n, POINT3DM *point)
Definition: lwgeom_api.c:268
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:173
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:468
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:941
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:172
POINTARRAY * ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm, double zval, double mval)
Definition: ptarray.c:1056
POINTARRAY * ptarray_clone(const POINTARRAY *ptarray)
Clone a POINTARRAY object.
Definition: ptarray.c:678
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:476
int lwpoint_is_empty(const LWPOINT *point)
int project_pt(const POINT2D *P, double distance, double azimuth, POINT2D *R)
Azimuth is angle in radians from vertical axis.
Definition: measures.c:2475
int project_pt_pt(const POINT4D *A, const POINT4D *B, double distance, POINT4D *R)
Azimuth is angle in radians from vertical axis.
Definition: measures.c:2499
char ptarray_same2d(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition: ptarray.c:501
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
Definition: lwutil.c:177
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
int lwpoint_getPoint4d_p(const LWPOINT *point, POINT4D *out)
Definition: lwpoint.c:57
LWPOINT * lwpoint_make4d(int32_t srid, double x, double y, double z, double m)
Definition: lwpoint.c:195
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition: lwpoint.c:163
double lwpoint_get_m(const LWPOINT *point)
Definition: lwpoint.c:107
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:213
void lwpoint_release(LWPOINT *lwpoint)
Definition: lwpoint.c:256
LWPOINT * lwpoint_make3dm(int32_t srid, double x, double y, double m)
Definition: lwpoint.c:184
int lwpoint_getPoint3dm_p(const LWPOINT *point, POINT3DM *out)
Definition: lwpoint.c:52
double lwpoint_get_x(const LWPOINT *point)
Definition: lwpoint.c:63
LWPOINT * lwpoint_project(const LWPOINT *lwpoint1, double distance, double azimuth)
Definition: lwpoint.c:290
char lwpoint_same2d(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:271
int lwpoint_getPoint3dz_p(const LWPOINT *point, POINT3DZ *out)
Definition: lwpoint.c:47
LWPOINT * lwpoint_make3dz(int32_t srid, double x, double y, double z)
Definition: lwpoint.c:173
LWPOINT * lwpoint_clone(const LWPOINT *g)
Definition: lwpoint.c:239
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
LWPOINT * lwpoint_project_lwpoint(const LWPOINT *lwpoint1, const LWPOINT *lwpoint2, double distance)
Definition: lwpoint.c:277
LWPOINT * lwpoint_force_dims(const LWPOINT *point, int hasz, int hasm, double zval, double mval)
Definition: lwpoint.c:304
void printLWPOINT(LWPOINT *point)
Definition: lwpoint.c:224
double lwpoint_get_z(const LWPOINT *point)
Definition: lwpoint.c:89
int lwpoint_getPoint2d_p(const LWPOINT *point, POINT2D *out)
Definition: lwpoint.c:40
double lwpoint_get_y(const LWPOINT *point)
Definition: lwpoint.c:76
char lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
Definition: lwpoint.c:264
LWPOINT * lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
Definition: lwpoint.c:206
static double distance(double x1, double y1, double x2, double y2)
Definition: lwtree.c:1032
POINTARRAY * point
Definition: liblwgeom.h:471
uint8_t type
Definition: liblwgeom.h:474
lwflags_t flags
Definition: liblwgeom.h:473
GBOX * bbox
Definition: liblwgeom.h:470
int32_t srid
Definition: liblwgeom.h:472
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
lwflags_t flags
Definition: liblwgeom.h:431