PostGIS  2.5.0dev-r@@SVN_REVISION@@

◆ ShpLoaderGenerateShapeRow()

int ShpLoaderGenerateShapeRow ( SHPDUMPERSTATE state)

Definition at line 1968 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().

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