[Commits] 760d9a3: MDEV-4045 Missing OGC Spatial functions.

holyfoot at askmonty.org holyfoot at askmonty.org
Wed Nov 12 11:29:19 EET 2014


revision-id: 760d9a37a03f9b9f24f940f292f91982c4925d51
parent(s): 1827d9e6d188e3ad53e057a583ebed02767b65ac
committer: Alexey Botchkov
branch nick: server
timestamp: 2014-11-11 15:32:10 +0400
message:

MDEV-4045 Missing OGC Spatial functions.
Implemetations added for functions:
        ConvexHull
        Relate
        IsSimple
        IsRing

---
 mysql-test/r/gis.result |  32 +++
 mysql-test/t/gis.test   |  53 +++--
 sql/item_create.cc      |  75 ++++++-
 sql/item_func.h         |   2 +-
 sql/item_geofunc.cc     | 508 +++++++++++++++++++++++++++++++++++++++++++++++-
 sql/item_geofunc.h      |  71 ++++++-
 6 files changed, 703 insertions(+), 38 deletions(-)

diff --git a/mysql-test/r/gis.result b/mysql-test/r/gis.result
index 70e5215..a3ffdbc 100644
--- a/mysql-test/r/gis.result
+++ b/mysql-test/r/gis.result
@@ -1326,6 +1326,18 @@ WHERE name = 'Route 5'
 AND aliases = 'Main Street';
 IsEmpty(centerline)
 0
+# Conformance Item T12 
+SELECT IsSimple(shore) 
+FROM lakes 
+WHERE name = 'Blue Lake';
+IsSimple(shore)
+1
+# Conformance Item T13 
+SELECT AsText(ST_Boundary(boundary))
+FROM named_places 
+WHERE name = 'Goose Island';
+AsText(ST_Boundary(boundary))
+NULL
 # Conformance Item T14 
 SELECT AsText(Envelope(boundary)) 
 FROM named_places 
@@ -1483,6 +1495,13 @@ WHERE forests.name = 'Green Forest'
 AND named_places.name = 'Ashton';
 ST_Contains(forests.boundary, named_places.boundary)
 0
+# Conformance Item T45 
+SELECT ST_Relate(forests.boundary, named_places.boundary, 'TTTTTTTTT') 
+FROM forests, named_places 
+WHERE forests.name = 'Green Forest' 
+AND named_places.name = 'Ashton';
+ST_Relate(forests.boundary, named_places.boundary, 'TTTTTTTTT')
+1
 # Conformance Item T46 
 SELECT ST_Distance(position, boundary) 
 FROM bridges, named_places 
@@ -1490,6 +1509,13 @@ WHERE bridges.name = 'Cam Bridge'
 AND named_places.name = 'Ashton';
 ST_Distance(position, boundary)
 12
+# Conformance Item T47 
+SELECT AsText(ST_Intersection(centerline, shore)) 
+FROM streams, lakes 
+WHERE streams.name = 'Cam Stream' 
+AND lakes.name = 'Blue Lake';
+AsText(ST_Intersection(centerline, shore))
+POINT(52 18)
 # Conformance Item T48 
 SELECT AsText(ST_Difference(named_places.boundary, forests.boundary)) 
 FROM named_places, forests 
@@ -1516,6 +1542,12 @@ FROM buildings, bridges
 WHERE ST_Contains(ST_Buffer(bridges.position, 15.0), buildings.footprint) = 1;
 count(*)
 1
+# Conformance Item T52 
+SELECT AsText(ConvexHull(shore)) 
+FROM lakes 
+WHERE lakes.name = 'Blue Lake';
+AsText(ConvexHull(shore))
+POLYGON((48 6,52 18,66 23,73 9,48 6))
 DROP DATABASE gis_ogs;
 USE test;
 #
diff --git a/mysql-test/t/gis.test b/mysql-test/t/gis.test
index 5dc5b48..fb2e122 100644
--- a/mysql-test/t/gis.test
+++ b/mysql-test/t/gis.test
@@ -1119,33 +1119,27 @@ FROM named_places
 WHERE name = 'Goose Island'; 
 
 --echo # Conformance Item T10 
-# TODO: ST_SRID() alias
 SELECT SRID(boundary) 
 FROM named_places 
 WHERE name = 'Goose Island'; 
 
 --echo # Conformance Item T11 
-# TODO: ST_IsEmpty() alias
 SELECT IsEmpty(centerline) 
 FROM road_segments 
 WHERE name = 'Route 5' 
 AND aliases = 'Main Street'; 
 
-# FIXME: get wrong result:0, expected 1.
-#--echo # Conformance Item T12 
-# TODO: ST_IsSimple() alias
-#SELECT IsSimple(shore) 
-#FROM lakes 
-#WHERE name = 'Blue Lake'; 
+--echo # Conformance Item T12 
+SELECT IsSimple(shore) 
+FROM lakes 
+WHERE name = 'Blue Lake'; 
 
-# TODO: WL#2377
-#--echo # Conformance Item T13 
-#SELECT AsText(Boundary((boundary),101) 
-#FROM named_places 
-#WHERE name = 'Goose Island'; 
+--echo # Conformance Item T13 
+SELECT AsText(ST_Boundary(boundary))
+FROM named_places 
+WHERE name = 'Goose Island'; 
 
 --echo # Conformance Item T14 
-# TODO: ST_Envelope( ) alias
 # FIXME: we get anticlockwise, GIS suggests clockwise
 SELECT AsText(Envelope(boundary)) 
 FROM named_places 
@@ -1336,12 +1330,11 @@ FROM forests, named_places
 WHERE forests.name = 'Green Forest' 
 AND named_places.name = 'Ashton'; 
 
-# TODO: WL#2377
-#--echo # Conformance Item T45 
-#SELECT Relate(forests.boundary, named_places.boundary, 'TTTTTTTTT') 
-#FROM forests, named_places 
-#WHERE forests.name = 'Green Forest' 
-#AND named_places.name = 'Ashton'; 
+--echo # Conformance Item T45 
+SELECT ST_Relate(forests.boundary, named_places.boundary, 'TTTTTTTTT') 
+FROM forests, named_places 
+WHERE forests.name = 'Green Forest' 
+AND named_places.name = 'Ashton'; 
 
 --echo # Conformance Item T46 
 SELECT ST_Distance(position, boundary) 
@@ -1349,12 +1342,11 @@ FROM bridges, named_places
 WHERE bridges.name = 'Cam Bridge' 
 AND named_places.name = 'Ashton'; 
 
-# FIXME: wrong result: NULL, expected 12
-#--echo # Conformance Item T47 
-#SELECT AsText(ST_Intersection(centerline, shore)) 
-#FROM streams, lakes 
-#WHERE streams.name = 'Cam Stream' 
-#AND lakes.name = 'Blue Lake'; 
+--echo # Conformance Item T47 
+SELECT AsText(ST_Intersection(centerline, shore)) 
+FROM streams, lakes 
+WHERE streams.name = 'Cam Stream' 
+AND lakes.name = 'Blue Lake'; 
 
 --echo # Conformance Item T48 
 SELECT AsText(ST_Difference(named_places.boundary, forests.boundary)) 
@@ -1379,11 +1371,10 @@ SELECT count(*)
 FROM buildings, bridges 
 WHERE ST_Contains(ST_Buffer(bridges.position, 15.0), buildings.footprint) = 1; 
 
-# TODO: WL#2377
-#--echo # Conformance Item T52 
-#SELECT AsText(ConvexHull(shore)) 
-#FROM lakes 
-#WHERE lakes.name = 'Blue Lake'; 
+--echo # Conformance Item T52 
+SELECT AsText(ConvexHull(shore)) 
+FROM lakes 
+WHERE lakes.name = 'Blue Lake'; 
 
 DROP DATABASE gis_ogs;
 USE test;
diff --git a/sql/item_create.cc b/sql/item_create.cc
index fa8249c..bfdf11e 100644
--- a/sql/item_create.cc
+++ b/sql/item_create.cc
@@ -513,7 +513,20 @@ class Create_func_centroid : public Create_func_arg1
   Create_func_centroid() {}
   virtual ~Create_func_centroid() {}
 };
-#endif
+
+
+class Create_func_convexhull : public Create_func_arg1
+{
+public:
+  virtual Item *create_1_arg(THD *thd, Item *arg1);
+
+  static Create_func_convexhull s_singleton;
+
+protected:
+  Create_func_convexhull() {}
+  virtual ~Create_func_convexhull() {}
+};
+#endif /*HAVE_SPATIAL*/
 
 
 class Create_func_char_length : public Create_func_arg1
@@ -1015,7 +1028,19 @@ class Create_func_envelope : public Create_func_arg1
   Create_func_envelope() {}
   virtual ~Create_func_envelope() {}
 };
-#endif
+
+class Create_func_boundary : public Create_func_arg1
+{
+public:
+  virtual Item *create_1_arg(THD *thd, Item *arg1);
+
+  static Create_func_boundary s_singleton;
+
+protected:
+  Create_func_boundary() {}
+  virtual ~Create_func_boundary() {}
+};
+#endif /*HAVE_SPATIAL*/
 
 
 #ifdef HAVE_SPATIAL
@@ -1466,6 +1491,19 @@ class Create_func_interiorringn : public Create_func_arg2
 
 
 #ifdef HAVE_SPATIAL
+class Create_func_relate : public Create_func_arg3
+{
+public:
+  virtual Item *create_3_arg(THD *thd, Item *arg1, Item *arg2, Item *arg3);
+
+  static Create_func_relate s_singleton;
+
+protected:
+  Create_func_relate() {}
+  virtual ~Create_func_relate() {}
+};
+
+
 class Create_func_mbr_intersects : public Create_func_arg2
 {
   public:
@@ -3338,7 +3376,16 @@ Create_func_centroid::create_1_arg(THD *thd, Item *arg1)
 {
   return new (thd->mem_root) Item_func_centroid(arg1);
 }
-#endif
+
+
+Create_func_convexhull Create_func_convexhull::s_singleton;
+
+Item*
+Create_func_convexhull::create_1_arg(THD *thd, Item *arg1)
+{
+  return new (thd->mem_root) Item_func_convexhull(arg1);
+}
+#endif /*HAVE_SPATIAL*/
 
 
 Create_func_char_length Create_func_char_length::s_singleton;
@@ -3819,6 +3866,15 @@ Create_func_envelope::create_1_arg(THD *thd, Item *arg1)
 {
   return new (thd->mem_root) Item_func_envelope(arg1);
 }
+
+
+Create_func_boundary Create_func_boundary::s_singleton;
+
+Item*
+Create_func_boundary::create_1_arg(THD *thd, Item *arg1)
+{
+  return new (thd->mem_root) Item_func_boundary(arg1);
+}
 #endif
 
 
@@ -4329,6 +4385,15 @@ Create_func_interiorringn::create_2_arg(THD *thd, Item *arg1, Item *arg2)
 
 
 #ifdef HAVE_SPATIAL
+Create_func_relate Create_func_relate::s_singleton;
+
+Item*
+Create_func_relate::create_3_arg(THD *thd, Item *arg1, Item *arg2, Item *matrix)
+{
+  return new (thd->mem_root) Item_func_spatial_rel(arg1, arg2, matrix);
+}
+
+
 Create_func_mbr_intersects Create_func_mbr_intersects::s_singleton;
 
 Item*
@@ -5673,6 +5738,7 @@ static Native_func_registry func_array[] =
   { { C_STRING_WITH_LEN("CONNECTION_ID") }, BUILDER(Create_func_connection_id)},
   { { C_STRING_WITH_LEN("CONV") }, BUILDER(Create_func_conv)},
   { { C_STRING_WITH_LEN("CONVERT_TZ") }, BUILDER(Create_func_convert_tz)},
+  { { C_STRING_WITH_LEN("CONVEXHULL") }, GEOM_BUILDER(Create_func_convexhull)},
   { { C_STRING_WITH_LEN("COS") }, BUILDER(Create_func_cos)},
   { { C_STRING_WITH_LEN("COT") }, BUILDER(Create_func_cot)},
   { { C_STRING_WITH_LEN("CRC32") }, BUILDER(Create_func_crc32)},
@@ -5839,9 +5905,11 @@ static Native_func_registry func_array[] =
   { { C_STRING_WITH_LEN("ST_ASTEXT") }, GEOM_BUILDER(Create_func_as_wkt)},
   { { C_STRING_WITH_LEN("ST_ASWKB") }, GEOM_BUILDER(Create_func_as_wkb)},
   { { C_STRING_WITH_LEN("ST_ASWKT") }, GEOM_BUILDER(Create_func_as_wkt)},
+  { { C_STRING_WITH_LEN("ST_BOUNDARY") }, GEOM_BUILDER(Create_func_boundary)},
   { { C_STRING_WITH_LEN("ST_BUFFER") }, GEOM_BUILDER(Create_func_buffer)},
   { { C_STRING_WITH_LEN("ST_CENTROID") }, GEOM_BUILDER(Create_func_centroid)},
   { { C_STRING_WITH_LEN("ST_CONTAINS") }, GEOM_BUILDER(Create_func_contains)},
+  { { C_STRING_WITH_LEN("ST_CONVEXHULL") }, GEOM_BUILDER(Create_func_convexhull)},
   { { C_STRING_WITH_LEN("ST_CROSSES") }, GEOM_BUILDER(Create_func_crosses)},
   { { C_STRING_WITH_LEN("ST_DIFFERENCE") }, GEOM_BUILDER(Create_func_difference)},
   { { C_STRING_WITH_LEN("ST_DIMENSION") }, GEOM_BUILDER(Create_func_dimension)},
@@ -5887,6 +5955,7 @@ static Native_func_registry func_array[] =
   { { C_STRING_WITH_LEN("ST_POLYFROMWKB") }, GEOM_BUILDER(Create_func_geometry_from_wkb)},
   { { C_STRING_WITH_LEN("ST_POLYGONFROMTEXT") }, GEOM_BUILDER(Create_func_geometry_from_text)},
   { { C_STRING_WITH_LEN("ST_POLYGONFROMWKB") }, GEOM_BUILDER(Create_func_geometry_from_wkb)},
+  { { C_STRING_WITH_LEN("ST_RELATE") }, GEOM_BUILDER(Create_func_relate)},
   { { C_STRING_WITH_LEN("ST_SRID") }, GEOM_BUILDER(Create_func_srid)},
   { { C_STRING_WITH_LEN("ST_STARTPOINT") }, GEOM_BUILDER(Create_func_startpoint)},
   { { C_STRING_WITH_LEN("ST_SYMDIFFERENCE") }, GEOM_BUILDER(Create_func_symdifference)},
diff --git a/sql/item_func.h b/sql/item_func.h
index 18265f6..171845d 100644
--- a/sql/item_func.h
+++ b/sql/item_func.h
@@ -61,7 +61,7 @@ class Item_func :public Item_result_field
 		  SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC,
 		  SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC,
 		  SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING,
-		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN,
+		  SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN, SP_RELATE_FUNC,
                   NOT_FUNC, NOT_ALL_FUNC,
                   NOW_FUNC, TRIG_COND_FUNC,
                   SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC,
diff --git a/sql/item_geofunc.cc b/sql/item_geofunc.cc
index d9200b3..abe5054 100644
--- a/sql/item_geofunc.cc
+++ b/sql/item_geofunc.cc
@@ -218,6 +218,130 @@ String *Item_func_envelope::val_str(String *str)
 }
 
 
