PostGIS  2.4.9dev-r@@SVN_REVISION@@

◆ ShpLoaderGenerateShapeRow()

int ShpLoaderGenerateShapeRow ( SHPDUMPERSTATE state)

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

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