1

When creating a new layer using OGR / GDAL in C++, how do i set the spatial reference?

OGRLayer  *poLayer2;
poLayer2 = poDS2->CreateLayer( "layerName", NULL, wkbPoint, NULL );
if( poLayer2 == NULL ) {
    printf( "Layer creation failed PODS2.\n" );
    exit( 1 );
}

1 Answer 1

2

Using the class & method reference for OGR Spatial Reference in combination with OGR tutorial, one should set a spatial reference and supply that (instead of NULL) as argument upon layer creation:

OGRLayer  *poLayer2;
OGRSpatialReference  *spatialReference;
//spatialReference.SetWellKnownGeogCS( "WGS84" );
spatialReference.SetWellKnownGeogCS( "EPSG:4326" ); 
poLayer2 = poDS2->CreateLayer( "layerName", spatialReference, wkbPoint, NULL );
if( poLayer2 == NULL ) {
    printf( "Layer creation failed PODS2.\n" );
    exit( 1 );
}

When cloning a layer, one can copy the spatial reference directly from original layer:

OGRLayer  *poLayer;
OGRLayer  *poLayer2;
... get poLayer from shapefile (not scope of this answer)
poLayer2 = poDS2->CreateLayer( poLayer->GetName(), poLayer->GetSpatialRef(), wkbPoint, NULL );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.