PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
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 */
39int
41{
42 return lwpoint_is_empty(point) ? 0 : getPoint2d_p(point->point, 0, out);
43}
44
45/* convenience functions to hide the POINTARRAY */
46int
48{
49 return lwpoint_is_empty(point) ? 0 : getPoint3dz_p(point->point,0,out);
50}
51int
53{
54 return lwpoint_is_empty(point) ? 0 : getPoint3dm_p(point->point,0,out);
55}
56int
58{
59 return lwpoint_is_empty(point) ? 0 : getPoint4d_p(point->point,0,out);
60}
61
62double
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
75double
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
88double
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
106double
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 */
128LWPOINT *
129lwpoint_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
150LWPOINT *
151lwpoint_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
162LWPOINT *
163lwpoint_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
169 return lwpoint_construct(srid, NULL, pa);
170}
171
172LWPOINT *
173lwpoint_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
179
180 return lwpoint_construct(srid, NULL, pa);
181}
182
183LWPOINT *
184lwpoint_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
190
191 return lwpoint_construct(srid, NULL, pa);
192}
193
194LWPOINT *
195lwpoint_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
201
202 return lwpoint_construct(srid, NULL, pa);
203}
204
205LWPOINT *
206lwpoint_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
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 */
238LWPOINT *
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
255void
257{
259}
260
261
262/* check coordinate equality */
263char
264lwpoint_same(const LWPOINT *p1, const LWPOINT *p2)
265{
266 return ptarray_same(p1->point, p2->point);
267}
268
269
270LWPOINT*
271lwpoint_force_dims(const LWPOINT *point, int hasz, int hasm)
272{
273 POINTARRAY *pdims = NULL;
274 LWPOINT *pointout;
275
276 /* Return 2D empty */
277 if( lwpoint_is_empty(point) )
278 {
279 pointout = lwpoint_construct_empty(point->srid, hasz, hasm);
280 }
281 else
282 {
283 /* Always we duplicate the ptarray and return */
284 pdims = ptarray_force_dims(point->point, hasz, hasm);
285 pointout = lwpoint_construct(point->srid, NULL, pdims);
286 }
287 pointout->type = point->type;
288 return pointout;
289}
290
291
GBOX * gbox_copy(const GBOX *box)
Return a copy of the GBOX, based on dimensionality of flags.
Definition gbox.c:426
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition lwgeom.c:326
uint16_t lwflags_t
Definition liblwgeom.h:313
#define FLAGS_GET_BBOX(flags)
Definition liblwgeom.h:181
#define FLAGS_SET_BBOX(flags, value)
Definition liblwgeom.h:188
void printPA(POINTARRAY *pa)
Definition lwgeom_api.c:447
int getPoint2d_p(const POINTARRAY *pa, uint32_t n, POINT2D *point)
Definition lwgeom_api.c:349
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition ptarray.c:59
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition liblwgeom.h:116
#define FLAGS_GET_Z(flags)
Definition liblwgeom.h:179
void * lwalloc(size_t size)
Definition lwutil.c:227
int getPoint3dz_p(const POINTARRAY *pa, uint32_t n, POINT3DZ *point)
Definition lwgeom_api.c:215
void lwfree(void *mem)
Definition lwutil.c:242
#define FLAGS_NDIMS(flags)
Definition liblwgeom.h:193
#define FLAGS_GET_M(flags)
Definition liblwgeom.h:180
int getPoint4d_p(const POINTARRAY *pa, uint32_t n, POINT4D *point)
Definition lwgeom_api.c:125
void ptarray_free(POINTARRAY *pa)
Definition ptarray.c:327
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
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:107
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:187
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition lwgeom.c:450
#define FLAGS_SET_Z(flags, value)
Definition liblwgeom.h:186
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
char ptarray_same(const POINTARRAY *pa1, const POINTARRAY *pa2)
Definition ptarray.c:484
int lwpoint_is_empty(const LWPOINT *point)
POINTARRAY * ptarray_clone(const POINTARRAY *ptarray)
Clone a POINTARRAY object.
Definition ptarray.c:665
POINTARRAY * ptarray_force_dims(const POINTARRAY *pa, int hasz, int hasm)
Definition ptarray.c:1043
#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
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_make3dz(int32_t srid, double x, double y, double z)
Definition lwpoint.c:173
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
int lwpoint_getPoint3dm_p(const LWPOINT *point, POINT3DM *out)
Definition lwpoint.c:52
LWPOINT * lwpoint_clone(const LWPOINT *g)
Definition lwpoint.c:239
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
Definition lwpoint.c:129
double lwpoint_get_x(const LWPOINT *point)
Definition lwpoint.c:63
LWPOINT * lwpoint_make3dm(int32_t srid, double x, double y, double m)
Definition lwpoint.c:184
int lwpoint_getPoint3dz_p(const LWPOINT *point, POINT3DZ *out)
Definition lwpoint.c:47
LWPOINT * lwpoint_make2d(int32_t srid, double x, double y)
Definition lwpoint.c:163
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition lwpoint.c:151
LWPOINT * lwpoint_make(int32_t srid, int hasz, int hasm, const POINT4D *p)
Definition lwpoint.c:206
void printLWPOINT(LWPOINT *point)
Definition lwpoint.c:224
double lwpoint_get_z(const LWPOINT *point)
Definition lwpoint.c:89
LWPOINT * lwpoint_force_dims(const LWPOINT *point, int hasz, int hasm)
Definition lwpoint.c:271
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
POINTARRAY * point
Definition liblwgeom.h:457
uint8_t type
Definition liblwgeom.h:460
lwflags_t flags
Definition liblwgeom.h:459
GBOX * bbox
Definition liblwgeom.h:456
int32_t srid
Definition liblwgeom.h:458
double m
Definition liblwgeom.h:400
double x
Definition liblwgeom.h:400
double z
Definition liblwgeom.h:400
double y
Definition liblwgeom.h:400
lwflags_t flags
Definition liblwgeom.h:417