PostGIS  3.4.0dev-r@@SVN_REVISION@@

◆ create_multipolygon()

static SHPObject * create_multipolygon ( SHPDUMPERSTATE state,
LWMPOLY lwmultipolygon 
)
static

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

263 {
264  SHPObject *obj;
265  POINT4D p4d;
266  uint32_t i, j, k;
267 
268  double *xpts, *ypts, *zpts, *mpts;
269 
270  int *shpparts, shppointtotal = 0, shppoint = 0, shpringtotal = 0, shpring = 0;
271 
272  /* NOTE: Multipolygons are stored in shapefiles as Polygon* shapes with multiple outer rings */
273 
274  /* First count through each ring of each polygon so we now know much memory is required */
275  for (i = 0; i < lwmultipolygon->ngeoms; i++)
276  {
277  for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
278  {
279  shpringtotal++;
280  shppointtotal += lwmultipolygon->geoms[i]->rings[j]->npoints;
281  }
282  }
283 
284  /* Allocate storage for ring pointers */
285  shpparts = malloc(sizeof(int) * shpringtotal);
286 
287  /* Allocate storage for points */
288  xpts = malloc(sizeof(double) * shppointtotal);
289  ypts = malloc(sizeof(double) * shppointtotal);
290  zpts = malloc(sizeof(double) * shppointtotal);
291  mpts = malloc(sizeof(double) * shppointtotal);
292 
293  LWDEBUGF(4, "Total number of rings: %d Total number of points: %d", shpringtotal, shppointtotal);
294 
295  /* Iterate through each ring of each polygon in turn */
296  for (i = 0; i < lwmultipolygon->ngeoms; i++)
297  {
298  for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
299  {
300  /* For each ring, store the integer coordinate offset for the start of each ring */
301  shpparts[shpring] = shppoint;
302 
303  LWDEBUGF(4, "Ring offset: %d", shpring);
304 
305  for (k = 0; k < lwmultipolygon->geoms[i]->rings[j]->npoints; k++)
306  {
307  p4d = getPoint4d(lwmultipolygon->geoms[i]->rings[j], k);
308 
309  xpts[shppoint] = p4d.x;
310  ypts[shppoint] = p4d.y;
311  zpts[shppoint] = p4d.z;
312  mpts[shppoint] = p4d.m;
313 
314  LWDEBUGF(4, "MultiPolygon %d Polygon Ring %d - Point: %g %g %g %g", i, j, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);
315 
316  /* Increment the point counter */
317  shppoint++;
318  }
319 
320  /*
321  * First ring should be clockwise,
322  * other rings should be counter-clockwise
323  */
324  if ( j == 0 )
325  {
326  if ( ! is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
327  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
328  {
329  LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");
330 
331  reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
332  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
333  &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
334  }
335  }
336  else
337  {
338  if ( is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
339  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
340  {
341  LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);
342 
343  reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
344  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
345  &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
346  }
347  }
348 
349  /* Increment the ring counter */
350  shpring++;
351  }
352  }
353 
354  obj = SHPCreateObject(state->outshptype, -1, shpringtotal, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);
355 
356  free(xpts);
357  free(ypts);
358  free(zpts);
359  free(mpts);
360  free(shpparts);
361 
362  return obj;
363 }
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
uint32_t ngeoms
Definition: liblwgeom.h:566
LWPOLY ** geoms
Definition: liblwgeom.h:561
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(), 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().

Here is the call graph for this function:
Here is the caller graph for this function: