PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ lwgeom_subdivide_recursive()

static void lwgeom_subdivide_recursive ( const LWGEOM geom,
uint8_t  dimension,
uint32_t  maxvertices,
uint32_t  depth,
LWCOLLECTION col,
double  gridSize 
)
static

Definition at line 2284 of file lwgeom.c.

2290 {
2291  const uint32_t maxdepth = 50;
2292 
2293  if (!geom)
2294  return;
2295 
2296  const GBOX *box_in = lwgeom_get_bbox(geom);
2297  if (!box_in)
2298  return;
2299 
2300  LW_ON_INTERRUPT(return);
2301 
2302  GBOX clip;
2303  gbox_duplicate(box_in, &clip);
2304  double width = clip.xmax - clip.xmin;
2305  double height = clip.ymax - clip.ymin;
2306 
2307  if ( geom->type == POLYHEDRALSURFACETYPE || geom->type == TINTYPE )
2308  lwerror("%s: unsupported geometry type '%s'", __func__, lwtype_name(geom->type));
2309 
2310  if ( width == 0.0 && height == 0.0 )
2311  {
2312  if ( geom->type == POINTTYPE && dimension == 0)
2314  return;
2315  }
2316 
2317  if (width == 0.0)
2318  {
2319  clip.xmax += FP_TOLERANCE;
2320  clip.xmin -= FP_TOLERANCE;
2321  width = 2 * FP_TOLERANCE;
2322  }
2323  if (height == 0.0)
2324  {
2325  clip.ymax += FP_TOLERANCE;
2326  clip.ymin -= FP_TOLERANCE;
2327  height = 2 * FP_TOLERANCE;
2328  }
2329 
2330  /* Always just recurse into collections */
2331  if ( lwgeom_is_collection(geom) && geom->type != MULTIPOINTTYPE )
2332  {
2333  LWCOLLECTION *incol = (LWCOLLECTION*)geom;
2334  /* Don't increment depth yet, since we aren't actually
2335  * subdividing geometries yet */
2336  for (uint32_t i = 0; i < incol->ngeoms; i++ )
2337  lwgeom_subdivide_recursive(incol->geoms[i], dimension, maxvertices, depth, col, gridSize);
2338  return;
2339  }
2340 
2341  if (lwgeom_dimension(geom) < dimension)
2342  {
2343  /* We've hit a lower dimension object produced by clipping at
2344  * a shallower recursion level. Ignore it. */
2345  return;
2346  }
2347 
2348  /* But don't go too far. 2^50 ~= 10^15, that's enough subdivision */
2349  /* Just add what's left */
2350  if ( depth > maxdepth )
2351  {
2353  return;
2354  }
2355 
2356  uint32_t nvertices = lwgeom_count_vertices(geom);
2357 
2358  /* Skip empties entirely */
2359  if (nvertices == 0)
2360  return;
2361 
2362  /* If it is under the vertex tolerance, just add it, we're done */
2363  if (nvertices <= maxvertices)
2364  {
2366  return;
2367  }
2368 
2369  uint8_t split_ordinate = (width > height) ? 0 : 1;
2370  double center = (split_ordinate == 0) ? (clip.xmin + clip.xmax) / 2 : (clip.ymin + clip.ymax) / 2;
2371  double pivot = DBL_MAX;
2372  if (geom->type == POLYGONTYPE)
2373  {
2374  uint32_t ring_to_trim = 0;
2375  double ring_area = 0;
2376  double pivot_eps = DBL_MAX;
2377  double pt_eps = DBL_MAX;
2378  POINTARRAY *pa;
2379  LWPOLY *lwpoly = (LWPOLY *)geom;
2380 
2381  /* if there are more points in holes than in outer ring */
2382  if (nvertices >= 2 * lwpoly->rings[0]->npoints)
2383  {
2384  /* trim holes starting from biggest */
2385  for (uint32_t i = 1; i < lwpoly->nrings; i++)
2386  {
2387  double current_ring_area = fabs(ptarray_signed_area(lwpoly->rings[i]));
2388  if (current_ring_area >= ring_area)
2389  {
2390  ring_area = current_ring_area;
2391  ring_to_trim = i;
2392  }
2393  }
2394  }
2395 
2396  pa = lwpoly->rings[ring_to_trim];
2397 
2398  /* find most central point in chosen ring */
2399  for (uint32_t i = 0; i < pa->npoints; i++)
2400  {
2401  double pt;
2402  if (split_ordinate == 0)
2403  pt = getPoint2d_cp(pa, i)->x;
2404  else
2405  pt = getPoint2d_cp(pa, i)->y;
2406  pt_eps = fabs(pt - center);
2407  if (pivot_eps > pt_eps)
2408  {
2409  pivot = pt;
2410  pivot_eps = pt_eps;
2411  }
2412  }
2413  }
2414  GBOX subbox1, subbox2;
2415  gbox_duplicate(&clip, &subbox1);
2416  gbox_duplicate(&clip, &subbox2);
2417 
2418  if (pivot == DBL_MAX)
2419  pivot = center;
2420 
2421  if (split_ordinate == 0)
2422  {
2423  if (FP_NEQUALS(subbox1.xmax, pivot) && FP_NEQUALS(subbox1.xmin, pivot))
2424  subbox1.xmax = subbox2.xmin = pivot;
2425  else
2426  subbox1.xmax = subbox2.xmin = center;
2427  }
2428  else
2429  {
2430  if (FP_NEQUALS(subbox1.ymax, pivot) && FP_NEQUALS(subbox1.ymin, pivot))
2431  subbox1.ymax = subbox2.ymin = pivot;
2432  else
2433  subbox1.ymax = subbox2.ymin = center;
2434  }
2435 
2436  ++depth;
2437 
2438  {
2440  geom->srid, subbox1.xmin, subbox1.ymin, subbox1.xmax, subbox1.ymax);
2441  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2442  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2443  lwgeom_free(subbox);
2444  if (clipped && !lwgeom_is_empty(clipped))
2445  {
2446  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2447  lwgeom_free(clipped);
2448  }
2449  }
2450  {
2452  geom->srid, subbox2.xmin, subbox2.ymin, subbox2.xmax, subbox2.ymax);
2453  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2454  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2455  lwgeom_free(subbox);
2456  if (clipped && !lwgeom_is_empty(clipped))
2457  {
2458  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2459  lwgeom_free(clipped);
2460  }
2461  }
2462 }
void gbox_duplicate(const GBOX *original, GBOX *duplicate)
Copy the values of original GBOX into duplicate.
Definition: gbox.c:433
#define MULTIPOINTTYPE
Definition: liblwgeom.h:105
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
#define TINTYPE
Definition: liblwgeom.h:116
LWPOLY * lwpoly_construct_envelope(int32_t srid, double x1, double y1, double x2, double y2)
Definition: lwpoly.c:98
#define POLYGONTYPE
Definition: liblwgeom.h:104
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:114
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
LWGEOM * lwgeom_intersection_prec(const LWGEOM *geom1, const LWGEOM *geom2, double gridSize)
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:188
#define LW_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:93
double ptarray_signed_area(const POINTARRAY *pa)
Returns the area in cartesian units.
Definition: ptarray.c:1016
#define LW_ON_INTERRUPT(x)
#define FP_TOLERANCE
Floating point comparators.
#define FP_NEQUALS(A, B)
int lwgeom_is_collection(const LWGEOM *geom)
Determine whether a LWGEOM can contain sub-geometries or not.
Definition: lwgeom.c:1097
LWGEOM * lwgeom_clone_deep(const LWGEOM *lwgeom)
Deep-clone an LWGEOM object.
Definition: lwgeom.c:529
uint32_t lwgeom_count_vertices(const LWGEOM *geom)
Count points in an LWGEOM.
Definition: lwgeom.c:1246
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:1298
const GBOX * lwgeom_get_bbox(const LWGEOM *lwg)
Get a non-empty geometry bounding box, computing and caching it if not already there.
Definition: lwgeom.c:743
void lwgeom_free(LWGEOM *lwgeom)
Definition: lwgeom.c:1155
static void lwgeom_subdivide_recursive(const LWGEOM *geom, uint8_t dimension, uint32_t maxvertices, uint32_t depth, LWCOLLECTION *col, double gridSize)
Definition: lwgeom.c:2284
int lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1737
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static double pivot(double *left, double *right)
Definition: rt_statistics.c:40
double ymax
Definition: liblwgeom.h:357
double xmax
Definition: liblwgeom.h:355
double ymin
Definition: liblwgeom.h:356
double xmin
Definition: liblwgeom.h:354
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
uint8_t type
Definition: liblwgeom.h:462
int32_t srid
Definition: liblwgeom.h:460
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
uint32_t npoints
Definition: liblwgeom.h:427

References FP_NEQUALS, FP_TOLERANCE, gbox_duplicate(), LWCOLLECTION::geoms, getPoint2d_cp(), LW_ON_INTERRUPT, LW_TRUE, lwcollection_add_lwgeom(), lwerror(), lwgeom_clone_deep(), lwgeom_count_vertices(), lwgeom_dimension(), lwgeom_free(), lwgeom_get_bbox(), lwgeom_intersection_prec(), lwgeom_is_collection(), lwgeom_is_empty(), lwgeom_simplify_in_place(), lwpoly_construct_envelope(), lwtype_name(), MULTIPOINTTYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, pivot(), POINTTYPE, POLYGONTYPE, POLYHEDRALSURFACETYPE, ptarray_signed_area(), LWPOLY::rings, LWGEOM::srid, TINTYPE, LWGEOM::type, POINT2D::x, GBOX::xmax, GBOX::xmin, POINT2D::y, GBOX::ymax, and GBOX::ymin.

Referenced by lwgeom_subdivide_prec().

Here is the call graph for this function:
Here is the caller graph for this function: