PostGIS  2.3.8dev-r@@SVN_REVISION@@

◆ LWGEOM2GEOS()

GEOSGeometry* LWGEOM2GEOS ( const LWGEOM g,
int  autofix 
)

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

References COLLECTIONTYPE, POINTARRAY::flags, FLAGS_NDIMS, free(), LWCOLLECTION::geoms, getPoint_internal(), LINETYPE, LWDEBUGF, lwerror(), LWGEOM2GEOS(), lwgeom_free(), lwgeom_has_arc(), lwgeom_is_empty(), lwgeom_stroke(), lwtype_name(), malloc(), MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, LWCOLLECTION::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, LWPOINT::point, LWLINE::points, POINTTYPE, POLYGONTYPE, ptarray_addPoint(), ptarray_to_GEOSCoordSeq(), ptarray_to_GEOSLinearRing(), LWPOLY::rings, LWGEOM::srid, and LWGEOM::type.

Referenced by _lwt_AddFaceSplit(), _lwt_CheckEdgeCrossing(), _lwt_EdgeMotionArea(), _lwt_GetEqualEdge(), boundary(), isvalid(), LWGEOM2GEOS(), lwgeom_buildarea(), lwgeom_centroid(), lwgeom_clip_by_rect(), lwgeom_delaunay_triangulation(), lwgeom_difference(), lwgeom_extract_unique_endpoints(), lwgeom_geos_noop(), lwgeom_intersection(), lwgeom_is_simple(), lwgeom_linemerge(), lwgeom_make_valid(), lwgeom_node(), lwgeom_normalize(), lwgeom_offsetcurve(), lwgeom_sharedpaths(), lwgeom_snap(), lwgeom_symdifference(), lwgeom_unaryunion(), lwgeom_union(), LWGEOMARRAY2GEOS(), lwline_split_by_line(), lwpoly_split_by_line(), lwpoly_to_points(), lwt_AddPoint(), lwt_AddPolygon(), lwt_ChangeEdgeGeom(), POSTGIS2GEOS(), PrepGeomCacheBuilder(), rt_raster_compute_skewed_raster(), rt_raster_gdal_polygonize(), rt_raster_gdal_rasterize(), rt_raster_geos_spatial_relationship(), rt_raster_intersects(), and rt_raster_surface().

328 {
329  GEOSCoordSeq sq;
330  GEOSGeom g, shell;
331  GEOSGeom *geoms = NULL;
332  /*
333  LWGEOM *tmp;
334  */
335  uint32_t ngeoms, i, j;
336  int geostype;
337 #if LWDEBUG_LEVEL >= 4
338  char *wkt;
339 #endif
340 
341  LWDEBUGF(4, "LWGEOM2GEOS got a %s", lwtype_name(lwgeom->type));
342 
343  if (lwgeom_has_arc(lwgeom))
344  {
345  LWGEOM *lwgeom_stroked = lwgeom_stroke(lwgeom, 32);
346  GEOSGeometry *g = LWGEOM2GEOS(lwgeom_stroked, autofix);
347  lwgeom_free(lwgeom_stroked);
348  return g;
349  }
350 
351  switch (lwgeom->type)
352  {
353  LWPOINT *lwp = NULL;
354  LWPOLY *lwpoly = NULL;
355  LWLINE *lwl = NULL;
356  LWCOLLECTION *lwc = NULL;
357 
358  case POINTTYPE:
359  lwp = (LWPOINT *)lwgeom;
360 
361  if ( lwgeom_is_empty(lwgeom) )
362  {
363  g = GEOSGeom_createEmptyPolygon();
364  }
365  else
366  {
367  sq = ptarray_to_GEOSCoordSeq(lwp->point);
368  g = GEOSGeom_createPoint(sq);
369  }
370  if ( ! g )
371  {
372  /* lwnotice("Exception in LWGEOM2GEOS"); */
373  return NULL;
374  }
375  break;
376  case LINETYPE:
377  lwl = (LWLINE *)lwgeom;
378  /* TODO: if (autofix) */
379  if ( lwl->points->npoints == 1 ) {
380  /* Duplicate point, to make geos-friendly */
381  lwl->points = ptarray_addPoint(lwl->points,
382  getPoint_internal(lwl->points, 0),
383  FLAGS_NDIMS(lwl->points->flags),
384  lwl->points->npoints);
385  }
386  sq = ptarray_to_GEOSCoordSeq(lwl->points);
387  g = GEOSGeom_createLineString(sq);
388  if ( ! g )
389  {
390  /* lwnotice("Exception in LWGEOM2GEOS"); */
391  return NULL;
392  }
393  break;
394 
395  case POLYGONTYPE:
396  lwpoly = (LWPOLY *)lwgeom;
397  if ( lwgeom_is_empty(lwgeom) )
398  {
399  g = GEOSGeom_createEmptyPolygon();
400  }
401  else
402  {
403  shell = ptarray_to_GEOSLinearRing(lwpoly->rings[0], autofix);
404  if ( ! shell ) return NULL;
405  /*lwerror("LWGEOM2GEOS: exception during polygon shell conversion"); */
406  ngeoms = lwpoly->nrings-1;
407  if ( ngeoms > 0 )
408  geoms = malloc(sizeof(GEOSGeom)*ngeoms);
409 
410  for (i=1; i<lwpoly->nrings; ++i)
411  {
412  geoms[i-1] = ptarray_to_GEOSLinearRing(lwpoly->rings[i], autofix);
413  if ( ! geoms[i-1] )
414  {
415  --i;
416  while (i) GEOSGeom_destroy(geoms[--i]);
417  free(geoms);
418  GEOSGeom_destroy(shell);
419  return NULL;
420  }
421  /*lwerror("LWGEOM2GEOS: exception during polygon hole conversion"); */
422  }
423  g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
424  if (geoms) free(geoms);
425  }
426  if ( ! g ) return NULL;
427  break;
428  case MULTIPOINTTYPE:
429  case MULTILINETYPE:
430  case MULTIPOLYGONTYPE:
431  case COLLECTIONTYPE:
432  if ( lwgeom->type == MULTIPOINTTYPE )
433  geostype = GEOS_MULTIPOINT;
434  else if ( lwgeom->type == MULTILINETYPE )
435  geostype = GEOS_MULTILINESTRING;
436  else if ( lwgeom->type == MULTIPOLYGONTYPE )
437  geostype = GEOS_MULTIPOLYGON;
438  else
439  geostype = GEOS_GEOMETRYCOLLECTION;
440 
441  lwc = (LWCOLLECTION *)lwgeom;
442 
443  ngeoms = lwc->ngeoms;
444  if ( ngeoms > 0 )
445  geoms = malloc(sizeof(GEOSGeom)*ngeoms);
446 
447  j = 0;
448  for (i=0; i<ngeoms; ++i)
449  {
450  GEOSGeometry* g;
451 
452  if( lwgeom_is_empty(lwc->geoms[i]) )
453  continue;
454 
455  g = LWGEOM2GEOS(lwc->geoms[i], 0);
456  if ( ! g )
457  {
458  while (j) GEOSGeom_destroy(geoms[--j]);
459  free(geoms);
460  return NULL;
461  }
462  geoms[j++] = g;
463  }
464  g = GEOSGeom_createCollection(geostype, geoms, j);
465  if ( geoms ) free(geoms);
466  if ( ! g ) return NULL;
467  break;
468 
469  default:
470  lwerror("Unknown geometry type: %d - %s", lwgeom->type, lwtype_name(lwgeom->type));
471  return NULL;
472  }
473 
474  GEOSSetSRID(g, lwgeom->srid);
475 
476 #if LWDEBUG_LEVEL >= 4
477  wkt = GEOSGeomToWKT(g);
478  LWDEBUGF(4, "LWGEOM2GEOS: GEOSGeom: %s", wkt);
479  free(wkt);
480 #endif
481 
482  return g;
483 }
#define LINETYPE
Definition: liblwgeom.h:85
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:462
int npoints
Definition: liblwgeom.h:370
#define POLYGONTYPE
Definition: liblwgeom.h:86
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1063
#define MULTIPOINTTYPE
Definition: liblwgeom.h:87
POINTARRAY * point
Definition: liblwgeom.h:410
GEOSCoordSeq ptarray_to_GEOSCoordSeq(const POINTARRAY *)
POINTARRAY * ptarray_addPoint(const POINTARRAY *pa, uint8_t *p, size_t pdims, uint32_t where)
Add a point in a pointarray.
Definition: ptarray.c:509
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
uint8_t flags
Definition: liblwgeom.h:368
LWGEOM ** geoms
Definition: liblwgeom.h:508
uint8_t * getPoint_internal(const POINTARRAY *pa, int n)
Definition: ptarray.c:1713
POINTARRAY ** rings
Definition: liblwgeom.h:456
int nrings
Definition: liblwgeom.h:454
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:55
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:89
static GEOSGeometry * ptarray_to_GEOSLinearRing(const POINTARRAY *pa, int autofix)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:84
void free(void *)
void * malloc(YYSIZE_T)
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1310
#define MULTILINETYPE
Definition: liblwgeom.h:88
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:151
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
#define COLLECTIONTYPE
Definition: liblwgeom.h:90
POINTARRAY * points
Definition: liblwgeom.h:421
Here is the call graph for this function:
Here is the caller graph for this function: