[gdal-dev] Error in OSGeo.GDAL.Gdal.Open for .tif format

Frank Warmerdam warmerdam at pobox.com
Thu Aug 2 14:36:52 PDT 2012


Zhenyu Lu,

Gdal.Open() with GA_Update mode cannot be used to create new files.  Instead
you will need to use the Create method on the driver object, and specify the
size, band count and other creation options.  GA_Update in Gdal.Open()
is intended for use with existing files that you want to update (replace
imagery, or metadata).

Best regards,
Frank

On Wed, Aug 1, 2012 at 8:33 PM, Zhenyu Lu <luzhenyu1981 at gmail.com> wrote:

>
> Hi Group,
>
> I just encountered an error in writting data to image in GTiff format
> using the OSGeo.GDAL.Gdal.Open command. The error message showing is
> "****.tif not recognised as a supported file format". The gdal librarary I
> am using is gdal19dev.dll. The weird thing is the Gdal.Open command worked
> if the image is in the format of HFA. The C# code I used to implement the
> writting process is attached below (the sentence where error happened was
> highlighted in red):
>          /// <summary>
>         /// Function of  WriteData2File
>         /// </summary>
>         /// <param name="LCx">the column index of left corner</param>
>         /// <param name="LCy">the row index of left corner</param>
>         /// <param name="width">the width of the rectangle</param>
>         /// <param name="height">the length of the rectangle</param>
>         /// <param name="fileName">the fileName where the data will be
> saved</param>
>         /// <returns>true, if the data is added correctly, false
> otherwise.</returns>
>         public bool WriteData2File(int LCx, int LCy, int width, int
> height, int band, byte[] Data, string fileName)
>         {
>             OSGeo.GDAL.Driver imgDriver =
> OSGeo.GDAL.Gdal.GetDriverByName("GTiff");
>             string extension = System.IO.Path.GetExtension(fileName);
>             if (extension == ".img")
>                 imgDriver = OSGeo.GDAL.Gdal.GetDriverByName("HFA");
>             if (imgDriver == null)
>                 throw new Exception("The file type is not supported!");
>             // OSGeo.GDAL.Gdal.AllRegister();
>             OSGeo.GDAL.Dataset ds = OSGeo.GDAL.Gdal.Open(fileName,
> OSGeo.GDAL.Access.GA_Update);
>             int[] bandMap = new int[] { 1, 1, 1, 1 };
>             try
>             {
>                 ds.WriteRaster(LCx, LCy, width, height, Data, width,
> height, 1, bandMap, 0, 0, 0);
>                 //ds.WriteRaster(LCx, LCy, width, height, Data, width,
> height, band, 0, 0, 0);
>                 ds.Dispose();
>                 return true;
>             }
>             catch
>             {
>                 return false;
>             }
>         }
>
> Can anyone point out where the problem is? Thank you so much,
>
>
> --
> Zhenyu Lu
> SUNY-ESF
>
>
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev
>



-- 
---------------------------------------+--------------------------------------
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    | Geospatial Software Developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20120802/eb18aefc/attachment.html>


More information about the gdal-dev mailing list