diff --git a/gdal/ogr/ogrsf_frmts/sqlite/drv_sqlite.html b/gdal/ogr/ogrsf_frmts/sqlite/drv_sqlite.html
index 6e6833e..a8084fb 100644
--- a/gdal/ogr/ogrsf_frmts/sqlite/drv_sqlite.html
+++ b/gdal/ogr/ogrsf_frmts/sqlite/drv_sqlite.html
@@ -55,22 +55,33 @@ created over an existing file.</p>
 <h3>Database Creation Options</h3>
 
 <ul>
+	
 <li> <b>METADATA=yes/no</b>: This can be used to avoid creating the 
 geometry_columns and spatial_ref_sys tables in a new database.  By default
 these metadata tables are created when a new database is created.
+
+<li> <b>SPATIALITE=yes/no</b>: Create the SpatiaLite flavour of the metadata
+tables, which are a bit differ from the metadata used by this OGR driver and
+from OGC specifications. Implies <b>METADATA=yes</b>.
+
 </ul>
 
 <h3>Layer Creation Options</h3>
 
 <ul>
-<li> <b>FORMAT=WKB/WKT</b>: Controls the format used for the geometry column.
-By default WKB (Well Known Binary) is used.  This is generally more space
-and processing efficient, but harder to inspect or use in simple applications
-than WKT (Well Known Text).
+
+<li> <b>FORMAT=WKB/WKT/SPATIALITE</b>: Controls the format used for the
+geometry column. By default WKB (Well Known Binary) is used.  This is
+generally more space and processing efficient, but harder to inspect or use in
+simple applications than WKT (Well Known Text). SpatiaLite extension uses its
+own binary format to store geometries and you can choose it as well. It will
+be selected automatically when SpatiaLite database is opened or created with
+<b>SPATIALITE=yes</b> option.
 
 <li> <b>LAUNDER=yes/no</b>: Controls whether layer and field names will be 
 laundered for easier use in SQLite.  Laundered names will be convered to lower 
 case and some special characters will be changed to underscores. 
+
 </ul>
 
 
@@ -79,7 +90,8 @@ case and some special characters will be changed to underscores.
 <li>Development of the OGR SQLite driver was supported by 
 <a href="http://www.dmsolutions.ca/">DM Solutions Group</a> and 
 <a href="http://www.gomoos.org/">GoMOOS</a>.</li>
-<li><a href="http://www.sqlite.org/">http://www.sqlite.org</a>: Main SQLite page.</li>
+<li><a href="http://www.sqlite.org/">http://www.sqlite.org</a>: Main SQLite page.
+<li> <a href="http://www.gaia-gis.it/spatialite/">http://www.gaia-gis.it/spatialite/</a>: SpatiaLite extension to SQLite.
 <li> <A href="http://trac.osgeo.org/fdo/wiki/FDORfc16">FDO RFC 16</a>: FDO Provider for SQLite</li>
 </ul>
 
diff --git a/gdal/ogr/ogrsf_frmts/sqlite/ogr_sqlite.h b/gdal/ogr/ogrsf_frmts/sqlite/ogr_sqlite.h
index f21122a..42d0e31 100644
--- a/gdal/ogr/ogrsf_frmts/sqlite/ogr_sqlite.h
+++ b/gdal/ogr/ogrsf_frmts/sqlite/ogr_sqlite.h
@@ -35,6 +35,19 @@
 #include "sqlite3.h"
 
 /************************************************************************/
+/*      Format used to store geometry data in the database.             */
+/************************************************************************/
+
+enum OGRSQLiteGeomFormat
+{
+    OSGF_None = 0,
+    OSGF_WKT = 1,
+    OSGF_WKB = 2,
+    OSGF_FGF = 3,
+    OSGF_SpatiaLite = 4
+};
+
+/************************************************************************/
 /*                            OGRSQLiteLayer                            */
 /************************************************************************/
 
@@ -56,7 +69,7 @@ class OGRSQLiteLayer : public OGRLayer
     OGRSQLiteDataSource *poDS;
 
     CPLString           osGeomColumn;
-    CPLString           osGeomFormat;
+    OGRSQLiteGeomFormat eGeomFormat;
 
     char                *pszFIDColumn;
 
@@ -67,9 +80,11 @@ class OGRSQLiteLayer : public OGRLayer
 
     virtual void	ClearStatement() = 0;
 
-   OGRErr               ImportSpatialiteGeometry( const GByte *pabyData, 
-                                                  int nBytes,
-                                                  OGRGeometry **ppoGeometry );
+    static OGRErr       ImportSpatiaLiteGeometry( const GByte *, int,
+                                                  OGRGeometry ** );
+    static OGRErr       ExportSpatiaLiteGeometry( const OGRGeometry *,
+                                                  GInt32, OGRwkbByteOrder,
+                                                  GByte **, int * );
 
   public:
                         OGRSQLiteLayer();
@@ -118,7 +133,8 @@ class OGRSQLiteTableLayer : public OGRSQLiteLayer
                                     const char *pszGeomCol,
                                     OGRwkbGeometryType eGeomType,
                                     const char *pszGeomFormat,
-                                    OGRSpatialReference *poSRS );
+                                    OGRSpatialReference *poSRS,
+                                    int nSRSId = -1 );
 
     virtual void        ResetReading();
     virtual int         GetFeatureCount( int );
@@ -182,10 +198,14 @@ class OGRSQLiteDataSource : public OGRDataSource
     int                *panSRID;
     OGRSpatialReference **papoSRS;
 
-    int                 bHaveGeometryColumns; 
+    int                 bHaveGeometryColumns;
+    int                 bIsSpatiaLite;
     
     virtual void        DeleteLayer( const char *pszLayer );
 
+    static OGRwkbGeometryType SpatiaLiteToOGRGeomType( const char * );
+    static const char   *OGRToSpatiaLiteGeomType( OGRwkbGeometryType );
+
   public:
                         OGRSQLiteDataSource();
                         ~OGRSQLiteDataSource();
@@ -195,7 +215,8 @@ class OGRSQLiteDataSource : public OGRDataSource
                                    const char *pszGeomCol = NULL,
                                    OGRwkbGeometryType eGeomType = wkbUnknown,
                                    const char *pszGeomFormat = NULL,
-                                   OGRSpatialReference *poSRS = NULL );
+                                   OGRSpatialReference *poSRS = NULL,
+                                   int nSRID = -1 );
 
     const char          *GetName() { return pszName; }
     int                 GetLayerCount() { return nLayers; }
diff --git a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp
index b2b0066..c616527 100644
--- a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp
+++ b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedatasource.cpp
@@ -32,6 +32,7 @@
 #include "cpl_string.h"
 
 CPL_CVSID("$Id$");
+
 /************************************************************************/
 /*                        OGRSQLiteDataSource()                         */
 /************************************************************************/
@@ -48,6 +49,9 @@ OGRSQLiteDataSource::OGRSQLiteDataSource()
     nKnownSRID = 0;
     panSRID = NULL;
     papoSRS = NULL;
+
+    bHaveGeometryColumns = FALSE;
+    bIsSpatiaLite = FALSE;
 }
 
 /************************************************************************/
@@ -79,6 +83,65 @@ OGRSQLiteDataSource::~OGRSQLiteDataSource()
 }
 
 /************************************************************************/
+/*                     SpatiaLiteToOGRGeomType()                        */
+/*      Map SpatiaLite geometry format strings to corresponding         */
+/*      OGR constants.                                                  */
+/************************************************************************/
+
+OGRwkbGeometryType
+OGRSQLiteDataSource::SpatiaLiteToOGRGeomType( const char *pszGeomType )
+{
+    if ( EQUAL(pszGeomType, "POINT") )
+        return wkbPoint;
+    else if ( EQUAL(pszGeomType, "LINESTRING") )
+        return wkbLineString;
+    else if ( EQUAL(pszGeomType, "POLYGON") )
+        return wkbPolygon;
+    else if ( EQUAL(pszGeomType, "MULTIPOINT") )
+        return wkbMultiPoint;
+    else if ( EQUAL(pszGeomType, "MULTILINESTRING") )
+        return wkbMultiLineString;
+    else if ( EQUAL(pszGeomType, "MULTIPOLYGON") )
+        return wkbMultiPolygon;
+    else if ( EQUAL(pszGeomType, "GEOMETRYCOLLECTION") )
+        return wkbGeometryCollection;
+    else
+        return wkbUnknown;
+}
+
+/************************************************************************/
+/*                     OGRToSpatiaLiteGeomType()                        */
+/*      Map OGR geometry format constants to corresponding              */
+/*      SpatiaLite strings                                              */
+/************************************************************************/
+
+const char *
+OGRSQLiteDataSource::OGRToSpatiaLiteGeomType( OGRwkbGeometryType eGeomType )
+{
+    switch ( wkbFlatten(eGeomType) )
+    {
+        case wkbUnknown:
+            return "GEOMETRY";
+        case wkbPoint:
+            return "POINT";
+        case wkbLineString:
+            return "LINESTRING";
+        case wkbPolygon:
+            return "POLYGON";
+        case wkbMultiPoint:
+            return "MULTIPOINT";
+        case wkbMultiLineString:
+            return "MULTILINESTRING";
+        case wkbMultiPolygon:
+            return "MULTIPOLYGON";
+        case wkbGeometryCollection:
+            return "GEOMETRYCOLLECTION";
+        default:
+            return "";
+    }
+}
+
+/************************************************************************/
 /*                                Open()                                */
 /*                                                                      */
 /*      Note, the Open() will implicitly create the database if it      */
@@ -149,36 +212,72 @@ int OGRSQLiteDataSource::Open( const char * pszNewName )
     }
 
 /* -------------------------------------------------------------------- */
-/*      Otherwise our final resort is to return all tables and views    */
-/*      as non-spatial tables.                                          */
+/*      Otherwise we can deal with SpatiaLite database.                 */
 /* -------------------------------------------------------------------- */
-    else
+    sqlite3_free( pszErrMsg );
+    rc = sqlite3_get_table( hDB,
+                            "SELECT f_table_name, f_geometry_column, "
+                            "type, coord_dimension, srid, "
+                            "spatial_index_enabled FROM geometry_columns",
+                            &papszResult, &nRowCount, 
+                            &nColCount, &pszErrMsg );
+
+    if ( rc == SQLITE_OK )
     {
-        sqlite3_free( pszErrMsg );
-        rc = sqlite3_get_table( hDB,
-                                "SELECT name FROM sqlite_master "
-                                "WHERE type IN ('table','view') "
-                                "UNION ALL "
-                                "SELECT name FROM sqlite_temp_master "
-                                "WHERE type IN ('table','view') "
-                                "ORDER BY 1",
-                                &papszResult, &nRowCount, 
-                                &nColCount, &pszErrMsg );
+        bIsSpatiaLite = TRUE;
+        bHaveGeometryColumns = TRUE;
 
-        if( rc != SQLITE_OK )
+        for ( iRow = 0; iRow < nRowCount; iRow++ )
         {
-            CPLError( CE_Failure, CPLE_AppDefined, 
-                      "Unable to fetch list of tables: %s", 
-                      pszErrMsg );
-            sqlite3_free( pszErrMsg );
-            return FALSE;
+            char **papszRow = papszResult + iRow * 6 + 6;
+            OGRwkbGeometryType eGeomType;
+            int nSRID = 0;
+
+            eGeomType = SpatiaLiteToOGRGeomType(papszRow[2]);
+
+            if( atoi(papszRow[3]) > 2 )
+                eGeomType = (OGRwkbGeometryType) (((int)eGeomType) | wkb25DBit);
+
+            if( papszRow[4] != NULL )
+                nSRID = atoi(papszRow[4]);
+
+            OpenTable( papszRow[0], papszRow[1], eGeomType, "SpatiaLite",
+                       FetchSRS( nSRID ), nSRID );
         }
-        
-        for( iRow = 0; iRow < nRowCount; iRow++ )
-            OpenTable( papszResult[iRow+1] );
-        
+
         sqlite3_free_table(papszResult);
+
+        return TRUE;
+    }
+
+/* -------------------------------------------------------------------- */
+/*      Otherwise our final resort is to return all tables and views    */
+/*      as non-spatial tables.                                          */
+/* -------------------------------------------------------------------- */
+    sqlite3_free( pszErrMsg );
+    rc = sqlite3_get_table( hDB,
+                            "SELECT name FROM sqlite_master "
+                            "WHERE type IN ('table','view') "
+                            "UNION ALL "
+                            "SELECT name FROM sqlite_temp_master "
+                            "WHERE type IN ('table','view') "
+                            "ORDER BY 1",
+                            &papszResult, &nRowCount, 
+                            &nColCount, &pszErrMsg );
+
+    if( rc != SQLITE_OK )
+    {
+        CPLError( CE_Failure, CPLE_AppDefined, 
+                  "Unable to fetch list of tables: %s", 
+                  pszErrMsg );
+        sqlite3_free( pszErrMsg );
+        return FALSE;
     }
