PostGIS  3.3.9dev-r@@SVN_REVISION@@

◆ create_polygon()

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

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

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

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: