#
# Spatial objects
#

--disable_warnings
DROP TABLE IF EXISTS pt, ls, p, mpt, mls, mp, gc, geo;
--enable_warnings

CREATE TABLE pt  (fid INTEGER NOT NULL PRIMARY KEY, g POINT);
CREATE TABLE ls  (fid INTEGER NOT NULL PRIMARY KEY, g LINESTRING);
CREATE TABLE p   (fid INTEGER NOT NULL PRIMARY KEY, g POLYGON);
CREATE TABLE mpt (fid INTEGER NOT NULL PRIMARY KEY, g MULTIPOINT);
CREATE TABLE mls (fid INTEGER NOT NULL PRIMARY KEY, g MULTILINESTRING);
CREATE TABLE mp  (fid INTEGER NOT NULL PRIMARY KEY, g MULTIPOLYGON);
CREATE TABLE gc  (fid INTEGER NOT NULL PRIMARY KEY, g GEOMETRYCOLLECTION);
CREATE TABLE geo (fid INTEGER NOT NULL PRIMARY KEY, g GEOMETRY);

SHOW FIELDS FROM pt;
SHOW FIELDS FROM ls;
SHOW FIELDS FROM p;
SHOW FIELDS FROM mpt;
SHOW FIELDS FROM mls;
SHOW FIELDS FROM mp;
SHOW FIELDS FROM gc;
SHOW FIELDS FROM geo;


INSERT INTO pt VALUES 
(101, PointFromText('POINT(10 10)')),
(102, PointFromText('POINT(20 10)')),
(103, PointFromText('POINT(20 20)')),
(104, PointFromWKB(AsWKB(PointFromText('POINT(10 20)'))));

INSERT INTO ls VALUES
(105, LineFromText('LINESTRING(0 0,0 10,10 0)')),
(106, LineStringFromText('LINESTRING(10 10,20 10,20 20,10 20,10 10)')),
(107, LineStringFromWKB(LineString(Point(10, 10), Point(40, 10))));

INSERT INTO p VALUES
(108, PolygonFromText('POLYGON((10 10,20 10,20 20,10 20,10 10))')),
(109, PolyFromText('POLYGON((0 0,50 0,50 50,0 50,0 0), (10 10,20 10,20 20,10 20,10 10))')),
(110, PolyFromWKB(Polygon(LineString(Point(0, 0), Point(30, 0), Point(30, 30), Point(0, 0)))));

INSERT INTO mpt VALUES
(111, MultiPointFromText('MULTIPOINT(0 0,10 10,10 20,20 20)')),
(112, MPointFromText('MULTIPOINT(1 1,11 11,11 21,21 21)')),
(113, MPointFromWKB(MultiPoint(Point(3, 6), Point(4, 10))));

INSERT INTO mls VALUES
(114, MultiLineStringFromText('MULTILINESTRING((10 48,10 21,10 0),(16 0,16 23,16 48))')),
(115, MLineFromText('MULTILINESTRING((10 48,10 21,10 0))')),
(116, MLineFromWKB(MultiLineString(LineString(Point(1, 2), Point(3, 5)), LineString(Point(2, 5), Point(5, 8), Point(21, 7)))));


INSERT INTO mp VALUES
(117, MultiPolygonFromText('MULTIPOLYGON(((28 26,28 0,84 0,84 42,28 26),(52 18,66 23,73 9,48 6,52 18)),((59 18,67 18,67 13,59 13,59 18)))')),
(118, MPolyFromText('MULTIPOLYGON(((28 26,28 0,84 0,84 42,28 26),(52 18,66 23,73 9,48 6,52 18)),((59 18,67 18,67 13,59 13,59 18)))')),
(119, MPolyFromWKB(MultiPolygon(Polygon(LineString(Point(0, 3), Point(3, 3), Point(3, 0), Point(0, 3))))));

INSERT INTO gc VALUES
(120, GeomCollFromText('GEOMETRYCOLLECTION(POINT(0 0), LINESTRING(0 0,10 10))')),
(121, GeometryFromWKB(GeometryCollection(Point(44, 6), LineString(Point(3, 6), Point(7, 9)))));

INSERT into geo SELECT * FROM pt;
INSERT into geo SELECT * FROM ls;
INSERT into geo SELECT * FROM p;
INSERT into geo SELECT * FROM mpt;
INSERT into geo SELECT * FROM mls;
INSERT into geo SELECT * FROM mp;
INSERT into geo SELECT * FROM gc;

SELECT fid, AsText(g) FROM pt;
SELECT fid, AsText(g) FROM ls;
SELECT fid, AsText(g) FROM p;
SELECT fid, AsText(g) FROM mpt;
SELECT fid, AsText(g) FROM mls;
SELECT fid, AsText(g) FROM mp;
SELECT fid, AsText(g) FROM gc;
SELECT fid, AsText(g) FROM geo;

SELECT fid, Dimension(g) FROM geo;
SELECT fid, GeometryType(g) FROM geo;
SELECT fid, IsEmpty(g) FROM geo;
SELECT fid, AsText(Envelope(g)) FROM geo;

SELECT fid, X(g) FROM pt;
SELECT fid, Y(g) FROM pt;

SELECT fid, AsText(StartPoint(g)) FROM ls;
SELECT fid, AsText(EndPoint(g)) FROM ls;
SELECT fid, GLength(g) FROM ls;
SELECT fid, NumPoints(g) FROM ls;
SELECT fid, AsText(PointN(g, 2)) FROM ls;
SELECT fid, IsClosed(g) FROM ls;

SELECT fid, AsText(Centroid(g)) FROM p;
SELECT fid, Area(g) FROM p;
SELECT fid, AsText(ExteriorRing(g)) FROM p;
SELECT fid, NumInteriorRings(g) FROM p;
SELECT fid, AsText(InteriorRingN(g, 1)) FROM p;

SELECT fid, IsClosed(g) FROM mls;

SELECT fid, AsText(Centroid(g)) FROM mp;
SELECT fid, Area(g) FROM mp;

SELECT fid, NumGeometries(g) from mpt;
SELECT fid, NumGeometries(g) from mls;
SELECT fid, NumGeometries(g) from mp;
SELECT fid, NumGeometries(g) from gc;

SELECT fid, AsText(GeometryN(g, 2)) from mpt;
SELECT fid, AsText(GeometryN(g, 2)) from mls;
SELECT fid, AsText(GeometryN(g, 2)) from mp;
SELECT fid, AsText(GeometryN(g, 2)) from gc;

SELECT g1.fid as first, g2.fid as second,
Within(g1.g, g2.g) as w, Contains(g1.g, g2.g) as c, Overlaps(g1.g, g2.g) as o,
Equals(g1.g, g2.g) as e, Disjoint(g1.g, g2.g) as d, Touches(g1.g, g2.g) as t,
Intersects(g1.g, g2.g) as i, Crosses(g1.g, g2.g) as r
FROM gc g1, gc g2 ORDER BY first, second;

DROP TABLE pt, ls, p, mpt, mls, mp, gc, geo;

#
# Check that ALTER TABLE doesn't loose geometry type
#
CREATE TABLE g1 (
  pt  point,
  ln  linestring,
  pg  polygon,
  mpt multipoint,
  mln multilinestring,
  mpg multipolygon,
  gc  geometrycollection,
  gm  geometry
);

SHOW FIELDS FROM g1;
ALTER TABLE g1 ADD fid INT NOT NULL;
SHOW FIELDS FROM g1;
DROP TABLE g1;

SELECT AsText(GeometryFromWKB(AsWKB(GeometryFromText('POINT(1 4)'))));
