[Gdal-dev] File extensions

Frank Warmerdam warmerdam at pobox.com
Thu Dec 7 13:12:52 EST 2006


Matt Hanson wrote:
> Hi,
> 
> I'm using GDAL in an app where I want the user to specify an output format
> and have it be transparent to me in my app (after all, that's the point of
> GDAL).    The problem is that some drivers (including a custom one we have
> written) have specific file extension requirements.    When I call Create()
> I give it a filename.  Now, the driver should be smart enough to give it the
> correct extension if it doesn't have one.....but the problem is that I then
> have no way of knowing if the filename has been altered or not.
> 
> So, the problem is that I either: - need to know what the driver is so I can
> give it the appropriate extension...but then that puts requirements on me to
> know the file format type which really is the responsibility of GDAL - need
> to retrieve from GDAL the new name, in case it's been altered by the driver,
> after I create a dataset.  But I don't see any method to do this....did I
> miss something?

Matt,

Ray has convered some of the general dangers of dependence on extensions.

But I think you can still address this at the application level if you want
to.  In order you create files you already need to know what format driver
you want to use so that you can pass that into CreateCopy or Create.  Given
the driver, you can also fetch it's preferred extension from the driver
metadata.

Then you could, for instance, add the extension to the user provided name
if the user provided name has no extension.  Or possibly even override
if they gave a different once, though for most formats the extension
is just a suggestion, not a requirement.

Note that many formats don't even have a preferred extension.  But if they
do the following call should fetch it (NULL if none):

   const char *pszExtension =
          GDALGetMetadataItem( hDriver, GDAL_DMD_EXTENSION, NULL );

You may find functions like CPLGetExtension(), and CPLResetExtension()
in gdal/port/cpl_conv.h helpful for parsing and modifying filenames in
a portable fashion.

I'm not keen on having drivers actually change extensions internally, though
if it is sufficiently critical a driver could chose to do so.

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    | President OSGeo, http://osgeo.org




More information about the Gdal-dev mailing list