PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ create_polygon()

static SHPObject * create_polygon ( SHPDUMPERSTATE state,
LWPOLY lwpolygon 
)
static

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

178 {
179  SHPObject *obj;
180  POINT4D p4d;
181  uint32_t i, j;
182 
183  double *xpts, *ypts, *zpts, *mpts;
184 
185  int *shpparts, shppointtotal = 0, shppoint = 0;
186 
187  /* Allocate storage for ring pointers */
188  shpparts = malloc(sizeof(int) * lwpolygon->nrings);
189 
190  /* First count through all the points in each ring so we now how much memory is required */
191  for (i = 0; i < lwpolygon->nrings; i++)
192  shppointtotal += lwpolygon->rings[i]->npoints;
193 
194  /* Allocate storage for points */
195  xpts = malloc(sizeof(double) * shppointtotal);
196  ypts = malloc(sizeof(double) * shppointtotal);
197  zpts = malloc(sizeof(double) * shppointtotal);
198  mpts = malloc(sizeof(double) * shppointtotal);
199 
200  LWDEBUGF(4, "Total number of points: %d", shppointtotal);
201 
202  /* Iterate through each ring setting up shpparts to point to the beginning of each ring */
203  for (i = 0; i < lwpolygon->nrings; i++)
204  {
205  /* For each ring, store the integer coordinate offset for the start of each ring */
206  shpparts[i] = shppoint;
207 
208  for (j = 0; j < lwpolygon->rings[i]->npoints; j++)
209  {
210  p4d = getPoint4d(lwpolygon->rings[i], j);
211 
212  xpts[shppoint] = p4d.x;
213  ypts[shppoint] = p4d.y;
214  zpts[shppoint] = p4d.z;
215  mpts[shppoint] = p4d.m;
216 
217  LWDEBUGF(4, "Polygon Ring %d - Point: %g %g %g %g", i, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);
218 
219  /* Increment the point counter */
220  shppoint++;
221  }
222 
223  /*
224  * First ring should be clockwise,
225  * other rings should be counter-clockwise
226  */
227  if ( i == 0 )
228  {
229  if ( ! is_clockwise(lwpolygon->rings[i]->npoints,
230  &xpts[shpparts[i]], &ypts[shpparts[i]], NULL) )
231  {
232  LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");
233 
234  reverse_points(lwpolygon->rings[i]->npoints,
235  &xpts[shpparts[i]], &ypts[shpparts[i]],
236  &zpts[shpparts[i]], &mpts[shpparts[i]]);
237  }
238  }
239  else
240  {
241  if ( is_clockwise(lwpolygon->rings[i]->npoints,
242  &xpts[shpparts[i]], &ypts[shpparts[i]], NULL) )
243  {
244  LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);
245 
246  reverse_points(lwpolygon->rings[i]->npoints,
247  &xpts[shpparts[i]], &ypts[shpparts[i]],
248  &zpts[shpparts[i]], &mpts[shpparts[i]]);
249  }
250  }
251  }
252 
253  obj = SHPCreateObject(state->outshptype, -1, lwpolygon->nrings, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);
254 
255  free(xpts);
256  free(ypts);
257  free(zpts);
258  free(mpts);
259  free(shpparts);
260 
261  return obj;
262 }
POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n)
Definition: lwgeom_api.c:107
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:101
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:106
void * malloc(YYSIZE_T)
void free(void *)
static int reverse_points(int num_points, double *x, double *y, double *z, double *m)
static int is_clockwise(int num_points, double *x, double *y, double *z)
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
POINTARRAY ** rings
Definition: liblwgeom.h:519
uint32_t nrings
Definition: liblwgeom.h:524
double m
Definition: liblwgeom.h:414
double x
Definition: liblwgeom.h:414
double z
Definition: liblwgeom.h:414
double y
Definition: liblwgeom.h:414
uint32_t npoints
Definition: liblwgeom.h:427

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().

Here is the call graph for this function:
Here is the caller graph for this function: