Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Working in C++, using GDAL and OGR: Depending on what the user has supplied for image input, I need to output point (or other) shape files in the same projection as the input raster images. The output shape files 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 shape files, so the projection process must be automated. How do I best transfer the input GDAL projection information to create a corresponding output shape file of the same projection? Do I have to go to OGR and use well-known text protocols to output the shape file? Can I instead use the GDAL projection information to write out the shape file using GDAL somehow?

share|improve this question
add comment

2 Answers 2

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.

share|improve this answer
add comment

This is more of a comment, but it is hard to format code in the comments...

With the OGR Python bindings, I write out a .prj file after outputting the shapefile. Note the MorphToESRI() function to translate the SRS WKT to ESRI's flavor. In the code below, latlong is a OSR SpatialReference object.

# write a .prj file for our shapefile
latlong.MorphToESRI() 
prjFile = open(output[:-4] + '.prj', 'w') 
prjFile.write(latlong.ExportToWkt()) 
prjFile.close()

I am guessing that there are similar methods in the C# bindings.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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