PostGIS  3.7.0dev-r@@SVN_REVISION@@

◆ create_multipolygon()

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

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

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