[GIS] Capture GDAL projection and create matching output (OGR or GDAL) shapefiles

coordinate systemgdalimageogrshapefile

Working in C++, using GDAL and OGR: Depending on what the user has supplied for image input, I need to output point (or other) shapefiles in the same projection as the input raster images.

The output shapefiles must be readable by ESRI as defined to be in the same projection as the input image. We are working with many input images and output shapefiles, so the projection process must be automated.

How do I best transfer the input GDAL projection information to create a corresponding output shapefile of the same projection?

Do I have to go to OGR and use well-known text protocols to output the shapefile?

Can I instead use the GDAL projection information to write out the shapefile using GDAL somehow?

Best Answer

Get the projection reference from the input GDALDataset:

GDALDatasetH hDS = GDALOpen("input.tif", GA_ReadOnly);
const char *pszWkt = GDALGetProjectionRef(hDS);
//make sure its not NULL
OGRSpatialReferenceH hSRS;
hSRS = OSRNewSpatialReference(pszWkt);
//create your ogr datasource, hOGRDS
//Create a layer and pass in the spatial reference.
OGRLayerH hLayer = OGR_DS_CreateLayer(hOGRDS, "layer", hSRS, wkbPoint, NULL );
//...

That is using the C API, and I don't know if it's perfect, from memory.

Related Question