Exemplo n.º 1
0
static void
conn_geos_warning (const char *msg, void *userdata)
{
/* reporting some GEOS warning - thread safe */
    int len;
    struct splite_internal_cache *cache =
	(struct splite_internal_cache *) userdata;
    if (cache == NULL)
	goto invalid_cache;
    if (cache->magic1 != SPATIALITE_CACHE_MAGIC1
	|| cache->magic2 != SPATIALITE_CACHE_MAGIC2)
	goto invalid_cache;

    if (cache->gaia_geos_warning_msg != NULL)
	free (cache->gaia_geos_warning_msg);
    cache->gaia_geos_warning_msg = NULL;
    if (msg)
      {
	  spatialite_e ("GEOS warning: %s\n", msg);
	  len = strlen (msg);
	  cache->gaia_geos_warning_msg = malloc (len + 1);
	  strcpy (cache->gaia_geos_warning_msg, msg);
      }
    return;

  invalid_cache:
    if (msg)
	spatialite_e ("GEOS warning: %s\n", msg);
}
Exemplo n.º 2
0
static void
vdbf_read_row (VirtualDbfCursorPtr cursor, int *deleted_row)
{
/* trying to read a "row" from DBF */
    int ret;
    int deleted;
    if (!(cursor->pVtab->dbf->Valid))
      {
	  cursor->eof = 1;
	  return;
      }
    ret = gaiaReadDbfEntity (cursor->pVtab->dbf, cursor->current_row, &deleted);
    if (!ret)
      {
	  if (!(cursor->pVtab->dbf->LastError))	/* normal DBF EOF */
	    {
		cursor->eof = 1;
		return;
	    }
	  /* an error occurred */
	  spatialite_e ("%s\n", cursor->pVtab->dbf->LastError);
	  cursor->eof = 1;
	  return;
      }
    cursor->current_row++;
    *deleted_row = deleted;
}
Exemplo n.º 3
0
static void
getProjParamsFromSpatialReferenceSystemTable (sqlite3 * sqlite, int srid,
					      char **proj_params)
{
/* retrives the PROJ params from SPATIAL_SYS_REF table, if possible */
    char *sql;
    char **results;
    int rows;
    int columns;
    int i;
    int ret;
    int len;
    const char *proj4text;
    char *errMsg = NULL;
    *proj_params = NULL;
    sql =
	sqlite3_mprintf
	("SELECT proj4text FROM spatial_ref_sys WHERE srid = %d", srid);
    ret = sqlite3_get_table (sqlite, sql, &results, &rows, &columns, &errMsg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  spatialite_e ("unknown SRID: %d\t<%s>\n", srid, errMsg);
	  sqlite3_free (errMsg);
	  return;
      }
    for (i = 1; i <= rows; i++)
      {
	  proj4text = results[(i * columns)];
	  if (proj4text != NULL)
	    {
		len = strlen (proj4text);
		*proj_params = malloc (len + 1);
		strcpy (*proj_params, proj4text);
	    }
      }
    if (*proj_params == NULL)
      {
	  spatialite_e ("unknown SRID: %d\n", srid);
      }
    sqlite3_free_table (results);
}
Exemplo n.º 4
0
static void
geos_warning_r (int pool_index, const char *fmt, va_list ap)
{
/* reporting some GEOS warning - thread safe */
    char *msg;
    msg = sqlite3_vmprintf (fmt, ap);
    if (msg)
      {
	  spatialite_e ("GEOS warning: %s\n", msg);
	  setGeosWarningMsg (pool_index, msg);
	  sqlite3_free (msg);
      }
    else
	setGeosWarningMsg (pool_index, NULL);
}
Exemplo n.º 5
0
/*
** The following routine is called if the stack overflows.
*/
static void yyStackOverflow(yyParser *yypParser, YYMINORTYPE *yypMinor){
   ParseARG_FETCH;
   yypParser->yyidx--;
#ifndef NDEBUG
   if( yyTraceFILE ){
     fprintf(yyTraceFILE,"%sStack Overflow!\n",yyTracePrompt);
   }
#endif
   while( yypParser->yyidx>=0 ) yy_pop_parser_stack(yypParser);
   /* Here code is inserted which will execute if the parser
   ** stack every overflows */

     spatialite_e( "Giving up.  Parser stack overflow\n");
   ParseARG_STORE; /* Suppress warning about unused %extra_argument var */
}
Exemplo n.º 6
0
static int
find_free_connection ()
{
    int i;
    for (i = 0; i < SPATIALITE_MAX_CONNECTIONS; i++)
      {
	  struct splite_connection *p = &(splite_connection_pool[i]);
	  if (p->conn_ptr == NULL)
	    {
		p->conn_ptr = GAIA_CONN_RESERVED;
		return i;
	    }
      }
    spatialite_e ("ERROR: Too many connections: max %d\n",
		  SPATIALITE_MAX_CONNECTIONS);
    return -1;
}
Exemplo n.º 7
0
static int
vxpath_create (sqlite3 * db, void *pAux, int argc, const char *const *argv,
	       sqlite3_vtab ** ppVTab, char **pzErr)
{
/* creates the virtual table for XPath */
    VirtualXPathPtr p_vt;
    char *vtable = NULL;
    char *table = NULL;
    char *column = NULL;
    char *xname;
    char *sql;
    int okTable = 0;
    int okCol = 0;
    if (argc == 5)
      {
	  vtable = gaiaDequotedSql ((char *) argv[2]);
	  table = gaiaDequotedSql ((char *) argv[3]);
	  column = gaiaDequotedSql ((char *) argv[4]);
      }
    else
      {
	  *pzErr =
	      sqlite3_mprintf
	      ("[VirtualXPath module] CREATE VIRTUAL: illegal arg list {void}\n");
	  return SQLITE_ERROR;
      }
    vxpath_check (db, table, column, &okTable, &okCol);
    if (!okTable || !okCol)
	goto illegal;
    xname = gaiaDoubleQuotedSql (vtable);
    sql = sqlite3_mprintf ("CREATE TABLE \"%s\" (pkid INTEGER, sub INTEGER, "
			   "parent TEXT, node TEXT, attribute TEXT, "
			   "value TEXT, xpath_expr TEXT)", xname);
    free (xname);
    if (sqlite3_declare_vtab (db, sql) != SQLITE_OK)
      {
	  sqlite3_free (sql);
	  *pzErr =
	      sqlite3_mprintf
	      ("[VirtualXPath module] CREATE VIRTUAL: invalid SQL statement \"%s\"",
	       sql);
	  goto error;
      }
    sqlite3_free (sql);
    p_vt = (VirtualXPathPtr) sqlite3_malloc (sizeof (VirtualXPath));
    if (!p_vt)
	return SQLITE_NOMEM;
    p_vt->db = db;
    p_vt->p_cache = pAux;
    if (p_vt->p_cache == NULL)
	spatialite_e ("VirtualXPath WARNING - no XML cache is available !!!\n");
    p_vt->nRef = 0;
    p_vt->zErrMsg = NULL;
    p_vt->table = table;
    p_vt->column = column;
    *ppVTab = (sqlite3_vtab *) p_vt;
    free (vtable);
    return SQLITE_OK;
  illegal:
/* something is going the wrong way */
    if (!okTable == 0)
	*pzErr =
	    sqlite3_mprintf
	    ("[VirtualXPath module] table \"%s\" doesn't exists\n", table);
    else if (!okCol)
	*pzErr =
	    sqlite3_mprintf
	    ("[VirtualXPath module] table \"%s\" exists, but has no \"%s\" column\n",
	     table, column);
  error:
    return SQLITE_ERROR;
}
Exemplo n.º 8
0
static int
vtxt_create (sqlite3 * db, void *pAux, int argc, const char *const *argv,
	     sqlite3_vtab ** ppVTab, char **pzErr)
{
/* creates the virtual table connected to some TEXT file */
    char path[2048];
    char encoding[128];
    const char *vtable;
    const char *pEncoding = NULL;
    int len;
    gaiaTextReaderPtr text = NULL;
    const char *pPath = NULL;
    char field_separator = '\t';
    char text_separator = '"';
    char decimal_separator = '.';
    char first_line_titles = 1;
    int i;
    char sql[65535];
    int seed;
    int dup;
    int idup;
    char dummyName[4096];
    char **col_name = NULL;
    VirtualTextPtr p_vt;
    if (pAux)
	pAux = pAux;		/* unused arg warning suppression */
/* checking for TEXTfile PATH */
    if (argc >= 5 && argc <= 9)
      {
	  vtable = argv[1];
	  pPath = argv[3];
	  len = strlen (pPath);
	  if ((*(pPath + 0) == '\'' || *(pPath + 0) == '"')
	      && (*(pPath + len - 1) == '\'' || *(pPath + len - 1) == '"'))
	    {
		/* the path is enclosed between quotes - we need to dequote it */
		strcpy (path, pPath + 1);
		len = strlen (path);
		*(path + len - 1) = '\0';
	    }
	  else
	      strcpy (path, pPath);
	  pEncoding = argv[4];
	  len = strlen (pEncoding);
	  if ((*(pEncoding + 0) == '\'' || *(pEncoding + 0) == '"')
	      && (*(pEncoding + len - 1) == '\''
		  || *(pEncoding + len - 1) == '"'))
	    {
		/* the charset-name is enclosed between quotes - we need to dequote it */
		strcpy (encoding, pEncoding + 1);
		len = strlen (encoding);
		*(encoding + len - 1) = '\0';
	    }
	  else
	      strcpy (encoding, pEncoding);
	  if (argc >= 6)
	    {
		if (*(argv[5]) == '0' || *(argv[5]) == 'n' || *(argv[5]) == 'N')
		    first_line_titles = 0;
	    }
	  if (argc >= 7)
	    {
		if (strcasecmp (argv[6], "COMMA") == 0)
		    decimal_separator = ',';
		if (strcasecmp (argv[6], "POINT") == 0)
		    decimal_separator = '.';
	    }
	  if (argc >= 8)
	    {
		if (strcasecmp (argv[7], "SINGLEQUOTE") == 0)
		    text_separator = '\'';
		if (strcasecmp (argv[7], "DOUBLEQUOTE") == 0)
		    text_separator = '"';
		if (strcasecmp (argv[7], "NONE") == 0)
		    text_separator = '\0';
	    }
	  if (argc == 9)
	    {
		if (strlen (argv[8]) == 3)
		  {
		      if (strcasecmp (argv[8], "TAB") == 0)
			  field_separator = '\t';
		      if (*(argv[8] + 0) == '\'' && *(argv[8] + 2) == '\'')
			  field_separator = *(argv[8] + 1);
		  }
	    }
      }
    else
      {
	  *pzErr =
	      sqlite3_mprintf
	      ("[VirtualText module] CREATE VIRTUAL: illegal arg list\n"
	       "\t\t{ text_path, encoding [, first_row_as_titles [, [decimal_separator [, text_separator, [field_separator] ] ] ] }\n");
	  return SQLITE_ERROR;
      }
    p_vt = (VirtualTextPtr) sqlite3_malloc (sizeof (VirtualText));
    if (!p_vt)
	return SQLITE_NOMEM;
    p_vt->pModule = &virtualtext_module;
    p_vt->nRef = 0;
    p_vt->zErrMsg = NULL;
    p_vt->db = db;
    text = gaiaTextReaderAlloc (path, field_separator,
				text_separator, decimal_separator,
				first_line_titles, encoding);
    if (text)
      {
	  if (gaiaTextReaderParse (text) == 0)
	    {
		gaiaTextReaderDestroy (text);
		text = NULL;
	    }
      }
    if (!text)
      {
	  /* something is going the wrong way; creating a stupid default table */
	  spatialite_e ("VirtualText: invalid data source\n");
	  sprintf (sql, "CREATE TABLE %s (ROWNO INTEGER)", vtable);
	  if (sqlite3_declare_vtab (db, sql) != SQLITE_OK)
	    {
		*pzErr =
		    sqlite3_mprintf
		    ("[VirtualText module] cannot build a table from TEXT file\n");
		return SQLITE_ERROR;
	    }
	  p_vt->reader = NULL;
	  *ppVTab = (sqlite3_vtab *) p_vt;
	  return SQLITE_OK;
      }
    p_vt->reader = text;
/* preparing the COLUMNs for this VIRTUAL TABLE */
    sprintf (sql, "CREATE TABLE %s (ROWNO INTEGER", vtable);
    col_name = malloc (sizeof (char *) * text->max_fields);
    seed = 0;
    for (i = 0; i < text->max_fields; i++)
      {
	  strcat (sql, ", ");
	  sprintf (dummyName, "\"%s\"", text->columns[i].name);
	  dup = 0;
	  for (idup = 0; idup < i; idup++)
	    {
		if (strcasecmp (dummyName, *(col_name + idup)) == 0)
		    dup = 1;
	    }
	  if (strcasecmp (dummyName, "ROWNO") == 0)
	      dup = 1;
	  if (dup)
	      sprintf (dummyName, "DUPCOL_%d", seed++);
	  len = strlen (dummyName);
	  *(col_name + i) = malloc (len + 1);
	  strcpy (*(col_name + i), dummyName);
	  strcat (sql, dummyName);
	  if (text->columns[i].type == VRTTXT_INTEGER)
	      strcat (sql, " INTEGER");
	  else if (text->columns[i].type == VRTTXT_DOUBLE)
	      strcat (sql, " DOUBLE");
	  else
	      strcat (sql, " TEXT");
      }
    strcat (sql, ")");
    if (col_name)
      {
	  /* releasing memory allocation for column names */
	  for (i = 0; i < text->max_fields; i++)
	      free (*(col_name + i));
	  free (col_name);
      }
    if (sqlite3_declare_vtab (db, sql) != SQLITE_OK)
      {
	  *pzErr =
	      sqlite3_mprintf
	      ("[VirtualText module] CREATE VIRTUAL: invalid SQL statement \"%s\"",
	       sql);
	  return SQLITE_ERROR;
      }
    *ppVTab = (sqlite3_vtab *) p_vt;
    return SQLITE_OK;
}
Exemplo n.º 9
0
static int
vbbox_create (sqlite3 * db, void *pAux, int argc, const char *const *argv,
              sqlite3_vtab ** ppVTab, char **pzErr)
{
    /* creates the virtual table connected to some BoundingBox table */
    char *vtable = NULL;
    char *table = NULL;
    char *col_minx = NULL;
    char *col_miny = NULL;
    char *col_maxx = NULL;
    char *col_maxy = NULL;
    char *col_srid = NULL;
    char *x_force_wgs84 = NULL;
    int ret;
    int i;
    int i2;
    int len;
    int n_rows;
    int n_columns;
    const char *col_name;
    const char *col_type;
    int force_wgs84;
    char **results;
    char *sql;
    char *xname;
    gaiaOutBuffer sql_statement;
    VirtualBBoxPtr p_vt = NULL;
    if (pAux)
        pAux = pAux;		/* unused arg warning suppression */
    gaiaOutBufferInitialize (&sql_statement);
    /* checking for table_name */
    if (argc >= 10)
    {
        vtable = gaiaDequotedSql ((char *) argv[2]);
        table = gaiaDequotedSql ((char *) argv[3]);
        col_minx = gaiaDequotedSql ((char *) argv[4]);
        col_miny = gaiaDequotedSql ((char *) argv[5]);
        col_maxx = gaiaDequotedSql ((char *) argv[6]);
        col_maxy = gaiaDequotedSql ((char *) argv[7]);
        col_srid = gaiaDequotedSql ((char *) argv[8]);
        x_force_wgs84 = gaiaDequotedSql ((char *) argv[9]);
    }
    else
    {
        *pzErr =
            sqlite3_mprintf
            ("[VirtualBBox module] CREATE VIRTUAL: illegal arg list {table_name, col_minx, col_miny, col_maxx, col_maxy, srid, longlat=1|0, columns}\n");
        goto error;
    }
    if (strcmp (x_force_wgs84, "0") == 0)
        force_wgs84 = 0;
    else if (strcmp (x_force_wgs84, "1") == 0)
        force_wgs84 = 1;
    else
    {
        *pzErr =
            sqlite3_mprintf
            ("[VirtualBBox module] CREATE VIRTUAL: illegal arg list {table_name, col_minx, col_miny, col_maxx, col_maxy, srid, longlat=1|0, columns}\n");
        goto error;
    }
    /* retrieving the base table columns */
    xname = gaiaDoubleQuotedSql (table);
    sql = sqlite3_mprintf ("PRAGMA table_info(\"%s\")", xname);
    free (xname);
    ret = sqlite3_get_table (db, sql, &results, &n_rows, &n_columns, NULL);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
        goto illegal;
    if (n_rows >= 1)
    {
        p_vt = (VirtualBBoxPtr) sqlite3_malloc (sizeof (VirtualBBox));
        if (!p_vt)
            return SQLITE_NOMEM;
        p_vt->db = db;
        p_vt->p_cache = pAux;
        p_vt->nRef = 0;
        p_vt->zErrMsg = NULL;
        len = strlen (table);
        p_vt->table = sqlite3_malloc (len + 1);
        strcpy (p_vt->table, table);
        p_vt->nColumns = n_rows;
        p_vt->Column = sqlite3_malloc (sizeof (char *) * n_rows);
        p_vt->Type = sqlite3_malloc (sizeof (char *) * n_rows);
        p_vt->Visible = sqlite3_malloc (sizeof (char *) * n_rows);
        memset (p_vt->Visible, 'N', n_rows);
        p_vt->Value = sqlite3_malloc (sizeof (SqliteValuePtr) * n_rows);
        p_vt->Srid = atoi (col_srid);
        p_vt->ForceWGS84 = force_wgs84;
#ifndef OMIT_PROJ		/* including PROJ.4 */
        if (p_vt->ForceWGS84)
            spatialite_e
            ("VirtualBBOX WARNING - WGS84 is requested, but PROJ4 support is currently disabled\n");
#endif /* end including PROJ.4 */
        p_vt->ColSrid = NULL;
        p_vt->MinX = NULL;
        p_vt->MinY = NULL;
        p_vt->MaxX = NULL;
        p_vt->MaxY = NULL;
        p_vt->BBoxGeom = NULL;
        for (i = 0; i < n_rows; i++)
        {
            *(p_vt->Column + i) = NULL;
            *(p_vt->Type + i) = NULL;
            *(p_vt->Value + i) = value_alloc ();
        }
        for (i = 1; i <= n_rows; i++)
        {
            col_name = results[(i * n_columns) + 1];
            col_type = results[(i * n_columns) + 2];
            len = strlen (col_name);
            if (strcasecmp (col_name, col_minx) == 0)
            {
                p_vt->MinX = sqlite3_malloc (len + 1);
                strcpy (p_vt->MinX, col_name);
            }
            if (strcasecmp (col_name, col_miny) == 0)
            {
                p_vt->MinY = sqlite3_malloc (len + 1);
                strcpy (p_vt->MinY, col_name);
            }
            if (strcasecmp (col_name, col_maxx) == 0)
            {
                p_vt->MaxX = sqlite3_malloc (len + 1);
                strcpy (p_vt->MaxX, col_name);
            }
            if (strcasecmp (col_name, col_maxy) == 0)
            {
                p_vt->MaxY = sqlite3_malloc (len + 1);
                strcpy (p_vt->MaxY, col_name);
            }
            if (strcasecmp (col_name, col_srid) == 0)
            {
                p_vt->ColSrid = sqlite3_malloc (len + 1);
                strcpy (p_vt->ColSrid, col_name);
            }
            *(p_vt->Column + (i - 1)) = sqlite3_malloc (len + 1);
            strcpy (*(p_vt->Column + (i - 1)), col_name);
            len = strlen (col_type);
            *(p_vt->Type + (i - 1)) = sqlite3_malloc (len + 1);
            strcpy (*(p_vt->Type + (i - 1)), col_type);
            for (i2 = 10; i2 < argc; i2++)
            {
                char *extra_col = gaiaDequotedSql ((char *) argv[i2]);
                if (strcasecmp (extra_col, col_name) == 0)
                    *(p_vt->Visible + (i - 1)) = 'Y';
                free (extra_col);
            }
        }
        sqlite3_free_table (results);
    }
    else
    {
        sqlite3_free_table (results);
        goto illegal;
    }
    if (p_vt->MinX == NULL || p_vt->MinY == NULL || p_vt->MaxX == NULL
            || p_vt->MaxY == NULL)
        goto illegal;
    /* preparing the COLUMNs for this VIRTUAL TABLE */
    xname = gaiaDoubleQuotedSql (vtable);
    sql = sqlite3_mprintf ("CREATE TABLE \"%s\" (Geometry Polygon", xname);
    free (xname);
    gaiaAppendToOutBuffer (&sql_statement, sql);
    sqlite3_free (sql);
    for (i = 0; i < p_vt->nColumns; i++)
    {
        if (*(p_vt->Visible + i) != 'Y')
            continue;
        xname = gaiaDoubleQuotedSql (*(p_vt->Column + i));
        sql = sqlite3_mprintf (", \"%s\" %s", xname, *(p_vt->Type + i));
        free (xname);
        gaiaAppendToOutBuffer (&sql_statement, sql);
        sqlite3_free (sql);
    }
    gaiaAppendToOutBuffer (&sql_statement, ")");
    if (sql_statement.Error == 0 && sql_statement.Buffer != NULL)
    {
        if (sqlite3_declare_vtab (db, sql_statement.Buffer) != SQLITE_OK)
        {
            *pzErr =
                sqlite3_mprintf
                ("[VirtualBBox module] CREATE VIRTUAL: invalid SQL statement \"%s\"",
                 sql);
            goto error;
        }
        gaiaOutBufferReset (&sql_statement);
    }
    else
        goto error;
    *ppVTab = (sqlite3_vtab *) p_vt;
    free (vtable);
    free (table);
    free (col_minx);
    free (col_miny);
    free (col_maxx);
    free (col_maxy);
    free (col_srid);
    free (x_force_wgs84);
    return SQLITE_OK;
illegal:
    /* something is going the wrong way */
    gaiaOutBufferReset (&sql_statement);
    if (p_vt)
        free_table (p_vt);
    *pzErr =
        sqlite3_mprintf
        ("[VirtualBBox module] '%s' isn't a valid BoundingBox table\n", table);
error:
    if (vtable)
        free (vtable);
    if (table)
        free (table);
    if (col_minx)
        free (col_minx);
    if (col_miny)
        free (col_miny);
    if (col_maxx)
        free (col_maxx);
    if (col_maxy)
        free (col_maxy);
    if (col_srid)
        free (col_srid);
    if (x_force_wgs84)
        free (x_force_wgs84);
    gaiaOutBufferReset (&sql_statement);
    return SQLITE_ERROR;
}
Exemplo n.º 10
0
static void
getProjParamsFromGeopackageTable (sqlite3 * sqlite, int srid,
				  char **proj_params)
{
    char *sql;
    char **results;
    int rows;
    int columns;
    int ret;
    int len;
    char *errMsg = NULL;
    struct epsg_defs *first = NULL;
    struct epsg_defs *last = NULL;
    struct epsg_defs *iter = NULL;
    const char *organization = NULL;
    int organization_coordsys_id = -1;

    *proj_params = NULL;

    sql =
	sqlite3_mprintf
	("SELECT organization, organization_coordsys_id FROM gpkg_spatial_ref_sys WHERE srs_id = %d",
	 srid);
    ret = sqlite3_get_table (sqlite, sql, &results, &rows, &columns, &errMsg);
    sqlite3_free (sql);
    if (ret != SQLITE_OK)
      {
	  spatialite_e ("unknown SRID: %d\t<%s>\n", srid, errMsg);
	  sqlite3_free (errMsg);
	  return;
      }
    if (rows == 0)
      {
	  printf
	      ("unknown SRID: %d\t(not in local database, ignoring authority and using best efforts...)\n",
	       srid);
	  organization_coordsys_id = srid;
      }
    else if (rows == 1)
      {
	  /* there are 'columns' entries in the header row (result indexes 0 to columns - 1), and our data is the next row */
	  organization = results[columns];
	  errno = 0;
	  organization_coordsys_id = strtol (results[columns + 1], NULL, 10);
	  if ((errno != 0) || (organization_coordsys_id == 0))
	    {
		spatialite_e ("Invalid organization_coordsys_id format: %s\n",
			      results[columns + 1]);
		sqlite3_free_table (results);
		return;
	    }
      }
    else if (rows > 1)
      {
	  spatialite_e
	      ("invalid or corrupt gpkg_spatial_ref_sys table - duplicate entries for : %d\n",
	       srid);
	  sqlite3_free_table (results);
	  return;
      }

    if (organization == NULL)
      {
	  /* best-effort mode */
	  initialize_epsg (srid, &first, &last);
      }
    else
      {
	  initialize_epsg (GAIA_EPSG_ANY, &first, &last);
      }
    iter = first;
    while (iter)
      {
	  if (coordinates_system_matches
	      (iter, organization, organization_coordsys_id))
	    {
		len = strlen (iter->proj4text);
		*proj_params = malloc (len + 1);
		strcpy (*proj_params, iter->proj4text);
		free_epsg (first);
		sqlite3_free_table (results);
		return;
	    }
	  iter = iter->next;
      }
    /* if we get here, we didn't find a match */
    free_epsg (first);
    sqlite3_free_table (results);
    spatialite_e ("unknown SRID: %d\n", srid);
}
Exemplo n.º 11
0
GAIAGEO_DECLARE int
gaiaExportDxf (gaiaDxfWriterPtr dxf, sqlite3 * db_handle,
	       const char *sql, const char *layer_col_name,
	       const char *geom_col_name, const char *label_col_name,
	       const char *text_height_col_name,
	       const char *text_rotation_col_name, gaiaGeomCollPtr geom_filter)
{
/* exporting a complex DXF by executing an arbitrary SQL query */
    sqlite3_stmt *stmt = NULL;
    int ret;
    int params;
    int first_row = 1;
    int layer_col = -1;
    int geom_col = -1;
    int label_col = -1;
    int text_height_col = -1;
    int text_rotation_col = -1;
    int i;
    unsigned char *p_blob;
    const unsigned char *blob;
    int len;
    const char *layer;
    const char *label = NULL;
    gaiaGeomCollPtr geom;
    gaiaDxfExportPtr aux = NULL;
    gaiaDxfExportLayerPtr lyr;
    if (dxf == NULL)
	return 0;
    if (dxf->error)
	return 0;
    if (dxf->out == NULL)
	return 0;
    if (db_handle == NULL)
	return 0;
    if (sql == NULL)
	return 0;
    if (layer_col_name == NULL)
	return 0;
    if (geom_col_name == NULL)
	return 0;

/* attempting to create the SQL prepared statement */
    ret = sqlite3_prepare_v2 (db_handle, sql, strlen (sql), &stmt, NULL);
    if (ret != SQLITE_OK)
      {
	  spatialite_e ("exportDXF - CREATE STATEMENT error: %s\n",
			sqlite3_errmsg (db_handle));
	  goto stop;
      }
    params = sqlite3_bind_parameter_count (stmt);

    if (params > 0 && geom_filter != NULL)
      {
	  /* parameter binding - Spatial Filter */
	  sqlite3_reset (stmt);
	  sqlite3_clear_bindings (stmt);
	  for (i = 1; i <= params; i++)
	    {
		gaiaToSpatiaLiteBlobWkb (geom_filter, &p_blob, &len);
		ret = sqlite3_bind_blob (stmt, i, p_blob, len, free);
		if (ret != SQLITE_OK)
		  {
		      spatialite_e ("exportDXF - parameter BIND error: %s\n",
				    sqlite3_errmsg (db_handle));
		      goto stop;
		  }
	    }
      }

/* pass #1 - sniffing the result set */
    while (1)
      {
	  /* scrolling the result set rows */
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	    {
		if (first_row)
		  {
		      /* this one is the first row of the resultset */
		      for (i = 0; i < sqlite3_column_count (stmt); i++)
			{
			    /* attempting to identify the resultset columns */
			    if (strcasecmp
				(layer_col_name,
				 sqlite3_column_name (stmt, i)) == 0)
				layer_col = i;
			    if (strcasecmp
				(geom_col_name,
				 sqlite3_column_name (stmt, i)) == 0)
				geom_col = i;
			    if (label_col_name != NULL)
			      {
				  if (strcasecmp
				      (label_col_name,
				       sqlite3_column_name (stmt, i)) == 0)
				      label_col = i;
			      }
			    if (text_height_col_name != NULL)
			      {
				  if (strcasecmp
				      (text_height_col_name,
				       sqlite3_column_name (stmt, i)) == 0)
				      text_height_col = i;
			      }
			    if (text_rotation_col_name != NULL)
			      {
				  if (strcasecmp
				      (text_rotation_col_name,
				       sqlite3_column_name (stmt, i)) == 0)
				      text_rotation_col = i;
			      }
			}
		      if (layer_col < 0)
			{
			    spatialite_e
				("exportDXF - Layer Column not found into the resultset\n");
			    goto stop;
			}
		      if (geom_col < 0)
			{
			    spatialite_e
				("exportDXF - Geometry Column not found into the resultset\n");
			    goto stop;
			}
		      first_row = 0;
		      aux = alloc_aux_exporter ();
		  }
		layer = (const char *) sqlite3_column_text (stmt, layer_col);
		blob = sqlite3_column_blob (stmt, geom_col);
		len = sqlite3_column_bytes (stmt, geom_col);
		geom = gaiaFromSpatiaLiteBlobWkb (blob, len);
		if (geom)
		  {
		      update_aux_exporter (aux, layer, geom);
		      gaiaFreeGeomColl (geom);
		  }
	    }
      }

/* pass #2 - exporting the DXF file */
    gaiaDxfWriteHeader (dxf, aux->minx, aux->miny, 0, aux->maxx, aux->maxy, 0);
    gaiaDxfWriteTables (dxf);
    lyr = aux->first;
    while (lyr != NULL)
      {
	  gaiaDxfWriteLayer (dxf, lyr->layer_name);
	  lyr = lyr->next;
      }
    gaiaDxfWriteEndSection (dxf);
    gaiaDxfWriteEntities (dxf);

    sqlite3_reset (stmt);
    while (1)
      {
	  /* scrolling the result set rows */
	  int ival;
	  double height = 10.0;
	  double rotation = 0.0;
	  ret = sqlite3_step (stmt);
	  if (ret == SQLITE_DONE)
	      break;		/* end of result set */
	  if (ret == SQLITE_ROW)
	    {
		layer = (const char *) sqlite3_column_text (stmt, layer_col);
		if (label_col >= 0)
		    label =
			(const char *) sqlite3_column_text (stmt, label_col);
		if (text_height_col >= 0)
		  {
		      if (sqlite3_column_type (stmt, text_height_col) ==
			  SQLITE_INTEGER)
			{
			    ival = sqlite3_column_int (stmt, text_height_col);
			    height = ival;
			}
		      if (sqlite3_column_type (stmt, text_height_col) ==
			  SQLITE_FLOAT)
			  height =
			      sqlite3_column_double (stmt, text_height_col);
		  }
		if (text_rotation_col >= 0)
		  {
		      if (sqlite3_column_type (stmt, text_rotation_col) ==
			  SQLITE_INTEGER)
			{
			    ival = sqlite3_column_int (stmt, text_rotation_col);
			    rotation = ival;
			}
		      if (sqlite3_column_type (stmt, text_height_col) ==
			  SQLITE_FLOAT)
			  rotation =
			      sqlite3_column_double (stmt, text_rotation_col);
		  }
		blob = sqlite3_column_blob (stmt, geom_col);
		len = sqlite3_column_bytes (stmt, geom_col);
		geom = gaiaFromSpatiaLiteBlobWkb (blob, len);
		if (geom)
		  {
		      gaiaDxfWriteGeometry (dxf, layer, label, height, rotation,
					    geom);
		      gaiaFreeGeomColl (geom);
		  }
	    }
      }
    gaiaDxfWriteEndSection (dxf);
    gaiaDxfWriteFooter (dxf);

    sqlite3_finalize (stmt);
    if (aux != NULL)
	destroy_aux_exporter (aux);
    return dxf->count;

  stop:
    if (stmt != NULL)
	sqlite3_finalize (stmt);
    if (aux != NULL)
	destroy_aux_exporter (aux);
    return 0;
}