PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ SFCGAL2LWGEOM()

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

Definition at line 323 of file lwgeom_sfcgal.c.

324 {
325  uint32_t ngeoms, nshells, nsolids;
326  uint32_t i, j, k;
327  int want3d;
328 
329  assert(geom);
330 
331  want3d = force3D || sfcgal_geometry_is_3d(geom);
332 
333  switch (sfcgal_geometry_type_id(geom))
334  {
335  case SFCGAL_TYPE_POINT: {
336  if (sfcgal_geometry_is_empty(geom))
337  return (LWGEOM *)lwpoint_construct_empty(srid, want3d, 0);
338 
339  POINTARRAY *pa = ptarray_from_SFCGAL(geom, want3d);
340  return (LWGEOM *)lwpoint_construct(srid, NULL, pa);
341  }
342 
343  case SFCGAL_TYPE_LINESTRING: {
344  if (sfcgal_geometry_is_empty(geom))
345  return (LWGEOM *)lwline_construct_empty(srid, want3d, 0);
346 
347  POINTARRAY *pa = ptarray_from_SFCGAL(geom, want3d);
348  return (LWGEOM *)lwline_construct(srid, NULL, pa);
349  }
350 
351  case SFCGAL_TYPE_TRIANGLE: {
352  if (sfcgal_geometry_is_empty(geom))
353  return (LWGEOM *)lwtriangle_construct_empty(srid, want3d, 0);
354 
355  POINTARRAY *pa = ptarray_from_SFCGAL(geom, want3d);
356  return (LWGEOM *)lwtriangle_construct(srid, NULL, pa);
357  }
358 
359  case SFCGAL_TYPE_POLYGON: {
360  if (sfcgal_geometry_is_empty(geom))
361  return (LWGEOM *)lwpoly_construct_empty(srid, want3d, 0);
362 
363  uint32_t nrings = sfcgal_polygon_num_interior_rings(geom) + 1;
364  POINTARRAY **pa = (POINTARRAY **)lwalloc(sizeof(POINTARRAY *) * nrings);
365 
366  pa[0] = ptarray_from_SFCGAL(sfcgal_polygon_exterior_ring(geom), want3d);
367  for (i = 1; i < nrings; i++)
368  pa[i] = ptarray_from_SFCGAL(sfcgal_polygon_interior_ring_n(geom, i - 1), want3d);
369 
370  return (LWGEOM *)lwpoly_construct(srid, NULL, nrings, pa);
371  }
372 
373  case SFCGAL_TYPE_MULTIPOINT:
374  case SFCGAL_TYPE_MULTILINESTRING:
375  case SFCGAL_TYPE_MULTIPOLYGON:
376  case SFCGAL_TYPE_MULTISOLID:
377  case SFCGAL_TYPE_GEOMETRYCOLLECTION: {
378  ngeoms = sfcgal_geometry_collection_num_geometries(geom);
379  LWGEOM **geoms = NULL;
380  if (ngeoms)
381  {
382  nsolids = 0;
383  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
384  for (i = 0; i < ngeoms; i++)
385  {
386  const sfcgal_geometry_t *g = sfcgal_geometry_collection_geometry_n(geom, i);
387  geoms[i] = SFCGAL2LWGEOM(g, 0, srid);
388  if (FLAGS_GET_SOLID(geoms[i]->flags))
389  ++nsolids;
390  }
391  geoms = (LWGEOM **)lwrealloc(geoms, sizeof(LWGEOM *) * ngeoms);
392  }
394  SFCGAL_type_to_lwgeom_type(sfcgal_geometry_type_id(geom)), srid, NULL, ngeoms, geoms);
395  if (ngeoms)
396  {
397  if (ngeoms == nsolids)
398  FLAGS_SET_SOLID(rgeom->flags, 1);
399  else if (nsolids)
400  lwnotice(
401  "SFCGAL2LWGEOM: SOLID in heterogeneous collection will be treated as a POLYHEDRALSURFACETYPE");
402  }
403  return rgeom;
404  }
405 
406 #if 0
407  case SFCGAL_TYPE_CIRCULARSTRING:
408  case SFCGAL_TYPE_COMPOUNDCURVE:
409  case SFCGAL_TYPE_CURVEPOLYGON:
410  case SFCGAL_TYPE_MULTICURVE:
411  case SFCGAL_TYPE_MULTISURFACE:
412  case SFCGAL_TYPE_CURVE:
413  case SFCGAL_TYPE_SURFACE:
414 
415  /* TODO curve types handling */
416 #endif
417 
418  case SFCGAL_TYPE_POLYHEDRALSURFACE: {
419  ngeoms = sfcgal_polyhedral_surface_num_polygons(geom);
420 
421  LWGEOM **geoms = NULL;
422  if (ngeoms)
423  {
424  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
425  for (i = 0; i < ngeoms; i++)
426  {
427  const sfcgal_geometry_t *g = sfcgal_polyhedral_surface_polygon_n(geom, i);
428  geoms[i] = SFCGAL2LWGEOM(g, 0, srid);
429  }
430  }
431  return (LWGEOM *)lwcollection_construct(POLYHEDRALSURFACETYPE, srid, NULL, ngeoms, geoms);
432  }
433 
434  /* Solid is map as a closed PolyhedralSurface (for now) */
435  case SFCGAL_TYPE_SOLID: {
436  nshells = sfcgal_solid_num_shells(geom);
437 
438  for (ngeoms = 0, i = 0; i < nshells; i++)
439  ngeoms += sfcgal_polyhedral_surface_num_polygons(sfcgal_solid_shell_n(geom, i));
440 
441  LWGEOM **geoms = 0;
442  if (ngeoms)
443  {
444  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
445  for (i = 0, k = 0; i < nshells; i++)
446  {
447  const sfcgal_geometry_t *shell = sfcgal_solid_shell_n(geom, i);
448  ngeoms = sfcgal_polyhedral_surface_num_polygons(shell);
449 
450  for (j = 0; j < ngeoms; j++)
451  {
452  const sfcgal_geometry_t *g = sfcgal_polyhedral_surface_polygon_n(shell, j);
453  geoms[k] = SFCGAL2LWGEOM(g, 1, srid);
454  k++;
455  }
456  }
457  }
458  LWGEOM *rgeom = (LWGEOM *)lwcollection_construct(POLYHEDRALSURFACETYPE, srid, NULL, ngeoms, geoms);
459  if (ngeoms)
460  FLAGS_SET_SOLID(rgeom->flags, 1);
461  return rgeom;
462  }
463 
464  case SFCGAL_TYPE_TRIANGULATEDSURFACE: {
465  ngeoms = sfcgal_triangulated_surface_num_triangles(geom);
466  LWGEOM **geoms = NULL;
467  if (ngeoms)
468  {
469  geoms = (LWGEOM **)lwalloc(sizeof(LWGEOM *) * ngeoms);
470  for (i = 0; i < ngeoms; i++)
471  {
472  const sfcgal_geometry_t *g = sfcgal_triangulated_surface_triangle_n(geom, i);
473  geoms[i] = SFCGAL2LWGEOM(g, 0, srid);
474  }
475  }
476  return (LWGEOM *)lwcollection_construct(TINTYPE, srid, NULL, ngeoms, geoms);
477  }
478 
479  default:
480  lwerror("SFCGAL2LWGEOM: Unknown Type");
481  return NULL;
482  }
483 }
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:116
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:242
LWPOINT * lwpoint_construct(int32_t srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
#define POLYHEDRALSURFACETYPE
Definition: liblwgeom.h:114
#define FLAGS_GET_SOLID(flags)
Definition: liblwgeom.h:170
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:177
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 lwnotice(const char *fmt,...) __attribute__((format(printf
Write a notice out to the notice handler.
void void lwerror(const char *fmt,...) __attribute__((format(printf
Write a notice out to the error handler.
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:73
LWGEOM * SFCGAL2LWGEOM(const sfcgal_geometry_t *geom, int force3D, int32_t srid)
lwflags_t flags
Definition: liblwgeom.h:461

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: