PostGIS  2.1.10dev-r@@SVN_REVISION@@
int GenerateLineStringGeometry ( SHPLOADERSTATE state,
SHPObject obj,
char **  geometry 
)

Generate an allocated geometry string for shapefile object obj using the state parameters.

Definition at line 329 of file shp2pgsql-core.c.

References _, shp_loader_state::config, FLAGS_SET_M, FLAGS_SET_Z, shp_loader_state::from_srid, shp_loader_state::has_m, shp_loader_state::has_z, LW_FALSE, lwcollection_as_lwgeom(), lwcollection_construct(), lwfree(), lwgeom_free(), lwgeom_to_hexwkb(), lwgeom_to_wkt(), lwline_as_lwgeom(), lwline_construct(), POINT4D::m, shp_loader_state::message, MULTILINETYPE, SHPObject::nParts, SHPObject::nVertices, SHPObject::padfM, SHPObject::padfX, SHPObject::padfY, SHPObject::padfZ, SHPObject::panPartStart, ptarray_append_point(), ptarray_construct_empty(), SHPLOADERERR, SHPLOADERMSGLEN, SHPLOADEROK, shp_loader_config::simple_geometries, shp_loader_config::use_wkt, WKB_EXTENDED, WKT_EXTENDED, WKT_PRECISION, POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by ShpLoaderGenerateSQLRowStatement().

330 {
331 
332  LWGEOM **lwmultilinestrings;
333  LWGEOM *lwgeom = NULL;
334  POINT4D point4d;
335  int dims = 0;
336  int u, v, start_vertex, end_vertex;
337  char *mem;
338  size_t mem_length;
339 
340 
341  FLAGS_SET_Z(dims, state->has_z);
342  FLAGS_SET_M(dims, state->has_m);
343 
344  if (state->config->simple_geometries == 1 && obj->nParts > 1)
345  {
346  snprintf(state->message, SHPLOADERMSGLEN, _("We have a Multilinestring with %d parts, can't use -S switch!"), obj->nParts);
347 
348  return SHPLOADERERR;
349  }
350 
351  /* Allocate memory for our array of LWLINEs and our dynptarrays */
352  lwmultilinestrings = malloc(sizeof(LWPOINT *) * obj->nParts);
353 
354  /* We need an array of pointers to each of our sub-geometries */
355  for (u = 0; u < obj->nParts; u++)
356  {
357  /* Create a ptarray containing the line points */
358  POINTARRAY *pa = ptarray_construct_empty(state->has_z, state->has_m, obj->nParts);
359 
360  /* Set the start/end vertices depending upon whether this is
361  a MULTILINESTRING or not */
362  if ( u == obj->nParts-1 )
363  end_vertex = obj->nVertices;
364  else
365  end_vertex = obj->panPartStart[u + 1];
366 
367  start_vertex = obj->panPartStart[u];
368 
369  for (v = start_vertex; v < end_vertex; v++)
370  {
371  /* Generate the point */
372  point4d.x = obj->padfX[v];
373  point4d.y = obj->padfY[v];
374 
375  if (state->has_z)
376  point4d.z = obj->padfZ[v];
377  if (state->has_m)
378  point4d.m = obj->padfM[v];
379 
380  ptarray_append_point(pa, &point4d, LW_FALSE);
381  }
382 
383  /* Generate the LWLINE */
384  lwmultilinestrings[u] = lwline_as_lwgeom(lwline_construct(state->from_srid, NULL, pa));
385  }
386 
387  /* If using MULTILINESTRINGs then generate the serialized collection, otherwise just a single LINESTRING */
388  if (state->config->simple_geometries == 0)
389  {
390  lwgeom = lwcollection_as_lwgeom(lwcollection_construct(MULTILINETYPE, state->from_srid, NULL, obj->nParts, lwmultilinestrings));
391  }
392  else
393  {
394  lwgeom = lwmultilinestrings[0];
395  lwfree(lwmultilinestrings);
396  }
397 
398  if (!state->config->use_wkt)
399  mem = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &mem_length);
400  else
401  mem = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, WKT_PRECISION, &mem_length);
402 
403  if ( !mem )
404  {
405  snprintf(state->message, SHPLOADERMSGLEN, "unable to write geometry");
406  return SHPLOADERERR;
407  }
408 
409  /* Free all of the allocated items */
410  lwgeom_free(lwgeom);
411 
412  /* Return the string - everything ok */
413  *geometry = mem;
414 
415  return SHPLOADEROK;
416 }
double x
Definition: liblwgeom.h:308
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:776
SHPLOADERCONFIG * config
int * panPartStart
Definition: shapefil.h:346
double m
Definition: liblwgeom.h:308
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:30
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:655
void lwfree(void *mem)
Definition: lwutil.c:190
int nVertices
Definition: shapefil.h:349
#define _(String)
Definition: shpcommon.h:23
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:57
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1006
double * padfX
Definition: shapefil.h:350
double * padfY
Definition: shapefil.h:351
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:112
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:249
int ptarray_append_point(POINTARRAY *pa, const POINT4D *pt, int allow_duplicates)
Append a point to the end of an existing POINTARRAY If allow_duplicate is LW_TRUE, then a duplicate point will not be added.
Definition: ptarray.c:141
#define LW_FALSE
Definition: liblwgeom.h:52
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:29
int nParts
Definition: shapefil.h:345
#define SHPLOADEROK
double z
Definition: liblwgeom.h:308
#define WKB_EXTENDED
Definition: liblwgeom.h:1769
#define WKT_PRECISION
#define WKT_EXTENDED
Definition: liblwgeom.h:1778
char message[SHPLOADERMSGLEN]
double * padfZ
Definition: shapefil.h:352
#define SHPLOADERERR
double y
Definition: liblwgeom.h:308
#define MULTILINETYPE
Definition: liblwgeom.h:64
double * padfM
Definition: shapefil.h:353
#define SHPLOADERMSGLEN
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:113
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:219

Here is the call graph for this function:

Here is the caller graph for this function: