[gdal-dev] Help on writting float type data into raster using writeRaster

Zhenyu Lu luzhenyu1981 at gmail.com
Fri Nov 2 20:10:27 PDT 2012


Hi All,

Recently I am trying to write float type data into a raster using the
function named WriteRaster in GDAL. However, I found that the data written
into the image does not make sense.

I tried to track down the issue from the very beginning, I found that the
values predicted by the non-GDAL part are correct which ranges from 0.0 to
1000.0. However, the values written into the image seems not right. I
"info"ed some pixels in ArcGIS, and found that some of the values are
correct, but some are incredibly large (> 1,000,000,000) or -0.0.

Beneath are the two function I created using C# to wrap up the create
raster and write data to raster operations. Would you please review the
code and let me know if you find anything wired? The dataType I passed in
the following function is DataType.GDT_Float64.

       /// <summary>
        /// Function of CreateImage
        /// </summary>
        /// <param name="width">the width of the image</param>
        /// <param name="height">the height of the image</param>
        /// <param name="band">the band number of the image</param>
        /// <param name="dataType">the dataType of the image</param>
        /// <param name="fileName">the filepath of the image</param>
        /// <param name="Projection">the projection of the image</param>
        /// <param name="geotrans">the extent of the image</param>
        /// <returns>true if the file is created successfully, false
otherwise</returns>
        public bool CreateImage(int width, int height, int band,
OSGeo.GDAL.DataType dataType, string fileName, string Projection, double[]
geotrans)
        {
            try
            {
                OSGeo.GDAL.Gdal.AllRegister();
                OSGeo.GDAL.Driver poDriver =
OSGeo.GDAL.Gdal.GetDriverByName("GTiff");
                if (System.IO.Path.GetExtension(fileName) == ".img")
                    poDriver = OSGeo.GDAL.Gdal.GetDriverByName("HFA");
                OSGeo.GDAL.Dataset poDstDs = poDriver.Create(fileName,
width, height, band, dataType, null);
                poDstDs.SetProjection(Projection);
                poDstDs.SetGeoTransform(geotrans);
                //****Added by ZLu on 08/03/2012****
                poDstDs.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }

       /// <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, double[] 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.Dataset ds = OSGeo.GDAL.Gdal.Open(fileName,
OSGeo.GDAL.Access.GA_Update);
            int[] bandMap = new int[] { 1, 1, 1};
            try
            {
                ds.WriteRaster(LCx, LCy, width, height, Data, width,
height, 1, bandMap, 1, 1, 1);
                //ds.WriteRaster(LCx, LCy, width, height, Data, width,
height, band, 0, 0, 0);
                ds.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }


Thank you so much and wish you have a nice weekend.

Zhenyu

-- 
Zhenyu Lu
SUNY-ESF
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20121102/d5e10567/attachment.html>


More information about the gdal-dev mailing list