PostGIS  2.2.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  * Copyright 2001-2006 Refractions Research Inc.
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "liblwgeom_internal.h"
16 
17 void
19 {
21 }
22 
23 LWMLINE *
24 lwmline_construct_empty(int srid, char hasz, char hasm)
25 {
26  LWMLINE *ret = (LWMLINE*)lwcollection_construct_empty(MULTILINETYPE, srid, hasz, hasm);
27  return ret;
28 }
29 
30 
31 
33 {
34  return (LWMLINE*)lwcollection_add_lwgeom((LWCOLLECTION*)mobj, (LWGEOM*)obj);
35 }
36 
41 LWMLINE*
42 lwmline_measured_from_lwmline(const LWMLINE *lwmline, double m_start, double m_end)
43 {
44  int i = 0;
45  int hasm = 0, hasz = 0;
46  double length = 0.0, length_so_far = 0.0;
47  double m_range = m_end - m_start;
48  LWGEOM **geoms = NULL;
49 
50  if ( lwmline->type != MULTILINETYPE )
51  {
52  lwerror("lwmline_measured_from_lmwline: only multiline types supported");
53  return NULL;
54  }
55 
56  hasz = FLAGS_GET_Z(lwmline->flags);
57  hasm = 1;
58 
59  /* Calculate the total length of the mline */
60  for ( i = 0; i < lwmline->ngeoms; i++ )
61  {
62  LWLINE *lwline = (LWLINE*)lwmline->geoms[i];
63  if ( lwline->points && lwline->points->npoints > 1 )
64  {
65  length += ptarray_length_2d(lwline->points);
66  }
67  }
68 
69  if ( lwgeom_is_empty((LWGEOM*)lwmline) )
70  {
71  return (LWMLINE*)lwcollection_construct_empty(MULTILINETYPE, lwmline->srid, hasz, hasm);
72  }
73 
74  geoms = lwalloc(sizeof(LWGEOM*) * lwmline->ngeoms);
75 
76  for ( i = 0; i < lwmline->ngeoms; i++ )
77  {
78  double sub_m_start, sub_m_end;
79  double sub_length = 0.0;
80  LWLINE *lwline = (LWLINE*)lwmline->geoms[i];
81 
82  if ( lwline->points && lwline->points->npoints > 1 )
83  {
84  sub_length = ptarray_length_2d(lwline->points);
85  }
86 
87  sub_m_start = (m_start + m_range * length_so_far / length);
88  sub_m_end = (m_start + m_range * (length_so_far + sub_length) / length);
89 
90  geoms[i] = (LWGEOM*)lwline_measured_from_lwline(lwline, sub_m_start, sub_m_end);
91 
92  length_so_far += sub_length;
93  }
94 
95  return (LWMLINE*)lwcollection_construct(lwmline->type, lwmline->srid, NULL, lwmline->ngeoms, geoms);
96 }
97 
98 void lwmline_free(LWMLINE *mline)
99 {
100  int i;
101  if ( ! mline ) return;
102 
103  if ( mline->bbox )
104  lwfree(mline->bbox);
105 
106  for ( i = 0; i < mline->ngeoms; i++ )
107  if ( mline->geoms && mline->geoms[i] )
108  lwline_free(mline->geoms[i]);
109 
110  if ( mline->geoms )
111  lwfree(mline->geoms);
112 
113  lwfree(mline);
114 }
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:367
uint8_t type
Definition: liblwgeom.h:461
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
void lwfree(void *mem)
Definition: lwutil.c:214
int npoints
Definition: liblwgeom.h:355
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:63
LWGEOM * lwmline_as_lwgeom(const LWMLINE *obj)
Definition: lwgeom.c:209
int ngeoms
Definition: liblwgeom.h:465
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:42
void lwmline_free(LWMLINE *mline)
Definition: lwmline.c:98
uint8_t flags
Definition: liblwgeom.h:462
void lwmline_release(LWMLINE *lwmline)
Definition: lwmline.c:18
#define FLAGS_GET_Z(flags)
Macros for manipulating the 'flags' byte.
Definition: liblwgeom.h:124
LWMLINE * lwmline_construct_empty(int srid, char hasz, char hasm)
Definition: lwmline.c:24
LWLINE ** geoms
Definition: liblwgeom.h:467
void lwgeom_release(LWGEOM *lwgeom)
Free the containing LWGEOM and the associated BOX.
Definition: lwgeom.c:372
void * lwalloc(size_t size)
Definition: lwutil.c:199
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:1297
#define MULTILINETYPE
Definition: liblwgeom.h:74
LWCOLLECTION * lwcollection_construct_empty(uint8_t type, int srid, char hasz, char hasm)
Definition: lwcollection.c:81
LWCOLLECTION * lwcollection_add_lwgeom(LWCOLLECTION *col, const LWGEOM *geom)
Appends geom to the collection managed by col.
Definition: lwcollection.c:174
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:74
if(!(yy_init))
Definition: lwin_wkt_lex.c:863
GBOX * bbox
Definition: liblwgeom.h:463
LWMLINE * lwmline_add_lwline(LWMLINE *mobj, const LWLINE *obj)
Definition: lwmline.c:32
int32_t srid
Definition: liblwgeom.h:464
POINTARRAY * points
Definition: liblwgeom.h:406