PostGIS  2.5.0dev-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 309 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, malloc(), 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().

310 {
311 
312  LWGEOM **lwmultilinestrings;
313  LWGEOM *lwgeom = NULL;
314  POINT4D point4d;
315  int dims = 0;
316  int u, v, start_vertex, end_vertex;
317  char *mem;
318  size_t mem_length;
319 
320 
321  FLAGS_SET_Z(dims, state->has_z);
322  FLAGS_SET_M(dims, state->has_m);
323 
324  if (state->config->simple_geometries == 1 && obj->nParts > 1)
325  {
326  snprintf(state->message, SHPLOADERMSGLEN, _("We have a Multilinestring with %d parts, can't use -S switch!"), obj->nParts);
327 
328  return SHPLOADERERR;
329  }
330 
331  /* Allocate memory for our array of LWLINEs and our dynptarrays */
332  lwmultilinestrings = malloc(sizeof(LWPOINT *) * obj->nParts);
333 
334  /* We need an array of pointers to each of our sub-geometries */
335  for (u = 0; u < obj->nParts; u++)
336  {
337  /* Create a ptarray containing the line points */
338  POINTARRAY *pa = ptarray_construct_empty(state->has_z, state->has_m, obj->nParts);
339 
340  /* Set the start/end vertices depending upon whether this is
341  a MULTILINESTRING or not */
342  if ( u == obj->nParts-1 )
343  end_vertex = obj->nVertices;
344  else
345  end_vertex = obj->panPartStart[u + 1];
346 
347  start_vertex = obj->panPartStart[u];
348 
349  for (v = start_vertex; v < end_vertex; v++)
350  {
351  /* Generate the point */
352  point4d.x = obj->padfX[v];
353  point4d.y = obj->padfY[v];
354 
355  if (state->has_z)
356  point4d.z = obj->padfZ[v];
357  if (state->has_m)
358  point4d.m = obj->padfM[v];
359 
360  ptarray_append_point(pa, &point4d, LW_FALSE);
361  }
362 
363  /* Generate the LWLINE */
364  lwmultilinestrings[u] = lwline_as_lwgeom(lwline_construct(state->from_srid, NULL, pa));
365  }
366 
367  /* If using MULTILINESTRINGs then generate the serialized collection, otherwise just a single LINESTRING */
368  if (state->config->simple_geometries == 0)
369  {
370  lwgeom = lwcollection_as_lwgeom(lwcollection_construct(MULTILINETYPE, state->from_srid, NULL, obj->nParts, lwmultilinestrings));
371  }
372  else
373  {
374  lwgeom = lwmultilinestrings[0];
375  lwfree(lwmultilinestrings);
376  }
377 
378  if (!state->config->use_wkt)
379  mem = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &mem_length);
380  else
381  mem = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, WKT_PRECISION, &mem_length);
382 
383  if ( !mem )
384  {
385  snprintf(state->message, SHPLOADERMSGLEN, "unable to write geometry");
386  return SHPLOADERERR;
387  }
388 
389  /* Free all of the allocated items */
390  lwgeom_free(lwgeom);
391 
392  /* Return the string - everything ok */
393  *geometry = mem;
394 
395  return SHPLOADEROK;
396 }
double x
Definition: liblwgeom.h:351
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:848
SHPLOADERCONFIG * config
int * panPartStart
Definition: shapefil.h:345
double m
Definition: liblwgeom.h:351
LWCOLLECTION * lwcollection_construct(uint8_t type, int srid, GBOX *bbox, uint32_t ngeoms, LWGEOM **geoms)
Definition: lwcollection.c:43
char * lwgeom_to_wkt(const LWGEOM *geom, uint8_t variant, int precision, size_t *size_out)
WKT emitter function.
Definition: lwout_wkt.c:675
void lwfree(void *mem)
Definition: lwutil.c:244
int nVertices
Definition: shapefil.h:348
#define _(String)
Definition: shpcommon.h:24
POINTARRAY * ptarray_construct_empty(char hasz, char hasm, uint32_t maxpoints)
Create a new POINTARRAY with no points.
Definition: ptarray.c:70
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1137
double * padfX
Definition: shapefil.h:349
double * padfY
Definition: shapefil.h:350
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:145
LWGEOM * lwline_as_lwgeom(const LWLINE *obj)
Definition: lwgeom.c:329
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_FALSE, then a duplicate point will not be added.
Definition: ptarray.c:156
#define LW_FALSE
Definition: liblwgeom.h:76
LWLINE * lwline_construct(int srid, GBOX *bbox, POINTARRAY *points)
Definition: lwline.c:42
int nParts
Definition: shapefil.h:344
#define SHPLOADEROK
double z
Definition: liblwgeom.h:351
#define WKB_EXTENDED
Definition: liblwgeom.h:2061
#define WKT_PRECISION
#define WKT_EXTENDED
Definition: liblwgeom.h:2070
char message[SHPLOADERMSGLEN]
double * padfZ
Definition: shapefil.h:351
void * malloc(YYSIZE_T)
#define SHPLOADERERR
double y
Definition: liblwgeom.h:351
#define MULTILINETYPE
Definition: liblwgeom.h:88
double * padfM
Definition: shapefil.h:352
#define SHPLOADERMSGLEN
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:146
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:299

Here is the call graph for this function:

Here is the caller graph for this function: