PostGIS  2.3.7dev-r@@SVN_REVISION@@
lwmline.c
Go to the documentation of this file.
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
33 {
35 }
36 
37 LWMLINE *
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 
47 {
48  return (LWMLINE*)lwcollection_add_lwgeom((LWCOLLECTION*)mobj, (LWGEOM*)obj);
49 }
50 
55 LWMLINE*
56 lwmline_measured_from_lwmline(const LWMLINE *lwmline, double m_start, double m_end)
57 {
58  int 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 
112 void lwmline_free(LWMLINE *mline)
113 {
114  int 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 }
LWLINE * lwline_measured_from_lwline(const LWLINE *lwline, double m_start, double m_end)
Add a measure dimension to a line, interpolating linearly from the start to the end value...
Definition: lwline.c:396
uint8_t type
Definition: liblwgeom.h:476
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
void lwfree(void *mem)
Definition: lwutil.c:242
int npoints
Definition: liblwgeom.h:370
double ptarray_length_2d(const POINTARRAY *pts)
Find the 2d length of the given POINTARRAY (even if it's 3d)
Definition: ptarray.c:1645
void lwline_free(LWLINE *line)
Definition: lwline.c:76
LWGEOM * lwmline_as_lwgeom(const LWMLINE *obj)
Definition: lwgeom.c:222
int ngeoms
Definition: liblwgeom.h:480
LWMLINE * lwmline_measured_from_lwmline(const LWMLINE *lwmline, double m_start, double m_end)
Re-write the measure ordinate (or add one, if it isn't already there) interpolating the measure betwe...
Definition: lwmline.c:56
void lwmline_free(LWMLINE *mline)
Definition: lwmline.c:112
uint8_t flags
Definition: liblwgeom.h:477
void lwmline_release(LWMLINE *lwmline)
Definition: lwmline.c:32
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:139
LWMLINE * lwmline_construct_empty(int srid, char hasz, char hasm)
Definition: lwmline.c:38
LWLINE ** geoms
Definition: liblwgeom.h:482
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:385
void * lwalloc(size_t size)
Definition: lwutil.c:227
int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members) ...
Definition: lwgeom.c:1310
#define MULTILINETYPE
Definition: liblwgeom.h:88
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:94
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:187
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:102
if(!(yy_init))
Definition: lwin_wkt_lex.c:888
GBOX * bbox
Definition: liblwgeom.h:478
LWMLINE * lwmline_add_lwline(LWMLINE *mobj, const LWLINE *obj)
Definition: lwmline.c:46
int32_t srid
Definition: liblwgeom.h:479
POINTARRAY * points
Definition: liblwgeom.h:421