[Gdal-dev] Re: OGR Questions

Frank Warmerdam warmerdam at pobox.com
Tue Apr 8 17:20:52 EDT 2003


Peter Lenson wrote:
> Hi Frank,
> 
> As usually we need this ASAP. 
> 
> To clarify what we want to do is readin the
> shapefile into memory, have the ability to add
> fields to this memory representation. Make use
> of this memory representation for drawing.
> And finally save this memory representation
> to disk.

Peter,

I have committed a bunch of changes implementing a new "Memory" type
OGR driver that supports adding fields when the layer already has features.
I also implemented an "easy copy" API for copying layers.

Included below is a simple C++ example utilizing the new capabilities.  I
have taken the liberty of cc:ing the gdal-dev list.  You might want to
introduce your project to the list at some point.

Best regards,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

#include <assert.h>
#include "ogrsf_frmts.h"

int main( int nArgc, char **papszArgv )

{
     OGRDataSource *poSrcDS, *poMemDS, *poDstDS;
     OGRSFDriver *poDriver;

/* -------------------------------------------------------------------- */
/*      Open the source file.                                           */
/* -------------------------------------------------------------------- */
     OGRRegisterAll();

     poSrcDS =
         OGRSFDriverRegistrar::GetRegistrar()->Open( "polygon.shp" );
     assert( poSrcDS != NULL );

/* -------------------------------------------------------------------- */
/*      Copy this to a datasource using the "Memory" driver ... all     */
/*      in memory.                                                      */
/* -------------------------------------------------------------------- */
     poDriver =
         OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName( "Memory" );

     assert( poDriver != NULL );

     poMemDS = poDriver->CopyDataSource( poSrcDS, "polygon.shp", NULL );

     delete poSrcDS;

/* -------------------------------------------------------------------- */
/*      Add a new field to the first layer in the datasource.           */
/* -------------------------------------------------------------------- */
     OGRFieldDefn oNewField( "StyleInfo", OFTString );
     oNewField.SetWidth( 64 );

     poMemDS->GetLayer( 0 )->CreateField( &oNewField );

     /* we could do lots of stuff with our in-memory data here */

/* -------------------------------------------------------------------- */
/*      Save back to disk in a new file.                                */
/* -------------------------------------------------------------------- */
     poDriver =
         OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName( "ESRI Shapefile" );

     assert( poDriver != NULL );

     poDstDS = poDriver->CopyDataSource( poMemDS, "polynew.shp", NULL );

     delete poMemDS;

     delete poDstDS;

     delete OGRSFDriverRegistrar::GetRegistrar();

}




More information about the Gdal-dev mailing list