+    
+    for( iRow = 0; iRow < nRowCount; iRow++ )
+        OpenTable( papszResult[iRow+1] );
+    
+    sqlite3_free_table(papszResult);
 
     return TRUE;
 }
@@ -191,7 +290,7 @@ int OGRSQLiteDataSource::OpenTable( const char *pszNewName,
                                     const char *pszGeomCol,
                                     OGRwkbGeometryType eGeomType,
                                     const char *pszGeomFormat,
-                                    OGRSpatialReference *poSRS )
+                                    OGRSpatialReference *poSRS, int nSRID )
 
 {
 /* -------------------------------------------------------------------- */
@@ -203,7 +302,7 @@ int OGRSQLiteDataSource::OpenTable( const char *pszNewName,
 
     if( poLayer->Initialize( pszNewName, pszGeomCol, 
                              eGeomType, pszGeomFormat,
-                             poSRS ) )
+                             poSRS, nSRID ) )
     {
         delete poLayer;
         return FALSE;
@@ -344,10 +443,16 @@ OGRSQLiteDataSource::CreateLayer( const char * pszLayerNameIn,
     
     pszGeomFormat = CSLFetchNameValue( papszOptions, "FORMAT" );
     if( pszGeomFormat == NULL )
-        pszGeomFormat = "WKB";
+    {
+        if ( !bIsSpatiaLite )
+            pszGeomFormat = "WKB";
+        else
+            pszGeomFormat = "SpatiaLite";
+    }
 
     if( !EQUAL(pszGeomFormat,"WKT") 
-        && !EQUAL(pszGeomFormat,"WKB") )
+        && !EQUAL(pszGeomFormat,"WKB")
+        && !EQUAL(pszGeomFormat, "SpatiaLite") )
     {
         CPLError( CE_Failure, CPLE_AppDefined, 
                   "FORMAT=%s not recognised or supported.", 
@@ -385,7 +490,7 @@ OGRSQLiteDataSource::CreateLayer( const char * pszLayerNameIn,
 
 /* -------------------------------------------------------------------- */
 /*      Try to get the SRS Id of this spatial reference system,         */
-/*      adding tot the srs table if needed.                             */
+/*      adding to the srs table if needed.                              */
 /* -------------------------------------------------------------------- */
     int nSRSId = -1;
 
@@ -427,7 +532,9 @@ OGRSQLiteDataSource::CreateLayer( const char * pszLayerNameIn,
         }
     }
 
+#ifdef DEBUG
     CPLDebug( "OGR_SQLITE", "exec(%s)", osCommand.c_str() );
+#endif
 
     rc = sqlite3_exec( hDB, osCommand, NULL, NULL, &pszErrMsg );
     if( rc != SQLITE_OK )
@@ -456,7 +563,10 @@ OGRSQLiteDataSource::CreateLayer( const char * pszLayerNameIn,
             "DELETE FROM geometry_columns WHERE f_table_name = '%s'", 
             pszLayerName );
                  
+#ifdef DEBUG
         CPLDebug( "OGR_SQLITE", "exec(%s)", osCommand.c_str() );
+#endif
+
         rc = sqlite3_exec( hDB, osCommand, NULL, NULL, &pszErrMsg );
         if( rc != SQLITE_OK )
         {
@@ -470,23 +580,48 @@ OGRSQLiteDataSource::CreateLayer( const char * pszLayerNameIn,
             nCoordDim = 3;
 
         if( nSRSId > 0 )
-            osCommand.Printf(
-                "INSERT INTO geometry_columns "
-                "(f_table_name, f_geometry_column, geometry_format, geometry_type, "
-                "coord_dimension, srid) VALUES "
-                "('%s','%s','%s', %d, %d, %d)", 
-                pszLayerName, pszGeomCol, pszGeomFormat, (int) wkbFlatten(eType), 
-                nCoordDim, nSRSId );
+        {
+            if ( bIsSpatiaLite )
+                osCommand.Printf(
+                    "INSERT INTO geometry_columns "
+                    "(f_table_name, f_geometry_column, type, "
+                    "coord_dimension, srid, spatial_index_enabled) "
+                    "VALUES ('%s','%s', '%s', %d, %d, 0)", 
+                    pszLayerName, pszGeomCol, OGRToSpatiaLiteGeomType(eType),
+                    nCoordDim, nSRSId );
+            else
+                osCommand.Printf(
+                    "INSERT INTO geometry_columns "
+                    "(f_table_name, f_geometry_column, geometry_format, "
+                    "geometry_type, coord_dimension, srid) VALUES "
+                    "('%s','%s','%s', %d, %d, %d)", 
+                    pszLayerName, pszGeomCol, pszGeomFormat,
+                    (int) wkbFlatten(eType), nCoordDim, nSRSId );
+        }
         else
-            osCommand.Printf(
-                "INSERT INTO geometry_columns "
-                "(f_table_name, f_geometry_column, geometry_format, geometry_type, "
-                "coord_dimension) VALUES "
-                "('%s','%s','%s', %d, %d)", 
-                pszLayerName, pszGeomCol, pszGeomFormat, (int) wkbFlatten(eType), 
-                nCoordDim );
-        
+        {
+            if ( bIsSpatiaLite )
+                osCommand.Printf(
+                    "INSERT INTO geometry_columns "
+                    "(f_table_name, f_geometry_column, type, "
+                    "coord_dimension, spatial_index_enabled) "
+                    "VALUES ('%s','%s', '%s', %d, 0)", 
+                    pszLayerName, pszGeomCol, OGRToSpatiaLiteGeomType(eType),
+                    nCoordDim );
+            else
+                osCommand.Printf(
+                    "INSERT INTO geometry_columns "
+                    "(f_table_name, f_geometry_column, geometry_format, "
+                    "geometry_type, coord_dimension) VALUES "
+                    "('%s','%s','%s', %d, %d)", 
+                    pszLayerName, pszGeomCol, pszGeomFormat,
+                    (int) wkbFlatten(eType), nCoordDim );
+        }
+
+#ifdef DEBUG
         CPLDebug( "OGR_SQLITE", "exec(%s)", osCommand.c_str() );
+#endif
+
         rc = sqlite3_exec( hDB, osCommand, NULL, NULL, &pszErrMsg );
         if( rc != SQLITE_OK )
         {
@@ -506,7 +641,7 @@ OGRSQLiteDataSource::CreateLayer( const char * pszLayerNameIn,
     poLayer = new OGRSQLiteTableLayer( this );
 
     poLayer->Initialize( pszLayerName, pszGeomCol, eType, pszGeomFormat, 
-                         FetchSRS(nSRSId) );
+                         FetchSRS(nSRSId), nSRSId );
 
     poLayer->SetLaunderFlag( CSLFetchBoolean(papszOptions,"LAUNDER",TRUE) );
 
@@ -637,7 +772,10 @@ OGRErr OGRSQLiteDataSource::SoftStartTransaction()
         int rc;
         char *pszErrMsg;
         
+#ifdef DEBUG
         CPLDebug( "OGR_SQLITE", "BEGIN Transaction" );
+#endif
+
         rc = sqlite3_exec( hDB, "BEGIN", NULL, NULL, &pszErrMsg );
         if( rc != SQLITE_OK )
         {
@@ -676,7 +814,10 @@ OGRErr OGRSQLiteDataSource::SoftCommit()
         int rc;
         char *pszErrMsg;
         
+#ifdef DEBUG
         CPLDebug( "OGR_SQLITE", "COMMIT Transaction" );
+#endif
+
         rc = sqlite3_exec( hDB, "COMMIT", NULL, NULL, &pszErrMsg );
         if( rc != SQLITE_OK )
         {
@@ -712,7 +853,10 @@ OGRErr OGRSQLiteDataSource::SoftRollback()
     int rc;
     char *pszErrMsg;
     
+#ifdef DEBUG
     CPLDebug( "OGR_SQLITE", "ROLLBACK Transaction" );
+#endif
+
     rc = sqlite3_exec( hDB, "ROLLBACK", NULL, NULL, &pszErrMsg );
     if( rc != SQLITE_OK )
     {
@@ -754,9 +898,8 @@ OGRErr OGRSQLiteDataSource::FlushSoftTransaction()
 int OGRSQLiteDataSource::FetchSRSId( OGRSpatialReference * poSRS )
 
 {
-    char                *pszWKT = NULL;
     int                 nSRSId = -1;
-    const char*         pszAuthorityName;
+    const char          *pszAuthorityName, *pszAuthorityCode = NULL;
     CPLString           osCommand;
     char *pszErrMsg;
     int   rc;
@@ -774,15 +917,70 @@ int OGRSQLiteDataSource::FetchSRSId( OGRSpatialReference * poSRS )
 /* -------------------------------------------------------------------- */
     if( pszAuthorityName != NULL && strlen(pszAuthorityName) > 0 )
     {
-        osCommand.Printf( "SELECT srid FROM spatial_ref_sys WHERE "
-                          "auth_name = '%s' AND auth_srid = '%s'",
-                          pszAuthorityName,
-                          poSRS->GetAuthorityCode(NULL) );
+        pszAuthorityCode = poSRS->GetAuthorityCode(NULL);
 
-        rc = sqlite3_get_table( hDB, osCommand, &papszResult, 
+        if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
+        {
+            // XXX: We are using case insensitive comparison for "auth_name"
+            // values, because there are variety of options exist. By default
+            // the driver uses 'EPSG' in upper case, but SpatiaLite extension
+            // uses 'epsg' in lower case.
+            osCommand.Printf( "SELECT srid FROM spatial_ref_sys WHERE "
+                              "auth_name = '%s' COLLATE NOCASE AND auth_srid = '%s'",
+                              pszAuthorityName, pszAuthorityCode );
+
+            rc = sqlite3_get_table( hDB, osCommand, &papszResult, 
+                                    &nRowCount, &nColCount, &pszErrMsg );
+            if( rc != SQLITE_OK )
+            {
+                CPLError( CE_Failure, CPLE_AppDefined,
+                          "Search for existing SRS ID failed: %s", pszErrMsg );
+                sqlite3_free( pszErrMsg );
+            }
+            else if( nRowCount == 1 )
+            {
+                nSRSId = atoi(papszResult[1]);
+                sqlite3_free_table(papszResult);
+                return nSRSId;
+            }
+            sqlite3_free_table(papszResult);
+        }
+    }
+
+/* -------------------------------------------------------------------- */
+/*      Search for existing record using either WKT definition or       */
+/*      PROJ.4 string (SpatiaLite variant).                             */
+/* -------------------------------------------------------------------- */
+    CPLString   osSRS;
+
+    if ( !bIsSpatiaLite )
+    {
+/* -------------------------------------------------------------------- */
+/*      Translate SRS to WKT.                                           */
+/* -------------------------------------------------------------------- */
+        char    *pszWKT = NULL;
+
+        if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
+            return -1;
+
+        osSRS = pszWKT;
+        CPLFree( pszWKT );
+        pszWKT = NULL;
+
+/* -------------------------------------------------------------------- */
+/*      Try to find based on the WKT match.                             */
+/* -------------------------------------------------------------------- */
+        osCommand.Printf( "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
+                          osSRS.c_str());
+        
+        rc = sqlite3_get_table( hDB, osCommand, &papszResult,
                                 &nRowCount, &nColCount, &pszErrMsg );
         if( rc != SQLITE_OK )
+        {
+            CPLError( CE_Failure, CPLE_AppDefined,
+                      "Search for existing SRS by WKT failed: %s", pszErrMsg );
             sqlite3_free( pszErrMsg );
+        }
         else if( nRowCount == 1 )
         {
             nSRSId = atoi(papszResult[1]);
@@ -793,35 +991,47 @@ int OGRSQLiteDataSource::FetchSRSId( OGRSpatialReference * poSRS )
     }
 
 /* -------------------------------------------------------------------- */
-/*      Translate SRS to WKT.                                           */
+/*      Handle SpatiaLite flavour of the spatial_ref_sys.               */
+/* -------------------------------------------------------------------- */
+    else
+    {
+/* -------------------------------------------------------------------- */
+/*      Translate SRS to PROJ.4 string.                                 */
 /* -------------------------------------------------------------------- */
-    CPLString osWKT;
+        char    *pszProj4 = NULL;
 
-    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
-        return -1;
+        if( poSRS->exportToProj4( &pszProj4 ) != OGRERR_NONE )
+            return -1;
 
-    osWKT = pszWKT;
-    CPLFree( pszWKT );
-    pszWKT = NULL;
+        osSRS = pszProj4;
+        CPLFree( pszProj4 );
+        pszProj4 = NULL;
 
 /* -------------------------------------------------------------------- */
 /*      Try to find based on the WKT match.                             */
 /* -------------------------------------------------------------------- */
-    osCommand.Printf( "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
-                      osWKT.c_str());
-    
-    rc = sqlite3_get_table( hDB, osCommand, 
-                            &papszResult, &nRowCount, &nColCount, &pszErrMsg );
-    if( rc != SQLITE_OK )
-        sqlite3_free( pszErrMsg );
-    else if( nRowCount == 1 )
-    {
-        nSRSId = atoi(papszResult[1]);
+        osCommand.Printf(
+            "SELECT srid FROM spatial_ref_sys WHERE proj4text = '%s'",
+            osSRS.c_str());
+        
+        rc = sqlite3_get_table( hDB, osCommand, &papszResult,
+                                &nRowCount, &nColCount, &pszErrMsg );
+        if( rc != SQLITE_OK )
+        {
+            CPLError( CE_Failure, CPLE_AppDefined,
+                      "Search for existing SRS by PROJ.4 string failed: %s",
+                      pszErrMsg );
+            sqlite3_free( pszErrMsg );
+        }
+        else if( nRowCount == 1 )
+        {
+            nSRSId = atoi(papszResult[1]);
+            sqlite3_free_table(papszResult);
+            return nSRSId;
+        }
         sqlite3_free_table(papszResult);
-        return nSRSId;
     }
-    sqlite3_free_table(papszResult);
-    
+
 /* -------------------------------------------------------------------- */
 /*      If the command actually failed, then the metadata table is      */
 /*      likely missing, so we give up.                                  */
@@ -830,40 +1040,111 @@ int OGRSQLiteDataSource::FetchSRSId( OGRSpatialReference * poSRS )
         return -1;
 
 /* -------------------------------------------------------------------- */
-/*      Get the current maximum srid in the srs table.                  */
+/*      If we have an authority code try to assign SRS ID the same      */
+/*      as that code.                                                   */
 /* -------------------------------------------------------------------- */
-    rc = sqlite3_get_table( hDB, "SELECT MAX(srid) FROM spatial_ref_sys", 
-                            &papszResult, &nRowCount, &nColCount, &pszErrMsg );
-    
-    if( rc != SQLITE_OK || nRowCount < 1 )
+    if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
     {
-        sqlite3_free( pszErrMsg );
-        return -1;
+        osCommand.Printf( "SELECT * FROM spatial_ref_sys WHERE auth_srid='%s'",
+                          pszAuthorityCode );
+        rc = sqlite3_get_table( hDB, osCommand, &papszResult,
+                                &nRowCount, &nColCount, &pszErrMsg );
+        
+        if( rc != SQLITE_OK )
+        {
+            CPLError( CE_Failure, CPLE_AppDefined,
+                      "exec(SELECT '%s' FROM spatial_ref_sys) failed: %s",
+                      pszAuthorityCode, pszErrMsg );
+            sqlite3_free( pszErrMsg );
+        }
+
+/* -------------------------------------------------------------------- */
+/*      If there is no SRS ID with such auth_srid, use it as SRS ID.    */
+/* -------------------------------------------------------------------- */
+        if ( nRowCount < 1 )
+            nSRSId = atoi(pszAuthorityCode);
+        sqlite3_free_table(papszResult);
     }
 
-    if( papszResult[1] )
-        nSRSId = atoi(papszResult[1]);
-    else
-        nSRSId = 50000;
-    sqlite3_free_table(papszResult);
+/* -------------------------------------------------------------------- */
+/*      Otherwise get the current maximum srid in the srs table.        */
+/* -------------------------------------------------------------------- */
+    if ( nSRSId == -1 )
+    {
+        rc = sqlite3_get_table( hDB, "SELECT MAX(srid) FROM spatial_ref_sys", 
+                                &papszResult, &nRowCount, &nColCount,
+                                &pszErrMsg );
+        
+        if( rc != SQLITE_OK )
+        {
+            CPLError( CE_Failure, CPLE_AppDefined,
+                      "SELECT of the maximum SRS ID failed: %s", pszErrMsg );
+            sqlite3_free( pszErrMsg );
+            return -1;
+        }
+
+        if ( nRowCount < 1 || !papszResult[1] )
+            nSRSId = 50000;
+        else
+            nSRSId = atoi(papszResult[1]) + 1;  // Insert as the next SRS ID
+        sqlite3_free_table(papszResult);
+    }
 
 /* -------------------------------------------------------------------- */
 /*      Try adding the SRS to the SRS table.                            */
 /* -------------------------------------------------------------------- */
-    if( pszAuthorityName != NULL )
+    if ( !bIsSpatiaLite )
     {
-        osCommand.Printf(
-            "INSERT INTO spatial_ref_sys (srid,srtext,auth_name,auth_srid) "
-            "                     VALUES (%d, '%s', '%s', '%s')",
-            nSRSId, osWKT.c_str(), 
-            pszAuthorityName, poSRS->GetAuthorityCode(NULL) );
+        if( pszAuthorityName != NULL )
+        {
+            osCommand.Printf(
+                "INSERT INTO spatial_ref_sys (srid,srtext,auth_name,auth_srid) "
+                "                     VALUES (%d, '%s', '%s', '%s')",
+                nSRSId, osSRS.c_str(), 
+                pszAuthorityName, poSRS->GetAuthorityCode(NULL) );
+        }
+        else
+        {
+            osCommand.Printf(
+                "INSERT INTO spatial_ref_sys (srid,srtext) "
+                "                     VALUES (%d, '%s')",
+                nSRSId, osSRS.c_str() );
+        }
     }
     else
     {
-        osCommand.Printf(
-            "INSERT INTO spatial_ref_sys (srid,srtext) "
-            "                     VALUES (%d, '%s')",
-            nSRSId, osWKT.c_str() );
+        const char  *pszProjCS = poSRS->GetAttrValue("PROJCS");
+
+        if( pszAuthorityName != NULL )
+        {
+            if ( pszProjCS )
+                osCommand.Printf(
+                    "INSERT INTO spatial_ref_sys "
+                    "(srid, auth_name, auth_srid, ref_sys_name, proj4text) "
+                    "VALUES (%d, '%s', '%s', '%s', '%s')",
+                    nSRSId, pszAuthorityName,
+                    poSRS->GetAuthorityCode(NULL), pszProjCS, osSRS.c_str() );
+            else
+                osCommand.Printf(
+                    "INSERT INTO spatial_ref_sys "
+                    "(srid, auth_name, auth_srid, proj4text) "
+                    "VALUES (%d, '%s', '%s', '%s')",
+                    nSRSId, pszAuthorityName,
+                    poSRS->GetAuthorityCode(NULL), osSRS.c_str() );
+        }
+        else
+        {
+            if ( pszProjCS )
+                osCommand.Printf(
+                    "INSERT INTO spatial_ref_sys "
+                    "(srid, ref_sys_name, proj4text) VALUES (%d, '%s', '%s')",
+                    nSRSId, pszProjCS, osSRS.c_str() );
+            else
+                osCommand.Printf(
+                    "INSERT INTO spatial_ref_sys "
+                    "(srid, proj4text) VALUES (%d, '%s')",
+                    nSRSId, osSRS.c_str() );
+        }
     }
 
     rc = sqlite3_exec( hDB, osCommand, NULL, NULL, &pszErrMsg );
@@ -918,29 +1199,67 @@ OGRSpatialReference *OGRSQLiteDataSource::FetchSRS( int nId )
                       nId );
     rc = sqlite3_get_table( hDB, osCommand, 
                             &papszResult, &nRowCount, &nColCount, &pszErrMsg );
-    if( rc != SQLITE_OK )
+
+    if ( rc == SQLITE_OK )
     {
-        CPLError( CE_Failure, CPLE_AppDefined, 
-                  "%s: %s", osCommand.c_str(), pszErrMsg );
-        sqlite3_free( pszErrMsg );
-        return NULL;
-    }
-    else if( nRowCount < 1 )
-        return NULL;
+        if( nRowCount < 1 )
+            return NULL;
 
-    CPLString osWKT = papszResult[1];
-    sqlite3_free_table(papszResult);
+        CPLString osWKT = papszResult[1];
+        sqlite3_free_table(papszResult);
 
 /* -------------------------------------------------------------------- */
 /*      Translate into a spatial reference.                             */
 /* -------------------------------------------------------------------- */
-    char *pszWKT = (char *) osWKT.c_str();
+        char *pszWKT = (char *) osWKT.c_str();
 
-    poSRS = new OGRSpatialReference();
-    if( poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE )
+        poSRS = new OGRSpatialReference();
+        if( poSRS->importFromWkt( &pszWKT ) != OGRERR_NONE )
+        {
+            delete poSRS;
+            poSRS = NULL;
+        }
+    }
+
+/* -------------------------------------------------------------------- */
+/*      Next try SpatiaLite flavour. SpatiaLite uses PROJ.4 strings     */
+/*      in 'proj4text' column instead of WKT in 'srtext'.               */
+/* -------------------------------------------------------------------- */
+    else
     {
-        delete poSRS;
-        poSRS = NULL;
+        osCommand.Printf(
+            "SELECT proj4text FROM spatial_ref_sys WHERE srid = %d", nId );
+        rc = sqlite3_get_table( hDB, osCommand, 
+                                &papszResult, &nRowCount,
+                                &nColCount, &pszErrMsg );
+        if ( rc == SQLITE_OK )
+        {
+            if( nRowCount < 1 )
+                return NULL;
+
+/* -------------------------------------------------------------------- */
+/*      Translate into a spatial reference.                             */
+/* -------------------------------------------------------------------- */
+            poSRS = new OGRSpatialReference();
+            if( poSRS->importFromProj4( papszResult[1] ) != OGRERR_NONE )
+            {
+                delete poSRS;
+                poSRS = NULL;
+            }
+
+            sqlite3_free_table(papszResult);
+        }
+
+/* -------------------------------------------------------------------- */
+/*      No success, report an error.                                    */
+/* -------------------------------------------------------------------- */
+        else
+        {
+            CPLError( CE_Failure, CPLE_AppDefined, 
+                      "%s: %s", osCommand.c_str(), pszErrMsg );
+            sqlite3_free( pszErrMsg );
+            return NULL;
+        }
     }
 
 /* -------------------------------------------------------------------- */
diff --git a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp
index ed9a7a5..c940122 100644
--- a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp
+++ b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitedriver.cpp
@@ -134,9 +134,53 @@ OGRDataSource *OGRSQLiteDriver::CreateDataSource( const char * pszName,
     }
 
 /* -------------------------------------------------------------------- */
-/*      Create the geometry_columns metadata table.                     */
+/*      Create the SpatiaLite metadata tables.                          */
 /* -------------------------------------------------------------------- */
-    if( CSLFetchBoolean( papszOptions, "METADATA", TRUE ) )
+    if ( CSLFetchBoolean( papszOptions, "SPATIALITE", FALSE ) )
+    {
+        CPLString osCommand;
+        char *pszErrMsg = NULL;
+
+        osCommand = 
+            "CREATE TABLE geometry_columns ("
+            "     f_table_name VARCHAR, "
+            "     f_geometry_column VARCHAR, "
+            "     type VARCHAR, "
+            "     coord_dimension INTEGER, "
+            "     srid INTEGER,"
+            "     spatial_index_enabled INTEGER )";
+        rc = sqlite3_exec( hDB, osCommand, NULL, NULL, &pszErrMsg );
+        if( rc != SQLITE_OK )
+        {
+            CPLError( CE_Failure, CPLE_AppDefined, 
+                      "Unable to create table geometry_columns: %s",
+                      pszErrMsg );
+            sqlite3_free( pszErrMsg );
+            return NULL;
+        }
+
+        osCommand = 
+            "CREATE TABLE spatial_ref_sys        ("
+            "     srid INTEGER UNIQUE,"
+            "     auth_name VARCHAR,"
+            "     auth_srid INTEGER,"
+            "     ref_sys_name VARCHAR,"
+            "     proj4text VARCHAR )";
+        rc = sqlite3_exec( hDB, osCommand, NULL, NULL, &pszErrMsg );
+        if( rc != SQLITE_OK )
+        {
+            CPLError( CE_Failure, CPLE_AppDefined, 
+                      "Unable to create table spatial_ref_sys: %s",
+                      pszErrMsg );
+            sqlite3_free( pszErrMsg );
+            return NULL;
+        }
+    }
+
+/* -------------------------------------------------------------------- */
+/*  Create the geometry_columns and spatial_ref_sys metadata tables.    */
+/* -------------------------------------------------------------------- */
+    else if( CSLFetchBoolean( papszOptions, "METADATA", TRUE ) )
     {
         CPLString osCommand;
         char *pszErrMsg = NULL;
diff --git a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitelayer.cpp b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitelayer.cpp
index 77f86d8..14731a8 100644
--- a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitelayer.cpp
+++ b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitelayer.cpp
@@ -45,6 +45,8 @@ OGRSQLiteLayer::OGRSQLiteLayer()
 
     pszFIDColumn = NULL;
 
+    eGeomFormat = OSGF_None;
+
     hStmt = NULL;
 
     iNextShapeId = 0;
@@ -131,13 +133,13 @@ CPLErr OGRSQLiteLayer::BuildFeatureDefn( const char *pszLayerName,
             if( sqlite3_column_type( hStmt, iCol ) == SQLITE_BLOB )
             {
                 osGeomColumn = oField.GetNameRef();
-                osGeomFormat = "WKB";
+                eGeomFormat = OSGF_WKB;
                 continue;
             }
             else if( sqlite3_column_type( hStmt, iCol ) == SQLITE_TEXT )
             {
                 osGeomColumn = oField.GetNameRef();
-                osGeomFormat = "WKT";
+                eGeomFormat = OSGF_WKT;
                 continue;
             }
         }
@@ -147,7 +149,7 @@ CPLErr OGRSQLiteLayer::BuildFeatureDefn( const char *pszLayerName,
             && osGeomColumn.size() == 0 )
         {
             osGeomColumn = oField.GetNameRef();
-            osGeomFormat = "SpatiaLite";
+            eGeomFormat = OSGF_SpatiaLite;
             continue;
         }
 
@@ -309,7 +311,7 @@ OGRFeature *OGRSQLiteLayer::GetNextRawFeature()
             return NULL;
         }
 
-        if( EQUAL(osGeomFormat,"WKT") )
+        if ( eGeomFormat == OSGF_WKT )
         {
             char *pszWKTCopy, *pszWKT = NULL;
             OGRGeometry *poGeometry = NULL;
@@ -320,7 +322,7 @@ OGRFeature *OGRSQLiteLayer::GetNextRawFeature()
                     &pszWKTCopy, NULL, &poGeometry ) == OGRERR_NONE )
                 poFeature->SetGeometryDirectly( poGeometry );
         }
-        else if( EQUAL(osGeomFormat,"WKB") )
+        else if ( eGeomFormat == OSGF_WKB )
         {
             const int nBytes = sqlite3_column_bytes( hStmt, iGeomCol );
             OGRGeometry *poGeometry = NULL;
@@ -330,7 +332,7 @@ OGRFeature *OGRSQLiteLayer::GetNextRawFeature()
                     NULL, &poGeometry, nBytes ) == OGRERR_NONE )
                 poFeature->SetGeometryDirectly( poGeometry );
         }
-        else if( EQUAL(osGeomFormat,"FGF") )
+        else if ( eGeomFormat == OSGF_FGF )
         {
             const int nBytes = sqlite3_column_bytes( hStmt, iGeomCol );
             OGRGeometry *poGeometry = NULL;
@@ -340,12 +342,12 @@ OGRFeature *OGRSQLiteLayer::GetNextRawFeature()
                     NULL, &poGeometry, nBytes, NULL ) == OGRERR_NONE )
                 poFeature->SetGeometryDirectly( poGeometry );
         }
-        else if( EQUAL(osGeomFormat,"SpatiaLite") )
+        else if ( eGeomFormat == OSGF_SpatiaLite )
         {
             const int nBytes = sqlite3_column_bytes( hStmt, iGeomCol );
             OGRGeometry *poGeometry = NULL;
 
-            if( ImportSpatialiteGeometry( 
+            if( ImportSpatiaLiteGeometry( 
                     (GByte*)sqlite3_column_blob( hStmt, iGeomCol ), nBytes,
                     &poGeometry ) == OGRERR_NONE )
                 poFeature->SetGeometryDirectly( poGeometry );
@@ -428,16 +430,19 @@ OGRFeature *OGRSQLiteLayer::GetFeature( long nFeatureId )
 }
 
 /************************************************************************/
-/*                      ImportSpatialiteGeometry()                      */
+/*                      ImportSpatiaLiteGeometry()                      */
 /************************************************************************/
 
-OGRErr OGRSQLiteLayer::ImportSpatialiteGeometry(
+OGRErr OGRSQLiteLayer::ImportSpatiaLiteGeometry(
     const GByte *pabyData, int nBytes, OGRGeometry **ppoGeometry )
 
 {
     *ppoGeometry = NULL;
 
-    if( nBytes < 43 || pabyData[0] != 0 || pabyData[nBytes-1] != 0xFE )
+    if( nBytes < 43
+        || pabyData[0] != 0
+        || pabyData[38] != 0x7C
+        || pabyData[nBytes-1] != 0xFE )
         return OGRERR_CORRUPT_DATA;
 
     GByte *pabyWKB = (GByte *) CPLMalloc(nBytes);
@@ -460,6 +465,64 @@ OGRErr OGRSQLiteLayer::ImportSpatialiteGeometry(
 }
 
 /************************************************************************/
+/*                      ExportSpatiaLiteGeometry()                      */
+/************************************************************************/
+
+OGRErr OGRSQLiteLayer::ExportSpatiaLiteGeometry( const OGRGeometry *poGeometry,
+                                                 GInt32 nSRID,
+                                                 OGRwkbByteOrder eByteOrder,
+                                                 GByte **ppabyData,
+                                                 int *pnDataLenght )
+
+{
+    int     nDataLen = poGeometry->WkbSize() + 39;
+    OGREnvelope sEnvelope;
+
+    *ppabyData =  (GByte *) CPLMalloc( nDataLen );
+
+    poGeometry->exportToWkb( wkbNDR, *ppabyData );
+
+    (*ppabyData)[nDataLen - 1] = 0xFE;
+    memmove( *ppabyData + 39, *ppabyData + 1, nDataLen - 40 );
+    (*ppabyData)[38] = 0x7C;
+
+    // Write out the geometry bounding rectangle
+    poGeometry->getEnvelope( &sEnvelope );
+    memcpy( *ppabyData + 6, &sEnvelope.MinX, 8 );
+    memcpy( *ppabyData + 14, &sEnvelope.MinY, 8 );
+    memcpy( *ppabyData + 22, &sEnvelope.MaxX, 8 );
+    memcpy( *ppabyData + 30, &sEnvelope.MaxY, 8 );
+
+    // Write out SRID
+    memcpy( *ppabyData + 2, &nSRID, 4 );
+
+    if( eByteOrder == wkbNDR )
+    {
+        CPL_LSBPTR64( *ppabyData + 6 );
+        CPL_LSBPTR64( *ppabyData + 14 );
+        CPL_LSBPTR64( *ppabyData + 22 );
+        CPL_LSBPTR64( *ppabyData + 30 );
+        CPL_LSBPTR32( *ppabyData + 2 );
+    }
+    else
+    {
+        CPL_MSBPTR64( *ppabyData + 6 );
+        CPL_MSBPTR64( *ppabyData + 14 );
+        CPL_MSBPTR64( *ppabyData + 22 );
+        CPL_MSBPTR64( *ppabyData + 30 );
+        CPL_MSBPTR32( *ppabyData + 2 );
+    }
+
+
+    memmove( *ppabyData + 1, *ppabyData, 1 );
+    (*ppabyData)[0] = 0x00;
+
+    *pnDataLenght = nDataLen;
+
+    return CE_None;
+}
+
+/************************************************************************/
 /*                           TestCapability()                           */
 /************************************************************************/
 
diff --git a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitetablelayer.cpp b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitetablelayer.cpp
index 40502eb..6c374ab 100644
--- a/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitetablelayer.cpp
+++ b/gdal/ogr/ogrsf_frmts/sqlite/ogrsqlitetablelayer.cpp
@@ -73,7 +73,8 @@ CPLErr OGRSQLiteTableLayer::Initialize( const char *pszTableName,
                                         const char *pszGeomCol,
                                         OGRwkbGeometryType eGeomType,
                                         const char *pszGeomFormat,
-                                        OGRSpatialReference *poSRS )
+                                        OGRSpatialReference *poSRS,
+                                        int nSRSId )
 
 {
     sqlite3 *hDB = poDS->GetDB();
@@ -84,12 +85,22 @@ CPLErr OGRSQLiteTableLayer::Initialize( const char *pszTableName,
         osGeomColumn = pszGeomCol;
 
     if( pszGeomFormat )
-        osGeomFormat = pszGeomFormat;
+    {
+        if ( EQUAL(pszGeomFormat, "WKT") )
+            eGeomFormat = OSGF_WKT;
+        else if ( EQUAL(pszGeomFormat,"WKB") )
+            eGeomFormat = OSGF_WKB;
+        else if ( EQUAL(pszGeomFormat,"FGF") )
+            eGeomFormat = OSGF_FGF;
+        else if( EQUAL(pszGeomFormat,"SpatiaLite") )
+            eGeomFormat = OSGF_SpatiaLite;
+    }
 
     CPLFree( pszFIDColumn );
     pszFIDColumn = NULL;
 
     this->poSRS = poSRS;
+    this->nSRSId = nSRSId;
 
     if( poSRS )
         poSRS->Reference();
@@ -449,7 +460,7 @@ OGRErr OGRSQLiteTableLayer::CreateField( OGRFieldDefn *poFieldIn,
         strcat( pszOldFieldList, osGeomColumn );
         strcat( pszNewFieldList, osGeomColumn );
 
-        if( EQUAL(osGeomFormat,"WKB") )
+        if ( eGeomFormat == OSGF_WKB )
             strcat( pszNewFieldList, " BLOB" );
         else
             strcat( pszNewFieldList, " VARCHAR" );
@@ -775,7 +786,9 @@ OGRErr OGRSQLiteTableLayer::CreateFeature( OGRFeature *poFeature )
     int rc;
     sqlite3_stmt *hInsertStmt;
 
-//    CPLDebug( "OGR_SQLITE", "prepare(%s)", osCommand.c_str() );
+#ifdef DEBUG
+    CPLDebug( "OGR_SQLITE", "prepare(%s)", osCommand.c_str() );
+#endif
 
     rc = sqlite3_prepare( hDB, osCommand, -1, &hInsertStmt, NULL );
     if( rc != SQLITE_OK )
@@ -798,13 +811,13 @@ OGRErr OGRSQLiteTableLayer::CreateFeature( OGRFeature *poFeature )
             /* no binding */
             rc = SQLITE_OK;
         }
-        else if( EQUAL(osGeomFormat,"WKT") )
+        else if ( eGeomFormat == OSGF_WKT )
         {
             char *pszWKT = NULL;
             poGeom->exportToWkt( &pszWKT );
             rc = sqlite3_bind_text( hInsertStmt, 1, pszWKT, -1, CPLFree );
         }
-        else if( EQUAL( osGeomFormat, "WKB" ) )
+        else if( eGeomFormat == OSGF_WKB )
         {
             int nWKBLen = poGeom->WkbSize();
             GByte *pabyWKB = (GByte *) CPLMalloc(nWKBLen + 1);
@@ -812,6 +825,15 @@ OGRErr OGRSQLiteTableLayer::CreateFeature( OGRFeature *poFeature )
             poGeom->exportToWkb( wkbNDR, pabyWKB );
             rc = sqlite3_bind_blob( hInsertStmt, 1, pabyWKB, nWKBLen, CPLFree );
         }
+        else if ( eGeomFormat == OSGF_SpatiaLite )
+        {
+            int     nBLOBLen;
+            GByte   *pabySLBLOB;
+
+            ExportSpatiaLiteGeometry( poGeom, nSRSId, wkbNDR,
+                                      &pabySLBLOB, &nBLOBLen );
+            rc = sqlite3_bind_blob( hInsertStmt, 1, pabySLBLOB, nBLOBLen, CPLFree );
+        }
 
         if( rc != SQLITE_OK )
         {
-- 
1.5.6.5


