2003-05-30 12:22:34 +02:00
|
|
|
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
|
|
|
|
|
|
|
|
|
|
/* This file defines all spatial functions */
|
|
|
|
|
2005-05-26 12:09:14 +02:00
|
|
|
#ifdef USE_PRAGMA_IMPLEMENTATION
|
2003-05-30 12:22:34 +02:00
|
|
|
#pragma implementation // gcc: Class implementation
|
|
|
|
#endif
|
|
|
|
|
2005-06-05 19:38:52 +02:00
|
|
|
#include "mysql_priv.h"
|
2004-01-23 16:54:22 +01:00
|
|
|
#ifdef HAVE_SPATIAL
|
2003-05-30 12:22:34 +02:00
|
|
|
#include <m_ctype.h>
|
|
|
|
|
2004-09-22 19:36:53 +02:00
|
|
|
void Item_geometry_func::fix_length_and_dec()
|
|
|
|
{
|
|
|
|
collation.set(&my_charset_bin);
|
|
|
|
decimals=0;
|
|
|
|
max_length=MAX_BLOB_WIDTH;
|
2007-02-21 11:45:19 +01:00
|
|
|
maybe_null= 1;
|
2004-09-22 19:36:53 +02:00
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
String *Item_func_geometry_from_text::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *wkt= args[0]->val_str(&arg_val);
|
2004-03-15 13:32:53 +01:00
|
|
|
|
|
|
|
if ((null_value= args[0]->null_value))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Gis_read_stream trs(wkt->charset(), wkt->ptr(), wkt->length());
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 srid= 0;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
if ((arg_count == 2) && !args[1]->null_value)
|
2003-06-04 05:59:32 +02:00
|
|
|
srid= (uint32)args[1]->val_int();
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
if (str->reserve(SRID_SIZE, 512))
|
|
|
|
return 0;
|
|
|
|
str->length(0);
|
|
|
|
str->q_append(srid);
|
2004-03-15 13:32:53 +01:00
|
|
|
if ((null_value= !Geometry::create_from_wkt(&buffer, &trs, str, 0)))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String *Item_func_geometry_from_wkb::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *wkb= args[0]->val_str(&arg_val);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 srid= 0;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
if ((arg_count == 2) && !args[1]->null_value)
|
2003-06-04 05:59:32 +02:00
|
|
|
srid= (uint32)args[1]->val_int();
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
if (str->reserve(SRID_SIZE, 512))
|
|
|
|
return 0;
|
|
|
|
str->length(0);
|
|
|
|
str->q_append(srid);
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!Geometry::create_from_wkb(&buffer, wkb->ptr(), wkb->length(), str))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-06-20 11:53:37 +02:00
|
|
|
String *Item_func_as_wkt::val_str(String *str)
|
2003-05-30 12:22:34 +02:00
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *swkb= args[0]->val_str(&arg_val);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom= NULL;
|
2004-03-04 07:50:37 +01:00
|
|
|
const char *dummy;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length())))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
str->length(0);
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value= geom->as_wkt(str, &dummy)))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-06-20 11:53:37 +02:00
|
|
|
void Item_func_as_wkt::fix_length_and_dec()
|
2003-05-30 12:22:34 +02:00
|
|
|
{
|
|
|
|
max_length=MAX_BLOB_WIDTH;
|
2007-02-21 11:45:19 +01:00
|
|
|
maybe_null= 1;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
String *Item_func_as_wkb::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *swkb= args[0]->val_str(&arg_val);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(Geometry::construct(&buffer, swkb->ptr(), swkb->length())))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
str->copy(swkb->ptr() + SRID_SIZE, swkb->length() - SRID_SIZE,
|
|
|
|
&my_charset_bin);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
String *Item_func_geometry_type::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(str);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom= NULL;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length())))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
2004-03-04 07:50:37 +01:00
|
|
|
/* String will not move */
|
2004-10-22 17:32:02 +02:00
|
|
|
str->copy(geom->get_class_info()->m_name.str,
|
|
|
|
geom->get_class_info()->m_name.length,
|
|
|
|
default_charset());
|
2003-05-30 12:22:34 +02:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String *Item_func_envelope::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *swkb= args[0]->val_str(&arg_val);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom= NULL;
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 srid;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length()))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
srid= uint4korr(swkb->ptr());
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
str->length(0);
|
|
|
|
if (str->reserve(SRID_SIZE, 512))
|
|
|
|
return 0;
|
|
|
|
str->q_append(srid);
|
2004-03-12 09:04:00 +01:00
|
|
|
return (null_value= geom->envelope(str)) ? 0 : str;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String *Item_func_centroid::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *swkb= args[0]->val_str(&arg_val);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom= NULL;
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 srid;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
if ((null_value= args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length()))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
if (str->reserve(SRID_SIZE, 512))
|
|
|
|
return 0;
|
|
|
|
str->length(0);
|
2004-03-04 07:50:37 +01:00
|
|
|
srid= uint4korr(swkb->ptr());
|
2003-05-30 12:22:34 +02:00
|
|
|
str->q_append(srid);
|
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
return (null_value= test(geom->centroid(str))) ? 0 : str;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Spatial decomposition functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
String *Item_func_spatial_decomp::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *swkb= args[0]->val_str(&arg_val);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom= NULL;
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 srid;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length())))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
srid= uint4korr(swkb->ptr());
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
if (str->reserve(SRID_SIZE, 512))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
str->length(0);
|
|
|
|
str->q_append(srid);
|
2004-03-04 07:50:37 +01:00
|
|
|
switch (decomp_func) {
|
2003-05-30 12:22:34 +02:00
|
|
|
case SP_STARTPOINT:
|
2004-03-12 09:04:00 +01:00
|
|
|
if (geom->start_point(str))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_ENDPOINT:
|
2004-03-12 09:04:00 +01:00
|
|
|
if (geom->end_point(str))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_EXTERIORRING:
|
2004-03-12 09:04:00 +01:00
|
|
|
if (geom->exterior_ring(str))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
2004-03-04 07:50:37 +01:00
|
|
|
return str;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
err:
|
|
|
|
null_value= 1;
|
|
|
|
return 0;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String *Item_func_spatial_decomp_n::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_val;
|
|
|
|
String *swkb= args[0]->val_str(&arg_val);
|
|
|
|
long n= (long) args[1]->val_int();
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom= NULL;
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 srid;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value || args[1]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length())))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
if (str->reserve(SRID_SIZE, 512))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
|
|
|
srid= uint4korr(swkb->ptr());
|
2003-05-30 12:22:34 +02:00
|
|
|
str->length(0);
|
|
|
|
str->q_append(srid);
|
2004-03-04 07:50:37 +01:00
|
|
|
switch (decomp_func_n)
|
2003-05-30 12:22:34 +02:00
|
|
|
{
|
|
|
|
case SP_POINTN:
|
2004-03-12 09:04:00 +01:00
|
|
|
if (geom->point_n(n,str))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_GEOMETRYN:
|
2004-03-12 09:04:00 +01:00
|
|
|
if (geom->geometry_n(n,str))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case SP_INTERIORRINGN:
|
2004-03-12 09:04:00 +01:00
|
|
|
if (geom->interior_ring_n(n,str))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
2004-03-04 07:50:37 +01:00
|
|
|
return str;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
err:
|
|
|
|
null_value=1;
|
|
|
|
return 0;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-07-02 23:22:46 +02:00
|
|
|
Functions to concatenate various spatial objects
|
2003-05-30 12:22:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-07-02 23:22:46 +02:00
|
|
|
* Concatenate doubles into Point
|
2003-05-30 12:22:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
String *Item_func_point::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
double x= args[0]->val();
|
|
|
|
double y= args[1]->val();
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
if ((null_value= (args[0]->null_value ||
|
|
|
|
args[1]->null_value ||
|
|
|
|
str->realloc(1 + 4 + SIZEOF_STORED_DOUBLE*2))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
str->length(0);
|
2004-03-12 09:04:00 +01:00
|
|
|
str->q_append((char)Geometry::wkb_ndr);
|
|
|
|
str->q_append((uint32)Geometry::wkb_point);
|
2003-05-30 12:22:34 +02:00
|
|
|
str->q_append(x);
|
|
|
|
str->q_append(y);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2004-07-02 23:22:46 +02:00
|
|
|
Concatenates various items into various collections
|
2003-05-30 12:22:34 +02:00
|
|
|
with checkings for valid wkb type of items.
|
|
|
|
For example, MultiPoint can be a collection of Points only.
|
|
|
|
coll_type contains wkb type of target collection.
|
|
|
|
item_type contains a valid wkb type of items.
|
|
|
|
In the case when coll_type is wkbGeometryCollection,
|
|
|
|
we do not check wkb type of items, any is valid.
|
|
|
|
*/
|
|
|
|
|
|
|
|
String *Item_func_spatial_collection::val_str(String *str)
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String arg_value;
|
|
|
|
uint i;
|
|
|
|
|
2004-09-07 13:44:25 +02:00
|
|
|
str->set_charset(&my_charset_bin);
|
2003-05-30 12:22:34 +02:00
|
|
|
str->length(0);
|
|
|
|
if (str->reserve(1 + 4 + 4, 512))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
str->q_append((char) Geometry::wkb_ndr);
|
2003-05-30 12:22:34 +02:00
|
|
|
str->q_append((uint32) coll_type);
|
|
|
|
str->q_append((uint32) arg_count);
|
|
|
|
|
|
|
|
for (i= 0; i < arg_count; ++i)
|
|
|
|
{
|
|
|
|
String *res= args[i]->val_str(&arg_value);
|
2007-02-21 11:45:19 +01:00
|
|
|
uint32 len;
|
|
|
|
if (args[i]->null_value || ((len= res->length()) < WKB_HEADER_SIZE))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if (coll_type == Geometry::wkb_geometrycollection)
|
2003-05-30 12:22:34 +02:00
|
|
|
{
|
|
|
|
/*
|
2004-03-04 07:50:37 +01:00
|
|
|
In the case of GeometryCollection we don't need any checkings
|
|
|
|
for item types, so just copy them into target collection
|
2003-05-30 12:22:34 +02:00
|
|
|
*/
|
2007-02-21 11:45:19 +01:00
|
|
|
if (str->append(res->ptr(), len, (uint32) 512))
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
enum Geometry::wkbType wkb_type;
|
|
|
|
const char *data= res->ptr() + 1;
|
|
|
|
|
|
|
|
/*
|
2004-07-02 23:22:46 +02:00
|
|
|
In the case of named collection we must check that items
|
2004-03-04 07:50:37 +01:00
|
|
|
are of specific type, let's do this checking now
|
2003-05-30 12:22:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
wkb_type= (Geometry::wkbType) uint4korr(data);
|
|
|
|
data+= 4;
|
|
|
|
len-= 5;
|
|
|
|
if (wkb_type != item_type)
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
switch (coll_type) {
|
2004-03-12 09:04:00 +01:00
|
|
|
case Geometry::wkb_multipoint:
|
|
|
|
case Geometry::wkb_multilinestring:
|
|
|
|
case Geometry::wkb_multipolygon:
|
2004-03-04 07:50:37 +01:00
|
|
|
if (len < WKB_HEADER_SIZE ||
|
|
|
|
str->append(data-WKB_HEADER_SIZE, len+WKB_HEADER_SIZE, 512))
|
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
case Geometry::wkb_linestring:
|
2004-03-04 07:50:37 +01:00
|
|
|
if (str->append(data, POINT_DATA_SIZE, 512))
|
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
break;
|
2004-03-12 09:04:00 +01:00
|
|
|
case Geometry::wkb_polygon:
|
2003-05-30 12:22:34 +02:00
|
|
|
{
|
|
|
|
uint32 n_points;
|
|
|
|
double x1, y1, x2, y2;
|
2004-03-04 07:50:37 +01:00
|
|
|
const char *org_data= data;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
if (len < 4 + 2 * POINT_DATA_SIZE)
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
n_points= uint4korr(data);
|
|
|
|
data+= 4;
|
|
|
|
float8get(x1, data);
|
2004-03-04 07:50:37 +01:00
|
|
|
data+= SIZEOF_STORED_DOUBLE;
|
2003-05-30 12:22:34 +02:00
|
|
|
float8get(y1, data);
|
2004-03-04 07:50:37 +01:00
|
|
|
data+= SIZEOF_STORED_DOUBLE;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
data+= (n_points - 2) * POINT_DATA_SIZE;
|
|
|
|
|
|
|
|
float8get(x2, data);
|
2004-03-04 07:50:37 +01:00
|
|
|
float8get(y2, data + SIZEOF_STORED_DOUBLE);
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
if ((x1 != x2) || (y1 != y2) ||
|
|
|
|
str->append(org_data, len, 512))
|
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (str->length() > current_thd->variables.max_allowed_packet)
|
2004-06-29 16:55:13 +02:00
|
|
|
{
|
|
|
|
push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
|
|
|
|
ER_WARN_ALLOWED_PACKET_OVERFLOWED,
|
2004-07-21 18:27:45 +02:00
|
|
|
ER(ER_WARN_ALLOWED_PACKET_OVERFLOWED),
|
|
|
|
func_name(), current_thd->variables.max_allowed_packet);
|
2004-03-04 07:50:37 +01:00
|
|
|
goto err;
|
2004-06-29 16:55:13 +02:00
|
|
|
}
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value = 0;
|
2004-03-04 07:50:37 +01:00
|
|
|
return str;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
err:
|
|
|
|
null_value= 1;
|
|
|
|
return 0;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
/*
|
|
|
|
Functions for spatial relations
|
|
|
|
*/
|
|
|
|
|
|
|
|
longlong Item_func_spatial_rel::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String *res1= args[0]->val_str(&tmp_value1);
|
|
|
|
String *res2= args[1]->val_str(&tmp_value2);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer1, buffer2;
|
|
|
|
Geometry *g1, *g2;
|
2003-05-30 12:22:34 +02:00
|
|
|
MBR mbr1, mbr2;
|
2004-03-04 07:50:37 +01:00
|
|
|
const char *dummy;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
2004-03-12 09:04:00 +01:00
|
|
|
if ((null_value=
|
|
|
|
(args[0]->null_value ||
|
|
|
|
args[1]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(g1= Geometry::construct(&buffer1, res1->ptr(), res1->length())) ||
|
|
|
|
!(g2= Geometry::construct(&buffer2, res2->ptr(), res2->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
g1->get_mbr(&mbr1, &dummy) ||
|
|
|
|
g2->get_mbr(&mbr2, &dummy))))
|
2003-05-30 12:22:34 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
switch (spatial_rel) {
|
2003-05-30 12:22:34 +02:00
|
|
|
case SP_CONTAINS_FUNC:
|
|
|
|
return mbr1.contains(&mbr2);
|
|
|
|
case SP_WITHIN_FUNC:
|
|
|
|
return mbr1.within(&mbr2);
|
|
|
|
case SP_EQUALS_FUNC:
|
|
|
|
return mbr1.equals(&mbr2);
|
|
|
|
case SP_DISJOINT_FUNC:
|
|
|
|
return mbr1.disjoint(&mbr2);
|
|
|
|
case SP_INTERSECTS_FUNC:
|
|
|
|
return mbr1.intersects(&mbr2);
|
|
|
|
case SP_TOUCHES_FUNC:
|
|
|
|
return mbr1.touches(&mbr2);
|
|
|
|
case SP_OVERLAPS_FUNC:
|
|
|
|
return mbr1.overlaps(&mbr2);
|
|
|
|
case SP_CROSSES_FUNC:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
null_value=1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_isempty::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2007-02-21 11:45:19 +01:00
|
|
|
String tmp;
|
|
|
|
String *swkb= args[0]->val_str(&tmp);
|
|
|
|
Geometry_buffer buffer;
|
|
|
|
|
|
|
|
null_value= args[0]->null_value ||
|
|
|
|
!(Geometry::construct(&buffer, swkb->ptr(), swkb->length()));
|
|
|
|
return null_value ? 1 : 0;
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_issimple::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String tmp;
|
2007-02-21 11:45:19 +01:00
|
|
|
String *swkb= args[0]->val_str(&tmp);
|
|
|
|
Geometry_buffer buffer;
|
|
|
|
|
|
|
|
null_value= args[0]->null_value ||
|
|
|
|
!(Geometry::construct(&buffer, swkb->ptr(), swkb->length()));
|
2003-05-30 12:22:34 +02:00
|
|
|
/* TODO: Ramil or Holyfoot, add real IsSimple calculation */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_isclosed::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String tmp;
|
|
|
|
String *swkb= args[0]->val_str(&tmp);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2004-03-04 07:50:37 +01:00
|
|
|
int isclosed= 0; // In case of error
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
|
|
|
args[0]->null_value ||
|
2004-03-12 09:04:00 +01:00
|
|
|
!(geom=
|
2005-09-21 11:35:01 +02:00
|
|
|
Geometry::construct(&buffer, swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->is_closed(&isclosed));
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
return (longlong) isclosed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Numerical functions
|
|
|
|
*/
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_dimension::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 dim= 0; // In case of error
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2004-03-04 07:50:37 +01:00
|
|
|
const char *dummy;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
|
|
|
args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->dimension(&dim, &dummy));
|
2003-05-30 12:22:34 +02:00
|
|
|
return (longlong) dim;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_numinteriorring::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 num= 0; // In case of error
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->num_interior_ring(&num));
|
2003-05-30 12:22:34 +02:00
|
|
|
return (longlong) num;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_numgeometries::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 num= 0; // In case of errors
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->num_geometries(&num));
|
2003-05-30 12:22:34 +02:00
|
|
|
return (longlong) num;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
longlong Item_func_numpoints::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
uint32 num= 0; // In case of errors
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
|
|
|
args[0]->null_value ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->num_points(&num));
|
2003-05-30 12:22:34 +02:00
|
|
|
return (longlong) num;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
double Item_func_x::val()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
double res= 0.0; // In case of errors
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->get_x(&res));
|
2003-05-30 12:22:34 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
double Item_func_y::val()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
double res= 0; // In case of errors
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->get_y(&res));
|
2003-05-30 12:22:34 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-03-04 07:50:37 +01:00
|
|
|
|
2003-05-30 12:22:34 +02:00
|
|
|
double Item_func_area::val()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
double res= 0; // In case of errors
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2004-03-04 07:50:37 +01:00
|
|
|
const char *dummy;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->area(&res, &dummy));
|
2003-05-30 12:22:34 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Item_func_glength::val()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2004-03-04 07:50:37 +01:00
|
|
|
double res= 0; // In case of errors
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
|
|
|
Geometry *geom;
|
2003-05-30 12:22:34 +02:00
|
|
|
|
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!(geom= Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length())) ||
|
2004-03-12 09:04:00 +01:00
|
|
|
geom->length(&res));
|
2003-05-30 12:22:34 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
longlong Item_func_srid::val_int()
|
|
|
|
{
|
2004-03-18 14:14:36 +01:00
|
|
|
DBUG_ASSERT(fixed == 1);
|
2003-05-30 12:22:34 +02:00
|
|
|
String *swkb= args[0]->val_str(&value);
|
2004-03-12 09:04:00 +01:00
|
|
|
Geometry_buffer buffer;
|
2004-05-05 07:59:17 +02:00
|
|
|
|
2004-04-05 12:56:05 +02:00
|
|
|
null_value= (!swkb ||
|
2005-09-21 11:35:01 +02:00
|
|
|
!Geometry::construct(&buffer,
|
|
|
|
swkb->ptr(), swkb->length()));
|
2004-03-04 07:50:37 +01:00
|
|
|
if (null_value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return (longlong) (uint4korr(swkb->ptr()));
|
2003-05-30 12:22:34 +02:00
|
|
|
}
|
2004-01-15 18:06:22 +01:00
|
|
|
|
|
|
|
#endif /*HAVE_SPATIAL*/
|