summaryrefslogtreecommitdiff
path: root/sql/spatial.h
blob: 5fda257f1b1a4e54a05dc0e18b23d8357d02d55e (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
/* 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 */

#ifndef _spatial_h
#define _spatial_h

const uint POINT_DATA_SIZE = 8+8; 
const uint WKB_HEADER_SIZE = 1+4;

struct stPoint2D
{
  double x;
  double y;
};

struct stLinearRing
{
  size_t n_points;
  stPoint2D points;
};

/***************************** MBR *******************************/

struct MBR
{
  MBR()
  {
    xmin=DBL_MAX;
    ymin=DBL_MAX;
    xmax=-DBL_MAX;
    ymax=-DBL_MAX;
  }

  MBR(const double &_xmin, const double &_ymin,
      const double &_xmax, const double &_ymax)
  {
    xmin=_xmin;
    ymin=_ymin;
    xmax=_xmax;
    ymax=_ymax;
  }

  MBR(const stPoint2D &min, const stPoint2D &max)
  {
    xmin=min.x;
    ymin=min.y;
    xmax=max.x;
    ymax=max.y;
  }
  
  double xmin;
  double ymin;
  double xmax;
  double ymax;
  
  void add_xy(double x, double y)
  {
    /* Not using "else" for proper one point MBR calculation */
    if (x<xmin)
    {
      xmin=x;
    }
    if (x>xmax)
    {
      xmax=x;
    }
    if (y<ymin)
    {
      ymin=y;
    }
    if (y>ymax)
    {
      ymax=y;
    }
  }

  void add_xy(const char *px, const char *py)
  {
    double x, y;
    float8get(x, px);
    float8get(y, py);
    /* Not using "else" for proper one point MBR calculation */
    if (x<xmin)
    {
      xmin=x;
    }
    if (x>xmax)
    {
      xmax=x;
    }
    if (y<ymin)
    {
      ymin=y;
    }
    if (y>ymax)
    {
      ymax=y;
    }
  }

  void add_mbr(const MBR *mbr)
  {
    if (mbr->xmin<xmin)
    {
      xmin=mbr->xmin;
    }
    if (mbr->xmax>xmax)
    {
      xmax=mbr->xmax;
    }
    if (mbr->ymin<ymin)
    {
      ymin=mbr->ymin;
    }
    if (mbr->ymax>ymax)
    {
      ymax=mbr->ymax;
    }
  }

  int equals(const MBR *mbr)
  {
    return ((mbr->xmin == xmin) && (mbr->ymin == ymin) &&
	    (mbr->xmax == xmax) && (mbr->ymax == ymax));
  }

  int disjoint(const MBR *mbr)
  {
    return ((mbr->xmin > xmax) || (mbr->ymin > ymax) ||
	    (mbr->xmax < xmin) || (mbr->ymax < ymin));
  }

  int intersects(const MBR *mbr)
  {
    return !disjoint(mbr);
  }

  int touches(const MBR *mbr)
  {
    return ((((mbr->xmin == xmax) || (mbr->xmax == xmin)) && 
	     ((mbr->ymin >= ymin) && (mbr->ymin <= ymax) || 
	      (mbr->ymax >= ymin) && (mbr->ymax <= ymax))) ||
	    (((mbr->ymin == ymax) || (mbr->ymax == ymin)) &&
	     ((mbr->xmin >= xmin) && (mbr->xmin <= xmax) ||
	      (mbr->xmax >= xmin) && (mbr->xmax <= xmax))));
  }

  int within(const MBR *mbr)
  {
    return ((mbr->xmin <= xmin) && (mbr->ymin <= ymin) &&
	    (mbr->xmax >= xmax) && (mbr->ymax >= ymax));
  }

  int contains(const MBR *mbr)
  {
    return ((mbr->xmin >= xmin) && (mbr->ymin >= ymin) &&
	    (mbr->xmax <= xmax) && (mbr->ymax <= ymax));
  }

  bool inner_point(double x, double y) const
  {
    return (xmin<x) && (xmax>x) && (ymin<y) && (ymax>x);
  }

  int overlaps(const MBR *mbr)
  {
    int lb = mbr->inner_point(xmin, ymin);
    int rb = mbr->inner_point(xmax, ymin);
    int rt = mbr->inner_point(xmax, ymax);
    int lt = mbr->inner_point(xmin, ymax);

    int a  = lb+rb+rt+lt;
    return (a>0) && (a<4) && (!within(mbr));
  }
};


/***************************** Geometry *******************************/

class Geometry;

typedef int (Geometry::*GF_InitFromText)(GTextReadStream *, String *);
typedef int (Geometry::*GF_GetDataAsText)(String *) const;
typedef size_t (Geometry::*GF_GetDataSize)() const;
typedef int (Geometry::*GF_GetMBR)(MBR *) const;

typedef int (Geometry::*GF_GetD)(double *) const;
typedef int (Geometry::*GF_GetI)(int *) const;
typedef int (Geometry::*GF_GetUI)(uint32 *) const;
typedef int (Geometry::*GF_GetWS)(String *) const;
typedef int (Geometry::*GF_GetUIWS)(uint32, String *) const;

#define GEOM_METHOD_PRESENT(geom_obj, method)\
    (geom_obj.m_vmt->method != &Geometry::method)

class Geometry
{
public:
  enum wkbType
  {
    wkbPoint = 1,
    wkbLineString = 2,
    wkbPolygon = 3,
    wkbMultiPoint = 4,
    wkbMultiLineString = 5,
    wkbMultiPolygon = 6,
    wkbGeometryCollection = 7
  };
  enum wkbByteOrder
  {
    wkbXDR = 0,    /* Big Endian */
    wkbNDR = 1     /* Little Endian */
  };                                    

  class GClassInfo
  {
  public:
    GF_InitFromText init_from_wkt;
    GF_GetDataAsText get_data_as_wkt;
    GF_GetDataSize get_data_size;
    GF_GetMBR get_mbr;
    GF_GetD get_x;
    GF_GetD get_y;
    GF_GetD length;
    GF_GetD area;

    GF_GetI is_closed;

    GF_GetUI num_interior_ring;
    GF_GetUI num_points;
    GF_GetUI num_geometries;
    GF_GetUI dimension;

    GF_GetWS start_point;
    GF_GetWS end_point;
    GF_GetWS exterior_ring;
    GF_GetWS centroid;

    GF_GetUIWS point_n;
    GF_GetUIWS interior_ring_n;
    GF_GetUIWS geometry_n;

    int m_type_id;
    const char *m_name;
    GClassInfo *m_next_rt;
  };
  GClassInfo *m_vmt;

  const GClassInfo *get_class_info() const { return m_vmt; }
  size_t get_data_size() const { return (this->*m_vmt->get_data_size)(); }
  
  int init_from_wkt(GTextReadStream *trs, String *wkb) 
  { return (this->*m_vmt->init_from_wkt)(trs, wkb); }

  int get_data_as_wkt(String *txt) const
  { return (this->*m_vmt->get_data_as_wkt)(txt); }

  int get_mbr(MBR *mbr) const { return (this->*m_vmt->get_mbr)(mbr); }
  int dimension(uint32 *dim) const
  { return (this->*m_vmt->dimension)(dim); }

  int get_x(double *x) const { return (this->*m_vmt->get_x)(x); }
  int get_y(double *y) const { return (this->*m_vmt->get_y)(y); }
  int length(double *len) const  { return (this->*m_vmt->length)(len); }
  int area(double *ar) const  { return (this->*m_vmt->area)(ar); }

  int is_closed(int *closed) const
  { return (this->*m_vmt->is_closed)(closed); }

  int num_interior_ring(uint32 *n_int_rings) const 
  { return (this->*m_vmt->num_interior_ring)(n_int_rings); }
  int num_points(uint32 *n_points) const
  { return (this->*m_vmt->num_points)(n_points); }

  int num_geometries(uint32 *num) const
  { return (this->*m_vmt->num_geometries)(num); }

  int start_point(String *point) const
  { return (this->*m_vmt->start_point)(point); }
  int end_point(String *point) const
  { return (this->*m_vmt->end_point)(point); }
  int exterior_ring(String *ring) const
  { return (this->*m_vmt->exterior_ring)(ring); }
  int centroid(String *point) const
  { return (this->*m_vmt->centroid)(point); }

  int point_n(uint32 num, String *result) const
  { return (this->*m_vmt->point_n)(num, result); }
  int interior_ring_n(uint32 num, String *result) const
  { return (this->*m_vmt->interior_ring_n)(num, result); }
  int geometry_n(uint32 num, String *result) const
  { return (this->*m_vmt->geometry_n)(num, result); }

public:
  int create_from_wkb(const char *data, uint32 data_len);
  int create_from_wkt(GTextReadStream *trs, String *wkt, int init_stream=1);
  int init(int type_id)
  {
    m_vmt = find_class(type_id);
    return !m_vmt;
  }
  int new_geometry(const char *name, size_t len)
  {
    m_vmt = find_class(name, len);
    return !m_vmt;
  }

  int as_wkt(String *wkt) const
  {
    if (wkt->reserve(strlen(get_class_info()->m_name) + 2, 512))
      return 1;
    wkt->qs_append(get_class_info()->m_name);
    wkt->qs_append('(');
    if (get_data_as_wkt(wkt))
      return 1;
    wkt->qs_append(')');
    return 0;
  }

  void init_from_wkb(const char *data, uint32 data_len)
  {
    m_data = data;
    m_data_end = data + data_len;
  }

  void shift_wkb_header()
  {
    m_data += WKB_HEADER_SIZE;
  }

  int envelope(String *result) const;

protected:
  static GClassInfo *find_class(int type_id);
  static GClassInfo *find_class(const char *name, size_t len);

  bool no_data(const char *cur_data, uint32 data_amount) const
  {
    return (cur_data + data_amount > m_data_end);
  }

  const char *m_data;
  const char *m_data_end;
};

#define SIZEOF_STORED_DOUBLE 8

/***************************** Point *******************************/
 
class GPoint: public Geometry
{
public:
  size_t get_data_size() const;
  int init_from_wkt(GTextReadStream *trs, String *wkb);
  int get_data_as_wkt(String *txt) const;
  int get_mbr(MBR *mbr) const;
  
  int get_xy(double *x, double *y) const
  {
    const char *data = m_data;
    if (no_data(data, SIZEOF_STORED_DOUBLE * 2)) return 1;
    float8get(*x, data);
    float8get(*y, data + SIZEOF_STORED_DOUBLE);
    return 0;
  }

  int get_x(double *x) const
  {
    if (no_data(m_data, SIZEOF_STORED_DOUBLE)) return 1;
    float8get(*x, m_data);
    return 0;
  }

  int get_y(double *y) const
  {
    const char *data = m_data;
    if (no_data(data, SIZEOF_STORED_DOUBLE * 2)) return 1;
    float8get(*y, data + SIZEOF_STORED_DOUBLE);
    return 0;
  }

  int dimension(uint32 *dim) const { *dim = 0; return 0; }
};

/***************************** LineString *******************************/

class GLineString: public Geometry
{
public:
   size_t get_data_size() const;
   int init_from_wkt(GTextReadStream *trs, String *wkb);
   int get_data_as_wkt(String *txt) const;
   int get_mbr(MBR *mbr) const;
  
  int length(double *len) const;
  int is_closed(int *closed) const;
  int num_points(uint32 *n_points) const;
  int start_point(String *point) const;
  int end_point(String *point) const;
  int point_n(uint32 n, String *result) const;
  int dimension(uint32 *dim) const { *dim = 1; return 0; }
};

/***************************** Polygon *******************************/

class GPolygon: public Geometry
{
public:
  size_t get_data_size() const;
  int init_from_wkt(GTextReadStream *trs, String *wkb);
  int get_data_as_wkt(String *txt) const;
  int get_mbr(MBR *mbr) const;

  int area(double *ar) const;
  int exterior_ring(String *result) const;
  int num_interior_ring(uint32 *n_int_rings) const;
  int interior_ring_n(uint32 num, String *result) const;
  int centroid_xy(double *x, double *y) const;
  int centroid(String *result) const;
  int dimension(uint32 *dim) const { *dim = 2; return 0; }
};

/***************************** MultiPoint *******************************/

class GMultiPoint: public Geometry
{
public:
  size_t get_data_size() const;
  int init_from_wkt(GTextReadStream *trs, String *wkb);
  int get_data_as_wkt(String *txt) const;
  int get_mbr(MBR *mbr) const;

  int num_geometries(uint32 *num) const;
  int geometry_n(uint32 num, String *result) const;
  int dimension(uint32 *dim) const { *dim = 0; return 0; }
};

/***************************** MultiLineString *******************************/

class GMultiLineString: public Geometry
{
public:
  size_t get_data_size() const;
  int init_from_wkt(GTextReadStream *trs, String *wkb);
  int get_data_as_wkt(String *txt) const;
  int get_mbr(MBR *mbr) const;

  int num_geometries(uint32 *num) const;
  int geometry_n(uint32 num, String *result) const;
  int length(double *len) const;
  int is_closed(int *closed) const;
  int dimension(uint32 *dim) const { *dim = 1; return 0; }
};

/***************************** MultiPolygon *******************************/

class GMultiPolygon: public Geometry
{
public:
  size_t get_data_size() const;
  int init_from_wkt(GTextReadStream *trs, String *wkb);
  int get_data_as_wkt(String *txt) const;
  int get_mbr(MBR *mbr) const;

  int num_geometries(uint32 *num) const;
  int geometry_n(uint32 num, String *result) const;
  int area(double *ar) const;
  int centroid(String *result) const;
  int dimension(uint32 *dim) const { *dim = 2; return 0; }
};

/***************************** GeometryCollection *******************************/

class GGeometryCollection: public Geometry
{
public:
  size_t get_data_size() const;
  int init_from_wkt(GTextReadStream *trs, String *wkb);
  int get_data_as_wkt(String *txt) const;
  int get_mbr(MBR *mbr) const;

  int num_geometries(uint32 *num) const;
  int geometry_n(uint32 num, String *result) const;
  int dimension(uint32 *dim) const;
};

#endif