PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ create_polygon()

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

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

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