+int Item_func_boundary::Transporter::single_point(double x, double y)
+{
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::start_line()
+{
+  n_points= 0;
+  current_type= Gcalc_function::shape_line;
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::complete_line()
+{
+  current_type= (Gcalc_function::shape_type) 0;
+  if (n_points > 1)
+    return m_receiver->single_point(last_x, last_y);
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::start_poly()
+{
+  current_type= Gcalc_function::shape_polygon;
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::complete_poly()
+{
+  current_type= (Gcalc_function::shape_type) 0;
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::start_ring()
+{
+  n_points= 0;
+  return m_receiver->start_shape(Gcalc_function::shape_line);
+}
+
+
+int Item_func_boundary::Transporter::complete_ring()
+{
+  if (n_points > 1)
+  {
+     m_receiver->add_point(last_x, last_y);
+  }
+  m_receiver->complete_shape();
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::add_point(double x, double y)
+{
+  ++n_points;
+  if (current_type== Gcalc_function::shape_polygon)
+  {
+    /* Polygon's ring case */
+    if (n_points == 1)
+    {
+      last_x= x;
+      last_y= y;
+    }
+    return m_receiver->add_point(x, y);
+  }
+  
+  if (current_type== Gcalc_function::shape_line)
+  {
+    /* Line's case */
+    last_x= x;
+    last_y= y;
+    if (n_points == 1)
+      return m_receiver->single_point(x, y);
+  }
+  return 0;
+}
+
+
+int Item_func_boundary::Transporter::start_collection(int n_objects)
+{
+  return 0;
+}
+
+
+String *Item_func_boundary::val_str(String *str_value)
+{
+  DBUG_ENTER("Item_func_boundary::val_str");
+  DBUG_ASSERT(fixed == 1);
+  String arg_val;
+  String *swkb= args[0]->val_str(&arg_val);
+  Geometry_buffer buffer;
+  Geometry *g;
+  uint32 srid= 0;
+  Transporter trn(&res_receiver);
+  
+  if ((null_value=
+       args[0]->null_value ||
+       !(g= Geometry::construct(&buffer, swkb->ptr(), swkb->length()))))
+    DBUG_RETURN(0);
+  
+  if (g->store_shapes(&trn))
+    goto mem_error;
+
+  str_value->set_charset(&my_charset_bin);
+  if (str_value->reserve(SRID_SIZE, 512))
+    goto mem_error;
+  str_value->length(0);
+  str_value->q_append(srid);
+
+  if (!Geometry::create_from_opresult(&buffer, str_value, res_receiver))
+    goto mem_error;
+
+  res_receiver.reset();
+  DBUG_RETURN(str_value);
+
+mem_error:
+  null_value= 1;
+  DBUG_RETURN(0);
+}
+
+
 Field::geometry_type Item_func_centroid::get_geometry_type() const
 {
   return Field::GEOM_POINT;
@@ -248,6 +372,289 @@ String *Item_func_centroid::val_str(String *str)
 }
 
 
+int Item_func_convexhull::add_node_to_line(ch_node **p_cur, int dir,
+                                           const Gcalc_heap::Info *pi)
+{
+  ch_node *new_node;
+  ch_node *cur= *p_cur;
+
+  while (cur->prev)
+  {
+    int v_sign= Gcalc_scan_iterator::point::cmp_dx_dy(
+                  cur->prev->pi, cur->pi, cur->pi, pi);
+    if (v_sign*dir <0)
+      break;
+    new_node= cur;
+    cur= cur->prev;
+    res_heap.free_item(new_node);
+  }
+  if (!(new_node= new_ch_node()))
+    return 1;
+  cur->next= new_node;
+  new_node->prev= cur;
+  new_node->pi= pi;
+  *p_cur= new_node;
+  return 0;
+}
+
+
+#ifndef HEAVY_CONVEX_HULL
+String *Item_func_convexhull::val_str(String *str_value)
+{
+  Geometry_buffer buffer;
+  Geometry *geom= NULL;
+  MBR mbr;
+  const char *c_end;
+  Gcalc_operation_transporter trn(&func, &collector);
+  uint32 srid= 0;
+  ch_node *left_first, *left_cur, *right_first, *right_cur;
+  Gcalc_heap::Info *cur_pi;
+  
+  DBUG_ENTER("Item_func_convexhull::val_str");
+  DBUG_ASSERT(fixed == 1);
+  String *swkb= args[0]->val_str(&tmp_value);
+
+  if ((null_value=
+       args[0]->null_value ||
+       !(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length()))))
+    DBUG_RETURN(0);
+  
+  geom->get_mbr(&mbr, &c_end);
+  collector.set_extent(mbr.xmin, mbr.xmax, mbr.ymin, mbr.ymax);
+  if ((null_value= geom->store_shapes(&trn)))
+  {
+    str_value= 0;
+    goto mem_error;
+  }
+
+  collector.prepare_operation();
+  if (!(cur_pi= collector.get_first()))
+    goto build_result; /* An EMPTY GEOMETRY */
+
+  if (!cur_pi->get_next())
+  {
+    /* Single point. */
+    if (res_receiver.single_point(cur_pi->x, cur_pi->y))
+      goto mem_error;
+    goto build_result;
+  }
+
+  left_cur= left_first= new_ch_node();
+  right_cur= right_first= new_ch_node();
+  right_first->prev= left_first->prev= 0;
+  right_first->pi= left_first->pi= cur_pi;
+
+  while ((cur_pi= cur_pi->get_next()))
+  {
+    /* Handle left part of the hull, then the right part. */
+    if (add_node_to_line(&left_cur, 1, cur_pi))
+      goto mem_error;
+    if (add_node_to_line(&right_cur, -1, cur_pi))
+      goto mem_error;
+  }
+
+  left_cur->next= 0;
+  if (left_first->get_next()->get_next() == NULL &&
+      right_cur->prev->prev == NULL)
+  {
+    /* We only have 2 nodes in the result, so we create a polyline. */
+    if (res_receiver.start_shape(Gcalc_function::shape_line) ||
+        res_receiver.add_point(left_first->pi->x, left_first->pi->y) ||
+        res_receiver.add_point(left_cur->pi->x, left_cur->pi->y) ||
+        res_receiver.complete_shape())
+
+      goto mem_error;
+
+    goto build_result;
+  }
+
+  if (res_receiver.start_shape(Gcalc_function::shape_polygon))
+    goto mem_error;
+
+  while (left_first)
+  {
+    if (res_receiver.add_point(left_first->pi->x, left_first->pi->y))
+      goto mem_error;
+    left_first= left_first->get_next();
+  }
+
+  /* Skip last point in the right part as it coincides */
+  /* with the last one in the left.                    */
+  right_cur= right_cur->prev;
+  while (right_cur->prev)
+  {
+    if (res_receiver.add_point(right_cur->pi->x, right_cur->pi->y))
+      goto mem_error;
+    right_cur= right_cur->prev;
+  }
+  res_receiver.complete_shape();
+
+build_result:
+  str_value->set_charset(&my_charset_bin);
+  if (str_value->reserve(SRID_SIZE, 512))
+    goto mem_error;
+  str_value->length(0);
+  str_value->q_append(srid);
+
+  if (!Geometry::create_from_opresult(&buffer, str_value, res_receiver))
+    goto mem_error;
+
+mem_error:
+  collector.reset();
+  func.reset();
+  res_receiver.reset();
+  res_heap.reset();
+  DBUG_RETURN(str_value);
+}
+
+#else /*HEAVY_CONVEX_HULL*/
+String *Item_func_convexhull::val_str(String *str_value)
+{
+  Geometry_buffer buffer;
+  Geometry *geom= NULL;
+  MBR mbr;
+  const char *c_end;
+  Gcalc_operation_transporter trn(&func, &collector);
+  const Gcalc_scan_iterator::event_point *ev;
+  uint32 srid= 0;
+  ch_node *left_first, *left_cur, *right_first, *right_cur;
+  
+  DBUG_ENTER("Item_func_convexhull::val_str");
+  DBUG_ASSERT(fixed == 1);
+  String *swkb= args[0]->val_str(&tmp_value);
+
+  if ((null_value=
+       args[0]->null_value ||
+       !(geom= Geometry::construct(&buffer, swkb->ptr(), swkb->length()))))
+    DBUG_RETURN(0);
+  
+  geom->get_mbr(&mbr, &c_end);
+  collector.set_extent(mbr.xmin, mbr.xmax, mbr.ymin, mbr.ymax);
+  if ((null_value= geom->store_shapes(&trn)))
+  {
+    str_value= 0;
+    goto mem_error;
+  }
+
+  collector.prepare_operation();
+  scan_it.init(&collector);
+  scan_it.killed= (int *) &(current_thd->killed);
+
+  if (!scan_it.more_points())
+    goto build_result; /* An EMPTY GEOMETRY */
+
+  if (scan_it.step())
+    goto mem_error;
+
+  if (!scan_it.more_points())
+  {
+    /* Single point. */
+    if (res_receiver.single_point(scan_it.get_events()->pi->x,
+                                  scan_it.get_events()->pi->y))
+      goto mem_error;
+    goto build_result;
+  }
+
+  left_cur= left_first= new_ch_node();
+  right_cur= right_first= new_ch_node();
+  right_first->prev= left_first->prev= 0;
+  right_first->pi= left_first->pi= scan_it.get_events()->pi;
+
+  while (scan_it.more_points())
+  {
+    if (scan_it.step())
+      goto mem_error;
+    ev= scan_it.get_events();
+    
+    /* Skip the intersections-only events. */
+    while (ev->event == scev_intersection)
+    {
+      ev= ev->get_next();
+      if (!ev)
+        goto skip_point;
+    }
+
+    {
+      Gcalc_point_iterator pit(&scan_it);
+      if (!pit.point() || scan_it.get_event_position() == pit.point())
+      {
+        /* Handle left part of the hull. */
+        if (add_node_to_line(&left_cur, 1, ev->pi))
+          goto mem_error;
+      }
+      if (pit.point())
+      {
+        /* Check the rightmost point */
+        for(; pit.point()->c_get_next(); ++pit)
+          ;
+      }
+      if (!pit.point() || pit.point()->event ||
+          scan_it.get_event_position() == pit.point()->c_get_next())
+      {
+        /* Handle right part of the hull. */
+        if (add_node_to_line(&right_cur, -1, ev->pi))
+          goto mem_error;
+      }
+    }
+skip_point:;
+  }
+
+  left_cur->next= 0;
+  if (left_first->get_next()->get_next() == NULL &&
+      right_cur->prev->prev == NULL)
+  {
+    /* We only have 2 nodes in the result, so we create a polyline. */
+    if (res_receiver.start_shape(Gcalc_function::shape_line) ||
+        res_receiver.add_point(left_first->pi->x, left_first->pi->y) ||
+        res_receiver.add_point(left_cur->pi->x, left_cur->pi->y) ||
+        res_receiver.complete_shape())
+
+      goto mem_error;
+
+    goto build_result;
+  }
+
+  if (res_receiver.start_shape(Gcalc_function::shape_polygon))
+    goto mem_error;
+
+  while (left_first)
+  {
+    if (res_receiver.add_point(left_first->pi->x, left_first->pi->y))
+      goto mem_error;
+    left_first= left_first->get_next();
+  }
+
+  /* Skip last point in the right part as it coincides */
+  /* with the last one in the left.                    */
+  right_cur= right_cur->prev;
+  while (right_cur->prev)
+  {
+    if (res_receiver.add_point(right_cur->pi->x, right_cur->pi->y))
+      goto mem_error;
+    right_cur= right_cur->prev;
+  }
+  res_receiver.complete_shape();
+
+build_result:
+  str_value->set_charset(&my_charset_bin);
+  if (str_value->reserve(SRID_SIZE, 512))
+    goto mem_error;
+  str_value->length(0);
+  str_value->q_append(srid);
+
+  if (!Geometry::create_from_opresult(&buffer, str_value, res_receiver))
+    goto mem_error;
+
+mem_error:
+  collector.reset();
+  func.reset();
+  res_receiver.reset();
+  res_heap.reset();
+  DBUG_RETURN(str_value);
+}
+#endif /*HEAVY_CONVEX_HULL*/
+
+
 /*
   Spatial decomposition functions
 */
@@ -593,12 +1000,17 @@ longlong Item_func_spatial_mbr_rel::val_int()
 
 Item_func_spatial_rel::Item_func_spatial_rel(Item *a,Item *b,
                                              enum Functype sp_rel) :
-    Item_bool_func2(a,b), collector()
+    Item_int_func(a,b), collector()
 {
   spatial_rel = sp_rel;
 }
 
 
+Item_func_spatial_rel::Item_func_spatial_rel(Item *a,Item *b, Item *mask) :
+    Item_int_func(a,b,mask), spatial_rel(SP_RELATE_FUNC)
+{}
+
+
 Item_func_spatial_rel::~Item_func_spatial_rel()
 {
 }
@@ -623,6 +1035,8 @@ const char *Item_func_spatial_rel::func_name() const
       return "st_crosses";
     case SP_OVERLAPS_FUNC:
       return "st_overlaps";
+    case SP_RELATE_FUNC:
+      return "st_relate";
     default:
       DBUG_ASSERT(0);  // Should never happened
       return "sp_unknown"; 
@@ -661,6 +1075,84 @@ static double distance_points(const Gcalc_heap::Info *a,
 }
 
 
+static Gcalc_function::op_type op_matrix(int n)
+{
+  switch (n)
+  {
+    case 0:
+      return Gcalc_function::op_border;
+    case 1:
+      return Gcalc_function::op_internals;
+    case 2:
+      return (Gcalc_function::op_type)
+        ((int) Gcalc_function::op_not | (int) Gcalc_function::op_union);
+  };
+  GCALC_DBUG_ASSERT(FALSE);
+  return Gcalc_function::op_any;
+}
+
+
+static int setup_relate_func(Geometry *g1, Geometry *g2,
+    Gcalc_operation_transporter *trn, Gcalc_function *func,
+    const char *mask)
+{
+  int do_store_shapes=1;
+  uint shape_a, shape_b;
+  uint n_operands= 0;
+  int last_shape_pos;
+
+  last_shape_pos= func->get_next_expression_pos();
+  func->add_operation(Gcalc_function::op_intersection, 0);
+  for (int nc=0; nc<9; nc++)
+  {
+    uint cur_op;
+
+    cur_op= Gcalc_function::op_intersection;
+    switch (mask[nc])
+    {
+      case '*':
+        continue;
+      case 'T':
+      case '0':
+      case '1':
+      case '2':
+        cur_op|= Gcalc_function::v_find_t;
+        break;
+      case 'F':
+        cur_op|= Gcalc_function::v_find_f;
+        break;
+    };
+    ++n_operands;
+    if (func->reserve_op_buffer(1))
+      return 1;
+    func->add_operation(cur_op, 2);
+
+    func->add_operation(op_matrix(nc/3), 1);
+    if (do_store_shapes)
+    {
+      shape_a= func->get_next_expression_pos();
+      if (g1->store_shapes(trn))
+        return 1;
+    }
+    else
+      func->repeat_expression(shape_a);
+    func->add_operation(op_matrix(nc%3), 1);
+    if (do_store_shapes)
+    {
+      shape_b= func->get_next_expression_pos();
+      if (g2->store_shapes(trn))
+        return 1;
+      do_store_shapes= 0;
+    }
+    else
+      func->repeat_expression(shape_b);
+  }
+  
+  func->add_operands_to_op(last_shape_pos, n_operands);
+  return 0;
+}
+
+
 #define GIS_ZERO 0.00000000001
 
 longlong Item_func_spatial_rel::val_int()
@@ -669,6 +1161,7 @@ longlong Item_func_spatial_rel::val_int()
   DBUG_ASSERT(fixed == 1);
   String *res1;
   String *res2;
+  String *res3;
   Geometry_buffer buffer1, buffer2;
   Geometry *g1, *g2;
   int result= 0;
@@ -736,6 +1229,8 @@ longlong Item_func_spatial_rel::val_int()
     case SP_OVERLAPS_FUNC:
     case SP_CROSSES_FUNC:
       func.add_operation(Gcalc_function::op_intersection, 2);
+      if (func.reserve_op_buffer(1))
+        break;
       func.add_operation(Gcalc_function::v_find_t |
                          Gcalc_function::op_intersection, 2);
       shape_a= func.get_next_expression_pos();
@@ -744,6 +1239,8 @@ longlong Item_func_spatial_rel::val_int()
       shape_b= func.get_next_expression_pos();
       if ((null_value= g2->store_shapes(&trn)))
         break;
+      if (func.reserve_op_buffer(7))
+        break;
       func.add_operation(Gcalc_function::v_find_t |
                          Gcalc_function::op_intersection, 2);
       func.add_operation(Gcalc_function::v_find_t |
@@ -756,6 +1253,8 @@ longlong Item_func_spatial_rel::val_int()
       func.repeat_expression(shape_a);
       break;
     case SP_TOUCHES_FUNC:
+      if (func.reserve_op_buffer(2))
+        break;
       func.add_operation(Gcalc_function::op_intersection, 2);
       func.add_operation(Gcalc_function::v_find_f |
                          Gcalc_function::op_not |
@@ -775,6 +1274,13 @@ longlong Item_func_spatial_rel::val_int()
       func.add_operation(Gcalc_function::op_border, 1);
       func.repeat_expression(shape_b);
       break;
+    case SP_RELATE_FUNC:
+      res3= args[2]->val_str(&tmp_matrix);
+      if ((null_value= args[2]->null_value))
+        break;
+      null_value= (res3->length() != 9) ||
+        setup_relate_func(g1, g2, &trn, &func, res3->ptr());
+      break;
     default:
       DBUG_ASSERT(FALSE);
       break;
diff --git a/sql/item_geofunc.h b/sql/item_geofunc.h
index 6d52661..a71ac03 100644
--- a/sql/item_geofunc.h
+++ b/sql/item_geofunc.h
@@ -93,6 +93,37 @@ class Item_func_geometry_type: public Item_str_ascii_func
   };
 };
 
+
+// #define HEAVY_CONVEX_HULL
+class Item_func_convexhull: public Item_geometry_func
+{
+  class ch_node: public Gcalc_dyn_list::Item
+  {
+  public:
+    const Gcalc_heap::Info *pi;
+    ch_node *prev;
+    Gcalc_dyn_list::Item *next;
+    ch_node *get_next() { return (ch_node *) next; }
+  };
+
+  Gcalc_heap collector;
+  Gcalc_function func;
+  Gcalc_dyn_list res_heap;
+
+  Gcalc_result_receiver res_receiver;
+  String tmp_value;
+#ifdef HEAVY_CONVEX_HULL
+  Gcalc_scan_iterator scan_it;
+#endif /*HEAVY_CONVEX_HULL*/
+  ch_node *new_ch_node() { return (ch_node *) res_heap.new_item(); }
+  int add_node_to_line(ch_node **p_cur, int dir, const Gcalc_heap::Info *pi);
+public:
+  Item_func_convexhull(Item *a): Item_geometry_func(a),
+    res_heap(8192, sizeof(ch_node))
+    {}
+  const char *func_name() const { return "st_convexhull"; }
+  String *val_str(String *);
+};
 class Item_func_centroid: public Item_geometry_func
 {
 public:
@@ -111,6 +142,38 @@ class Item_func_envelope: public Item_geometry_func
   Field::geometry_type get_geometry_type() const;
 };
 
+
+class Item_func_boundary: public Item_geometry_func
+{
+  class Transporter : public Gcalc_shape_transporter
+  {
+    Gcalc_result_receiver *m_receiver;
+    uint n_points;
+    Gcalc_function::shape_type current_type;
+    double last_x, last_y;
+  public:
+    Transporter(Gcalc_result_receiver *receiver) :
+      Gcalc_shape_transporter(NULL), m_receiver(receiver)
+    {}
+    int single_point(double x, double y);
+    int start_line();
+    int complete_line();
+    int start_poly();
+    int complete_poly();
+    int start_ring();
+    int complete_ring();
+    int add_point(double x, double y);
+
+    int start_collection(int n_objects);
+  };
+  Gcalc_result_receiver res_receiver;
+public:
+  Item_func_boundary(Item *a): Item_geometry_func(a) {}
+  const char *func_name() const { return "st_boundary"; }
+  String *val_str(String *);
+};
+
+
 class Item_func_point: public Item_geometry_func
 {
 public:
@@ -229,15 +292,16 @@ class Item_func_spatial_mbr_rel: public Item_bool_func2
 };
 
 
-class Item_func_spatial_rel: public Item_bool_func2
+class Item_func_spatial_rel: public Item_int_func
 {
   enum Functype spatial_rel;
   Gcalc_heap collector;
   Gcalc_scan_iterator scan_it;
   Gcalc_function func;
-  String tmp_value1,tmp_value2;
+  String tmp_value1,tmp_value2, tmp_matrix;
 public:
   Item_func_spatial_rel(Item *a,Item *b, enum Functype sp_rel);
+  Item_func_spatial_rel(Item *a, Item *b, Item *matrix);
   virtual ~Item_func_spatial_rel();
   longlong val_int();
   enum Functype functype() const 
@@ -253,6 +317,9 @@ class Item_func_spatial_rel: public Item_bool_func2
 
   void fix_length_and_dec() { maybe_null= 1; }
   bool is_null() { (void) val_int(); return null_value; }
+  bool is_bool_func() { return 1; }
+  uint decimal_precision() const { return 1; }
+  optimize_type select_optimize() const { return OPTIMIZE_OP; }
 };
 
 


More information about the commits mailing list