PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ GeneratePointGeometry()

int GeneratePointGeometry ( SHPLOADERSTATE state,
SHPObject obj,
char **  geometry,
int  force_multi 
)

Generate an allocated geometry string for shapefile object obj using the state parameters if "force_multi" is true, single points will instead be created as multipoints with a single vertice.

Definition at line 227 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_TRUE, lwcollection_as_lwgeom(), lwcollection_construct(), lwfree(), lwgeom_free(), lwgeom_to_hexwkb(), lwgeom_to_wkt(), lwpoint_as_lwgeom(), lwpoint_construct(), lwpoint_construct_empty(), POINT4D::m, malloc(), shp_loader_state::message, MULTIPOINTTYPE, SHPObject::nVertices, SHPObject::padfM, SHPObject::padfX, SHPObject::padfY, SHPObject::padfZ, ptarray_append_point(), ptarray_construct_empty(), SHPLOADERERR, SHPLOADERMSGLEN, SHPLOADEROK, shp_loader_config::use_wkt, WKB_EXTENDED, WKT_EXTENDED, WKT_PRECISION, POINT4D::x, POINT4D::y, and POINT4D::z.

Referenced by ShpLoaderGenerateSQLRowStatement().

228 {
229  LWGEOM **lwmultipoints;
230  LWGEOM *lwgeom = NULL;
231 
232  POINT4D point4d;
233 
234  int dims = 0;
235  int u;
236 
237  char *mem;
238  size_t mem_length;
239 
240  FLAGS_SET_Z(dims, state->has_z);
241  FLAGS_SET_M(dims, state->has_m);
242 
243  /* POINT EMPTY encoded as POINT(NaN NaN) */
244  if (obj->nVertices == 1 && isnan(obj->padfX[0]) && isnan(obj->padfY[0]))
245  {
246  lwgeom = lwpoint_as_lwgeom(lwpoint_construct_empty(state->from_srid, state->has_z, state->has_m));
247  }
248  /* Not empty */
249  else
250  {
251  /* Allocate memory for our array of LWPOINTs and our dynptarrays */
252  lwmultipoints = malloc(sizeof(LWPOINT *) * obj->nVertices);
253 
254  /* We need an array of pointers to each of our sub-geometries */
255  for (u = 0; u < obj->nVertices; u++)
256  {
257  /* Create a ptarray containing a single point */
258  POINTARRAY *pa = ptarray_construct_empty(state->has_z, state->has_m, 1);
259 
260  /* Generate the point */
261  point4d.x = obj->padfX[u];
262  point4d.y = obj->padfY[u];
263 
264  if (state->has_z)
265  point4d.z = obj->padfZ[u];
266  if (state->has_m)
267  point4d.m = obj->padfM[u];
268 
269  /* Add in the point! */
270  ptarray_append_point(pa, &point4d, LW_TRUE);
271 
272  /* Generate the LWPOINT */
273  lwmultipoints[u] = lwpoint_as_lwgeom(lwpoint_construct(state->from_srid, NULL, pa));
274  }
275 
276  /* If we have more than 1 vertex then we are working on a MULTIPOINT and so generate a MULTIPOINT
277  rather than a POINT */
278  if ((obj->nVertices > 1) || force_multi)
279  {
280  lwgeom = lwcollection_as_lwgeom(lwcollection_construct(MULTIPOINTTYPE, state->from_srid, NULL, obj->nVertices, lwmultipoints));
281  }
282  else
283  {
284  lwgeom = lwmultipoints[0];
285  lwfree(lwmultipoints);
286  }
287  }
288 
289  if (state->config->use_wkt)
290  {
291  mem = lwgeom_to_wkt(lwgeom, WKT_EXTENDED, WKT_PRECISION, &mem_length);
292  }
293  else
294  {
295  mem = lwgeom_to_hexwkb(lwgeom, WKB_EXTENDED, &mem_length);
296  }
297 
298  if ( !mem )
299  {
300  snprintf(state->message, SHPLOADERMSGLEN, "unable to write geometry");
301  return SHPLOADERERR;
302  }
303 
304  /* Free all of the allocated items */
305  lwgeom_free(lwgeom);
306 
307  /* Return the string - everything ok */
308  *geometry = mem;
309 
310  return SHPLOADEROK;
311 }
double x
Definition: liblwgeom.h:352
char * lwgeom_to_hexwkb(const LWGEOM *geom, uint8_t variant, size_t *size_out)
Definition: lwout_wkb.c:848
SHPLOADERCONFIG * config
double m
Definition: liblwgeom.h:352
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:669
void lwfree(void *mem)
Definition: lwutil.c:244
int nVertices
Definition: shapefil.h:348
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:1099
#define MULTIPOINTTYPE
Definition: liblwgeom.h:88
double * padfX
Definition: shapefil.h:349
double * padfY
Definition: shapefil.h:350
LWPOINT * lwpoint_construct_empty(int srid, char hasz, char hasm)
Definition: lwpoint.c:151
#define FLAGS_SET_Z(flags, value)
Definition: liblwgeom.h:146
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_TRUE
Return types for functions with status returns.
Definition: liblwgeom.h:76
#define SHPLOADEROK
double z
Definition: liblwgeom.h:352
#define WKB_EXTENDED
Definition: liblwgeom.h:2076
#define WKT_PRECISION
#define WKT_EXTENDED
Definition: liblwgeom.h:2085
char message[SHPLOADERMSGLEN]
double * padfZ
Definition: shapefil.h:351
LWGEOM * lwpoint_as_lwgeom(const LWPOINT *obj)
Definition: lwgeom.c:303
void * malloc(YYSIZE_T)
LWPOINT * lwpoint_construct(int srid, GBOX *bbox, POINTARRAY *point)
Definition: lwpoint.c:129
#define SHPLOADERERR
double y
Definition: liblwgeom.h:352
double * padfM
Definition: shapefil.h:352
#define SHPLOADERMSGLEN
#define FLAGS_SET_M(flags, value)
Definition: liblwgeom.h:147
LWGEOM * lwcollection_as_lwgeom(const LWCOLLECTION *obj)
Definition: lwgeom.c:268
Here is the call graph for this function:
Here is the caller graph for this function: