PostGIS  3.1.6dev-r@@SVN_REVISION@@

◆ ShpLoaderGenerateShapeRow()

int ShpLoaderGenerateShapeRow ( SHPDUMPERSTATE state)

Definition at line 1965 of file pgsql2shp-core.c.

1966 {
1967  char *hexewkb = NULL;
1968  unsigned char *hexewkb_binary = NULL;
1969  size_t hexewkb_len;
1970  char *val;
1971  SHPObject *obj = NULL;
1972  LWGEOM *lwgeom;
1973 
1974  int i, geocolnum = 0;
1975 
1976  /* If we try to go pass the end of the table, fail immediately */
1977  if (state->currow > state->rowcount)
1978  {
1979  snprintf(state->message, SHPDUMPERMSGLEN, _("Tried to read past end of table!"));
1980  PQclear(state->fetchres);
1981  return SHPDUMPERERR;
1982  }
1983 
1984  /* If we have reached the end of the current batch, fetch a new one */
1985  if (state->curresrow == state->currescount && state->currow < state->rowcount)
1986  {
1987  /* Clear the previous batch results */
1988  if (state->fetchres)
1989  PQclear(state->fetchres);
1990 
1991  state->fetchres = PQexec(state->conn, state->fetch_query);
1992  if (PQresultStatus(state->fetchres) != PGRES_TUPLES_OK)
1993  {
1994  snprintf(state->message, SHPDUMPERMSGLEN, _("Error executing fetch query: %s"), PQresultErrorMessage(state->fetchres));
1995  PQclear(state->fetchres);
1996  return SHPDUMPERERR;
1997  }
1998 
1999  state->curresrow = 0;
2000  state->currescount = PQntuples(state->fetchres);
2001  }
2002 
2003  /* Grab the id of the geo column if we have one */
2004  if (state->geo_col_name)
2005  geocolnum = PQfnumber(state->fetchres, "_geoX");
2006 
2007  /* Process the next record within the batch. First write out all of
2008  the non-geo fields */
2009  for (i = 0; i < state->fieldcount; i++)
2010  {
2011  /*
2012  * Transform NULL numbers to '0'
2013  * This is because the shapelib
2014  * won't easly take care of setting
2015  * nulls unless paying the acquisition
2016  * of a bug in long integer values
2017  */
2018  if (PQgetisnull(state->fetchres, state->curresrow, i))
2019  {
2020  val = nullDBFValue(state->dbffieldtypes[i]);
2021  }
2022  else
2023  {
2024  val = PQgetvalue(state->fetchres, state->curresrow, i);
2025  val = goodDBFValue(val, state->dbffieldtypes[i]);
2026  }
2027 
2028  /* Write it to the DBF file */
2029  if (!DBFWriteAttributeDirectly(state->dbf, state->currow, i, val))
2030  {
2031  snprintf(state->message, SHPDUMPERMSGLEN, _("Error: record %d could not be created"), state->currow);
2032  PQclear(state->fetchres);
2033  return SHPDUMPERERR;
2034  }
2035  }
2036 
2037  /* Now process the geo field, if present */
2038  if (state->geo_col_name)
2039  {
2040  /* Handle NULL shapes */
2041  if (PQgetisnull(state->fetchres, state->curresrow, geocolnum))
2042  {
2043  obj = SHPCreateSimpleObject(SHPT_NULL, 0, NULL, NULL, NULL);
2044  if (SHPWriteObject(state->shp, -1, obj) == -1)
2045  {
2046  snprintf(state->message, SHPDUMPERMSGLEN, _("Error writing NULL shape for record %d"), state->currow);
2047  PQclear(state->fetchres);
2048  SHPDestroyObject(obj);
2049  return SHPDUMPERERR;
2050  }
2051  SHPDestroyObject(obj);
2052  }
2053  else
2054  {
2055  /* Get the value from the result set */
2056  val = PQgetvalue(state->fetchres, state->curresrow, geocolnum);
2057 
2058  if (!state->config->binary)
2059  {
2060  if (state->pgis_major_version > 0)
2061  {
2062  LWDEBUG(4, "PostGIS >= 1.0, non-binary cursor");
2063 
2064  /* Input is bytea encoded text field, so it must be unescaped and
2065  then converted to hexewkb string */
2066  hexewkb_binary = PQunescapeBytea((unsigned char *)val, &hexewkb_len);
2067  hexewkb = convert_bytes_to_hex(hexewkb_binary, hexewkb_len);
2068  }
2069  else
2070  {
2071  LWDEBUG(4, "PostGIS < 1.0, non-binary cursor");
2072 
2073  /* Input is already hexewkb string, so we can just
2074  copy directly to hexewkb */
2075  hexewkb_len = PQgetlength(state->fetchres, state->curresrow, geocolnum);
2076  hexewkb = malloc(hexewkb_len + 1);
2077  strncpy(hexewkb, val, hexewkb_len + 1);
2078  }
2079  }
2080  else /* binary */
2081  {
2082  LWDEBUG(4, "PostGIS (any version) using binary cursor");
2083 
2084  /* Input is binary field - must convert to hexewkb string */
2085  hexewkb_len = PQgetlength(state->fetchres, state->curresrow, geocolnum);
2086  hexewkb = convert_bytes_to_hex((unsigned char *)val, hexewkb_len);
2087  }
2088 
2089  LWDEBUGF(4, "HexEWKB - length: %d value: %s", strlen(hexewkb), hexewkb);
2090 
2091  /* Deserialize the LWGEOM */
2092  lwgeom = lwgeom_from_hexwkb(hexewkb, LW_PARSER_CHECK_NONE);
2093  if (!lwgeom)
2094  {
2095  snprintf(state->message, SHPDUMPERMSGLEN, _("Error parsing HEXEWKB for record %d"), state->currow);
2096  PQclear(state->fetchres);
2097  free(hexewkb);
2098  return SHPDUMPERERR;
2099  }
2100 
2101  /* Call the relevant method depending upon the geometry type */
2102  LWDEBUGF(4, "geomtype: %s\n", lwtype_name(lwgeom->type));
2103 
2104  switch (lwgeom->type)
2105  {
2106  case POINTTYPE:
2107  if (lwgeom_is_empty(lwgeom))
2108  {
2109  obj = create_point_empty(state, lwgeom_as_lwpoint(lwgeom));
2110  }
2111  else
2112  {
2113  obj = create_point(state, lwgeom_as_lwpoint(lwgeom));
2114  }
2115  break;
2116 
2117  case MULTIPOINTTYPE:
2118  obj = create_multipoint(state, lwgeom_as_lwmpoint(lwgeom));
2119  break;
2120 
2121  case POLYGONTYPE:
2122  obj = create_polygon(state, lwgeom_as_lwpoly(lwgeom));
2123  break;
2124 
2125  case MULTIPOLYGONTYPE:
2126  obj = create_multipolygon(state, lwgeom_as_lwmpoly(lwgeom));
2127  break;
2128 
2129  case LINETYPE:
2130  obj = create_linestring(state, lwgeom_as_lwline(lwgeom));
2131  break;
2132 
2133  case MULTILINETYPE:
2134  obj = create_multilinestring(state, lwgeom_as_lwmline(lwgeom));
2135  break;
2136 
2137  default:
2138  snprintf(state->message, SHPDUMPERMSGLEN, _("Unknown WKB type (%d) for record %d"), lwgeom->type, state->currow);
2139  PQclear(state->fetchres);
2140  SHPDestroyObject(obj);
2141  return SHPDUMPERERR;
2142  }
2143 
2144  /* Free both the original and geometries */
2145  lwgeom_free(lwgeom);
2146 
2147  /* Write the shape out to the file */
2148  if (SHPWriteObject(state->shp, -1, obj) == -1)
2149  {
2150  snprintf(state->message, SHPDUMPERMSGLEN, _("Error writing shape %d"), state->currow);
2151  PQclear(state->fetchres);
2152  SHPDestroyObject(obj);
2153  return SHPDUMPERERR;
2154  }
2155 
2156  SHPDestroyObject(obj);
2157 
2158  /* Free the hexewkb (and temporary bytea unescaped string if used) */
2159  if (hexewkb) free(hexewkb);
2160  if (hexewkb_binary) PQfreemem(hexewkb_binary);
2161  }
2162  }
2163 
2164  /* Increment ready for next time */
2165  state->curresrow++;
2166  state->currow++;
2167 
2168  return SHPDUMPEROK;
2169 }
int SHPAPI_CALL DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField, void *pValue)
Definition: dbfopen.c:1437
LWLINE * lwgeom_as_lwline(const LWGEOM *lwgeom)
Definition: lwgeom.c:162
LWGEOM * lwgeom_from_hexwkb(const char *hexwkb, const char check)
Definition: lwin_wkb.c:858
LWMPOINT * lwgeom_as_lwmpoint(const LWGEOM *lwgeom)
Definition: lwgeom.c:225
void lwgeom_free(LWGEOM *geom)
Definition: lwgeom.c:1138
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:2085
#define MULTILINETYPE
Definition: liblwgeom.h:120
#define LINETYPE
Definition: liblwgeom.h:117
#define MULTIPOINTTYPE
Definition: liblwgeom.h:119
LWMPOLY * lwgeom_as_lwmpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:243
#define POINTTYPE
LWTYPE numbers, used internally by PostGIS.
Definition: liblwgeom.h:116
LWMLINE * lwgeom_as_lwmline(const LWGEOM *lwgeom)
Definition: lwgeom.c:234
#define MULTIPOLYGONTYPE
Definition: liblwgeom.h:121
#define POLYGONTYPE
Definition: liblwgeom.h:118
const char * lwtype_name(uint8_t type)
Return the type name string associated with a type number (e.g.
Definition: lwutil.c:216
LWPOLY * lwgeom_as_lwpoly(const LWGEOM *lwgeom)
Definition: lwgeom.c:198
#define LWDEBUG(level, msg)
Definition: lwgeom_log.h:83
#define LWDEBUGF(level, msg,...)
Definition: lwgeom_log.h:88
void * malloc(YYSIZE_T)
void free(void *)
static int lwgeom_is_empty(const LWGEOM *geom)
Return true or false depending on whether a geometry is an "empty" geometry (no vertices members)
Definition: lwinline.h:203
static LWPOINT * lwgeom_as_lwpoint(const LWGEOM *lwgeom)
Definition: lwinline.h:131
static SHPObject * create_polygon(SHPDUMPERSTATE *state, LWPOLY *lwpolygon)
static SHPObject * create_multipolygon(SHPDUMPERSTATE *state, LWMPOLY *lwmultipolygon)
char * convert_bytes_to_hex(uint8_t *ewkb, size_t size)
Binary to hexewkb conversion function.
static SHPObject * create_linestring(SHPDUMPERSTATE *state, LWLINE *lwlinestring)
static char * goodDBFValue(char *in, char fieldType)
Make appropriate formatting of a DBF value based on type.
static SHPObject * create_point(SHPDUMPERSTATE *state, LWPOINT *lwpoint)
static SHPObject * create_point_empty(SHPDUMPERSTATE *state, LWPOINT *lwpoint)
static char * nullDBFValue(char fieldType)
static SHPObject * create_multipoint(SHPDUMPERSTATE *state, LWMPOINT *lwmultipoint)
static SHPObject * create_multilinestring(SHPDUMPERSTATE *state, LWMLINE *lwmultilinestring)
#define SHPDUMPERMSGLEN
#define SHPDUMPEROK
#define SHPDUMPERERR
#define SHPT_NULL
Definition: shapefil.h:348
void SHPAPI_CALL SHPDestroyObject(SHPObject *psObject)
Definition: shpopen.c:2643
#define _(String)
Definition: shpcommon.h:24
SHPObject SHPAPI_CALL1 * SHPCreateSimpleObject(int nSHPType, int nVertices, const double *padfX, const double *padfY, const double *padfZ){ return(SHPCreateObject(nSHPType, -1, 0, SHPLIB_NULLPTR, SHPLIB_NULLPTR, nVertices, padfX, padfY, padfZ, SHPLIB_NULLPTR)
int SHPAPI_CALL SHPWriteObject(SHPHandle psSHP, int nShapeId, SHPObject *psObject)
Definition: shpopen.c:1336
uint8_t type
Definition: liblwgeom.h:476
SHPDUMPERCONFIG * config
PGresult * fetchres
char message[SHPDUMPERMSGLEN]

References _, shp_dumper_config::binary, shp_dumper_state::config, shp_dumper_state::conn, convert_bytes_to_hex(), create_linestring(), create_multilinestring(), create_multipoint(), create_multipolygon(), create_point(), create_point_empty(), create_polygon(), shp_dumper_state::currescount, shp_dumper_state::curresrow, shp_dumper_state::currow, shp_dumper_state::dbf, shp_dumper_state::dbffieldtypes, DBFWriteAttributeDirectly(), shp_dumper_state::fetch_query, shp_dumper_state::fetchres, shp_dumper_state::fieldcount, free(), shp_dumper_state::geo_col_name, goodDBFValue(), LINETYPE, LW_PARSER_CHECK_NONE, LWDEBUG, LWDEBUGF, lwgeom_as_lwline(), lwgeom_as_lwmline(), lwgeom_as_lwmpoint(), lwgeom_as_lwmpoly(), lwgeom_as_lwpoint(), lwgeom_as_lwpoly(), lwgeom_free(), lwgeom_from_hexwkb(), lwgeom_is_empty(), lwtype_name(), malloc(), shp_dumper_state::message, MULTILINETYPE, MULTIPOINTTYPE, MULTIPOLYGONTYPE, nullDBFValue(), shp_dumper_state::pgis_major_version, POINTTYPE, POLYGONTYPE, shp_dumper_state::rowcount, shp_dumper_state::shp, SHPCreateSimpleObject(), SHPDestroyObject(), SHPDUMPERERR, SHPDUMPERMSGLEN, SHPDUMPEROK, SHPT_NULL, SHPWriteObject(), and LWGEOM::type.

Referenced by main(), and pgui_action_export().

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