PostGIS  2.5.7dev-r@@SVN_REVISION@@

◆ create_multipolygon()

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

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

246 {
247  SHPObject *obj;
248  POINT4D p4d;
249  uint32_t i, j, k;
250 
251  double *xpts, *ypts, *zpts, *mpts;
252 
253  int *shpparts, shppointtotal = 0, shppoint = 0, shpringtotal = 0, shpring = 0;
254 
255  /* NOTE: Multipolygons are stored in shapefiles as Polygon* shapes with multiple outer rings */
256 
257  /* First count through each ring of each polygon so we now know much memory is required */
258  for (i = 0; i < lwmultipolygon->ngeoms; i++)
259  {
260  for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
261  {
262  shpringtotal++;
263  shppointtotal += lwmultipolygon->geoms[i]->rings[j]->npoints;
264  }
265  }
266 
267  /* Allocate storage for ring pointers */
268  shpparts = malloc(sizeof(int) * shpringtotal);
269 
270  /* Allocate storage for points */
271  xpts = malloc(sizeof(double) * shppointtotal);
272  ypts = malloc(sizeof(double) * shppointtotal);
273  zpts = malloc(sizeof(double) * shppointtotal);
274  mpts = malloc(sizeof(double) * shppointtotal);
275 
276  LWDEBUGF(4, "Total number of rings: %d Total number of points: %d", shpringtotal, shppointtotal);
277 
278  /* Iterate through each ring of each polygon in turn */
279  for (i = 0; i < lwmultipolygon->ngeoms; i++)
280  {
281  for (j = 0; j < lwmultipolygon->geoms[i]->nrings; j++)
282  {
283  /* For each ring, store the integer coordinate offset for the start of each ring */
284  shpparts[shpring] = shppoint;
285 
286  LWDEBUGF(4, "Ring offset: %d", shpring);
287 
288  for (k = 0; k < lwmultipolygon->geoms[i]->rings[j]->npoints; k++)
289  {
290  p4d = getPoint4d(lwmultipolygon->geoms[i]->rings[j], k);
291 
292  xpts[shppoint] = p4d.x;
293  ypts[shppoint] = p4d.y;
294  zpts[shppoint] = p4d.z;
295  mpts[shppoint] = p4d.m;
296 
297  LWDEBUGF(4, "MultiPolygon %d Polygon Ring %d - Point: %g %g %g %g", i, j, xpts[shppoint], ypts[shppoint], zpts[shppoint], mpts[shppoint]);
298 
299  /* Increment the point counter */
300  shppoint++;
301  }
302 
303  /*
304  * First ring should be clockwise,
305  * other rings should be counter-clockwise
306  */
307  if ( j == 0 )
308  {
309  if ( ! is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
310  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
311  {
312  LWDEBUG(4, "Outer ring not clockwise, forcing clockwise\n");
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  else
320  {
321  if ( is_clockwise(lwmultipolygon->geoms[i]->rings[j]->npoints,
322  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]], NULL) )
323  {
324  LWDEBUGF(4, "Inner ring %d not counter-clockwise, forcing counter-clockwise\n", i);
325 
326  reverse_points(lwmultipolygon->geoms[i]->rings[j]->npoints,
327  &xpts[shpparts[shpring]], &ypts[shpparts[shpring]],
328  &zpts[shpparts[shpring]], &mpts[shpparts[shpring]]);
329  }
330  }
331 
332  /* Increment the ring counter */
333  shpring++;
334  }
335  }
336 
337  obj = SHPCreateObject(state->outshptype, -1, shpringtotal, shpparts, NULL, shppointtotal, xpts, ypts, zpts, mpts);
338 
339  free(xpts);
340  free(ypts);
341  free(zpts);
342  free(mpts);
343  free(shpparts);
344 
345  return obj;
346 }
POINT4D getPoint4d(const POINTARRAY *pa, uint32_t n)
Definition: lwgeom_api.c:106
#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:497
LWPOLY ** geoms
Definition: liblwgeom.h:499
POINTARRAY ** rings
Definition: liblwgeom.h:460
uint32_t nrings
Definition: liblwgeom.h:458
double m
Definition: liblwgeom.h:355
double x
Definition: liblwgeom.h:355
double z
Definition: liblwgeom.h:355
double y
Definition: liblwgeom.h:355
uint32_t npoints
Definition: liblwgeom.h:374
unsigned int uint32_t
Definition: uthash.h:78

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: