PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ LWGEOM2GEOS()

GEOSGeometry* LWGEOM2GEOS ( const LWGEOM g,
uint8_t  autofix 
)

Definition at line 432 of file liblwgeom/lwgeom_geos.c.

433 {
434  GEOSCoordSeq sq;
435  GEOSGeom g, shell;
436  GEOSGeom* geoms = NULL;
437  uint32_t ngeoms, i, j;
438  int is_empty = LW_FALSE;
439 #if LWDEBUG_LEVEL >= 4
440  char* wkt;
441 #endif
442 
443  if (autofix)
444  {
445  /* cross fingers and try without autofix, maybe it'll work? */
446  g = LWGEOM2GEOS(lwgeom, LW_FALSE);
447  if (g) return g;
448  }
449 
450  LWDEBUGF(4, "LWGEOM2GEOS got a %s", lwtype_name(lwgeom->type));
451 
452  if (lwgeom_type_arc(lwgeom))
453  {
454  LWGEOM* lwgeom_stroked = lwgeom_stroke(lwgeom, 32);
455  GEOSGeometry* g = LWGEOM2GEOS(lwgeom_stroked, autofix);
456  lwgeom_free(lwgeom_stroked);
457  return g;
458  }
459 
460  is_empty = lwgeom_is_empty(lwgeom);
461 
462  switch (lwgeom->type)
463  {
464  case POINTTYPE:
465  {
466  if (is_empty)
467  g = GEOSGeom_createEmptyPoint();
468  else
469  {
470  LWPOINT* lwp = (LWPOINT*)lwgeom;
471  #if POSTGIS_GEOS_VERSION < 30800
472  sq = ptarray_to_GEOSCoordSeq(lwp->point, 0);
473  g = GEOSGeom_createPoint(sq);
474  #else
475  if (lwgeom_has_z(lwgeom))
476  {
477  sq = ptarray_to_GEOSCoordSeq(lwp->point, 0);
478  g = GEOSGeom_createPoint(sq);
479  }
480  else
481  {
482  const POINT2D* p = getPoint2d_cp(lwp->point, 0);
483  g = GEOSGeom_createPointFromXY(p->x, p->y);
484  }
485  #endif
486  }
487  if (!g) return NULL;
488  break;
489  }
490 
491  case LINETYPE:
492  {
493  if (is_empty)
494  g = GEOSGeom_createEmptyLineString();
495  else
496  {
497  LWLINE* lwl = (LWLINE*)lwgeom;
498  /* TODO: if (autofix) */
499  if (lwl->points->npoints == 1)
500  {
501  /* Duplicate point, to make geos-friendly */
502  lwl->points = ptarray_addPoint(lwl->points,
503  getPoint_internal(lwl->points, 0),
504  FLAGS_NDIMS(lwl->points->flags),
505  lwl->points->npoints);
506  }
507  sq = ptarray_to_GEOSCoordSeq(lwl->points, 0);
508  g = GEOSGeom_createLineString(sq);
509  }
510  if (!g) return NULL;
511  break;
512  }
513 
514  case POLYGONTYPE:
515  {
516  LWPOLY* lwpoly = (LWPOLY*)lwgeom;
517  if (is_empty)
518  g = GEOSGeom_createEmptyPolygon();
519  else
520  {
521  shell = ptarray_to_GEOSLinearRing(lwpoly->rings[0], autofix);
522  if (!shell) return NULL;
523  ngeoms = lwpoly->nrings - 1;
524  if (ngeoms > 0) geoms = lwalloc(sizeof(GEOSGeom) * ngeoms);
525 
526  for (i = 1; i < lwpoly->nrings; i++)
527  {
528  geoms[i - 1] = ptarray_to_GEOSLinearRing(lwpoly->rings[i], autofix);
529  if (!geoms[i - 1])
530  {
531  uint32_t k;
532  for (k = 0; k < i - 1; k++)
533  GEOSGeom_destroy(geoms[k]);
534  lwfree(geoms);
535  GEOSGeom_destroy(shell);
536  return NULL;
537  }
538  }
539  g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
540  if (geoms) lwfree(geoms);
541  }
542  if (!g) return NULL;
543  break;
544  }
545 
546  case TRIANGLETYPE:
547  {
548  if (is_empty)
549  g = GEOSGeom_createEmptyPolygon();
550  else
551  {
552  LWTRIANGLE *lwt = (LWTRIANGLE *)lwgeom;
553  shell = ptarray_to_GEOSLinearRing(lwt->points, autofix);
554  if (!shell) return NULL;
555  g = GEOSGeom_createPolygon(shell, NULL, 0);
556  }
557  if (!g) return NULL;
558  break;
559  }
560  case MULTIPOINTTYPE:
561  case MULTILINETYPE:
562  case MULTIPOLYGONTYPE:
563  case TINTYPE:
564  case COLLECTIONTYPE:
565  {
566  int geostype;
567  if (lwgeom->type == MULTIPOINTTYPE)
568  geostype = GEOS_MULTIPOINT;
569  else if (lwgeom->type == MULTILINETYPE)
570  geostype = GEOS_MULTILINESTRING;
571  else if (lwgeom->type == MULTIPOLYGONTYPE)
572  geostype = GEOS_MULTIPOLYGON;
573  else
574  geostype = GEOS_GEOMETRYCOLLECTION;
575 
576  LWCOLLECTION* lwc = (LWCOLLECTION*)lwgeom;
577 
578  ngeoms = lwc->ngeoms;
579  if (ngeoms > 0) geoms = lwalloc(sizeof(GEOSGeom) * ngeoms);
580 
581  j = 0;
582  for (i = 0; i < ngeoms; ++i)
583  {
584  GEOSGeometry* g;
585 
586  /* if (lwgeom_is_empty(lwc->geoms[i])) continue; */
587 
588  g = LWGEOM2GEOS(lwc->geoms[i], 0);
589  if (!g)
590  {
591  uint32_t k;
592  for (k = 0; k < j; k++)
593  GEOSGeom_destroy(geoms[k]);
594  lwfree(geoms);
595  return NULL;
596  }
597  geoms[j++] = g;
598  }
599  g = GEOSGeom_createCollection(geostype, geoms, j);
600  if (ngeoms > 0) lwfree(geoms);
601  if (!g) return NULL;
602  break;
603  }
604 
605  default:
606  {
607  lwerror("Unknown geometry type: %d - %s", lwgeom->type, lwtype_name(lwgeom->type));
608  return NULL;
609  }
610  }
611 
612  GEOSSetSRID(g, lwgeom->srid);
613 
614 #if LWDEBUG_LEVEL >= 4
615  wkt = GEOSGeomToWKT(g);
616  LWDEBUGF(4, "LWGEOM2GEOS: GEOSGeom: %s", wkt);
617  GEOSFree(wkt);
618 #endif
619 
620  return g;
621 }
static GEOSGeometry * ptarray_to_GEOSLinearRing(const POINTARRAY *pa, uint8_t autofix)
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, uint8_t autofix)
GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *, uint8_t fix_ring)
#define LW_FALSE
Definition: liblwgeom.h:94
#define COLLECTIONTYPE
Definition: liblwgeom.h:108
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1155
#define MULTILINETYPE
Definition: liblwgeom.h:106
#define LINETYPE
Definition: liblwgeom.h:103
POINTARRAY * ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
Add a point in a pointarray.
Definition: ptarray.c:522
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Convert type with arcs into equivalent linearized type.
Definition: lwstroke.c:871
#define MULTIPOINTTYPE
Definition: liblwgeom.h:105
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:934
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:102
int lwgeom_type_arc(const LWGEOM *geom)
Geometry type is one of the potentially "arc containing" types (circstring, multicurve,...
Definition: lwstroke.c:89
#define TINTYPE
Definition: liblwgeom.h:116
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:107
void lwfree(void *mem)
Definition: lwutil.c:242
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:179
#define POLYGONTYPE
Definition: liblwgeom.h:104
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
#define TRIANGLETYPE
Definition: liblwgeom.h:115
void * lwalloc(size_t size)
Definition: lwutil.c:227
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:190
static const POINT2D * getPoint2d_cp(const POINTARRAY *pa, uint32_t n)
Returns a POINT2D pointer into the POINTARRAY serialized_ptlist, suitable for reading from.
Definition: lwinline.h:101
static uint8_t * getPoint_internal(const POINTARRAY *pa, uint32_t n)
Definition: lwinline.h:77
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
uint32_t ngeoms
Definition: liblwgeom.h:580
LWGEOM ** geoms
Definition: liblwgeom.h:575
POINTARRAY * points
Definition: liblwgeom.h:483
POINTARRAY * point
Definition: liblwgeom.h:471
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
POINTARRAY * points
Definition: liblwgeom.h:495
double y
Definition: liblwgeom.h:390
double x
Definition: liblwgeom.h:390
lwflags_t flags
Definition: liblwgeom.h:431
uint32_t npoints
Definition: liblwgeom.h:427

References COLLECTIONTYPE, POINTARRAY::flags, FLAGS_NDIMS, LWCOLLECTION::geoms, getPoint2d_cp(), getPoint_internal(), LINETYPE, LW_FALSE, lwalloc(), LWDEBUGF, lwerror(), lwfree(), lwgeom_free(), lwgeom_has_z(), lwgeom_is_empty(), lwgeom_stroke(), lwgeom_type_arc(), lwtype_name(), MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, LWPOINT::point, LWLINE::points, LWTRIANGLE::points, POINTTYPE, POLYGONTYPE, ptarray_addPoint(), ptarray_to_GEOSCoordSeq(), ptarray_to_GEOSLinearRing(), LWPOLY::rings, LWGEOM::srid, TINTYPE, TRIANGLETYPE, LWGEOM::type, POINT2D::x, and POINT2D::y.

Referenced by _lwt_CheckEdgeCrossing(), _lwt_FindFaceContainingRing(), _lwt_GetEqualEdge(), isvalid(), lwgeom_buildarea(), lwgeom_centroid(), lwgeom_clip_by_rect(), lwgeom_concavehull(), lwgeom_delaunay_triangulation(), lwgeom_difference_prec(), lwgeom_extract_unique_endpoints(), lwgeom_geos_noop(), lwgeom_intersection_prec(), lwgeom_is_simple(), lwgeom_linemerge_directed(), lwgeom_make_valid_params(), lwgeom_node(), lwgeom_normalize(), lwgeom_pointonsurface(), lwgeom_reduceprecision(), lwgeom_sharedpaths(), lwgeom_simplify_polygonal(), lwgeom_snap(), lwgeom_symdifference_prec(), lwgeom_triangulate_polygon(), lwgeom_unaryunion_prec(), lwgeom_union_prec(), LWGEOMARRAY2GEOS(), lwline_offsetcurve(), lwline_split_by_line(), lwpoly_split_by_line(), lwpoly_to_points(), lwt_AddPolygon(), POSTGIS2GEOS(), PrepGeomCacheBuilder(), read_geos_from_partition(), rt_raster_compute_skewed_raster(), rt_raster_geos_spatial_relationship(), rt_raster_intersects(), rt_raster_surface(), and topologypreservesimplify().

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