PostGIS  3.0.6dev-r@@SVN_REVISION@@

◆ create_polygon()

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

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

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

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: