summaryrefslogtreecommitdiff
path: root/Docs/gis.txt
blob: d80a200d5a6a7af46c3535c5a7fc006bbe538710 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861

  OpenGIS <http://www.opengis.org> support in MySQL

------------------------------------------------------------------------
Note: Blue colored lines among the text is features not implemented yet.
They are:

    * Spatial Reference Systems and their IDs (SRIDs) related things:
          o Functions like Length() and Area() assume planar coordinate
            system.
          o All objects are currently considered to be in the same
      planar coordinate system.
    * Function Length() on LineString and MultiLineString currently
      should be called as GLength().
* No binary constructors like GeomFromWKB().

We also have to add "PostGIS compatibility" sections.


  1 Introduction

MySQL implements a subset of *SQL2 with Geometry Types* environment
proposed by OpenGIS consortium's *Simple Features Specification For
SQL*. In this environment a geometry-valued column is implemented as a
column whose SQL type is drawn from the set of Geometry Types. SQL
server supports both textual and binary access to geometry.


  2 OpenGIS Geometry Model in MySQL

MySQL supports the Open GIS Geometry Model based hierarcy of spatial
objects classes, which consists of:

    * Geometry
          o *Point*
          o Curve
          + *LineString*
          o Surface
          + *Polygon*
          o *GeometryCollection*
                + *MultiPoint*
                + MultiCurve
                # *MultiLineString*
                + MultiSurface
                # *MultiPolygon*

The base *Geometry* class has subclasses for Point, Curve, Surface and
GeometryCollection.

Geometry, Curve, Surface, MultiCurve and MultiSurface are defined to be
non-instantiable classes, it is not possible to create an object of
these classes.

Point, LineString, Polygon, GeometryCollection, MultiPoint,
MultiLineString, MultiPolygon are instantiable classes (bolded on the
hierarcy tree). MySQL provides a number of functions to construct
instances of these classes.

TODO: Each spatial object is associated with a Spatial Reference System,
which describes the coordinate space in which the geometric object is
defined.


    2.1 Geometry

Geometry is the root class of the hierarchy. Geometry is an abstract
(non-instantiable) class. The instantiable subclasses of Geometry
defined in this specification are restricted to 0, 1 and two-dimensional
geometric objects that exist in two-dimensional coordinate space. All
instantiable geometry classes are defined so that valid instances of a
geometry class are topologically closed (i.e. all defined geometries
include their boundary).


    2.2 Point

A *Point* is a 0-dimensional geometry and represents a single location
in coordinate space. A Point in the case of 2D has a x-coordinate value
and a y-coordinate value. In the case of more dimensions, a Point has a
coordinate value for each dimension. The boundary of a Point is the
empty set.


    2.3 Curve

A *Curve* is a one-dimensional geometric object usually stored as a
sequence of points, with the subclass of Curve specifying the form of
the interpolation between points. MySQL implementation defines only one
subclass of Curve, LineString, which uses linear interpolation between
points.

A Curve is simple if it does not pass through the same point twice. A
Curve is closed if its start point is equal to its end point. The
boundary of a closed Curve is empty. A Curve that is simple and closed
is a Ring. The boundary of a non-closed Curve consists of its two end
points. A Curve is defined as topologically closed.


    2.4 LineString, Line, LinearRing

A LineString is a Curve with linear interpolation between points. Each
consecutive pair of points defines a line segment. A Line is a
LineString with exactly 2 points. A LinearRing is a LineString that is
both closed and simple.


    2.5 Surface

A *Surface* is a two-dimensional geometric object. The OpenGIS Abstract
Specification defines a simple Surface as consisting of a single 'patch'
that is associated with one 'exterior boundary' and 0 or more 'interior'
boundaries. Simple surfaces in three-dimensional space are isomorphic to
planar surfaces. Polyhedral surfaces are formed by 'stitching' together
simple surfaces along their boundaries, polyhedral surfaces in
three-dimensional space may not be planar as a whole.

The boundary of a simple Surface is the set of closed curves
corresponding to its exterior and interior boundaries.

The only instantiable subclass of Surface defined in this specification,
Polygon, is a simple Surface that is planar.


    2.6 Polygon

A Polygon is a planar Surface, defined by 1 exterior boundary and 0 or
more interior boundaries. Each interior boundary defines a hole in the
Polygon. The assertions for polygons (the rules that define valid
polygons) are:

    * Polygons are topologically closed.
    * The boundary of a Polygon consists of a set of LinearRings (i.e.
      LineStrings which are both simple and closed) that make up its
      exterior and interior boundaries.
    * No two rings in the boundary cross, the rings in the boundary of a
      Polygon may intersect at a Point but only as a tangent.
    * A Polygon may not have cut lines, spikes or punctures.
    * The Interior of every Polygon is a connected point set.
    * The Exterior of a Polygon with 1 or more holes is not connected.
Each hole defines a connected component of the Exterior.

In the above assertions, Interior, Closure and Exterior have the
standard topological definitions. The combination of 1 and 3 make a
Polygon a Regular Closed point set. Polygons are simple geometries.


    2.6 GeometryCollection

A *GeometryCollection* is a geometry that is a collection of 1 or more
geometries of any class. All the elements in a GeometryCollection must
be in the same Spatial Reference (i.e. in the same coordinate system).
GeometryCollection places no other constraints on its elements. However
subclasses of GeometryCollection described below may restrict membership
based on dimension and may also place other constraints on the degree of
spatial overlap between elements.


    2.7 MultiPoint

A *MultiPoint* is a 0 dimensional geometric collection. The elements of
a MultiPoint are restricted to Points. The points are not connected or
ordered. A MultiPoint is simple if no two Points in the MultiPoint are
equal (have identical coordinate values). The boundary of a MultiPoint
is the empty set.


    2.8 MultiCurve

A MultiCurve is a one-dimensional geometry collection whose elements are
Curves. MultiCurve is a non-instantiable class, it defines a set of
methods for its subclasses and is included for reasons of extensibility.

A MultiCurve is simple if and only if all of its elements are simple,
the only intersections between any two elements occur at points that are
on the boundaries of both elements.

The boundary of a MultiCurve is obtained by applying the "mod 2 union
rule": A point is in the boundary of a MultiCurve if it is in the
boundaries of an odd number of elements of the MultiCurve.

A MultiCurve is closed if all of its elements are closed. The boundary
of a closed MultiCurve is always empty. A MultiCurve is defined as
topologically closed.


    2.9 MultiLineString

A *MultiLineString* is a MultiCurve whose elements are LineStrings.


    2.10 MultiSurface

A MultiSurface is a two-dimensional geometric collection whose elements
are surfaces. The interiors of any two surfaces in a MultiSurface may
not intersect. The boundaries of any two elements in a MultiSurface may
intersect at most at a finite number of points.

MultiSurface is a non-instantiable class in this specification, it
defines a set of methods for its subclasses and is included for reasons
of extensibility. The instantiable subclass of MultiSurface is
MultiPolygon, corresponding to a collection of Polygons.


    2.11 MultiPolygon

A MultiPolygon is a MultiSurface whose elements are Polygons.

The assertions for MultiPolygons are :

    * The interiors of 2 Polygons that are elements of a MultiPolygon
      may not intersect.
    * The Boundaries of any 2 Polygons that are elements of a
      MultiPolygon may not cross and may touch at only a finite number
      of points. (Note that crossing is prevented by assertion 1 above).
    * A MultiPolygon is defined as topologically closed.
    * A MultiPolygon may not have cut lines, spikes or punctures, a
      MultiPolygon is a Regular, Closed point set.
    * The interior of a MultiPolygon with more than 1 Polygon is not
      connected, the number of connected components of the interior of a
MultiPolygon is equal to the number of Polygons in the MultiPolygon.

The boundary of a MultiPolygon is a set of closed curves (LineStrings)
corresponding to the boundaries of its element Polygons. Each Curve in
the boundary of the MultiPolygon is in the boundary of exactly 1 element
Polygon, and every Curve in the boundary of an element Polygon is in the
boundary of the MultiPolygon.


  3 Exchange of spatial data

MySQL provides binary and textual mechanismes to exchange spatial data.
Exchange is provided via so called Well Known Binary (WKB) and Well
Known Textual (WKT) representations of spatial data proposed by OpenGIS
specifications.


    3.1 Well-known Text representation (WKT)

The Well-known Text (WKT) representation of Geometry is designed to
exchange geometry data in textual format.

WKT is defined below in Bechus-Naur forms:

    * the notation {}* denotes 0 or more repetitions of the tokens
      within the braces;
* the braces do not appear in the output token list.

The text representation of the implemented instantiable geometric types
conforms to this grammar:

<Geometry Tagged Text> :=
            <Point Tagged Text>
          | <LineString Tagged Text>
          | <Polygon Tagged Text>
          | <MultiPoint Tagged Text>
          | <MultiLineString Tagged Text>
          | <MultiPolygon Tagged Text>
          | <GeometryCollection Tagged Text>

<Point Tagged Text> :=

            POINT <Point Text>

<LineString Tagged Text> :=

            LINESTRING <LineString Text>

<Polygon Tagged Text> :=

            POLYGON <Polygon Text>

<MultiPoint Tagged Text> :=

            MULTIPOINT <Multipoint Text>

<MultiLineString Tagged Text> :=

            MULTILINESTRING <MultiLineString Text>

<MultiPolygon Tagged Text> :=

            MULTIPOLYGON <MultiPolygon Text>

<GeometryCollection Tagged Text> :=

            GEOMETRYCOLLECTION <GeometryCollection Text>

<Point Text> := EMPTY | ( <Point> )

<Point> := <x> <y>

<x> := double precision literal

<y> := double precision literal

<LineString Text> := EMPTY

          | ( <Point >  {, <Point > }* )

<Polygon Text> := EMPTY

          | ( <LineString Text >  {, < LineString Text > }*)

<Multipoint Text> := EMPTY

          | ( <Point Text >  {, <Point Text > }* )

<MultiLineString Text> := EMPTY

          | ( <LineString Text >  {, < LineString Text > }* )

<MultiPolygon Text> := EMPTY

          | ( < Polygon Text >  {, < Polygon Text > }* )

<GeometryCollection Text> := EMPTY

          | ( <Geometry Tagged Text> {, <Geometry Tagged Text> }* )


      WKT examples

Examples of textual representations of Geometry objects are shown below:

    * |POINT(10 10)| - a Point
    * |LINESTRING( 10 10, 20 20, 30 40)| - a LineString with three points
    * |POLYGON((10 10, 10 20, 20 20,20 15, 10 10))| - a Polygon with one
      exterior ring and 0 interior rings
    * |MULTIPOINT(10 10, 20 20)| - a MultiPoint with two Points
    * |MULTILINESTRING((10 10, 20 20), (15 15, 30 15))| - a
      MultiLineString with two LineStrings
    * |MULTIPOLYGON(((10 10, 10 20, 20 20, 20 15, 10 10)), ((60 60, 70
      70, 80 60, 60 60 ) ))| - a MultiPolygon with two Polygons
    * |GEOMETRYCOLLECTION( POINT (10 10),POINT (30 30), LINESTRING (15
      15, 20 20))| - a GeometryCollection consisting of two Points and
one LineString


    3.2 Well-known Binary representation (WKB)

Well Known Binary Representations is proposed by OpenGIS specifications
to exchange geometry data in binary format. This is WKB description:

// Basic Type definitions
// byte : 1 byte
// uint32 : 32 bit unsigned integer (4 bytes)
// double : double precision number (8 bytes)
// Building Blocks : Point, LinearRing

Point {
	double [numDimentions];
};

LinearRing  {
	uint32 numPoints;
	Point    points[numPoints];
}

enum wkbGeometryType {
	wkbPoint = 1,
	wkbLineString = 2,
	wkbPolygon = 3,
	wkbMultiPoint = 4,
	wkbMultiLineString = 5,
	wkbMultiPolygon = 6,
	wkbGeometryCollection = 7
};

enum wkbByteOrder {
	wkbXDR = 0,    // Big Endian
	wkbNDR = 1    // Little Endian
};

WKBPoint {
	byte    byteOrder;
	uint32    wkbType;     // 1
	Point    point;
}

WKBLineString {
	byte    byteOrder;
	uint32    wkbType;    // 2
	uint32    numPoints;
	Point    points[numPoints];
}

WKBPolygon {
	byte    byteOrder;
	uint32    wkbType;    // 3
	uint32    numRings;
	LinearRing    rings[numRings];
}

WKBMultiPoint {
	byte    byteOrder;
	uint32    wkbType;       // 4
	uint32    num_wkbPoints;
	WKBPoint    WKBPoints[num_wkbPoints];
}

WKBMultiLineString {
	byte    byteOrder;
	uint32    wkbType;    // 5
	uint32    num_wkbLineStrings;
	WKBLineString WKBLineStrings[num_wkbLineStrings];
}

wkbMultiPolygon {
	byte    byteOrder;
	uint32    wkbType;    // 6
	uint32    num_wkbPolygons;
	WKBPolygon    wkbPolygons[num_wkbPolygons];
}

WKBGeometry {
	union {
		WKBPoint    point;
		WKBLineString    linestring;
		WKBPolygon    polygon;
		WKBGeometryCollection    collection;
		WKBMultiPoint    mpoint;
		WKBMultiLineString    mlinestring;
		WKBMultiPolygon    mpolygon;
	}

};

WKBGeometryCollection {
	byte    byte_order;
	uint32    wkbType;    // 7
	uint32    num_wkbGeometries;
	WKBGeometry    wkbGeometries[num_wkbGeometries];
}


    3.3 MySQL data types for spatial objects

MySQL implementation of OpenGIS provides the *GEOMETRY* data type to be
used in CREATE TABLE statements. For example, this statement creates a
table *geom* with spatial field *g*:

CREATE TABLE geom (
	g Geometry;
);

A field of *GEOMETRY* type can store a spatial objects of any OpenGIS
geometry class described above.


    3.4 Internal spatial data representation

Internally (in *.MYD* files) spatial objects are stored in *WKB*,
combined with object's *SRID* (a numeric ID of Spatial Reference System
object associated with). During spatial analysis, for example,
calculating the fact that one object crosses another one, only those
with the same *SRID* are accepted.

*SRID* may affect a way in which various spatial characteristics are
calculated. For example, in different coordinate systems distance
between two objects may differ even objects have the same coordinates,
like distance on plane coordinate system and distance on geocentric
(coordinates on Earth surface) systems are different things.

There is a plan to provide a number of commonly used coordinate systems
in MySQL OpenGIS implementation.


    3.5 INSERTing spatial objects

Spatial data can be INSERTed using a spatial constructor. The term
*spatial constructor* is used in this manual to refer to any function
which can construct a value of GEOMETRY type, i.e. an internal MySQL
representation of spatial data.


      3.5.1 Textual spatial constructors

Textual spatial constructors take a gemometry description in WKT and
built GEOMETRY value.

    * |*GeomFromText(geometryTaggedText String [, SRID
      Integer]):Geometry *| - constructs a Geometry value from its
      well-known textual representation.

      |*GeomFromText()*| function accepts a WKT of any Geometry class as
      it's first argument.

      For construction of Geometry values restricted to a particular
      subclass, an implementation also provides a class-specific
      construction function for each instantiable subtype as described
      in the list below:

    * |*PointFromText(pointTaggedText String [,SRID Integer]):Point *| -
      constructs a Point

    * |*LineFromText(lineStringTaggedText String [,SRID
      Integer]):LineString *| - constructs a LineString

    * |*PolyFromText(polygonTaggedText String [,SRID Integer]):Polygon
      *|- constructs a Polygon

    * |*MPointFromText(multiPointTaggedText String [,SRID
      Integer]):MultiPoint *| - constructs a MultiPoint

    * |*MLineFromText(multiLineStringTaggedText String [,SRID
      Integer]):MultiLineString *| - constructs a MultiLineString

    * |*MPolyFromText(multiPolygonTaggedText String [,SRID
      Integer]):MultiPolygon *| - constructs a MultiPolygon

    * |*GeomCollFromText(geometryCollectionTaggedText String [,SRID
      Integer]):GeomCollection *| - constructs a GeometryCollection

Usage examples:

INSERT INTO geom VALUES (GeomFromText('POINT(1 1)'))
INSERT INTO geom VALUES (GeomFromText('LINESTRING(0 0,1 1,2 2)'))
INSERT INTO geom VALUES (GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))'))
INSERT INTO geom VALUES (GeomFromText('GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'))

The second argument of spatial constructirs, described above, is
currently ignored, It will be used to specify SRID in the future.
Nowdays, it is added for reasons of compatibility with OpenGIS
specifications and PostGIS implementation.

As an optional feature, an implementation may also support building of
Polygon or MultiPolygon values given an arbitrary collection of possibly
intersecting rings or closed LineString values. Implementations that
support this feature should include the following functions:

    * |*BdPolyFromText(multiLineStringTaggedText String, SRID
      Integer):Polygon *| - constructs a Polygon given an arbitrary
      collection of closed linestrings as a MultiLineString text
      representation.
    * |*BdMPolyFromText(multiLineStringTaggedText String, SRID
      Integer):MultiPolygon *| - constructs a MultiPolygon given an
      arbitrary collection of closed linestrings as a MultiLineString
text representation.


      3.5.2 Binary spatial constructors

    * |*GeomFromWKB(WKBGeometry Binary, SRID Integer):Geometry *| -
      constructs a Geometry value given its well-known binary
      representation.

      |*GeomFromWKB()*| function can accept in it's first argument a WKB
      of Geometry of any class. For construction of Geometry values
      restricted to a particular subclass, an implementation also
      provides a class-specific construction function for each
      instantiable subclass as described in the list below:

    * |*PointFromWKB(WKBPoint Binary, SRID Integer):Point - *|constructs
      a Point
    * |* LineFromWKB(WKBLineString Binary, SRID Integer):LineString *| -
      constructs a LineString
    * |* PolyFromWKB(WKBPolygon Binary, SRID Integer):Polygon *| -
      constructs a Polygon
    * |* MPointFromWKB(WKBMultiPoint Binary, SRID Integer):MultiPoint *|
      - constructs a MultiPoint
    * |* MLineFromWKB(WKBMultiLineString Binary, SRID
      Integer):MultiLineString *| - constructs a MultiLineString
    * |* MPolyFromWKB(WKBMultiPolygon Binary, SRID Integer):
      MultiPolygon *| - constructs a MultiPolygon
    * |* GeomCollFromWKB(WKBGeometryCollection Binary, SRID Integer):
GeomCollection *| - constructs a GeometryCollection

As an optional feature, an implementation may also support the uilding'
of Polygon or MultiPolygon values given an arbitrary collection of
possibly intersecting rings or closed LineString values. Implementations
that support this feature should include the following functions:

    * |* BdPolyFromWKB (WKBMultiLineString Binary,SRID Integer): Polygon
      *| - constructs a Polygon given an arbitrary collection of closed
      linestrings as a MultiLineString binary representation.
    * |*BdMPolyFromWKB(WKBMultiLineString Binary, SRID
      Integer):MultiPolygon *| - constructs a MultiPolygon given an
      arbitrary collection of closed linestrings as a MultiLineString
binary representation.

Inserting in *WKB* assumes that |GeomFromWKB()| function argument
contains a buffer with a correctly formed spatial object in WKB. In ODBC
applications it can be done using binding of argument. One also can
insert object in *WKB* using |mysql_escape_string()| in |libmysqlclient|
applications.

For example:

INSERT INTO geom VALUES (GeomFromWKB(buf,SRID));

where |buf| is a binary buffer with a spatial object in *WKB*
representation.


    3.5 SELECTing spatial objects

Spatial objects are selected either in *WKT* or *WKB* representation by
use of AsText() and AsBinary() functions correspondently.


mysql> select AsText(g) as g from geom;
+-------------------------+
| g                       |
+-------------------------+
| POINT(1 1)              |
| LINESTRING(0 0,1 1,2 2) |
+-------------------------+
2 rows in set (0.00 sec)

mysql> 

The query:

SELECT AsBinary(g) FROM geom

returns a BLOB which contains *WKB* representation of object.


  4 Functions for spatial analysis


    4.1 Basic functions on Geometry

    * |*AsText(g:Geometry):String*| - Exports this Geometry to a
      specific well-known text representation of Geometry.
    * |*AsBinary(g:Geometry):Binary*| - Exports this Geometry to a
      specific well-known binary representation of Geometry.
    * |*GeometryType(g:Geometry):String*| - Returns the name of the
      instantiable subtype of Geometry of which this Geometry instance
      is a member. The name of the instantiable subtype of Geometry is
      returned as a string.
    * |*Dimension(g:Geometry):Integer*| - The inherent dimension of this
      Geometry object, which must be less than or equal to the
      coordinate dimension. This specification is restricted to
      geometries in two-dimensional coordinate space.
    * |*IsEmpty(g:Geometry):Integer*| - Returns 1 (TRUE) if this
      Geometry is the empty geometry . If true, then this Geometry
      represents the empty point set, , for the coordinate space.
    * |*IsSimple(g:Geometry):Integer *| - Returns 1 (TRUE) if this
      Geometry has no anomalous geometric points, such as self
      intersection or self tangency. The description of each
      instantiable geometric class includes the specific conditions that
      cause an instance of that class to be classified as not simple.
    * |*SRID(g:Geometry):Integer*| - Returns the Spatial Reference
      System ID for this Geometry.
    * |*Distance(g1:Geometry,g2:Geometry):Double*| - the shortest
distance between any two points in the two geometries.


    4.2 Functions for specific geometry type


      GeometryCollection functions

    * *NumGeometries(g:GeometryCollection ):Integer * -Returns the
      number of geometries in this GeometryCollection.
    * *GeometryN(g:GeometryCollection,N:integer):Geometry * -Returns the
Nth geometry in this GeometryCollection.


      Point functions

    * *X(p:Point):Double* -The x-coordinate value for this Point.
* *Y(p:Point):Double* -The y-coordinate value for this Point.


      LineString functions

    * *StartPoint(l:LineString):Point* The start point of this LineString.
    * *EndPoint(l:LineString):Point* The end point of this LineString.
    * *PointN(l:LineString,N:Integer):Point* Returns the specified point
      N in this Linestring.
    * *Length(l:LineString):Double* The length of this LineString in its
      associated spatial reference.
    * *IsRing(l:LineString):Integer* Returns 1 (TRUE) if this LineString
      is closed (StartPoint ( ) = EndPoint ( )) and this LineString is
      simple (does not pass through the same point more than once).
    * *IsClosed(l:LineString):Integer* Returns 1 (TRUE) if this
      LineString is closed (StartPoint ( ) = EndPoint ( )).
    * *NumPoints(l:LineString):Integer* The number of points in this
LineString.


      MultiLineString functions

    * *Length(m:MultiLineString):Double* The Length of this
      MultiLineString which is equal to the sum of the lengths of the
      elements.
    * *IsClosed(m:MultiLineString):Integer* Returns 1 (TRUE) if this
      MultiLineString is closed (StartPoint() = EndPoint() for each
LineString in this MultiLineString)


      Polygon functions

    * *Area(p:Polygon):Double* The area of this Polygon, as measured in
      the spatial reference system of this Polygon.
    * *Centroid(p:Polygon):Point* The mathematical centroid for this
      Polygon as a Point. The result is not guaranteed to be on this
      Polygon.
    * *PointOnSurface(p:Polygon):Point* A point guaranteed to be on this
      Polygon.
    * *NumInteriorRing(p:Polygon):Integer* Returns the number of
      interior rings in this Polygon.
    * *ExteriorRing(p:Polygon):LineString* Returns the exterior ring of
      this Polygon as a LineString.
    * *InteriorRingN(p:Polygon,N:Integer):LineString* Returns the Nth
interior ring for this Polygon as a LineString.


      MultiPolygon functions

    * *Area(m:MultuSurface):Double* The area of this MultiPolygon, as
      measured in the spatial reference system of this MultiPolygon.
    * *Centroid(m:MultyPolygon):Point* The mathematical centroid for
      this MultiPolygon as a Point. The result is not guaranteed to be
      on this MultiPolygon.
    * *PointOnSurface(m:MultuPolygon):Point* A Point guaranteed to be on
this MultiPolygon.

Notes: /functions for specific geometry type retrun NULL if passed
object type is incorrect. For example Area() returns NULL if object type
is neither Polygon nor MultiPolygon/


    4.3 Spatial operations (compound spatial constructors)

    * |*Envelope(g:Geometry):Geometry*|The minimum bounding box for this
      Geometry, returned as a Geometry. The polygon is defined by the
      corner points of the bounding box
      |POLYGON((MINX,MINY),(MAXX,MINY),(MAXX,MAXY),(MINX,MAXY),(MINX,MINY))|.

    * |*Boundary(g:Geometry):Geometry*| - returns the closure of the
      combinatorial boundary of this Geometry.
    * |*Intersection(g1,g2:Geometry):Geometry*| - a geometry that
      represents the point set intersection of g1 with g2.
    * |*Union(g1,g2:Geometry):Geometry*| - a geometry that represents
      the point set union of g1 with g2.
    * |*Difference(g1,g2:Geometry):Geometry*| - a geometry that
      represents the point set difference of g1 with g2.
    * |*SymDifference(g1,g2:Geometry):Geometry*| - a geometry that
      represents the point set symmetric difference of g1 with g2.
    * |*Buffer(g:Geometry,distance:Double):Geometry*| - a geometry that
      represents all points whose distance from g is less than or equal
      to distance.
    * |*ConvexHull(g:Geometry):Geometry*| - a geometry that represents
