PostGIS  2.3.7dev-r@@SVN_REVISION@@
lwgeom_geos_node.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) 2011 Sandro Santilli <strk@kbt.io>
22  *
23  **********************************************************************/
24 
25 
26 #include "lwgeom_geos.h"
27 #include "liblwgeom_internal.h"
28 
29 #include <string.h>
30 #include <assert.h>
31 
32 static int
34 {
36  if ( c ) return c->ngeoms;
37  else return 1;
38 }
39 
40 static const LWGEOM*
41 lwgeom_subgeom(const LWGEOM* g, int n)
42 {
44  if ( c ) return lwcollection_getsubgeom((LWCOLLECTION*)c, n);
45  else return g;
46 }
47 
48 
49 static void
51 {
52  int i, n;
53  LWLINE* l;
54 
55  switch (lwg->type)
56  {
57  case MULTILINETYPE:
58  for ( i = 0,
59  n = lwgeom_ngeoms(lwg);
60  i < n; ++i )
61  {
63  lwgeom_subgeom(lwg, i),
64  col);
65  }
66  break;
67  case LINETYPE:
68  l = (LWLINE*)lwg;
69  col = lwmpoint_add_lwpoint(col,
70  lwline_get_lwpoint(l, 0));
71  col = lwmpoint_add_lwpoint(col,
73  break;
74  default:
75  lwerror("lwgeom_collect_endpoints: invalid type %s",
76  lwtype_name(lwg->type));
77  break;
78  }
79 }
80 
81 static LWMPOINT*
83 {
85  FLAGS_GET_Z(lwg->flags),
86  FLAGS_GET_M(lwg->flags));
87  lwgeom_collect_endpoints(lwg, col);
88 
89  return col;
90 }
91 
92 /* Assumes initGEOS was called already */
93 /* May return LWPOINT or LWMPOINT */
94 static LWGEOM*
96 {
97  LWGEOM* ret;
98  GEOSGeometry *gepu;
99  LWMPOINT *epall = lwgeom_extract_endpoints(lwg);
100  GEOSGeometry *gepall = LWGEOM2GEOS((LWGEOM*)epall, 1);
101  lwmpoint_free(epall);
102  if ( ! gepall ) {
103  lwerror("LWGEOM2GEOS: %s", lwgeom_geos_errmsg);
104  return NULL;
105  }
106 
107  /* UnaryUnion to remove duplicates */
108  /* TODO: do it all within pgis using indices */
109  gepu = GEOSUnaryUnion(gepall);
110  if ( ! gepu ) {
111  GEOSGeom_destroy(gepall);
112  lwerror("GEOSUnaryUnion: %s", lwgeom_geos_errmsg);
113  return NULL;
114  }
115  GEOSGeom_destroy(gepall);
116 
117  ret = GEOS2LWGEOM(gepu, FLAGS_GET_Z(lwg->flags));
118  GEOSGeom_destroy(gepu);
119  if ( ! ret ) {
120  lwerror("Error during GEOS2LWGEOM");
121  return NULL;
122  }
123 
124  return ret;
125 }
126 
127 /* exported */
128 extern LWGEOM* lwgeom_node(const LWGEOM* lwgeom_in);
129 LWGEOM*
130 lwgeom_node(const LWGEOM* lwgeom_in)
131 {
132  GEOSGeometry *g1, *gu, *gm;
133  LWGEOM *ep, *lines;
134  LWCOLLECTION *col, *tc;
135  int pn, ln, np, nl;
136 
137  if ( lwgeom_dimension(lwgeom_in) != 1 ) {
138  lwerror("Noding geometries of dimension != 1 is unsupported");
139  return NULL;
140  }
141 
143  g1 = LWGEOM2GEOS(lwgeom_in, 1);
144  if ( ! g1 ) {
145  lwerror("LWGEOM2GEOS: %s", lwgeom_geos_errmsg);
146  return NULL;
147  }
148 
149  ep = lwgeom_extract_unique_endpoints(lwgeom_in);
150  if ( ! ep ) {
151  GEOSGeom_destroy(g1);
152  lwerror("Error extracting unique endpoints from input");
153  return NULL;
154  }
155 
156  /* Unary union input to fully node */
157  gu = GEOSUnaryUnion(g1);
158  GEOSGeom_destroy(g1);
159  if ( ! gu ) {
160  lwgeom_free(ep);
161  lwerror("GEOSUnaryUnion: %s", lwgeom_geos_errmsg);
162  return NULL;
163  }
164 
165  /* Linemerge (in case of overlaps) */
166  gm = GEOSLineMerge(gu);
167  GEOSGeom_destroy(gu);
168  if ( ! gm ) {
169  lwgeom_free(ep);
170  lwerror("GEOSLineMerge: %s", lwgeom_geos_errmsg);
171  return NULL;
172  }
173 
174  lines = GEOS2LWGEOM(gm, FLAGS_GET_Z(lwgeom_in->flags));
175  GEOSGeom_destroy(gm);
176  if ( ! lines ) {
177  lwgeom_free(ep);
178  lwerror("Error during GEOS2LWGEOM");
179  return NULL;
180  }
181 
182  /*
183  * Reintroduce endpoints from input, using split-line-by-point.
184  * Note that by now we can be sure that each point splits at
185  * most _one_ segment as any point shared by multiple segments
186  * would already be a node. Also we can be sure that any of
187  * the segments endpoints won't split any other segment.
188  * We can use the above 2 assertions to early exit the loop.
189  */
190 
192  FLAGS_GET_Z(lwgeom_in->flags),
193  FLAGS_GET_M(lwgeom_in->flags));
194 
195  np = lwgeom_ngeoms(ep);
196  for (pn=0; pn<np; ++pn) { /* for each point */
197 
198  const LWPOINT* p = (LWPOINT*)lwgeom_subgeom(ep, pn);
199 
200  nl = lwgeom_ngeoms(lines);
201  for (ln=0; ln<nl; ++ln) { /* for each line */
202 
203  const LWLINE* l = (LWLINE*)lwgeom_subgeom(lines, ln);
204 
205  int s = lwline_split_by_point_to(l, p, (LWMLINE*)col);
206 
207  if ( ! s ) continue; /* not on this line */
208 
209  if ( s == 1 ) {
210  /* found on this line, but not splitting it */
211  break;
212  }
213 
214  /* splits this line */
215 
216  /* replace this line with the two splits */
217  if ( lwgeom_is_collection(lines) ) {
218  tc = (LWCOLLECTION*)lines;
219  lwcollection_reserve(tc, nl + 1);
220  while (nl > ln+1) {
221  tc->geoms[nl] = tc->geoms[nl-1];
222  --nl;
223  }
224  lwgeom_free(tc->geoms[ln]);
225  tc->geoms[ln] = col->geoms[0];
226  tc->geoms[ln+1] = col->geoms[1];
227  tc->ngeoms++;
228  } else {
229  lwgeom_free(lines);
230  /* transfer ownership rather than cloning */
231  lines = (LWGEOM*)lwcollection_clone_deep(col);
232  assert(col->ngeoms == 2);
233  lwgeom_free(col->geoms[0]);
234  lwgeom_free(col->geoms[1]);
235  }
236 
237  /* reset the vector */
238  assert(col->ngeoms == 2);
239  col->ngeoms = 0;
240 
241  break;
242  }
243 
244  }
245 
246  lwgeom_free(ep);
247  lwcollection_free(col);
248 
249  lines->srid = lwgeom_in->srid;
250  return (LWGEOM*)lines;
251 }
252 
#define LINETYPE
Definition: liblwgeom.h:85
int lwgeom_is_collection(const LWGEOM *lwgeom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1004
static LWGEOM * lwgeom_extract_unique_endpoints(const LWGEOM *lwg)
LWGEOM * lwgeom_node(const LWGEOM *lwgeom_in)
int npoints
Definition: liblwgeom.h:370
LWGEOM * lwcollection_getsubgeom(LWCOLLECTION *col, int gnum)
Definition: lwcollection.c:113
uint8_t flags
Definition: liblwgeom.h:396
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
static int lwgeom_ngeoms(const LWGEOM *n)
char lwgeom_geos_errmsg[LWGEOM_GEOS_ERRMSG_MAXSIZE]
int32_t srid
Definition: liblwgeom.h:398
void lwmpoint_free(LWMPOINT *mpt)
Definition: lwmpoint.c:72
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
void lwgeom_geos_error(const char *fmt,...)
static const LWGEOM * lwgeom_subgeom(const LWGEOM *g, int n)
LWGEOM ** geoms
Definition: liblwgeom.h:508
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:187
LWMPOINT * lwmpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwmpoint.c:39
char * s
Definition: cu_in_wkt.c:23
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
LWCOLLECTION * lwcollection_clone_deep(const LWCOLLECTION *lwgeom)
Deep clone LWCOLLECTION object.
Definition: lwcollection.c:149
static void lwgeom_collect_endpoints(const LWGEOM *lwg, LWMPOINT *col)
LWCOLLECTION * lwgeom_as_lwcollection(const LWGEOM *lwgeom)
Definition: lwgeom.c:156
int lwgeom_dimension(const LWGEOM *geom)
For an LWGEOM, returns 0 for points, 1 for lines, 2 for polygons, 3 for volume, and the max dimension...
Definition: lwgeom.c:1205
LWMPOINT * lwmpoint_add_lwpoint(LWMPOINT *mobj, const LWPOINT *obj)
Definition: lwmpoint.c:45
#define FLAGS_GET_M(flags)
Definition: liblwgeom.h:140
LWGEOM * GEOS2LWGEOM(const GEOSGeometry *geom, char want3d)
LWPOINT * lwline_get_lwpoint(const LWLINE *line, int where)
Returns freshly allocated LWPOINT that corresponds to the index where.
Definition: lwline.c:324
uint8_t type
Definition: liblwgeom.h:395
void lwcollection_free(LWCOLLECTION *col)
Definition: lwcollection.c:339
#define MULTILINETYPE
Definition: liblwgeom.h:88
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
static LWMPOINT * lwgeom_extract_endpoints(const LWGEOM *lwg)
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
void lwcollection_reserve(LWCOLLECTION *col, int ngeoms)
Ensure the collection can hold at least up to ngeoms geometries.
Definition: lwcollection.c:174
int lwline_split_by_point_to(const LWLINE *ln, const LWPOINT *pt, LWMLINE *to)
Split a line by a point and push components to the provided multiline.
POINTARRAY * points
Definition: liblwgeom.h:421