PostGIS  2.2.7dev-r@@SVN_REVISION@@
static SHPObject * create_multipolygon ( SHPDUMPERSTATE state,
LWMPOLY lwmultipolygon 
)
static

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

References free(), LWMPOLY::geoms, getPoint4d(), is_clockwise(), LWDEBUG, LWDEBUGF, POINT4D::m, malloc(), LWMPOLY::ngeoms, POINTARRAY::npoints, LWPOLY::nrings, shp_dumper_state::outshptype, reverse_points(), LWPOLY::rings, SHPCreateObject(), POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by ShpLoaderGenerateShapeRow().

234 {
235  SHPObject *obj;
236  POINT4D p4d;
237  int i, j, k;
238 
239  double *xpts, *ypts, *zpts, *mpts;
240 
241  int *shpparts, shppointtotal = 0, shppoint = 0, shpringtotal = 0, shpring = 0;
242 
243  /* NOTE: Multipolygons are stored in shapefiles as Polygon* shapes with multiple outer rings */
244 
245  /* First count through each ring of each polygon so we now know much memory is required */
246  for (i = 0; i < lwmultipolygon->ngeoms; i++)
247  {
248  for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
249  {
250  shpringtotal++;
251  shppointtotal += lwmultipolygon->geoms[i]->rings[j]->npoints;
252  }
253  }
254 
255  /* Allocate storage for ring pointers */
256  shpparts = malloc(sizeof(int) * shpringtotal);
257 
258  /* Allocate storage for points */
259  xpts = malloc(sizeof(double) * shppointtotal);
260  ypts = malloc(sizeof(double) * shppointtotal);
261  zpts = malloc(sizeof(double) * shppointtotal);
262  mpts = malloc(sizeof(double) * shppointtotal);
263 
264  LWDEBUGF(4, "Total number of rings: %d Total number of points: %d", shpringtotal, shppointtotal);
265 
266  /* Iterate through each ring of each polygon in turn */
267  for (i = 0; i < lwmultipolygon->ngeoms; i++)
268  {
269  for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
270  {
271  /* For each ring, store the integer coordinate offset for the start of each ring */
272  shpparts[shpring] = shppoint;
273 
274  LWDEBUGF(4, "Ring offset: %d", shpring);
275 
276  for (k = 0; k < lwmultipolygon->geoms[i]->rings[j]->npoints; k++)
277  {
278  p4d = getPoint4d(lwmultipolygon->geoms[i]->rings[j], k);
279 
280  xpts[shppoint] = p4d.x;
281  ypts[shppoint] = p4d.y;
282  zpts[shppoint] = p4d.z;
283  mpts[shppoint] = p4d.m;
284 
285  LWDEBUGF(4, "MultiPolygon %d Polygon Ring %d - Point: %g %g %g %g", i, j, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);
286 
287  /* Increment the point counter */
288  shppoint++;
289  }
290 
291  /*
292  * First ring should be clockwise,
293  * other rings should be counter-clockwise
294  */
295  if ( j == 0 )
296  {
297  if ( ! is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
298  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
299  {
300  LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");
301 
302  reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
303  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
304  &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
305  }
306  }
307  else
308  {
309  if ( is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
310  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
311  {
312  LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);
313 
314  reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
315  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
316  &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
317  }
318  }
319 
320  /* Increment the ring counter */
321  shpring++;
322  }
323  }
324 
325  obj = SHPCreateObject(state->outshptype, -1, shpringtotal, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);
326 
327  free(xpts);
328  free(ypts);
329  free(zpts);
330  free(mpts);
331  free(shpparts);
332 
333  return obj;
334 }
double x
Definition: liblwgeom.h:336
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:336
int npoints
Definition: liblwgeom.h:355
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:50
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:216
LWPOLY ** geoms
Definition: liblwgeom.h:480
static int is_clockwise(int num_points, double *x, double *y, double *z)
POINTARRAY ** rings
Definition: liblwgeom.h:441
int nrings
Definition: liblwgeom.h:439
double z
Definition: liblwgeom.h:336
int ngeoms
Definition: liblwgeom.h:478
void free(void *)
void * malloc(YYSIZE_T)
double y
Definition: liblwgeom.h:336
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:55

Here is the call graph for this function:

Here is the caller graph for this function: