PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ SFCGAL2LWGEOM()

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

Definition at line 278 of file liblwgeom/lwgeom_sfcgal.c.

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(), SFCGAL2LWGEOM(), SFCGAL_type_to_lwgeom_type(), and TINTYPE.

Referenced by lwgeom_sfcgal_noop(), SFCGAL2LWGEOM(), and SFCGALGeometry2POSTGIS().

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