1 /**********************************************************************
2 *
3 * PostGIS - Spatial Types for PostgreSQL
4 * http://postgis.net
5 *
6 * PostGIS is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * PostGIS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with PostGIS. If not, see <http://www.gnu.org/licenses/>.
18 *
19 **********************************************************************
20 *
21 * Copyright 2001-2006 Refractions Research Inc.
22 *
23 **********************************************************************/
24
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "liblwgeom_internal.h"
30
31 void
lwmline_release(LWMLINE * lwmline)32 lwmline_release(LWMLINE *lwmline)
33 {
34 lwgeom_release(lwmline_as_lwgeom(lwmline));
35 }
36
37 LWMLINE *
lwmline_construct_empty(int srid,char hasz,char hasm)38 lwmline_construct_empty(int srid, char hasz, char hasm)
39 {
40 LWMLINE *ret = (LWMLINE*)lwcollection_construct_empty(MULTILINETYPE, srid, hasz, hasm);
41 return ret;
42 }
43
44
45
lwmline_add_lwline(LWMLINE * mobj,const LWLINE * obj)46 LWMLINE* lwmline_add_lwline(LWMLINE *mobj, const LWLINE *obj)
47 {
48 return (LWMLINE*)lwcollection_add_lwgeom((LWCOLLECTION*)mobj, (LWGEOM*)obj);
49 }
50
51 /**
52 * Re-write the measure ordinate (or add one, if it isn't already there) interpolating
53 * the measure between the supplied start and end values.
54 */
55 LWMLINE*
lwmline_measured_from_lwmline(const LWMLINE * lwmline,double m_start,double m_end)56 lwmline_measured_from_lwmline(const LWMLINE *lwmline, double m_start, double m_end)
57 {
58 uint32_t i = 0;
59 int hasm = 0, hasz = 0;
60 double length = 0.0, length_so_far = 0.0;
61 double m_range = m_end - m_start;
62 LWGEOM **geoms = NULL;
63
64 if ( lwmline->type != MULTILINETYPE )
65 {
66 lwerror("lwmline_measured_from_lmwline: only multiline types supported");
67 return NULL;
68 }
69
70 hasz = FLAGS_GET_Z(lwmline->flags);
71 hasm = 1;
72
73 /* Calculate the total length of the mline */
74 for ( i = 0; i < lwmline->ngeoms; i++ )
75 {
76 LWLINE *lwline = (LWLINE*)lwmline->geoms[i];
77 if ( lwline->points && lwline->points->npoints > 1 )
78 {
79 length += ptarray_length_2d(lwline->points);
80 }
81 }
82
83 if ( lwgeom_is_empty((LWGEOM*)lwmline) )
84 {
85 return (LWMLINE*)lwcollection_construct_empty(MULTILINETYPE, lwmline->srid, hasz, hasm);
86 }
87
88 geoms = lwalloc(sizeof(LWGEOM*) * lwmline->ngeoms);
89
90 for ( i = 0; i < lwmline->ngeoms; i++ )
91 {
92 double sub_m_start, sub_m_end;
93 double sub_length = 0.0;
94 LWLINE *lwline = (LWLINE*)lwmline->geoms[i];
95
96 if ( lwline->points && lwline->points->npoints > 1 )
97 {
98 sub_length = ptarray_length_2d(lwline->points);
99 }
100
101 sub_m_start = (m_start + m_range * length_so_far / length);
102 sub_m_end = (m_start + m_range * (length_so_far + sub_length) / length);
103
104 geoms[i] = (LWGEOM*)lwline_measured_from_lwline(lwline, sub_m_start, sub_m_end);
105
106 length_so_far += sub_length;
107 }
108
109 return (LWMLINE*)lwcollection_construct(lwmline->type, lwmline->srid, NULL, lwmline->ngeoms, geoms);
110 }
111
lwmline_free(LWMLINE * mline)112 void lwmline_free(LWMLINE *mline)
113 {
114 uint32_t i;
115 if ( ! mline ) return;
116
117 if ( mline->bbox )
118 lwfree(mline->bbox);
119
120 for ( i = 0; i < mline->ngeoms; i++ )
121 if ( mline->geoms && mline->geoms[i] )
122 lwline_free(mline->geoms[i]);
123
124 if ( mline->geoms )
125 lwfree(mline->geoms);
126
127 lwfree(mline);
128 }
129