PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ SFCGAL2LWGEOM()

LWGEOM* SFCGAL2LWGEOM ( const sfcgal_geometry_t *  geom,
int  force3D,
int32_t  srid 
)

Definition at line 293 of file lwgeom_sfcgal.c.

294 {
295  uint32_t ngeoms, nshells, nsolids;
296  uint32_t i, j, k;
297  int want3d;
298 
299  assert(geom);
300 
301  want3d = force3D || sfcgal_geometry_is_3d(geom);
302 
303  switch (sfcgal_geometry_type_id(geom))
304  {
305  case SFCGAL_TYPE_POINT:
306  {
307  if (sfcgal_geometry_is_empty(geom))
308  return (LWGEOM *)lwpoint_construct_empty(srid, want3d, 0);
309 
310  POINTARRAY *pa = ptarray_from_SFCGAL(geom, want3d);
311  return (LWGEOM *)lwpoint_construct(srid, NULL, pa);
312  }
313 
314  case SFCGAL_TYPE_LINESTRING:
315  {
316  if (sfcgal_geometry_is_empty(geom))
317  return (LWGEOM *)lwline_construct_empty(srid, want3d, 0);
318 
319  POINTARRAY *pa = ptarray_from_SFCGAL(geom, want3d);
320  return (LWGEOM *)lwline_construct(srid, NULL, pa);
321  }
322 
323  case SFCGAL_TYPE_TRIANGLE:
324  {
325  if (sfcgal_geometry_is_empty(geom))
326  return (LWGEOM *)lwtriangle_construct_empty(srid, want3d, 0);
327 
328  POINTARRAY *pa = ptarray_from_SFCGAL(geom, want3d);
329  return (LWGEOM *)lwtriangle_construct(srid, NULL, pa);
330  }
331 
332  case SFCGAL_TYPE_POLYGON:
333  {
334  if (sfcgal_geometry_is_empty(geom))
335  return (LWGEOM *)lwpoly_construct_empty(srid, want3d, 0);
336 
337  uint32_t nrings = sfcgal_polygon_num_interior_rings(geom) + 1;
338  POINTARRAY **pa = (POINTARRAY **)lwalloc(sizeof(POINTARRAY *) * nrings);
339 
340  pa[0] = ptarray_from_SFCGAL(sfcgal_polygon_exterior_ring(geom), want3d);
341  for (i = 1; i < nrings; i++)
342  pa[i] = ptarray_from_SFCGAL(sfcgal_polygon_interior_ring_n(geom, i - 1), want3d);
343 
344  return (LWGEOM *)lwpoly_construct(srid, NULL, nrings, pa);
345  }
346 
347  case SFCGAL_TYPE_MULTIPOINT:
348  case SFCGAL_TYPE_MULTILINESTRING:
349  case SFCGAL_TYPE_MULTIPOLYGON:
350  case SFCGAL_TYPE_MULTISOLID:
351  case SFCGAL_TYPE_GEOMETRYCOLLECTION:
352  {
353  ngeoms = sfcgal_geometry_collection_num_geometries(geom);
354  LWGEOM **geoms = NULL;
355  if (ngeoms)
356  {
357  nsolids = 0;
358  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
359  for (i = 0; i < ngeoms; i++)
360  {
361  const sfcgal_geometry_t *g = sfcgal_geometry_collection_geometry_n(geom, i);
362  geoms[i] = SFCGAL2LWGEOM(g, 0, srid);
363  if (FLAGS_GET_SOLID(geoms[i]->flags))
364  ++nsolids;
365  }
366  geoms = (LWGEOM **)lwrealloc(geoms, sizeof(LWGEOM *) * ngeoms);
367  }
369  SFCGAL_type_to_lwgeom_type(sfcgal_geometry_type_id(geom)), srid, NULL, ngeoms, geoms);
370  if (ngeoms)
371  {
372  if (ngeoms == nsolids)
373  FLAGS_SET_SOLID(rgeom->flags, 1);
374  else if (nsolids)
375  lwnotice(
376  "SFCGAL2LWGEOM: SOLID in heterogeneous collection will be treated as a POLYHEDRALSURFACETYPE");
377  }
378  return rgeom;
379  }
380 
381 #if 0
382  case SFCGAL_TYPE_CIRCULARSTRING:
383  case SFCGAL_TYPE_COMPOUNDCURVE:
384  case SFCGAL_TYPE_CURVEPOLYGON:
385  case SFCGAL_TYPE_MULTICURVE:
386  case SFCGAL_TYPE_MULTISURFACE:
387  case SFCGAL_TYPE_CURVE:
388  case SFCGAL_TYPE_SURFACE:
389 
390  /* TODO curve types handling */
391 #endif
392 
393  case SFCGAL_TYPE_POLYHEDRALSURFACE:
394  {
395  ngeoms = sfcgal_polyhedral_surface_num_polygons(geom);
396 
397  LWGEOM **geoms = NULL;
398  if (ngeoms)
399  {
400  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
401  for (i = 0; i < ngeoms; i++)
402  {
403  const sfcgal_geometry_t *g = sfcgal_polyhedral_surface_polygon_n(geom, i);
404  geoms[i] = SFCGAL2LWGEOM(g, 0, srid);
405  }
406  }
407  return (LWGEOM *)lwcollection_construct(POLYHEDRALSURFACETYPE, srid, NULL, ngeoms, geoms);
408  }
409 
410  /* Solid is map as a closed PolyhedralSurface (for now) */
411  case SFCGAL_TYPE_SOLID:
412  {
413  nshells = sfcgal_solid_num_shells(geom);
414 
415  for (ngeoms = 0, i = 0; i < nshells; i++)
416  ngeoms += sfcgal_polyhedral_surface_num_polygons(sfcgal_solid_shell_n(geom, i));
417 
418  LWGEOM **geoms = 0;
419  if (ngeoms)
420  {
421  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
422  for (i = 0, k = 0; i < nshells; i++)
423  {
424  const sfcgal_geometry_t *shell = sfcgal_solid_shell_n(geom, i);
425  ngeoms = sfcgal_polyhedral_surface_num_polygons(shell);
426 
427  for (j = 0; j < ngeoms; j++)
428  {
429  const sfcgal_geometry_t *g = sfcgal_polyhedral_surface_polygon_n(shell, j);
430  geoms[k] = SFCGAL2LWGEOM(g, 1, srid);
431  k++;
432  }
433  }
434  }
435  LWGEOM *rgeom = (LWGEOM *)lwcollection_construct(POLYHEDRALSURFACETYPE, srid, NULL, ngeoms, geoms);
436  if (ngeoms)
437  FLAGS_SET_SOLID(rgeom->flags, 1);
438  return rgeom;
439  }
440 
441  case SFCGAL_TYPE_TRIANGULATEDSURFACE:
442  {
443  ngeoms = sfcgal_triangulated_surface_num_triangles(geom);
444  LWGEOM **geoms = NULL;
445  if (ngeoms)
446  {
447  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
448  for (i = 0; i < ngeoms; i++)
449  {
450  const sfcgal_geometry_t *g = sfcgal_triangulated_surface_triangle_n(geom, i);
451  geoms[i] = SFCGAL2LWGEOM(g, 0, srid);
452  }
453  }
454  return (LWGEOM *)lwcollection_construct(TINTYPE, srid, NULL, ngeoms, geoms);
455  }
456 
457  default:
458  lwerror("SFCGAL2LWGEOM: Unknown Type");
459  return NULL;
460  }
461 }
LWPOINT * lwpoint_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoint.c:151
LWLINE * lwline_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
LWTRIANGLE * lwtriangle_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwtriangle.c:58
#define TINTYPE
Definition: liblwgeom.h:131
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:235
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:129
#define FLAGS_GET_SOLID(flags)
Definition: liblwgeom.h:185
void * lwalloc(size_t size)
Definition: lwutil.c:227
LWCOLLECTION * lwcollection_construct(uint8_t type, int32_t srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:42
LWPOLY * lwpoly_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwpoly.c:161
LWPOLY * lwpoly_construct(int32_t srid, GBOX *bbox, uint32_t nrings, POINTARRAY **points)
Definition: lwpoly.c:43
#define FLAGS_SET_SOLID(flags, value)
Definition: liblwgeom.h:192
LWLINE * lwline_construct_empty(int32_t srid, char hasz, char hasm)
Definition: lwline.c:55
LWTRIANGLE * lwtriangle_construct(int32_t srid, GBOX *bbox, POINTARRAY *points)
Definition: lwtriangle.c:40
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
static POINTARRAY * ptarray_from_SFCGAL(const sfcgal_geometry_t *geom, int force3D)
static int SFCGAL_type_to_lwgeom_type(sfcgal_geometry_type_t type)
Definition: lwgeom_sfcgal.c:60
LWGEOM * SFCGAL2LWGEOM(const sfcgal_geometry_t *geom, int force3D, int32_t srid)
lwflags_t flags
Definition: liblwgeom.h:476

References LWGEOM::flags, FLAGS_GET_SOLID, FLAGS_SET_SOLID, lwalloc(), lwcollection_construct(), lwerror(), lwline_construct(), lwline_construct_empty(), lwnotice(), lwpoint_construct(), lwpoint_construct_empty(), lwpoly_construct(), lwpoly_construct_empty(), lwrealloc(), lwtriangle_construct(), lwtriangle_construct_empty(), POLYHEDRALSURFACETYPE, ptarray_from_SFCGAL(), SFCGAL_type_to_lwgeom_type(), and TINTYPE.

Referenced by lwgeom_sfcgal_noop().

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