the convex hull of g.


    4.4 Functions for testing Spatial Relations between geometric objects

    * |*Equals(g1,g2)*| - Returns 1 if g1 is spatially equal to g2.
    * |*Disjoint(g1,g2)*| - Returns 1 if g1 is spatially disjoint from g2.
    * |*Intersects(g1,g2)*| - Returns 1 if g1 spatially intersects g2.
    * |*Touches(g1,g2)*| - Returns 1 if g1 spatially touches g2.
    * |*Crosses(g1,g2)*| - Returns 1 if g1 spatially crosses g2.
    * |*Within(g1,g2)*| - Returns 1 if g1 is spatially within g2.
    * |*Contains(g1,g2)*| - Returns 1 if g1 spatially contains g2.
* |*Overlaps(g1,g2)*| - Returns 1 if g1 spatially overlaps g2.


  5 Optimizing spatial analysis


    5.1 MBR

MBR is a minimal bounding rectangle (box) for spatial object. It can be
represented as a set of min and max values of each dimension.

For example:

(Xmin,Xmax,Ymin,Ymax)


    5.2 Using SPATIAL indexes

To optimize spatial object relationships analysis it is possible to
create a spatial index on geometry field using R-tree algorythm. R-tree
based spatial indexes store MBRs of spatial objects as a key values.

CREATE SPATIAL INDEX gind ON geom (g);

Or together with table definition:

CREATE TABLE geom (
  g GEOMETRY,
  SPATIAL INDEX(g)
);

Optimizer attaches R-tree based SPATIAL index when a query with spatial
objects relationship functions is executed in WHERE clause.

For example:

SELECT geom.name FROM geom 
       WHERE Within(geom.g,GeomFromText('POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))',SRID));


    8 OpenGIS extensions implemented in MySQL

MySQL provides it's own constructors to build geometry objects:

    * |*Point(double,double,SRID)*| - constructs a geometry of Point
      class using it's coordinates and SRID.
    * |*MultiPoint(Point,Point,...,Point)*| - constructs a MultiPoint
      using Points. When any argument is not a geometry of Point class
      the return value is NULL.
    * |*LineString(Point,Point,...,Point)*| - constructs a LineString
      from a number of Points. When any argument is not a geometry of
      Point class the return value is NULL. When the number of Points is
      less than two the return value is NULL.
    * |*MultiLineString(LineString,LineString,...,LineString)*| -
      constructs a MultiLineString using using LineStrings. When any
      argument is not a geometry of LineStringClass return value is NULL.
    * |*Polygon(LineString,LineString,...,LineString)*| - constructs a
      Polygon from a number of LineStrings. When any argument is not a
      LinearRing (i.e. not closed and simple geometry of class
      LineString) the return value is NULL.
    * |*MultiPolygon(Polygon,Polygon,...,Polygon)*| - constructs a
      MultiPolygon from a set of Polygons. When any argument is not a
      Polygon, the rerurn value is NULL.
    * |*GeometryCollection(Geometry,Geometry,..,Geometry)*| - constucts
      a GeometryCollection. When any argument is not a valid geometry
object of any instantiable class, the return value is NULL.

The above functions (except Point()) return NULL if arguments are not in
the same spatial reference system (i.e. have different SRIDs).


        Examples:

INSERT INTO geom SELECT Point(x,y,SRID) FROM coords;
SELECT AsText(g) FROM geom WHERE 
  Contains(Polygon(LineString(Point(0,0),Point(0,1),Point(1,1),Point(1,0),Point(0,0)),SRID),geom.g);


    9 Things that differ in MySQL implemention and OpenGIS specifications


      9.1 Single GEOMETRY type

Besides a GEOMETRY type, OpenGIS consortium specifications suggest the
implementation of several spatial field types correspondent to every
instansiable object subclass. For example a *Point* type is proposed to
restrict data stored in a field of this type to only Point OpenGIS
subclass. MySQL provides an implementation of single GEOMETRY type which
doesn't restrict objects to certain OpenGIS subclass.


      9.2 No additional Metadata Views

OpenGIS specifications propose several additional metadata views. For
example, a system view named GEOMETRY_COLUMNS contains a description of
geometry columns, one row for each geometry column in the database.


      9.3 No functions to add/drop spatial columns

OpenGIS assumes that columns can be added/dropped using
AddGeometryColumn() and DropGeometryColumn() functions correspondently.
In MySQL implementation one should use ALTER TABLE instead.