int OGRIngresDataSource::FetchSRSId( OGRSpatialReference * poSRS )

{
    char            **papszRow;
    char            szCommand[10000];
    char            *pszWKT = NULL;
    char            *pszProj4 = NULL;
    const char      *pszAuthName;
    const char      *pszAuthID;
    int             nSRSId;

    if( poSRS == NULL )
        return -1;

    /* -------------------------------------------------------------------- */
    /*  If it is a EPSG	Spatial Reference, search with special type			*/
    /* -------------------------------------------------------------------- */
    pszAuthName = poSRS->GetAuthorityName(NULL);
    pszAuthID = poSRS->GetAuthorityCode(NULL);

    if (pszAuthName && pszAuthID && EQUAL(pszAuthName, "EPSG"))
    {
         sprintf( szCommand, 
             "SELECT srid FROM spatial_ref_sys WHERE auth_name = 'EPSG' and auth_srid= %s",
             pszAuthID );


        OGRIngresStatement  oStateSRID(GetConn());
        oStateSRID.ExecuteSQL(szCommand);

        papszRow = oStateSRID.GetRow();
        if (papszRow == NULL)
        {
            CPLDebug("INGRES", "No rows exists matching EPSG:%s in spatial_ref_sys", pszAuthID );
        }
        else if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]);
            return nSRSId;
        }
    }

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to WKT.                                           */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
        return -1;

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to Proj4.                                         */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToProj4( &pszProj4 ) != OGRERR_NONE )
        return -1;

    CPLAssert( strlen(pszProj4) + strlen(pszWKT) < sizeof(szCommand) - 500 );

/* -------------------------------------------------------------------- */
/*      Try to find in the existing table.                              */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
             pszWKT );

    {
        OGRIngresStatement  oStateSRID(GetConn());
        oStateSRID.ExecuteSQL(szCommand);

        papszRow = oStateSRID.GetRow();
        if (papszRow == NULL)
        {
            CPLDebug("INGRES", "No rows exist currently exist in spatial_ref_sys");
        }
        else if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]);
            return nSRSId;
        }
    }

/* -------------------------------------------------------------------- */
/*      Get the current maximum srid in the srs table.                  */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT MAX(srid) FROM spatial_ref_sys");    

    {
        OGRIngresStatement  oStateMaxSRID(GetConn());
        oStateMaxSRID.ExecuteSQL(szCommand);
        papszRow = oStateMaxSRID.GetRow();

        // The spatial reference created by user must be greater than
        // the 10000. The below are system maintained.
#define USER_DEFINED_SR_START   10000
        if( papszRow != NULL && papszRow[0] != NULL )
        {
            // if there is no row in spatial reference, a random value 
            // will be return, how to judge?
            nSRSId = *((II_INT4 *)papszRow[0]) ;
            if (nSRSId <= 0)
            {
                nSRSId = USER_DEFINED_SR_START+1; 
            }
            else
            {
                nSRSId = *((II_INT4 *)papszRow[0]) + 1;
            }
             
        }
        else
            nSRSId = USER_DEFINED_SR_START+1;

    }  
    
    if(pszAuthName == NULL || strlen(pszAuthName) == 0)
    {
        poSRS->AutoIdentifyEPSG();
        pszAuthName = poSRS->GetAuthorityName(NULL);
        if (pszAuthName != NULL && EQUAL(pszAuthName, "EPSG"))
        {
            const char* pszAuthorityCode = poSRS->GetAuthorityCode(NULL);
            if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
            {
                /* Import 'clean' SRS */
                poSRS->importFromEPSG( atoi(pszAuthorityCode) );

                pszAuthName = poSRS->GetAuthorityName(NULL);
                pszAuthID = poSRS->GetAuthorityCode(NULL);
            }
        }
    }
/* -------------------------------------------------------------------- */
/*      Try adding the SRS to the SRS table.                            */
/* -------------------------------------------------------------------- */
    if(pszAuthName != NULL)
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,'%s',%s,'%s','%s')",
                 nSRSId, pszAuthName, pszAuthID, pszWKT, pszProj4 );
    }
    else
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,NULL,NULL,'%s','%s')",
                 nSRSId, pszWKT, pszProj4 );
    }

    {
        OGRIngresStatement  oStateNewSRID(GetConn());
        if(!oStateNewSRID.ExecuteSQL(szCommand))
        {
            CPLDebug("INGRES", "Failed to create new spatial reference system");
        }
    }

    return nSRSId;
}
int OGRIngresDataSource::FetchSRSId( OGRSpatialReference * poSRS )

{
    char            **papszRow;
    char            szCommand[10000];
    char            *pszWKT = NULL;
    char            *pszProj4 = NULL;
    const char      *pszAuthName;
    const char      *pszAuthID;
    int             nSRSId;

    if( poSRS == NULL )
        return -1;

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to WKT.                                           */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToWkt( &pszWKT ) != OGRERR_NONE )
        return -1;

    /* -------------------------------------------------------------------- */
    /*      Translate SRS to Proj4.                                         */
    /* -------------------------------------------------------------------- */
    if( poSRS->exportToProj4( &pszProj4 ) != OGRERR_NONE )
        return -1;

    CPLAssert( strlen(pszProj4) + strlen(pszWKT) < sizeof(szCommand) - 500 );

/* -------------------------------------------------------------------- */
/*      Try to find in the existing table.                              */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT srid FROM spatial_ref_sys WHERE srtext = '%s'",
             pszWKT );

    {
        OGRIngresStatement  oStateSRID(GetConn());
        oStateSRID.ExecuteSQL(szCommand);

        papszRow = oStateSRID.GetRow();
        if (papszRow == NULL)
        {
            CPLDebug("INGRES", "No rows exist currently exist in spatial_ref_sys");
        }
        else if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]);
            return nSRSId;
        }
    }

/* -------------------------------------------------------------------- */
/*      Get the current maximum srid in the srs table.                  */
/* -------------------------------------------------------------------- */
    sprintf( szCommand, 
             "SELECT MAX(srid) FROM spatial_ref_sys");    

    {
        OGRIngresStatement  oStateMaxSRID(GetConn());
        oStateMaxSRID.ExecuteSQL(szCommand);
        papszRow = oStateMaxSRID.GetRow();

        if( papszRow != NULL && papszRow[0] != NULL )
        {
            nSRSId = *((II_INT4 *)papszRow[0]) + 1;
        }
        else
            nSRSId = 1;

    }
    
    pszAuthName = poSRS->GetAuthorityName(NULL);
    pszAuthID = poSRS->GetAuthorityCode(NULL);

    if(pszAuthName == NULL || strlen(pszAuthName) == 0)
    {
        poSRS->AutoIdentifyEPSG();
        pszAuthName = poSRS->GetAuthorityName(NULL);
        if (pszAuthName != NULL && EQUAL(pszAuthName, "EPSG"))
        {
            const char* pszAuthorityCode = poSRS->GetAuthorityCode(NULL);
            if ( pszAuthorityCode != NULL && strlen(pszAuthorityCode) > 0 )
            {
                /* Import 'clean' SRS */
                poSRS->importFromEPSG( atoi(pszAuthorityCode) );

                pszAuthName = poSRS->GetAuthorityName(NULL);
                pszAuthID = poSRS->GetAuthorityCode(NULL);
            }
        }
    }

/* -------------------------------------------------------------------- */
/*      Try adding the SRS to the SRS table.                            */
/* -------------------------------------------------------------------- */
    if(pszAuthName != NULL)
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,'%s',%s,'%s','%s')",
                 nSRSId, pszAuthName, pszAuthID, pszWKT, pszProj4 );
    }
    else
    {
        sprintf( szCommand,
                 "INSERT INTO spatial_ref_sys (srid,auth_name,auth_srid,"
                 "srtext,proj4text) VALUES (%d,NULL,NULL,'%s','%s')",
                 nSRSId, pszWKT, pszProj4 );
    }

    {
        OGRIngresStatement  oStateNewSRID(GetConn());
        if(!oStateNewSRID.ExecuteSQL(szCommand))
        {
            CPLDebug("INGRES", "Failed to create new spatial reference system");
        }
    }

    return nSRSId;
}