PostGIS  2.2.7dev-r@@SVN_REVISION@@
static SHPObject * create_polygon ( SHPDUMPERSTATE state,
LWPOLY lwpolygon 
)
static

Definition at line 145 of file pgsql2shp-core.c.

References free(), getPoint4d(), is_clockwise(), LWDEBUG, LWDEBUGF, POINT4D::m, malloc(), POINTARRAY::npoints, LWPOLY::nrings, shp_dumper_state::outshptype, reverse_points(), LWPOLY::rings, SHPCreateObject(), POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by ShpLoaderGenerateShapeRow().

146 {
147  SHPObject *obj;
148  POINT4D p4d;
149  int i, j;
150 
151  double *xpts, *ypts, *zpts, *mpts;
152 
153  int *shpparts, shppointtotal = 0, shppoint = 0;
154 
155  /* Allocate storage for ring pointers */
156  shpparts = malloc(sizeof(int) * lwpolygon->nrings);
157 
158  /* First count through all the points in each ring so we now how much memory is required */
159  for (i = 0; i < lwpolygon->nrings; i++)
160  shppointtotal += lwpolygon->rings[i]->npoints;
161 
162  /* Allocate storage for points */
163  xpts = malloc(sizeof(double) * shppointtotal);
164  ypts = malloc(sizeof(double) * shppointtotal);
165  zpts = malloc(sizeof(double) * shppointtotal);
166  mpts = malloc(sizeof(double) * shppointtotal);
167 
168  LWDEBUGF(4, "Total number of points: %d", shppointtotal);
169 
170  /* Iterate through each ring setting up shpparts to point to the beginning of each ring */
171  for (i = 0; i < lwpolygon->nrings; i++)
172  {
173  /* For each ring, store the integer coordinate offset for the start of each ring */
174  shpparts[i] = shppoint;
175 
176  for (j = 0; j < lwpolygon->rings[i]->npoints; j++)
177  {
178  p4d = getPoint4d(lwpolygon->rings[i], j);
179 
180  xpts[shppoint] = p4d.x;
181  ypts[shppoint] = p4d.y;
182  zpts[shppoint] = p4d.z;
183  mpts[shppoint] = p4d.m;
184 
185  LWDEBUGF(4, "Polygon Ring %d - Point: %g %g %g %g", i, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);
186 
187  /* Increment the point counter */
188  shppoint++;
189  }
190 
191  /*
192  * First ring should be clockwise,
193  * other rings should be counter-clockwise
194  */
195  if ( i == 0 )
196  {
197  if ( ! is_clockwise(lwpolygon->rings[i]->npoints,
198  &xpts[shpparts[i]], &ypts[shpparts[i]], NULL) )
199  {
200  LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");
201 
202  reverse_points(lwpolygon->rings[i]->npoints,
203  &xpts[shpparts[i]], &ypts[shpparts[i]],
204  &zpts[shpparts[i]], &mpts[shpparts[i]]);
205  }
206  }
207  else
208  {
209  if ( is_clockwise(lwpolygon->rings[i]->npoints,
210  &xpts[shpparts[i]], &ypts[shpparts[i]], NULL) )
211  {
212  LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);
213 
214  reverse_points(lwpolygon->rings[i]->npoints,
215  &xpts[shpparts[i]], &ypts[shpparts[i]],
216  &zpts[shpparts[i]], &mpts[shpparts[i]]);
217  }
218  }
219  }
220 
221  obj = SHPCreateObject(state->outshptype, -1, lwpolygon->nrings, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);
222 
223  free(xpts);
224  free(ypts);
225  free(zpts);
226  free(mpts);
227  free(shpparts);
228 
229  return obj;
230 }
double x
Definition: liblwgeom.h:336
SHPObject SHPAPI_CALL1 * SHPCreateObject(int nSHPType, int nShapeId, int nParts, const int *panPartStart, const int *panPartType, int nVertices, const double *padfX, const double *padfY, const double *padfZ, const double *padfM);SHPObject SHPAPI_CALL1(*) SHPCreateSimpleObject(int nSHPType, int nVertices, const double *padfX, const double *padfY, const double *padfZ
double m
Definition: liblwgeom.h:336
int npoints
Definition: liblwgeom.h:355
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
static int reverse_points(int num_points, double *x, double *y, double *z, double *m)
POINT4D getPoint4d(const POINTARRAY *pa, int n)
Definition: lwgeom_api.c:216
static int is_clockwise(int num_points, double *x, double *y, double *z)
POINTARRAY ** rings
Definition: liblwgeom.h:441
int nrings
Definition: liblwgeom.h:439
double z
Definition: liblwgeom.h:336
void free(void *)
void * malloc(YYSIZE_T)
double y
Definition: liblwgeom.h:336
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55

Here is the call graph for this function:

Here is the caller graph for this function: