This document is intended to document using the OGR C++ classes to read and write data from a file. It is strongly advised that the read first review the OGR Architecture document describing the key classes and their roles in OGR.

It also includes code snippets for the corresponding functions in C and Python.

Reading From OGR

For purposes of demonstrating reading with OGR, we will construct a small utility for dumping point layers from an OGR data source to stdout in comma-delimited format.

Initially it is necessary to register all the format drivers that are desired. This is normally accomplished by calling OGRRegisterAll() which registers all format drivers built into GDAL/OGR.

In C++ :

#include "ogrsf_frmts.h"

int main()

{
OGRRegisterAll();

In C :

#include "ogr_api.h"

int main()

{
OGRRegisterAll();

Next we need to open the input OGR datasource. Datasources can be files, RDBMSes, directories full of files, or even remote web services depending on the driver being used. However, the datasource name is always a single string. In this case we are hardcoded to open a particular shapefile. The second argument (FALSE) tells the OGRSFDriverRegistrar::Open() method that we don't require update access. On failure NULL is returned, and we report an error.

In C++ :

    OGRDataSource       *poDS;

    poDS = OGRSFDriverRegistrar::Open( "point.shp", FALSE );
if( poDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
}

In C :

    OGRDataSourceH hDS;

    hDS = OGROpen( "point.shp", FALSE, NULL );
if( hDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
}

An OGRDataSource can potentially have many layers associated with it. The number of layers available can be queried with OGRDataSource::GetLayerCount() and individual layers fetched by index usingOGRDataSource::GetLayer(). However, we will just fetch the layer by name.

In C++ :

    OGRLayer  *poLayer;

    poLayer = poDS->GetLayerByName( "point" );

In C :

    OGRLayerH hLayer;

    hLayer = OGR_DS_GetLayerByName( hDS, "point" );

Now we want to start reading features from the layer. Before we start we could assign an attribute or spatial filter to the layer to restrict the set of feature we get back, but for now we are interested in getting all features.

While it isn't strictly necessary in this circumstance since we are starting fresh with the layer, it is often wise to call OGRLayer::ResetReading() to ensure we are starting at the beginning of the layer. We iterate through all the features in the layer using OGRLayer::GetNextFeature(). It will return NULL when we run out of features.

In C++ :

    OGRFeature *poFeature;

    poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{

In C :

    OGRFeatureH hFeature;

    OGR_L_ResetReading(hLayer);
while( (hFeature = OGR_L_GetNextFeature(hLayer)) != NULL )
{

In order to dump all the attribute fields of the feature, it is helpful to get the OGRFeatureDefn. This is an object, associated with the layer, containing the definitions of all the fields. We loop over all the fields, and fetch and report the attributes based on their type.

In C++ :

        OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
}

In C :

        OGRFeatureDefnH hFDefn = OGR_L_GetLayerDefn(hLayer);
int iField; for( iField = 0; iField < OGR_FD_GetFieldCount(hFDefn); iField++ )
{
OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn( hFDefn, iField ); if( OGR_Fld_GetType(hFieldDefn) == OFTInteger )
printf( "%d,", OGR_F_GetFieldAsInteger( hFeature, iField ) );
else if( OGR_Fld_GetType(hFieldDefn) == OFTReal )
printf( "%.3f,", OGR_F_GetFieldAsDouble( hFeature, iField) );
else if( OGR_Fld_GetType(hFieldDefn) == OFTString )
printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) );
else
printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) );
}

There are a few more field types than those explicitly handled above, but a reasonable representation of them can be fetched with the OGRFeature::GetFieldAsString() method. In fact we could shorten the above by using OGRFeature::GetFieldAsString() for all the types.

Next we want to extract the geometry from the feature, and write out the point geometry x and y. Geometries are returned as a generic OGRGeometry pointer. We then determine the specific geometry type, and if it is a point, we cast it to point and operate on it. If it is something else we write placeholders.

In C++ :

        OGRGeometry *poGeometry;

        poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry; printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry\n" );
}

In C :

        OGRGeometryH hGeometry;

        hGeometry = OGR_F_GetGeometryRef(hFeature);
if( hGeometry != NULL
&& wkbFlatten(OGR_G_GetGeometryType(hGeometry)) == wkbPoint )
{
printf( "%.3f,%3.f\n", OGR_G_GetX(hGeometry, 0), OGR_G_GetY(hGeometry, 0) );
}
else
{
printf( "no point geometry\n" );
}

The wkbFlatten() macro is used above to convert the type for a wkbPoint25D (a point with a z coordinate) into the base 2D geometry type code (wkbPoint). For each 2D geometry type there is a corresponding 2.5D type code. The 2D and 2.5D geometry cases are handled by the same C++ class, so our code will handle 2D or 3D cases properly.

Starting with OGR 2.0, several geometry fields can be associated to a feature.

In C++ :

    OGRGeometry *poGeometry;
int iGeomField;
int nGeomFieldCount; nGeomFieldCount = poFeature->GetGeomFieldCount();
for(iGeomField = 0; iGeomField < nGeomFieldCount; iGeomField ++ )
{
poGeometry = poFeature->GetGeomFieldRef(iGeomField);
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry; printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry\n" );
}
}

In C :

    OGRGeometryH hGeometry;
int iGeomField;
int nGeomFieldCount; nGeomFieldCount = OGR_F_GetGeomFieldCount(hFeature);
for(iGeomField = 0; iGeomField < nGeomFieldCount; iGeomField ++ )
{
hGeometry = OGR_F_GetGeomFieldRef(hFeature, iGeomField);
if( hGeometry != NULL
&& wkbFlatten(OGR_G_GetGeometryType(hGeometry)) == wkbPoint )
{
printf( "%.3f,%3.f\n", OGR_G_GetX(hGeometry, 0),
OGR_G_GetY(hGeometry, 0) );
}
else
{
printf( "no point geometry\n" );
}
}

In Python:

    nGeomFieldCount = feat.GetGeomFieldCount()
for iGeomField in range(nGeomFieldCount):
geom = feat.GetGeomFieldRef(iGeomField)
if geom is not None and geom.GetGeometryType() == ogr.wkbPoint:
print "%.3f, %.3f" % ( geom.GetX(), geom.GetY() )
else:
print "no point geometry\n"

Note that OGRFeature::GetGeometryRef() and OGRFeature::GetGeomFieldRef() return a pointer to the internal geometry owned by the OGRFeature. There we don't actually deleted the return geometry. However, theOGRLayer::GetNextFeature() method returns a copy of the feature that is now owned by us. So at the end of use we must free the feature. We could just "delete" it, but this can cause problems in windows builds where the GDAL DLL has a different "heap" from the main program. To be on the safe side we use a GDAL function to delete the feature.

In C++ :

        OGRFeature::DestroyFeature( poFeature );
}

In C :

        OGR_F_Destroy( hFeature );
}

The OGRLayer returned by OGRDataSource::GetLayerByName() is also a reference to an internal layer owned by the OGRDataSource so we don't need to delete it. But we do need to delete the datasource in order to close the input file. Once again we do this with a custom delete method to avoid special win32 heap issus.

In C++ :

In C :

    OGR_DS_Destroy( hDS );
}

All together our program looks like this.

In C++ :

#include "ogrsf_frmts.h"

int main()

{
OGRRegisterAll(); OGRDataSource *poDS; poDS = OGRSFDriverRegistrar::Open( "point.shp", FALSE );
if( poDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
} OGRLayer *poLayer; poLayer = poDS->GetLayerByName( "point" ); OGRFeature *poFeature; poLayer->ResetReading();
while( (poFeature = poLayer->GetNextFeature()) != NULL )
{
OGRFeatureDefn *poFDefn = poLayer->GetLayerDefn();
int iField; for( iField = 0; iField < poFDefn->GetFieldCount(); iField++ )
{
OGRFieldDefn *poFieldDefn = poFDefn->GetFieldDefn( iField ); if( poFieldDefn->GetType() == OFTInteger )
printf( "%d,", poFeature->GetFieldAsInteger( iField ) );
else if( poFieldDefn->GetType() == OFTReal )
printf( "%.3f,", poFeature->GetFieldAsDouble(iField) );
else if( poFieldDefn->GetType() == OFTString )
printf( "%s,", poFeature->GetFieldAsString(iField) );
else
printf( "%s,", poFeature->GetFieldAsString(iField) );
} OGRGeometry *poGeometry; poGeometry = poFeature->GetGeometryRef();
if( poGeometry != NULL
&& wkbFlatten(poGeometry->getGeometryType()) == wkbPoint )
{
OGRPoint *poPoint = (OGRPoint *) poGeometry; printf( "%.3f,%3.f\n", poPoint->getX(), poPoint->getY() );
}
else
{
printf( "no point geometry\n" );
}
OGRFeature::DestroyFeature( poFeature );
} OGRDataSource::DestroyDataSource( poDS );
}

In C :

#include "ogr_api.h"

int main()

{
OGRRegisterAll(); OGRDataSourceH hDS;
OGRLayerH hLayer;
OGRFeatureH hFeature; hDS = OGROpen( "point.shp", FALSE, NULL );
if( hDS == NULL )
{
printf( "Open failed.\n" );
exit( 1 );
} hLayer = OGR_DS_GetLayerByName( hDS, "point" ); OGR_L_ResetReading(hLayer);
while( (hFeature = OGR_L_GetNextFeature(hLayer)) != NULL )
{
OGRFeatureDefnH hFDefn;
int iField;
OGRGeometryH hGeometry; hFDefn = OGR_L_GetLayerDefn(hLayer); for( iField = 0; iField < OGR_FD_GetFieldCount(hFDefn); iField++ )
{
OGRFieldDefnH hFieldDefn = OGR_FD_GetFieldDefn( hFDefn, iField ); if( OGR_Fld_GetType(hFieldDefn) == OFTInteger )
printf( "%d,", OGR_F_GetFieldAsInteger( hFeature, iField ) );
else if( OGR_Fld_GetType(hFieldDefn) == OFTReal )
printf( "%.3f,", OGR_F_GetFieldAsDouble( hFeature, iField) );
else if( OGR_Fld_GetType(hFieldDefn) == OFTString )
printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) );
else
printf( "%s,", OGR_F_GetFieldAsString( hFeature, iField) );
} hGeometry = OGR_F_GetGeometryRef(hFeature);
if( hGeometry != NULL
&& wkbFlatten(OGR_G_GetGeometryType(hGeometry)) == wkbPoint )
{
printf( "%.3f,%3.f\n", OGR_G_GetX(hGeometry, 0), OGR_G_GetY(hGeometry, 0) );
}
else
{
printf( "no point geometry\n" );
} OGR_F_Destroy( hFeature );
} OGR_DS_Destroy( hDS );
}

In Python:

import sys
import ogr ds = ogr.Open( "point.shp" )
if ds is None:
print "Open failed.\n"
sys.exit( 1 ) lyr = ds.GetLayerByName( "point" ) lyr.ResetReading() for feat in lyr: feat_defn = lyr.GetLayerDefn()
for i in range(feat_defn.GetFieldCount()):
field_defn = feat_defn.GetFieldDefn(i) # Tests below can be simplified with just :
# print feat.GetField(i)
if field_defn.GetType() == ogr.OFTInteger:
print "%d" % feat.GetFieldAsInteger(i)
elif field_defn.GetType() == ogr.OFTReal:
print "%.3f" % feat.GetFieldAsDouble(i)
elif field_defn.GetType() == ogr.OFTString:
print "%s" % feat.GetFieldAsString(i)
else:
print "%s" % feat.GetFieldAsString(i) geom = feat.GetGeometryRef()
if geom is not None and geom.GetGeometryType() == ogr.wkbPoint:
print "%.3f, %.3f" % ( geom.GetX(), geom.GetY() )
else:
print "no point geometry\n" ds = None

Writing To OGR

As an example of writing through OGR, we will do roughly the opposite of the above. A short program that reads comma separated values from input text will be written to a point shapefile via OGR.

As usual, we start by registering all the drivers, and then fetch the Shapefile driver as we will need it to create our output file.

In C++ :

#include "ogrsf_frmts.h"

int main()
{
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver; OGRRegisterAll(); poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
pszDriverName );
if( poDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( 1 );
}

In C :

#include "ogr_api.h"

int main()
{
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriverH hDriver; OGRRegisterAll(); hDriver = OGRGetDriverByName( pszDriverName );
if( hDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( 1 );
}

Next we create the datasource. The ESRI Shapefile driver allows us to create a directory full of shapefiles, or a single shapefile as a datasource. In this case we will explicitly create a single file by including the extension in the name. Other drivers behave differently. The second argument to the call is a list of option values, but we will just be using defaults in this case. Details of the options supported are also format specific.

In C ++ :

    OGRDataSource *poDS;

    poDS = poDriver->CreateDataSource( "point_out.shp", NULL );
if( poDS == NULL )
{
printf( "Creation of output file failed.\n" );
exit( 1 );
}

In C :

    OGRDataSourceH hDS;

    hDS = OGR_Dr_CreateDataSource( hDriver, "point_out.shp", NULL );
if( hDS == NULL )
{
printf( "Creation of output file failed.\n" );
exit( 1 );
}

Now we create the output layer. In this case since the datasource is a single file, we can only have one layer. We pass wkbPoint to specify the type of geometry supported by this layer. In this case we aren't passing any coordinate system information or other special layer creation options.

In C++ :

    OGRLayer *poLayer;

    poLayer = poDS->CreateLayer( "point_out", NULL, wkbPoint, NULL );
if( poLayer == NULL )
{
printf( "Layer creation failed.\n" );
exit( 1 );
}

In C :

    OGRLayerH hLayer;

    hLayer = OGR_DS_CreateLayer( hDS, "point_out", NULL, wkbPoint, NULL );
if( hLayer == NULL )
{
printf( "Layer creation failed.\n" );
exit( 1 );
}

Now that the layer exists, we need to create any attribute fields that should appear on the layer. Fields must be added to the layer before any features are written. To create a field we initialize an OGRField object with the information about the field. In the case of Shapefiles, the field width and precision is significant in the creation of the output .dbf file, so we set it specifically, though generally the defaults are OK. For this example we will just have one attribute, a name string associated with the x,y point.

Note that the template OGRField we pass to CreateField() is copied internally. We retain ownership of the object.

In C++:

    OGRFieldDefn oField( "Name", OFTString );

    oField.SetWidth(32);

    if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
printf( "Creating Name field failed.\n" );
exit( 1 );
}

In C:

    OGRFieldDefnH hFieldDefn;

    hFieldDefn = OGR_Fld_Create( "Name", OFTString );

    OGR_Fld_SetWidth( hFieldDefn, 32);

    if( OGR_L_CreateField( hLayer, hFieldDefn, TRUE ) != OGRERR_NONE )
{
printf( "Creating Name field failed.\n" );
exit( 1 );
} OGR_Fld_Destroy(hFieldDefn);

The following snipping loops reading lines of the form "x,y,name" from stdin, and parsing them.

In C++ and in C :

    double x, y;
char szName[33]; while( !feof(stdin)
&& fscanf( stdin, "%lf,%lf,%32s", &x, &y, szName ) == 3 )
{

To write a feature to disk, we must create a local OGRFeature, set attributes and attach geometry before trying to write it to the layer. It is imperative that this feature be instantiated from the OGRFeatureDefnassociated with the layer it will be written to.

In C++ :

        OGRFeature *poFeature;

        poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
poFeature->SetField( "Name", szName );

In C :

        OGRFeatureH hFeature;

        hFeature = OGR_F_Create( OGR_L_GetLayerDefn( hLayer ) );
OGR_F_SetFieldString( hFeature, OGR_F_GetFieldIndex(hFeature, "Name"), szName );

We create a local geometry object, and assign its copy (indirectly) to the feature. The OGRFeature::SetGeometryDirectly() differs from OGRFeature::SetGeometry() in that the direct method gives ownership of the geometry to the feature. This is generally more efficient as it avoids an extra deep object copy of the geometry.

In C++ :

        OGRPoint pt;
pt.setX( x );
pt.setY( y ); poFeature->SetGeometry( &pt );

In C :

        OGRGeometryH hPt;
hPt = OGR_G_CreateGeometry(wkbPoint);
OGR_G_SetPoint_2D(hPt, 0, x, y); OGR_F_SetGeometry( hFeature, hPt );
OGR_G_DestroyGeometry(hPt);

Now we create a feature in the file. The OGRLayer::CreateFeature() does not take ownership of our feature so we clean it up when done with it.

In C++ :

        if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
} OGRFeature::DestroyFeature( poFeature );
}

In C :

        if( OGR_L_CreateFeature( hLayer, hFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
} OGR_F_Destroy( hFeature );
}

Finally we need to close down the datasource in order to ensure headers are written out in an orderly way and all resources are recovered.

In C++ :

In C :

    OGR_DS_Destroy( hDS );
}

The same program all in one block looks like this:

In C++ :

#include "ogrsf_frmts.h"

int main()
{
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriver *poDriver; OGRRegisterAll(); poDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(
pszDriverName );
if( poDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( 1 );
} OGRDataSource *poDS; poDS = poDriver->CreateDataSource( "point_out.shp", NULL );
if( poDS == NULL )
{
printf( "Creation of output file failed.\n" );
exit( 1 );
} OGRLayer *poLayer; poLayer = poDS->CreateLayer( "point_out", NULL, wkbPoint, NULL );
if( poLayer == NULL )
{
printf( "Layer creation failed.\n" );
exit( 1 );
} OGRFieldDefn oField( "Name", OFTString ); oField.SetWidth(32); if( poLayer->CreateField( &oField ) != OGRERR_NONE )
{
printf( "Creating Name field failed.\n" );
exit( 1 );
} double x, y;
char szName[33]; while( !feof(stdin)
&& fscanf( stdin, "%lf,%lf,%32s", &x, &y, szName ) == 3 )
{
OGRFeature *poFeature; poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
poFeature->SetField( "Name", szName ); OGRPoint pt; pt.setX( x );
pt.setY( y ); poFeature->SetGeometry( &pt ); if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
} OGRFeature::DestroyFeature( poFeature );
} OGRDataSource::DestroyDataSource( poDS );
}

In C :

#include "ogr_api.h"

int main()
{
const char *pszDriverName = "ESRI Shapefile";
OGRSFDriverH hDriver;
OGRDataSourceH hDS;
OGRLayerH hLayer;
OGRFieldDefnH hFieldDefn;
double x, y;
char szName[33]; OGRRegisterAll(); hDriver = OGRGetDriverByName( pszDriverName );
if( hDriver == NULL )
{
printf( "%s driver not available.\n", pszDriverName );
exit( 1 );
} hDS = OGR_Dr_CreateDataSource( hDriver, "point_out.shp", NULL );
if( hDS == NULL )
{
printf( "Creation of output file failed.\n" );
exit( 1 );
} hLayer = OGR_DS_CreateLayer( hDS, "point_out", NULL, wkbPoint, NULL );
if( hLayer == NULL )
{
printf( "Layer creation failed.\n" );
exit( 1 );
} hFieldDefn = OGR_Fld_Create( "Name", OFTString ); OGR_Fld_SetWidth( hFieldDefn, 32); if( OGR_L_CreateField( hLayer, hFieldDefn, TRUE ) != OGRERR_NONE )
{
printf( "Creating Name field failed.\n" );
exit( 1 );
} OGR_Fld_Destroy(hFieldDefn); while( !feof(stdin)
&& fscanf( stdin, "%lf,%lf,%32s", &x, &y, szName ) == 3 )
{
OGRFeatureH hFeature;
OGRGeometryH hPt; hFeature = OGR_F_Create( OGR_L_GetLayerDefn( hLayer ) );
OGR_F_SetFieldString( hFeature, OGR_F_GetFieldIndex(hFeature, "Name"), szName ); hPt = OGR_G_CreateGeometry(wkbPoint);
OGR_G_SetPoint_2D(hPt, 0, x, y); OGR_F_SetGeometry( hFeature, hPt );
OGR_G_DestroyGeometry(hPt); if( OGR_L_CreateFeature( hLayer, hFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
} OGR_F_Destroy( hFeature );
} OGR_DS_Destroy( hDS );
}

In Python :

import sys
import ogr
import string driverName = "ESRI Shapefile"
drv = ogr.GetDriverByName( driverName )
if drv is None:
print "%s driver not available.\n" % driverName
sys.exit( 1 ) ds = drv.CreateDataSource( "point_out.shp" )
if ds is None:
print "Creation of output file failed.\n"
sys.exit( 1 ) lyr = ds.CreateLayer( "point_out", None, ogr.wkbPoint )
if lyr is None:
print "Layer creation failed.\n"
sys.exit( 1 ) field_defn = ogr.FieldDefn( "Name", ogr.OFTString )
field_defn.SetWidth( 32 ) if lyr.CreateField ( field_defn ) != 0:
print "Creating Name field failed.\n"
sys.exit( 1 ) # Expected format of user input: x y name
linestring = raw_input()
linelist = string.split(linestring) while len(linelist) == 3:
x = float(linelist[0])
y = float(linelist[1])
name = linelist[2] feat = ogr.Feature( lyr.GetLayerDefn())
feat.SetField( "Name", name ) pt = ogr.Geometry(ogr.wkbPoint)
pt.SetPoint_2D(0, x, y) feat.SetGeometry(pt) if lyr.CreateFeature(feat) != 0:
print "Failed to create feature in shapefile.\n"
sys.exit( 1 ) feat.Destroy() linestring = raw_input()
linelist = string.split(linestring) ds = None

Starting with OGR 2.0, several geometry fields can be associated to a feature. This capability is just available for a few file formats, such as PostGIS.

To create such datasources, geometry fields must be first created. Spatial reference system objects can be associated to each geometry field.

In C++ :

    OGRGeomFieldDefn oPointField( "PointField", wkbPoint );
OGRSpatialReference* poSRS = new OGRSpatialReference();
poSRS->importFromEPSG(4326);
oPointField.SetSpatialRef(poSRS);
poSRS->Release(); if( poLayer->CreateGeomField( &oPointField ) != OGRERR_NONE )
{
printf( "Creating field PointField failed.\n" );
exit( 1 );
} OGRGeomFieldDefn oFieldPoint2( "PointField2", wkbPoint );
poSRS = new OGRSpatialReference();
poSRS->importFromEPSG(32631);
oPointField2.SetSpatialRef(poSRS);
poSRS->Release(); if( poLayer->CreateGeomField( &oPointField2 ) != OGRERR_NONE )
{
printf( "Creating field PointField2 failed.\n" );
exit( 1 );
}

In C :

    OGRGeomFieldDefnH hPointField;
OGRGeomFieldDefnH hPointField2;
OGRSpatialReferenceH hSRS; hPointField = OGR_GFld_Create( "PointField", wkbPoint );
hSRS = OSRNewSpatialReference( NULL );
OSRImportFromEPSG(hSRS, 4326);
OGR_GFld_SetSpatialRef(hPointField, hSRS);
OSRRelease(hSRS); if( OGR_L_CreateGeomField( hLayer, hPointField ) != OGRERR_NONE )
{
printf( "Creating field PointField failed.\n" );
exit( 1 );
} OGR_GFld_Destroy( hPointField ); hPointField2 = OGR_GFld_Create( "PointField2", wkbPoint );
OSRImportFromEPSG(hSRS, 32631);
OGR_GFld_SetSpatialRef(hPointField2, hSRS);
OSRRelease(hSRS); if( OGR_L_CreateGeomField( hLayer, hPointField2 ) != OGRERR_NONE )
{
printf( "Creating field PointField2 failed.\n" );
exit( 1 );
} OGR_GFld_Destroy( hPointField2 );

To write a feature to disk, we must create a local OGRFeature, set attributes and attach geometries before trying to write it to the layer. It is imperative that this feature be instantiated from the OGRFeatureDefnassociated with the layer it will be written to.

In C++ :

        OGRFeature *poFeature;
OGRGeometry *poGeometry;
char* pszWKT; poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); pszWKT = (char*) "POINT (2 49)";
OGRGeometryFactory::createFromWkt( &pszWKT, NULL, &poGeometry );
poFeature->SetGeomFieldDirectly( "PointField", poGeometry ); pszWKT = (char*) "POINT (500000 4500000)";
OGRGeometryFactory::createFromWkt( &pszWKT, NULL, &poGeometry );
poFeature->SetGeomFieldDirectly( "PointField2", poGeometry ); if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature.\n" );
exit( 1 );
} OGRFeature::DestroyFeature( poFeature );

In C :

        OGRFeatureH hFeature;
OGRGeometryH hGeometry;
char* pszWKT; poFeature = OGR_F_Create( OGR_L_GetLayerDefn(hLayer) ); pszWKT = (char*) "POINT (2 49)";
OGR_G_CreateFromWkt( &pszWKT, NULL, &hGeometry );
OGR_F_SetGeomFieldDirectly( hFeature,
OGR_F_GetGeomFieldIndex(hFeature, "PointField"), hGeometry ); pszWKT = (char*) "POINT (500000 4500000)";
OGR_G_CreateFromWkt( &pszWKT, NULL, &hGeometry );
OGR_F_SetGeomFieldDirectly( hFeature,
OGR_F_GetGeomFieldIndex(hFeature, "PointField2"), hGeometry ); if( OGR_L_CreateFeature( hFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature.\n" );
exit( 1 );
} OGR_F_Destroy( hFeature );

In Python :

        feat = ogr.Feature( lyr.GetLayerDefn() )

        feat.SetGeomFieldDirectly( "PointField",
ogr.CreateGeometryFromWkt( "POINT (2 49)" ) )
feat.SetGeomFieldDirectly( "PointField2",
ogr.CreateGeometryFromWkt( "POINT (500000 4500000)" ) ) if lyr.CreateFeature( feat ) != 0 )
{
print( "Failed to create feature.\n" );
sys.exit( 1 );
}

OGR API Tutorial的更多相关文章

  1. Laravel API Tutorial: How to Build and Test a RESTful API

    With the rise of mobile development and JavaScript frameworks, using a RESTful API is the best optio ...

  2. Instant Buy Android API Tutorial

    转自:https://developers.google.com/wallet/instant-buy/android/tutorial This tutorial guides you throug ...

  3. Storm API,Tutorial,Trident,Windowing BOOKS

    http://storm.apache.org/releases/1.0.0/Trident-API-Overview.html https://stackoverflow.com/questions ...

  4. 一劳永逸搭建android开发环境(android官网reference sample api tutorial全下载)

    [摘要]本文简单介绍了android开发环境的搭建,重点介绍了SDK manager和AVD升级问题:并提供了android reference,sample,api,及docs的下载信息. [1]为 ...

  5. Activiti Rest API tutorial

    http://192.168.66.182:8080/activiti-rest/service/repository/deployments/ {"data":[{"i ...

  6. OGR 官方文档

    OGR 官方文档 http://www.gdal.org/ogr/index.html The OGR Simple Features Library is a C++ open source lib ...

  7. 常用open api

    SNS类网站API Facebook - http://developers.facebook.com/ 人人网开放平台 - http://dev.renren.com/ 51.com开放平台 - h ...

  8. 互联网常见Open API文档资源

    原文地址:http://blog.sina.com.cn/s/blog_4d8713560100y272.html 所谓的开放API(OpenAPI)是服务型网站常见的一种应用,网站的服务商将自己的网 ...

  9. Libgdx New 3D API 教程之 -- Libgdx中使用Materials

    This blog is a chinese version of xoppa's Libgdx new 3D api tutorial. For English version, please re ...

随机推荐

  1. JavaSE学习总结第01天_Java概述

      01.01 计算机概述 计算机(Computer):全称电子计算机,俗称电脑. 是一种能够按照程序运行,自动.高速处理海量数据的现代化智能电子设备. 由硬件和软件所组成,没有安装任何软件的计算机称 ...

  2. BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会( dp + dfs )

    选取任意一个点为root , size[ x ] 表示以 x 为根的子树的奶牛数 , dp一次计算出size[ ] && 选 root 为集会地点的不方便程度 . 考虑集会地点由 x ...

  3. Linux学习之查找命令find

    1.find  -name 根据名称查找文件 *通配所有字符: ?统配单个字符 2.find -iname 根据名称不区分大小写 3.find -size  根据文件大小 find / -size + ...

  4. C++基础-位运算

    昨天笔试遇到一道题,让实现乘法的计算方法,设计方案并优化,后来总结位运算相关知识如下: 在计算机中,数据是以1010的二进制形式存储的,1bytes = 8 bits,bit就是位,所以位运算就是对每 ...

  5. 这些屌炸天的创业者为何对投资人说NO

    曾有人说,世上的创业者只分为两种,一种是找到投资的,一种是没有找到的. 但其实还有第三种,就是那些拒绝了投资人的创业者. 他们摒弃了投资人抛来的橄榄枝,并非不差钱,不接受投资的原因大体出于两个方面,一 ...

  6. poj 1056 IMMEDIATE DECODABILITY(KMP)

    题目链接:http://poj.org/problem?id=1056 思路分析:检测某字符串是否为另一字符串的前缀,数据很弱,可以使用暴力解法.这里为了练习KMP算法使用了KMP算法. 代码如下: ...

  7. mysql记录慢查询

    1,配置开启 Linux: 在mysql配置文件my.cnf中增加 log-slow-queries=/var/lib/mysql/slowquery.log (指定日志文件存放位置,可以为空,系统会 ...

  8. 一次搞定cocos2d-x的 Android.mk 文件

    cocos2d-x 打 android 包时要修改 Android.mk 文件,但每次修改很麻烦,如果源文件很多的话就坑死了,也可以写一个脚本来自动生成这个文件,但每次还要重新生成一下,将其修改下面的 ...

  9. 【JQuery】Could not find action or result No result defined for action

    ajax调用时,返回值错误了.一般返回null即可.

  10. POJ 2479 不相交最大子段和

    题目意思还是很好理解的,在一个数列中,找出不相交的两个子串使得其和最大. 解题思路: 对于每个i来说,求出[0 ~ i - 1] 的最大子段和以及[i ~ n - 1]的最大子段和,在加起来,求最大的 ...