PostGIS  2.2.7dev-r@@SVN_REVISION@@
int ShpLoaderGenerateShapeRow ( SHPDUMPERSTATE state)

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

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

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

Here is the call graph for this function:

Here is the caller graph for this function: