Beispiel #1
0
static void _relation_compute(sqlite3_context *context,int argc,sqlite3_value **argv,RelationCompute Func)
{
	if(argc == 2 && sqlite3_value_type(argv[0]) == SQLITE_BLOB &&
		sqlite3_value_type(argv[1]) == SQLITE_BLOB)
	{ 
		GEOSGeometry* geometry1;
		GEOSGeometry* geometry2;
		GEOSGeometry* geo_result;
		unsigned char* wkb;
		size_t size;
		const void* data1 = sqlite3_value_blob(argv[0]);
		size_t data_size1 = sqlite3_value_bytes(argv[0]);

		const void* data2 = sqlite3_value_blob(argv[1]);
		size_t data_size2 = sqlite3_value_bytes(argv[1]);

		_init_geos();
		geometry1 = _geo_from_wkb((const unsigned char*)data1,data_size1);
		geometry2 = _geo_from_wkb((const unsigned char*)data2,data_size2);
		if(geometry1 != 0 && geometry2 != 0)
		{
			geo_result = Func(geometry1,geometry2);
			if(geo_result != 0)
			{
				wkb = GEOSGeomToWKB_buf(geo_result,&size);
				sqlite3_result_blob(context,wkb,size,SQLITE_TRANSIENT);
				GEOSGeom_destroy(geo_result);
				GEOSFree(wkb);
			}
		}
		if(geometry1!=0)GEOSGeom_destroy(geometry1);
		if(geometry2!=0)GEOSGeom_destroy(geometry2);
		finishGEOS();
	}
}
Beispiel #2
0
void geo_polyline_decode(sqlite3_context *context,int argc,sqlite3_value **argv)
{
	if(argc >= 1 && sqlite3_value_type(argv[0]) == SQLITE_TEXT)
	{ 
		GEOSGeometry* geometry;
		const unsigned char* data = sqlite3_value_text(argv[0]);
		int point = 1;
		size_t size = 0;

		if(argc > 1)
		{
			point = sqlite3_value_int(argv[1]);
		}

		_init_geos();
		geometry = polyline_decode(data,point);
		if(geometry != 0)
		{
			unsigned char* wkb = GEOSGeomToWKB_buf(geometry,&size);
			if(wkb != NULL)
			{
				sqlite3_result_blob(context,wkb,size,SQLITE_TRANSIENT);
				GEOSFree(wkb);
			}
		}
		GEOSGeom_destroy(geometry);
		finishGEOS();
	}
}
Beispiel #3
0
void geo_simplify(sqlite3_context *context,int argc,sqlite3_value **argv)
{
	if(argc == 2 && sqlite3_value_type(argv[0]) == SQLITE_BLOB)
	{ 
		GEOSGeometry* geometry;
		GEOSGeometry* simplify_geo;
		unsigned char* wkb;
		size_t size;
		const void* data = sqlite3_value_blob(argv[0]);
		size_t data_size = sqlite3_value_bytes(argv[0]);

		double tolerance = sqlite3_value_double(argv[1]);

		_init_geos();
		geometry = _geo_from_wkb((const unsigned char*)data,data_size);
		if(geometry != 0)
		{
			simplify_geo = GEOSSimplify(geometry,tolerance);
			if(simplify_geo != 0)
			{
				wkb = GEOSGeomToWKB_buf(simplify_geo,&size);
				sqlite3_result_blob(context,wkb,size,SQLITE_TRANSIENT);
				GEOSGeom_destroy(simplify_geo);
				GEOSFree(wkb);
			}
		}
		GEOSGeom_destroy(geometry);
		finishGEOS();
	}
}
char *msudf_transform(UDF_INIT *initid, UDF_ARGS *args, char *buf,
	unsigned long *length, char *is_null, char *error)
{
	unsigned char *wkb = NULL;
	size_t wkbsize;
	GEOSGeom g1,g2 =NULL;
	
	msudf_params *params;
	int srid_src,srid_dst;


	params = (msudf_params *) initid->ptr;
	

	DEBUG("msudf_transform");
	wkb = (unsigned char *) (args->args[0]);
	g1 = GEOSGeomFromWKB_buf(wkb + 4,args->lengths[0] - 4);
	srid_src = msudf_getInt(wkb);
	wkb = NULL;

	DEBUG("srid_src: %d",srid_src);
	if ((params->pj_src == NULL) || (params->srid_src != srid_src)) {
		params->pj_src = pj_init_plus(args->args[1]);
		params->srid_src = srid_src;
	}

	srid_dst = msudf_getInt((unsigned char *) args->args[2]);
	DEBUG("srid_dst: %d",srid_dst);
	if ((params->pj_dst == NULL) || (params->srid_dst != srid_dst)) {
		params->pj_dst = pj_init_plus(args->args[3]);
		params->srid_dst = srid_dst;
	}

	if (params->pj_src != NULL && params->pj_dst != NULL && g1 != NULL) {
		g2 = gu_transformGeom(g1,params->pj_src,params->pj_dst);
		wkb = GEOSGeomToWKB_buf(g2,&wkbsize);
	} else {
		// initid->ptr = NULL;
		strcpy(error,"Invalid geometry.");
	}

	if (g1 != NULL) GEOSGeom_destroy(g1);
	if (g2 != NULL) GEOSGeom_destroy(g2);


	if (wkb != NULL) {
		*length = (long)wkbsize + 4;
		if (params->buf != NULL) free(params->buf);
		params->buf = (char *) malloc(*length);
		memcpy(params->buf,args->args[2],4);
		memcpy((char *)params->buf + 4,wkb,wkbsize);
		GEOSFree((char *)wkb);
		return params->buf;
	} else {
		*is_null = 1;
		return NULL;
	}

}
Beispiel #5
0
void geo_bound(sqlite3_context *context,int argc,sqlite3_value **argv)
{
	if(argc >= 1)
	{
		const unsigned char* ogc;
		unsigned char* ret_geo_buf;
		size_t size;
		double x1,x2,y1,y2;
		GEOSCoordSequence* seq = 0;
		GEOSGeometry* geometry = 0;
		GEOSGeometry* middle_geo = 0;
		
		_init_geos();
		if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_BLOB)
		{
			size = sqlite3_value_bytes(argv[0]);
			ogc = (const unsigned char*)sqlite3_value_blob(argv[0]);
			middle_geo = _geo_from_wkb(ogc,size);
		}
		else if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_TEXT)
		{
			ogc = sqlite3_value_text(argv[0]);
			middle_geo = _geo_from_wkt(ogc);
		}
		else if(argc == 4)
		{
			x1 = sqlite3_value_double(argv[0]);
			y1 = sqlite3_value_double(argv[1]);
			x2 = sqlite3_value_double(argv[2]);
			y2 = sqlite3_value_double(argv[3]);

			seq = GEOSCoordSeq_create(2,2);
			GEOSCoordSeq_setX(seq,0,x1);
			GEOSCoordSeq_setY(seq,0,y1);
			GEOSCoordSeq_setX(seq,1,x2);
			GEOSCoordSeq_setY(seq,1,y2);

			middle_geo = GEOSGeom_createLineString(seq);
		}

		if(middle_geo != 0)
		{
			geometry = GEOSEnvelope(middle_geo);
			if(geometry != 0)
			{
				ret_geo_buf = GEOSGeomToWKB_buf(geometry,&size);
				sqlite3_result_blob(context,ret_geo_buf,size,SQLITE_TRANSIENT);
				GEOSGeom_destroy(geometry);
				GEOSFree(ret_geo_buf);
			}
			GEOSGeom_destroy(middle_geo);
		}

		finishGEOS();
	}
}
char *msudf_returnGeometry(UDF_INIT *initid,unsigned long* length,GEOSGeom geom)
{
	unsigned char *wkb;
	msudf_params *params;
	size_t wkbsize;
	int srid;

	wkb = GEOSGeomToWKB_buf(geom,&wkbsize);
	*length = (long)wkbsize + 4;
	srid = GEOSGetSRID(geom);

	params = (msudf_params *) initid->ptr;
	if (params->buf != NULL) free(params->buf);
	params->buf = (char *) malloc(*length);

	memcpy(params->buf,&srid,4);
	memcpy((char *)params->buf + 4,wkb,wkbsize);
	GEOSFree((char *)wkb);
	return params->buf;
}
Beispiel #7
0
void geo_wkb(sqlite3_context *context,int argc,sqlite3_value **argv)
{
	if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_TEXT)
	{ 
		GEOSGeometry* geometry;
		const char* wkt = (const char*)sqlite3_value_text(argv[0]);
		unsigned char* wkb;
		size_t size;

		_init_geos();
		geometry = _geo_from_wkt(wkt);
		if(geometry != 0)
		{
			wkb = GEOSGeomToWKB_buf(geometry,&size);
			sqlite3_result_blob(context,(const void*)wkb,size,SQLITE_TRANSIENT);

			GEOSFree(wkb);
		}
		GEOSGeom_destroy(geometry);
		finishGEOS();
	}
}
OGRGeometry *
OGRGeometryFactory::createFromGEOS( GEOSGeom geosGeom )

{
#ifndef HAVE_GEOS 

    CPLError( CE_Failure, CPLE_NotSupported, 
              "GEOS support not enabled." );
    return NULL;

#else

    size_t nSize = 0;
    unsigned char *pabyBuf = NULL;
    OGRGeometry *poGeometry = NULL;

    pabyBuf = GEOSGeomToWKB_buf( geosGeom, &nSize );
    if( pabyBuf == NULL || nSize == 0 )
    {
        return NULL;
    }

    if( OGRGeometryFactory::createFromWkb( (unsigned char *) pabyBuf, 
                                           NULL, &poGeometry, (int) nSize )
        != OGRERR_NONE )
    {
        poGeometry = NULL;
    }

    if( pabyBuf != NULL )
    {
        free( pabyBuf );
    }

    return poGeometry;

#endif /* HAVE_GEOS */
}
 geos_wkb_ptr (GEOSGeometry* const geometry)
     : data_ (NULL),
       size_ (0)
 {
     data_ = GEOSGeomToWKB_buf(geometry, &size_);
 }