PostGIS  3.4.0dev-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 229 of file shp2pgsql-core.c.

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

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_buffer(), lwgeom_to_wkt(), lwpoint_as_lwgeom(), lwpoint_construct(), lwpoint_construct_empty(), POINT4D::m, malloc(), shp_loader_state::message, MULTIPOINTTYPE, tagSHPObject::nVertices, tagSHPObject::padfM, tagSHPObject::padfX, tagSHPObject::padfY, tagSHPObject::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().

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