PostGIS  2.1.10dev-r@@SVN_REVISION@@
int ptarray_insert_point ( POINTARRAY pa,
const POINT4D p,
int  where 
)

Insert a point into an existing POINTARRAY.

Zero is the index of the start of the array.

Definition at line 84 of file ptarray.c.

References POINTARRAY::flags, FLAGS_GET_READONLY, getPoint_internal(), LW_FAILURE, LW_SUCCESS, lwalloc(), LWDEBUGF, lwerror(), lwrealloc(), POINTARRAY::maxpoints, POINTARRAY::npoints, ptarray_point_size(), ptarray_set_point4d(), and POINTARRAY::serialized_pointlist.

Referenced by lwline_add_lwpoint(), ptarray_append_point(), and test_ptarray_insert_point().

85 {
86  size_t point_size = ptarray_point_size(pa);
87  LWDEBUGF(5,"pa = %p; p = %p; where = %d", pa, p, where);
88  LWDEBUGF(5,"pa->npoints = %d; pa->maxpoints = %d", pa->npoints, pa->maxpoints);
89 
90  if ( FLAGS_GET_READONLY(pa->flags) )
91  {
92  lwerror("ptarray_insert_point: called on read-only point array");
93  return LW_FAILURE;
94  }
95 
96  /* Error on invalid offset value */
97  if ( where > pa->npoints || where < 0)
98  {
99  lwerror("ptarray_insert_point: offset out of range (%d)", where);
100  return LW_FAILURE;
101  }
102 
103  /* If we have no storage, let's allocate some */
104  if( pa->maxpoints == 0 || ! pa->serialized_pointlist )
105  {
106  pa->maxpoints = 32;
107  pa->npoints = 0;
109  }
110 
111  /* Error out if we have a bad situation */
112  if ( pa->npoints > pa->maxpoints )
113  lwerror("npoints (%d) is greated than maxpoints (%d)", pa->npoints, pa->maxpoints);
114 
115  /* Check if we have enough storage, add more if necessary */
116  if( pa->npoints == pa->maxpoints )
117  {
118  pa->maxpoints *= 2;
120  }
121 
122  /* Make space to insert the new point */
123  if( where < pa->npoints )
124  {
125  size_t copy_size = point_size * (pa->npoints - where);
126  memmove(getPoint_internal(pa, where+1), getPoint_internal(pa, where), copy_size);
127  LWDEBUGF(5,"copying %d bytes to start vertex %d from start vertex %d", copy_size, where+1, where);
128  }
129 
130  /* We have one more point */
131  ++pa->npoints;
132 
133  /* Copy the new point into the gap */
134  ptarray_set_point4d(pa, where, p);
135  LWDEBUGF(5,"copying new point to start vertex %d", point_size, where);
136 
137  return LW_SUCCESS;
138 }
void ptarray_set_point4d(POINTARRAY *pa, int n, const POINT4D *p4d)
Definition: lwgeom_api.c:501
uint8_t * serialized_pointlist
Definition: liblwgeom.h:322
int ptarray_point_size(const POINTARRAY *pa)
Definition: ptarray.c:41
#define FLAGS_GET_READONLY(flags)
Definition: liblwgeom.h:110
int npoints
Definition: liblwgeom.h:327
#define LW_SUCCESS
Definition: liblwgeom.h:55
#define LW_FAILURE
Definition: liblwgeom.h:54
void lwerror(const char *fmt,...)
Write a notice out to the error handler.
Definition: lwutil.c:67
uint8_t flags
Definition: liblwgeom.h:325
void * lwrealloc(void *mem, size_t size)
Definition: lwutil.c:183
int maxpoints
Definition: liblwgeom.h:328
void * lwalloc(size_t size)
Definition: lwutil.c:175
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55
uint8_t * getPoint_internal(const POINTARRAY *pa, int n)
Definition: ptarray.c:1645

Here is the call graph for this function:

Here is the caller graph for this function: