PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ create_polygon()

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

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

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