PostGIS  2.2.7dev-r@@SVN_REVISION@@
GEOSGeometry* LWGEOM2GEOS ( const LWGEOM g,
int  autofix 
)

Definition at line 322 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_has_m(), lwgeom_has_z(), 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_construct_empty(), 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_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(), 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().

323 {
324  GEOSCoordSeq sq;
325  GEOSGeom g, shell;
326  GEOSGeom *geoms = NULL;
327  /*
328  LWGEOM *tmp;
329  */
330  uint32_t ngeoms, i, j;
331  int geostype;
332 #if LWDEBUG_LEVEL >= 4
333  char *wkt;
334 #endif
335 
336  LWDEBUGF(4, "LWGEOM2GEOS got a %s", lwtype_name(lwgeom->type));
337 
338  if (lwgeom_has_arc(lwgeom))
339  {
340  LWGEOM *lwgeom_stroked = lwgeom_stroke(lwgeom, 32);
341  GEOSGeometry *g = LWGEOM2GEOS(lwgeom_stroked, autofix);
342  lwgeom_free(lwgeom_stroked);
343  return g;
344  }
345 
346  switch (lwgeom->type)
347  {
348  LWPOINT *lwp = NULL;
349  LWPOLY *lwpoly = NULL;
350  LWLINE *lwl = NULL;
351  LWCOLLECTION *lwc = NULL;
352 #if POSTGIS_GEOS_VERSION < 33
353  POINTARRAY *pa = NULL;
354 #endif
355 
356  case POINTTYPE:
357  lwp = (LWPOINT *)lwgeom;
358 
359  if ( lwgeom_is_empty(lwgeom) )
360  {
361 #if POSTGIS_GEOS_VERSION < 33
362  pa = ptarray_construct_empty(lwgeom_has_z(lwgeom), lwgeom_has_m(lwgeom), 2);
363  sq = ptarray_to_GEOSCoordSeq(pa);
364  shell = GEOSGeom_createLinearRing(sq);
365  g = GEOSGeom_createPolygon(shell, NULL, 0);
366 #else
367  g = GEOSGeom_createEmptyPolygon();
368 #endif
369  }
370  else
371  {
372  sq = ptarray_to_GEOSCoordSeq(lwp->point);
373  g = GEOSGeom_createPoint(sq);
374  }
375  if ( ! g )
376  {
377  /* lwnotice("Exception in LWGEOM2GEOS"); */
378  return NULL;
379  }
380  break;
381  case LINETYPE:
382  lwl = (LWLINE *)lwgeom;
383  /* TODO: if (autofix) */
384  if ( lwl->points->npoints == 1 ) {
385  /* Duplicate point, to make geos-friendly */
386  lwl->points = ptarray_addPoint(lwl->points,
387  getPoint_internal(lwl->points, 0),
388  FLAGS_NDIMS(lwl->points->flags),
389  lwl->points->npoints);
390  }
391  sq = ptarray_to_GEOSCoordSeq(lwl->points);
392  g = GEOSGeom_createLineString(sq);
393  if ( ! g )
394  {
395  /* lwnotice("Exception in LWGEOM2GEOS"); */
396  return NULL;
397  }
398  break;
399 
400  case POLYGONTYPE:
401  lwpoly = (LWPOLY *)lwgeom;
402  if ( lwgeom_is_empty(lwgeom) )
403  {
404 #if POSTGIS_GEOS_VERSION < 33
405  POINTARRAY *pa = ptarray_construct_empty(lwgeom_has_z(lwgeom), lwgeom_has_m(lwgeom), 2);
406  sq = ptarray_to_GEOSCoordSeq(pa);
407  shell = GEOSGeom_createLinearRing(sq);
408  g = GEOSGeom_createPolygon(shell, NULL, 0);
409 #else
410  g = GEOSGeom_createEmptyPolygon();
411 #endif
412  }
413  else
414  {
415  shell = ptarray_to_GEOSLinearRing(lwpoly->rings[0], autofix);
416  if ( ! shell ) return NULL;
417  /*lwerror("LWGEOM2GEOS: exception during polygon shell conversion"); */
418  ngeoms = lwpoly->nrings-1;
419  if ( ngeoms > 0 )
420  geoms = malloc(sizeof(GEOSGeom)*ngeoms);
421 
422  for (i=1; i<lwpoly->nrings; ++i)
423  {
424  geoms[i-1] = ptarray_to_GEOSLinearRing(lwpoly->rings[i], autofix);
425  if ( ! geoms[i-1] )
426  {
427  --i;
428  while (i) GEOSGeom_destroy(geoms[--i]);
429  free(geoms);
430  GEOSGeom_destroy(shell);
431  return NULL;
432  }
433  /*lwerror("LWGEOM2GEOS: exception during polygon hole conversion"); */
434  }
435  g = GEOSGeom_createPolygon(shell, geoms, ngeoms);
436  if (geoms) free(geoms);
437  }
438  if ( ! g ) return NULL;
439  break;
440  case MULTIPOINTTYPE:
441  case MULTILINETYPE:
442  case MULTIPOLYGONTYPE:
443  case COLLECTIONTYPE:
444  if ( lwgeom->type == MULTIPOINTTYPE )
445  geostype = GEOS_MULTIPOINT;
446  else if ( lwgeom->type == MULTILINETYPE )
447  geostype = GEOS_MULTILINESTRING;
448  else if ( lwgeom->type == MULTIPOLYGONTYPE )
449  geostype = GEOS_MULTIPOLYGON;
450  else
451  geostype = GEOS_GEOMETRYCOLLECTION;
452 
453  lwc = (LWCOLLECTION *)lwgeom;
454 
455  ngeoms = lwc->ngeoms;
456  if ( ngeoms > 0 )
457  geoms = malloc(sizeof(GEOSGeom)*ngeoms);
458 
459  j = 0;
460  for (i=0; i<ngeoms; ++i)
461  {
462  GEOSGeometry* g;
463 
464  if( lwgeom_is_empty(lwc->geoms[i]) )
465  continue;
466 
467  g = LWGEOM2GEOS(lwc->geoms[i], 0);
468  if ( ! g )
469  {
470  while (j) GEOSGeom_destroy(geoms[--j]);
471  free(geoms);
472  return NULL;
473  }
474  geoms[j++] = g;
475  }
476  g = GEOSGeom_createCollection(geostype, geoms, j);
477  if ( geoms ) free(geoms);
478  if ( ! g ) return NULL;
479  break;
480 
481  default:
482  lwerror("Unknown geometry type: %d - %s", lwgeom->type, lwtype_name(lwgeom->type));
483  return NULL;
484  }
485 
486  GEOSSetSRID(g, lwgeom->srid);
487 
488 #if LWDEBUG_LEVEL >= 4
489  wkt = GEOSGeomToWKT(g);
490  LWDEBUGF(4, "LWGEOM2GEOS: GEOSGeom: %s", wkt);
491  free(wkt);
492 #endif
493 
494  return g;
495 }
#define LINETYPE
Definition: liblwgeom.h:71
LWGEOM * lwgeom_stroke(const LWGEOM *geom, uint32_t perQuad)
Definition: lwstroke.c:446
int npoints
Definition: liblwgeom.h:355
#define POLYGONTYPE
Definition: liblwgeom.h:72
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1050
#define MULTIPOINTTYPE
Definition: liblwgeom.h:73
POINTARRAY * point
Definition: liblwgeom.h:395
int lwgeom_has_z(const LWGEOM *geom)
Return LW_TRUE if geometry has Z ordinates.
Definition: lwgeom.c:836
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:188
uint8_t flags
Definition: liblwgeom.h:353
LWGEOM ** geoms
Definition: liblwgeom.h:493
uint8_t * getPoint_internal(const POINTARRAY *pa, int n)
Definition: ptarray.c:1706
POINTARRAY ** rings
Definition: liblwgeom.h:441
int nrings
Definition: liblwgeom.h:439
int lwgeom_has_arc(const LWGEOM *geom)
Definition: lwstroke.c:41
GEOSGeometry * LWGEOM2GEOS(const LWGEOM *lwgeom, int autofix)
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:75
static GEOSGeometry * ptarray_to_GEOSLinearRing(const POINTARRAY *pa, int autofix)
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:70
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:1297
#define MULTILINETYPE
Definition: liblwgeom.h:74
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
#define FLAGS_NDIMS(flags)
Definition: liblwgeom.h:136
int lwgeom_has_m(const LWGEOM *geom)
Return LW_TRUE if geometry has M ordinates.
Definition: lwgeom.c:843
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
#define COLLECTIONTYPE
Definition: liblwgeom.h:76
POINTARRAY * points
Definition: liblwgeom.h:406

Here is the call graph for this function:

Here is the caller graph for this function: