PostGIS 3.0.6dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches

◆ GenerateLineStringGeometry()

int GenerateLineStringGeometry ( SHPLOADERSTATE state,
SHPObject obj,
char **  geometry 
)

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

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

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

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().

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