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

Definition at line 146 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().

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

Here is the call graph for this function:

Here is the caller graph for this function: