PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ SFCGAL2LWGEOM()

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

Definition at line 295 of file lwgeom_sfcgal.c.

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