~ubuntu-branches/ubuntu/trusty/postgis/trusty-security

« back to all changes in this revision

Viewing changes to lwgeom/lwgeom_functions_lrs.c

  • Committer: Bazaar Package Importer
  • Author(s): Francesco Paolo Lovergine
  • Date: 2009-12-11 13:10:34 UTC
  • mfrom: (1.1.9 upstream) (5.2.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20091211131034-wmsz69wxvt95pe5r
Tags: 1.4.0-2
* Upload to unstable.
* Better parameterized debian/rules against postgis $(VERSION).
* Added dblatex and libcunit1-dev among build-deps.
* Added postgis_comments.sql to contrib/ SQL templates.
* Dropping 8.3 support, no more supported for squeeze.
  (closes: #559587)
* Do not stop on error in postrm if the target dir does not exist.
  (closes: #560409)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**********************************************************************
2
 
 * $Id: lwgeom_functions_lrs.c 3358 2008-12-03 16:27:44Z mcayland $
3
 
 *
4
 
 * PostGIS - Spatial Types for PostgreSQL
5
 
 * http://postgis.refractions.net
6
 
 * Copyright 2001-2005 Refractions Research Inc.
7
 
 *
8
 
 * This is free software; you can redistribute and/or modify it under
9
 
 * the terms of the GNU General Public Licence. See the COPYING file.
10
 
 * 
11
 
 **********************************************************************/
12
 
 
13
 
#include "postgres.h"
14
 
#include "fmgr.h"
15
 
#include "liblwgeom.h"
16
 
#include "lwgeom_pg.h"
17
 
#include "math.h"
18
 
 
19
 
#define DEBUG_LRS 0
20
 
#define DEBUG_INTERPOLATION 0
21
 
 
22
 
Datum LWGEOM_locate_between_m(PG_FUNCTION_ARGS);
23
 
 
24
 
typedef struct {
25
 
        POINTARRAY **ptarrays;
26
 
        uint32 nptarrays;
27
 
} POINTARRAYSET;
28
 
 
29
 
static POINTARRAYSET ptarray_locate_between_m(
30
 
        POINTARRAY *ipa, double m0, double m1);
31
 
 
32
 
static LWGEOM *lwcollection_locate_between_m(
33
 
        LWCOLLECTION *lwcoll, double m0, double m1);
34
 
 
35
 
static LWGEOM *lwgeom_locate_between_m(
36
 
        LWGEOM *lwin, double m0, double m1);
37
 
 
38
 
static LWGEOM *lwline_locate_between_m(
39
 
        LWLINE *lwline_in, double m0, double m1);
40
 
 
41
 
static LWGEOM *lwpoint_locate_between_m(
42
 
        LWPOINT *lwpoint, double m0, double m1);
43
 
 
44
 
static int clip_seg_by_m_range(
45
 
        POINT4D *p1, POINT4D *p2, double m0, double m1);
46
 
 
47
 
 
48
 
/*
49
 
 * Clip a segment by a range of measures.
50
 
 * Z and M values are interpolated in case of clipping.
51
 
 *
52
 
 * Returns a bitfield, flags being:
53
 
 *    0x0001 : segment intersects the range
54
 
 *    0x0010 : first point is modified
55
 
 *    0x0100 : second point is modified
56
 
 *
57
 
 * Values:
58
 
 *     - 0 segment fully outside the range, no modifications
59
 
 *     - 1 segment fully inside the range, no modifications
60
 
 *     - 7 segment crosses the range, both points modified.
61
 
 *     - 3 first point out, second in, first point modified
62
 
 *     - 5 first point in, second out, second point modified
63
 
 */
64
 
static int
65
 
clip_seg_by_m_range(POINT4D *p1, POINT4D *p2, double m0, double m1)
66
 
{
67
 
        double dM0, dM1, dX, dY, dZ;
68
 
        POINT4D *tmp;
69
 
        int swapped=0;
70
 
        int ret=0;
71
 
 
72
 
        /* Handle corner case of m values being the same */
73
 
        if ( p1->m == p2->m )
74
 
        {
75
 
                /* out of range, no clipping */
76
 
                if ( p1->m < m0 || p1->m > m1 )
77
 
                        return 0;
78
 
 
79
 
                /* inside range, no clipping */
80
 
                        return 1;
81
 
        }
82
 
 
83
 
        /*
84
 
         * Order points so that p1 has the smaller M
85
 
         */
86
 
        if ( p1->m > p2->m )
87
 
        {
88
 
                tmp=p2;
89
 
                p2=p1;
90
 
                p1=tmp;
91
 
                swapped=1;
92
 
        }
93
 
 
94
 
        /*
95
 
         * The M range is not intersected, segment
96
 
         * fully out of range, no clipping.
97
 
         */
98
 
        if ( p2->m < m0 || p1->m > m1 )
99
 
                return 0;
100
 
 
101
 
        /*
102
 
         * The segment is fully inside the range,
103
 
         * no clipping.
104
 
         */
105
 
        if ( p1->m >= m0 && p2->m <= m1 ) 
106
 
                return 1;
107
 
 
108
 
        /*
109
 
         * Segment intersects range, lets compute
110
 
         * the proportional location of the two
111
 
         * measures wrt p1/p2 m range.
112
 
         *
113
 
         * if p1 and p2 have the same measure
114
 
         * this should never be reached (either
115
 
         * both inside or both outside)
116
 
         * 
117
 
         */
118
 
        dM0=(m0-p1->m)/(p2->m-p1->m); /* delta-M0 */
119
 
        dM1=(m1-p2->m)/(p2->m-p1->m); /* delta-M1 */
120
 
        dX=p2->x-p1->x;
121
 
        dY=p2->y-p1->y;
122
 
        dZ=p2->z-p1->z;
123
 
#if DEBUG_INTERPOLATION
124
 
        lwnotice("dM0:%g dM1:%g", dM0, dM1);
125
 
        lwnotice("dX:%g dY:%g dZ:%g", dX, dY, dZ);
126
 
#endif
127
 
 
128
 
 
129
 
        /* 
130
 
         * First point out of range, project 
131
 
         * it on the range
132
 
         */
133
 
        if ( p1->m < m0 )
134
 
        {
135
 
                /*
136
 
                 * To prevent rounding errors, then if m0==m1 and p2 lies within the range, copy
137
 
                 * p1 as a direct copy of p2
138
 
                 */
139
 
                if (m0 == m1 && p2->m <= m1)
140
 
                {
141
 
                        memcpy(p1, p2, sizeof(POINT4D));
142
 
 
143
 
#if DEBUG_INTERPOLATION
144
 
                        lwnotice("Projected p1 on range (as copy of p2)");
145
 
#endif
146
 
                }
147
 
                else
148
 
                {
149
 
                        /* Otherwise interpolate coordinates */
150
 
                        p1->x += (dX*dM0);
151
 
                        p1->y += (dY*dM0);
152
 
                        p1->z += (dZ*dM0);
153
 
                        p1->m = m0;
154
 
 
155
 
#if DEBUG_INTERPOLATION
156
 
                        lwnotice("Projected p1 on range");
157
 
#endif
158
 
                }
159
 
 
160
 
                if ( swapped ) ret |= 0x0100;
161
 
                else ret |= 0x0010;
162
 
        }
163
 
 
164
 
        /* 
165
 
         * Second point out of range, project 
166
 
         * it on the range
167
 
         */
168
 
        if ( p2->m > m1 )
169
 
        {
170
 
                /*
171
 
                 * To prevent rounding errors, then if m0==m1 and p1 lies within the range, copy
172
 
                 * p2 as a direct copy of p1
173
 
                 */
174
 
                if (m0 == m1 && p1->m >= m0)
175
 
                {
176
 
                        memcpy(p2, p1, sizeof(POINT4D));
177
 
 
178
 
#if DEBUG_INTERPOLATION
179
 
                        lwnotice("Projected p2 on range (as copy of p1)");
180
 
#endif
181
 
                }
182
 
                else
183
 
                {
184
 
                        /* Otherwise interpolate coordinates */
185
 
                        p2->x += (dX*dM1);
186
 
                        p2->y += (dY*dM1);
187
 
                        p2->z += (dZ*dM1);
188
 
                        p2->m = m1;
189
 
 
190
 
#if DEBUG_INTERPOLATION
191
 
                        lwnotice("Projected p2 on range");
192
 
#endif
193
 
                }
194
 
 
195
 
                if ( swapped ) ret |= 0x0010;
196
 
                else ret |= 0x0100;
197
 
        }
198
 
 
199
 
        /* Clipping occurred */
200
 
        return ret;
201
 
 
202
 
}
203
 
 
204
 
static POINTARRAYSET 
205
 
ptarray_locate_between_m(POINTARRAY *ipa, double m0, double m1)
206
 
{
207
 
        POINTARRAYSET ret;
208
 
        DYNPTARRAY *dpa=NULL;
209
 
        int i;
210
 
 
211
 
        ret.nptarrays=0;
212
 
 
213
 
        /*
214
 
         * We allocate space for as many pointarray as
215
 
         * segments in the input POINTARRAY, as worst
216
 
         * case is for each segment to cross the M range
217
 
         * window. 
218
 
         * TODO: rework this to reduce used memory
219
 
         */
220
 
        ret.ptarrays=lwalloc(sizeof(POINTARRAY *)*ipa->npoints-1);
221
 
 
222
 
#if DEBUG_LRS
223
 
        lwnotice("ptarray_locate...: called for pointarray %x, m0:%g, m1:%g",
224
 
                ipa, m0, m1);
225
 
#endif
226
 
 
227
 
 
228
 
        for(i=1; i<ipa->npoints; i++)
229
 
        {
230
 
                POINT4D p1, p2;
231
 
                int clipval;
232
 
 
233
 
                getPoint4d_p(ipa, i-1, &p1);
234
 
                getPoint4d_p(ipa, i, &p2);
235
 
 
236
 
#if DEBUG_LRS
237
 
                lwnotice(" segment %d-%d [ %g %g %g %g -  %g %g %g %g ]",
238
 
                        i-1, i,
239
 
                        p1.x, p1.y, p1.z, p1.m,
240
 
                        p2.x, p2.y, p2.z, p2.m);
241
 
#endif
242
 
 
243
 
 
244
 
                clipval = clip_seg_by_m_range(&p1, &p2, m0, m1);
245
 
                                
246
 
                /* segment completely outside, nothing to do */
247
 
                if ( ! clipval ) continue;
248
 
 
249
 
#if DEBUG_LRS
250
 
                lwnotice(" clipped to: [ %g %g %g %g - %g %g %g %g ]   clipval: %x", p1.x, p1.y, p1.z, p1.m,
251
 
                        p2.x, p2.y, p2.z, p2.m, clipval);
252
 
#endif
253
 
 
254
 
                /* If no points have been accumulated so far, then if clipval != 0 the first point must be the
255
 
                   start of the intersection */
256
 
                if (dpa == NULL)
257
 
                {
258
 
#if DEBUG_LRS
259
 
                        lwnotice(" 1 creating new POINTARRAY with first point %g,%g,%g,%g", p1.x, p1.y, p1.z, p1.m);
260
 
#endif
261
 
                        dpa = dynptarray_create(ipa->npoints-i, ipa->dims);
262
 
                        dynptarray_addPoint4d(dpa, &p1, 1);
263
 
                }
264
 
 
265
 
                /* Otherwise always add the next point, avoiding duplicates */
266
 
                if (dpa)
267
 
                        dynptarray_addPoint4d(dpa, &p2, 0);
268
 
 
269
 
                /*
270
 
                 * second point has been clipped
271
 
                 */
272
 
                if ( clipval & 0x0100 || i == ipa->npoints-1 )
273
 
                {
274
 
#if DEBUG_LRS
275
 
                        lwnotice(" closing pointarray %x with %d points", dpa->pa, dpa->pa->npoints);
276
 
#endif
277
 
                        ret.ptarrays[ret.nptarrays++] = dpa->pa;
278
 
                        lwfree(dpa); dpa=NULL;
279
 
                }
280
 
        }
281
 
 
282
 
        /* 
283
 
         * if dpa!=NULL it means we didn't close it yet.
284
 
         * this should never happen.
285
 
         */
286
 
        if ( dpa != NULL ) lwerror("Something wrong with algorithm");
287
 
 
288
 
        return ret;
289
 
}
290
 
 
291
 
/*
292
 
 * Point is assumed to have an M value.
293
 
 * Return NULL if point is not in the given range (inclusive)
294
 
 * Return an LWPOINT *copy* otherwise.
295
 
 */
296
 
static LWGEOM *
297
 
lwpoint_locate_between_m(LWPOINT *lwpoint, double m0, double m1)
298
 
{
299
 
        POINT3DM p3dm;
300
 
 
301
 
#if DEBUG_LRS
302
 
        lwnotice("lwpoint_locate_between called for lwpoint %x", lwpoint);
303
 
#endif
304
 
 
305
 
        lwpoint_getPoint3dm_p(lwpoint, &p3dm);
306
 
        if ( p3dm.m >= m0 && p3dm.m <= m1)
307
 
        {
308
 
#if DEBUG_LRS
309
 
                lwnotice(" lwpoint... returning a clone of input");
310
 
#endif
311
 
                return (LWGEOM *)lwpoint_clone(lwpoint);
312
 
        }
313
 
        else
314
 
        {
315
 
#if DEBUG_LRS
316
 
                lwnotice(" lwpoint... returning a clone of input");
317
 
#endif
318
 
                return NULL;
319
 
        }
320
 
}
321
 
 
322
 
/*
323
 
 * Line is assumed to have an M value.
324
 
 *
325
 
 * Return NULL if no parts of the line are in the given range (inclusive)
326
 
 *
327
 
 * Return an LWCOLLECTION with LWLINES and LWPOINT being consecutive
328
 
 * and isolated points on the line falling in the range.
329
 
 *
330
 
 * X,Y and Z (if present) ordinates are interpolated.
331
 
 *
332
 
 */
333
 
static LWGEOM *
334
 
lwline_locate_between_m(LWLINE *lwline_in, double m0, double m1)
335
 
{               
336
 
        POINTARRAY *ipa=lwline_in->points;
337
 
        int i;
338
 
        LWGEOM **geoms;
339
 
        int ngeoms;
340
 
        int outtype;
341
 
        int typeflag=0; /* see flags below */
342
 
        const int pointflag=0x01;
343
 
        const int lineflag=0x10;
344
 
 
345
 
#if DEBUG_LRS
346
 
        lwnotice("lwline_locate_between called for lwline %x", lwline_in);
347
 
#endif
348
 
 
349
 
        POINTARRAYSET paset=ptarray_locate_between_m(ipa, m0, m1);
350
 
 
351
 
#if DEBUG_LRS
352
 
        lwnotice(" ptarray_locate... returned %d pointarrays",
353
 
                paset.nptarrays);
354
 
#endif
355
 
 
356
 
        if ( paset.nptarrays == 0 )
357
 
        {
358
 
                return NULL;
359
 
        }
360
 
 
361
 
        ngeoms=paset.nptarrays;
362
 
        /* TODO: rework this to reduce used memory */
363
 
        geoms=lwalloc(sizeof(LWGEOM *)*ngeoms);
364
 
        for (i=0; i<ngeoms; i++)
365
 
        {
366
 
                LWPOINT *lwpoint;
367
 
                LWLINE *lwline;
368
 
 
369
 
                POINTARRAY *pa=paset.ptarrays[i];
370
 
 
371
 
                /* This is a point */
372
 
                if ( pa->npoints == 1 )
373
 
                {
374
 
 
375
 
                        lwpoint=lwalloc(sizeof(LWPOINT));
376
 
                        lwpoint->type=lwgeom_makeType_full(
377
 
                                TYPE_HASZ(pa->dims),
378
 
                                TYPE_HASM(pa->dims),
379
 
                                lwline_in->SRID,
380
 
                                POINTTYPE,
381
 
                                0);
382
 
                        lwpoint->SRID=lwline_in->SRID;
383
 
                        lwpoint->bbox=NULL;
384
 
                        lwpoint->point=pa;
385
 
                        geoms[i]=(LWGEOM *)lwpoint;
386
 
                        typeflag|=pointflag;
387
 
                }
388
 
 
389
 
                /* This is a line */
390
 
                else if ( pa->npoints > 1 )
391
 
                {
392
 
                        lwline=lwalloc(sizeof(LWLINE));
393
 
                        lwline->type=lwgeom_makeType_full(
394
 
                                TYPE_HASZ(pa->dims),
395
 
                                TYPE_HASM(pa->dims),
396
 
                                lwline_in->SRID,
397
 
                                LINETYPE,
398
 
                                0);
399
 
                        lwline->SRID=lwline_in->SRID;
400
 
                        lwline->bbox=NULL;
401
 
                        lwline->points=pa;
402
 
                        geoms[i]=(LWGEOM *)lwline;
403
 
                        typeflag|=lineflag;
404
 
                }
405
 
 
406
 
                /* This is a bug */
407
 
                else
408
 
                {
409
 
                        lwerror("ptarray_locate_between_m returned a POINARRAY set containing POINTARRAY with 0 points");
410
 
                }
411
 
 
412
 
 
413
 
 
414
 
        }
415
 
 
416
 
        if ( ngeoms == 1 )
417
 
        {
418
 
                return geoms[0];
419
 
        }
420
 
        else
421
 
        {
422
 
                /* Choose best type */
423
 
                if ( typeflag == 1 ) outtype=MULTIPOINTTYPE;
424
 
                else if ( typeflag == 2 ) outtype=MULTILINETYPE;
425
 
                else outtype = COLLECTIONTYPE;
426
 
 
427
 
                return (LWGEOM *)lwcollection_construct(outtype,
428
 
                        lwline_in->SRID, NULL, ngeoms, geoms);
429
 
        }
430
 
}
431
 
 
432
 
/*
433
 
 * Return a fully new allocated LWCOLLECTION
434
 
 * always tagged as COLLECTIONTYPE.
435
 
 */
436
 
static LWGEOM *
437
 
lwcollection_locate_between_m(LWCOLLECTION *lwcoll, double m0, double m1)
438
 
{
439
 
        int i;
440
 
        int ngeoms=0;
441
 
        LWGEOM **geoms;
442
 
 
443
 
#if DEBUG_LRS
444
 
        lwnotice("lwcollection_locate_between_m called for lwcoll %x", lwcoll);
445
 
#endif
446
 
 
447
 
        geoms=lwalloc(sizeof(LWGEOM *)*lwcoll->ngeoms);
448
 
        for (i=0; i<lwcoll->ngeoms; i++)
449
 
        {
450
 
                LWGEOM *sub=lwgeom_locate_between_m(lwcoll->geoms[i],
451
 
                        m0, m1);
452
 
                if ( sub != NULL )
453
 
                        geoms[ngeoms++] = sub;
454
 
        }
455
 
 
456
 
        if ( ngeoms == 0 ) return NULL;
457
 
 
458
 
        return (LWGEOM *)lwcollection_construct(COLLECTIONTYPE,
459
 
                lwcoll->SRID, NULL, ngeoms, geoms);
460
 
}
461
 
 
462
 
/*
463
 
 * Return a fully allocated LWGEOM containing elements
464
 
 * intersected/interpolated with the given M range.
465
 
 * Return NULL if none of the elements fall in the range.
466
 
 *
467
 
 * m0 is assumed to be less-or-equal to m1.
468
 
 * input LWGEOM is assumed to contain an M value.
469
 
 *
470
 
 */
471
 
static LWGEOM *
472
 
lwgeom_locate_between_m(LWGEOM *lwin, double m0, double m1)
473
 
{
474
 
#if DEBUG_LRS
475
 
        lwnotice("lwgeom_locate_between called for lwgeom %x", lwin);
476
 
#endif
477
 
        switch (TYPE_GETTYPE(lwin->type))
478
 
        {
479
 
                case POINTTYPE:
480
 
                        return lwpoint_locate_between_m(
481
 
                                (LWPOINT *)lwin, m0, m1);
482
 
                case LINETYPE:
483
 
                        return lwline_locate_between_m(
484
 
                                (LWLINE *)lwin, m0, m1);
485
 
 
486
 
                case MULTIPOINTTYPE:
487
 
                case MULTILINETYPE:
488
 
                case COLLECTIONTYPE:
489
 
                        return lwcollection_locate_between_m(
490
 
                                (LWCOLLECTION *)lwin, m0, m1);
491
 
 
492
 
                /* Polygon types are not supported */
493
 
                case POLYGONTYPE:
494
 
                case MULTIPOLYGONTYPE:
495
 
                        lwerror("Areal geometries are not supported by locate_between_measures");
496
 
                        return NULL;
497
 
        }
498
 
 
499
 
        lwerror("Unkonwn geometry type (%s:%d)", __FILE__, __LINE__);
500
 
        return NULL;
501
 
}
502
 
 
503
 
/*
504
 
 * Return a derived geometry collection value with elements that match
505
 
 * the specified range of measures inclusively.
506
 
 *
507
 
 * Implements SQL/MM ST_LocateBetween(measure, measure) method.
508
 
 * See ISO/IEC CD 13249-3:200x(E)
509
 
 *
510
 
 */
511
 
PG_FUNCTION_INFO_V1(LWGEOM_locate_between_m);
512
 
Datum LWGEOM_locate_between_m(PG_FUNCTION_ARGS)
513
 
{
514
 
        PG_LWGEOM *gin = (PG_LWGEOM *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));
515
 
        PG_LWGEOM *gout;
516
 
        double start_measure = PG_GETARG_FLOAT8(1);
517
 
        double end_measure = PG_GETARG_FLOAT8(2);
518
 
        LWGEOM *lwin, *lwout;
519
 
        int type;
520
 
 
521
 
        if( end_measure < start_measure )
522
 
        {
523
 
                lwerror("locate_between_m: 2nd arg must be bigger then 1st arg");
524
 
                PG_RETURN_NULL();
525
 
        }
526
 
 
527
 
        /*
528
 
         * Return NULL if input doesn't have a measure
529
 
         */
530
 
        if ( ! lwgeom_hasM(gin->type) )
531
 
        {
532
 
                PG_RETURN_NULL();
533
 
        }
534
 
 
535
 
        /*
536
 
         * Raise an error if input is a polygon, a multipolygon
537
 
         * or a collection
538
 
         */
539
 
        type=lwgeom_getType(gin->type);
540
 
        if ( type == POLYGONTYPE || type == MULTIPOLYGONTYPE || type == COLLECTIONTYPE )
541
 
        {
542
 
                lwerror("Areal or Collection types are not supported");
543
 
                PG_RETURN_NULL();
544
 
        }
545
 
 
546
 
        lwin = pglwgeom_deserialize(gin);
547
 
 
548
 
        lwout = lwgeom_locate_between_m(lwin,
549
 
                start_measure, end_measure);
550
 
 
551
 
        lwgeom_release(lwin);
552
 
 
553
 
        if ( lwout == NULL )
554
 
        {
555
 
                lwout = (LWGEOM *)lwcollection_construct_empty(
556
 
                        pglwgeom_getSRID(gin), lwgeom_hasZ(gin->type),
557
 
                        lwgeom_hasM(gin->type));
558
 
        }
559
 
 
560
 
        gout = pglwgeom_serialize(lwout);
561
 
        lwgeom_release(lwout);
562
 
 
563
 
        PG_RETURN_POINTER(gout);
564
 
}
565