PostGIS  3.7.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 2410 of file lwgeom.c.

2416 {
2417  const uint32_t maxdepth = 50;
2418 
2419  if (!geom)
2420  return;
2421 
2422  const GBOX *box_in = lwgeom_get_bbox(geom);
2423  if (!box_in)
2424  return;
2425 
2426  LW_ON_INTERRUPT(return);
2427 
2428  GBOX clip;
2429  gbox_duplicate(box_in, &clip);
2430  double width = clip.xmax - clip.xmin;
2431  double height = clip.ymax - clip.ymin;
2432 
2433  if ( geom->type == POLYHEDRALSURFACETYPE || geom->type == TINTYPE )
2434  lwerror("%s: unsupported geometry type '%s'", __func__, lwtype_name(geom->type));
2435 
2436  if ( width == 0.0 && height == 0.0 )
2437  {
2438  if ( geom->type == POINTTYPE && dimension == 0)
2440  return;
2441  }
2442 
2443  if (width == 0.0)
2444  {
2445  clip.xmax += FP_TOLERANCE;
2446  clip.xmin -= FP_TOLERANCE;
2447  width = 2 * FP_TOLERANCE;
2448  }
2449  if (height == 0.0)
2450  {
2451  clip.ymax += FP_TOLERANCE;
2452  clip.ymin -= FP_TOLERANCE;
2453  height = 2 * FP_TOLERANCE;
2454  }
2455 
2456  /* Always just recurse into collections */
2457  if ( lwgeom_is_collection(geom) && geom->type != MULTIPOINTTYPE )
2458  {
2459  LWCOLLECTION *incol = (LWCOLLECTION*)geom;
2460  /* Don't increment depth yet, since we aren't actually
2461  * subdividing geometries yet */
2462  for (uint32_t i = 0; i < incol->ngeoms; i++ )
2463  lwgeom_subdivide_recursive(incol->geoms[i], dimension, maxvertices, depth, col, gridSize);
2464  return;
2465  }
2466 
2467  if (lwgeom_dimension(geom) < dimension)
2468  {
2469  /* We've hit a lower dimension object produced by clipping at
2470  * a shallower recursion level. Ignore it. */
2471  return;
2472  }
2473 
2474  /* But don't go too far. 2^50 ~= 10^15, that's enough subdivision */
2475  /* Just add what's left */
2476  if ( depth > maxdepth )
2477  {
2479  return;
2480  }
2481 
2482  uint32_t nvertices = lwgeom_count_vertices(geom);
2483 
2484  /* Skip empties entirely */
2485  if (nvertices == 0)
2486  return;
2487 
2488  /* If it is under the vertex tolerance, just add it, we're done */
2489  if (nvertices <= maxvertices)
2490  {
2492  return;
2493  }
2494 
2495  uint8_t split_ordinate = (width > height) ? 0 : 1;
2496  double center = (split_ordinate == 0) ? (clip.xmin + clip.xmax) / 2 : (clip.ymin + clip.ymax) / 2;
2497  double pivot = DBL_MAX;
2498  if (geom->type == POLYGONTYPE)
2499  {
2500  uint32_t ring_to_trim = 0;
2501  double ring_area = 0;
2502  double pivot_eps = DBL_MAX;
2503  double pt_eps = DBL_MAX;
2504  POINTARRAY *pa;
2505  LWPOLY *lwpoly = (LWPOLY *)geom;
2506 
2507  /* if there are more points in holes than in outer ring */
2508  if (nvertices >= 2 * lwpoly->rings[0]->npoints)
2509  {
2510  /* trim holes starting from biggest */
2511  for (uint32_t i = 1; i < lwpoly->nrings; i++)
2512  {
2513  double current_ring_area = fabs(ptarray_signed_area(lwpoly->rings[i]));
2514  if (current_ring_area >= ring_area)
2515  {
2516  ring_area = current_ring_area;
2517  ring_to_trim = i;
2518  }
2519  }
2520  }
2521 
2522  pa = lwpoly->rings[ring_to_trim];
2523 
2524  /* find most central point in chosen ring */
2525  for (uint32_t i = 0; i < pa->npoints; i++)
2526  {
2527  double pt;
2528  if (split_ordinate == 0)
2529  pt = getPoint2d_cp(pa, i)->x;
2530  else
2531  pt = getPoint2d_cp(pa, i)->y;
2532  pt_eps = fabs(pt - center);
2533  if (pivot_eps > pt_eps)
2534  {
2535  pivot = pt;
2536  pivot_eps = pt_eps;
2537  }
2538  }
2539  }
2540  GBOX subbox1, subbox2;
2541  gbox_duplicate(&clip, &subbox1);
2542  gbox_duplicate(&clip, &subbox2);
2543 
2544  if (pivot == DBL_MAX)
2545  pivot = center;
2546 
2547  if (split_ordinate == 0)
2548  {
2549  if (FP_NEQUALS(subbox1.xmax, pivot) && FP_NEQUALS(subbox1.xmin, pivot))
2550  subbox1.xmax = subbox2.xmin = pivot;
2551  else
2552  subbox1.xmax = subbox2.xmin = center;
2553  }
2554  else
2555  {
2556  if (FP_NEQUALS(subbox1.ymax, pivot) && FP_NEQUALS(subbox1.ymin, pivot))
2557  subbox1.ymax = subbox2.ymin = pivot;
2558  else
2559  subbox1.ymax = subbox2.ymin = center;
2560  }
2561 
2562  ++depth;
2563 
2564  {
2566  geom->srid, subbox1.xmin, subbox1.ymin, subbox1.xmax, subbox1.ymax);
2567  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2568  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2569  lwgeom_free(subbox);
2570  if (clipped && !lwgeom_is_empty(clipped))
2571  {
2572  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2573  lwgeom_free(clipped);
2574  }
2575  }
2576  {
2578  geom->srid, subbox2.xmin, subbox2.ymin, subbox2.xmax, subbox2.ymax);
2579  LWGEOM *clipped = lwgeom_intersection_prec(geom, subbox, gridSize);
2580  lwgeom_simplify_in_place(clipped, 0.0, LW_TRUE);
2581  lwgeom_free(subbox);
2582  if (clipped && !lwgeom_is_empty(clipped))
2583  {
2584  lwgeom_subdivide_recursive(clipped, dimension, maxvertices, depth, col, gridSize);
2585  lwgeom_free(clipped);
2586  }
2587  }
2588 }
void gbox_duplicate(const GBOX *original, GBOX *duplicate)
Copy the values of original GBOX into duplicate.
Definition: gbox.c:445
#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:189
#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:1020
#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 contains sub-geometries or not This basically just checks that the struct ...
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:1309
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:1361
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:1218
static void lwgeom_subdivide_recursive(const LWGEOM *geom, uint8_t dimension, uint32_t maxvertices, uint32_t depth, LWCOLLECTION *col, double gridSize)
Definition: lwgeom.c:2410
int lwgeom_simplify_in_place(LWGEOM *geom, double epsilon, int preserve_collapsed)
Definition: lwgeom.c:1823
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
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:97
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:199
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: