char *msudf_centroid(UDF_INIT *initid,UDF_ARGS *args, char *buf,
	unsigned long *length, char *is_null, char *error)
{
	char *result;
	GEOSGeom geom1,geom2;

	DEBUG("msudf_centroid");

	geom1 = msudf_getGeometry((unsigned char *)args->args[0],args->lengths[0]);	
	if (geom1 == NULL) {
		strcpy(error,"Invalid geometry.");
		*is_null = 1;
		return NULL;
	}
	
	geom2 = GEOSGetCentroid(geom1);

	if (geom2 != NULL) {
		GEOSSetSRID(geom2,GEOSGetSRID(geom1));
		result = msudf_returnGeometry(initid,length,geom2);
		GEOSGeom_destroy(geom1);
		GEOSGeom_destroy(geom2);
		return result;
	} else {
		GEOSGeom_destroy(geom1);
		*is_null = 1;
		return NULL;
	}
}
Example #2
0
pointObj *msGEOSGetCentroid(shapeObj *shape)
{
#ifdef USE_GEOS
  GEOSGeom g1, g2;
  GEOSCoordSeq coords;
  pointObj *point;

  if(!shape) return NULL;

  if(!shape->geometry) /* if no geometry for the shape then build one */
    shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape);
  g1 = (GEOSGeom) shape->geometry;
  if(!g1) return NULL;

  g2 = GEOSGetCentroid(g1);

  point = (pointObj *) malloc(sizeof(pointObj));

  coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq(g2);

  GEOSCoordSeq_getX(coords, 0, &(point->x));
  GEOSCoordSeq_getY(coords, 0, &(point->y));
  /* GEOSCoordSeq_getZ(coords, 0, &(point->z)); */

  GEOSCoordSeq_destroy(coords);

  return point;
#else
  msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSGetCentroid()");
  return NULL;
#endif
}
    void object::test<2>()
    {
        geom1_ = GEOSGeomFromWKT("LINESTRING(0 0, 10 0)");

        ensure( 0 != geom1_ );

        geom2_ = GEOSGetCentroid(geom1_);

        ensure( 0 != geom2_ );

        wkt_ = GEOSWKTWriter_write(wktw_, geom2_);

        ensure_equals(std::string(wkt_), std::string( "POINT (5 0)"));

    }
    void object::test<3>()
    {
        geom1_ = GEOSGeomFromWKT("POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))");

        ensure( 0 != geom1_ );

        geom2_ = GEOSGetCentroid(geom1_);

        ensure( 0 != geom2_ );

        wkt_ = GEOSWKTWriter_write(wktw_, geom2_);

        ensure_equals(std::string(wkt_), std::string( "POINT (5 5)"));

    }
Example #5
0
    void object::test<1>()
    {
        geom1_ = GEOSGeomFromWKT("POINT(10 0)");

        ensure( nullptr != geom1_ );

        geom2_ = GEOSGetCentroid(geom1_);

        ensure( nullptr != geom2_ );

        wkt_ = GEOSWKTWriter_write(wktw_, geom2_);

        ensure_equals(std::string(wkt_), std::string( "POINT (10 0)"));

    }
    // Tiny triangle, see http://trac.osgeo.org/geos/ticket/559
    template<>
    template<>
    void object::test<4>()
    {
        geom1_ = GEOSGeomFromWKT(
"POLYGON(( \
56.528666666700 25.2101666667, \
56.529000000000 25.2105000000, \
56.528833333300 25.2103333333, \
56.528666666700 25.2101666667))");

        ensure( 0 != geom1_ );

        geom2_ = GEOSGetCentroid(geom1_);

        ensure( 0 != geom2_ );

        wkt_ = GEOSWKTWriter_write(wktw_, geom2_);

        ensure_equals(std::string(wkt_), std::string( "POINT (56.528833 25.210333)" ) );

    }

    // Empty geometry -- see http://trac.osgeo.org/geos/ticket/560
    template<>
    template<>
    void object::test<5>()
    {
        geom1_ = GEOSGeomFromWKT("LINESTRING EMPTY");