[Gdal-dev] Postgis DeleteLayer(int iLayer) method

Craig Miller craig.miller at spatialminds.com
Mon Dec 12 18:20:07 EST 2005


The existing PostGIS driver didn't have support for deleting a layer via the
datasource (OGRDatasource::DeleteLayer(int iLayer).  It did have a method
for deleting a layer via a name, but this isn't exposed via the C++ API.  I
quickly adapted that function to the DeleteLayer(int iLayer) method, so
there would be an implementation in the driver available.  I've done very
little testing of the method, and I didn't make very good use of the
OGRErr/CPLError mechanism.  I only implemented enough error handling to get
it to work.

Here is the code (Just add it to ogrpgdatasource.cpp and delcare the method
in ogr_pg.h):

/************************************************************************/
/*                            DeleteLayer()                             */
/************************************************************************/
OGRErr OGRPGDataSource::DeleteLayer(int iLayer)
{
	if (iLayer >= nLayers)
	{
		CPLDebug( "OGR_PG", "Layer index is out of range.");
        return OGRERR_FAILURE;  // Index out of range error.
	}
	
	if (papoLayers[iLayer] == NULL)
	{
		CPLDebug( "OGR_PG", "In memory datasource model is
corrupt.");
		return OGRERR_FAILURE;  // Corrupt layer
	}

/* -------------------------------------------------------------------- */
/*      Blow away our OGR structures related to the layer.  This is     */
/*      pretty dangerous if anything has a reference to this layer!     */
/* -------------------------------------------------------------------- */
    CPLDebug( "OGR_PG", "DeleteLayer()" );
	
	char* pszLayerName =
strdup(papoLayers[iLayer]->GetLayerDefn()->GetName());


    delete papoLayers[iLayer];
    memmove( papoLayers + iLayer, papoLayers + iLayer + 1,
             sizeof(void *) * (nLayers - iLayer - 1) );
    nLayers--;

/* -------------------------------------------------------------------- */
/*      Remove from the database.                                       */
/* -------------------------------------------------------------------- */
    PGresult            *hResult;
    char                szCommand[1024];

    hResult = PQexec(hPGConn, "BEGIN");
    PQclear( hResult );

    if( bHavePostGIS )
    {
        sprintf( szCommand,
                 "SELECT DropGeometryColumn('%s','%s','wkb_geometry')",
                 pszDBName, pszLayerName );

        CPLDebug( "OGR_PG", "PGexec(%s)", szCommand );

        hResult = PQexec( hPGConn, szCommand );
        PQclear( hResult );
    }

    sprintf( szCommand, "DROP TABLE %s", pszLayerName );
    CPLDebug( "OGR_PG", "PGexec(%s)", szCommand );
    hResult = PQexec( hPGConn, szCommand );
    PQclear( hResult );

    hResult = PQexec(hPGConn, "COMMIT");
    PQclear( hResult );

    hResult = PQexec(hPGConn, "BEGIN");
    PQclear( hResult );

    sprintf( szCommand, "DROP SEQUENCE %s_ogc_fid_seq", pszLayerName );
    CPLDebug( "OGR_PG", "PGexec(%s)", szCommand );
    hResult = PQexec( hPGConn, szCommand );
    PQclear( hResult );

    hResult = PQexec(hPGConn, "COMMIT");
    PQclear( hResult );

	delete pszLayerName;
}





More information about the Gdal-dev mailing list