PostGIS  2.2.7dev-r@@SVN_REVISION@@
LWGEOM* SFCGAL2LWGEOM ( const sfcgal_geometry_t *  geom,
int  force3D,
int  srid 
)

Definition at line 267 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().

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