[gdal-dev] Re: OFTReal values truncated to integer

diegogb diego.gnesi at gmail.com
Sat Apr 28 13:16:37 EDT 2012


Ok, I executed many tests and I found something strange.
I tried:

- QT Creator,
- CodeLite,
- Python

I wrote the following programs;

// C++ CodeLite version.
#include "gdal/ogrsf_frmts.h"

int main(int argc, char *argv[])
{
    OGRRegisterAll();
    OGRSFDriver *driver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
    OGRDataSource *dSource =
driver->CreateDataSource("/home/diego/Documenti/P2/test.shp", NULL);
    OGRLayer *layer = dSource->CreateLayer("TestLayer", NULL, wkbPoint,
NULL);
    OGRFieldDefn elevField("Elev", OFTReal);
    layer->CreateField(&elevField);
    OGRFeature *pointFeature =
OGRFeature::CreateFeature(layer->GetLayerDefn());
    OGRPoint thePoint;
    double x=1.1, y=1.2, z=1.3;
    thePoint.setX(x);
    thePoint.setY(y);
    thePoint.setZ(z);
    pointFeature->SetGeometry(&thePoint);
    pointFeature->SetField("Elev", z);
    layer->CreateFeature(pointFeature);
    OGRFeature::DestroyFeature(pointFeature);
    OGRDataSource::DestroyDataSource(dSource);
    return 0;
}



//C++ Qt Creator version.
// (same code as above, but written as implementation of the class method
AltConverter.createFile().
#include "altconverter.h"
#include "gdal/ogrsf_frmts.h"

void AltConverter::createFile() {
        OGRRegisterAll();
        OGRSFDriver *driver =
OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName("ESRI Shapefile");
        OGRDataSource *dSource =
driver->CreateDataSource("/home/diego/Documenti/P2/test.shp", NULL);
        OGRLayer *layer = dSource->CreateLayer("TestLayer", NULL, wkbPoint,
NULL);
        OGRFieldDefn elevField("Elev", OFTReal);
        layer->CreateField(&elevField);
        OGRFeature *pointFeature =
OGRFeature::CreateFeature(layer->GetLayerDefn());
        OGRPoint thePoint;
        double x=1.1, y=1.2, z=1.3;
        thePoint.setX(x);
        thePoint.setY(y);
        thePoint.setZ(z);
        pointFeature->SetGeometry(&thePoint);
        pointFeature->SetField("Elev", z);
        layer->CreateFeature(pointFeature);
        OGRFeature::DestroyFeature(pointFeature);
        OGRDataSource::DestroyDataSource(dSource);
}



#! /usr/bin/python
# Python version.
import osgeo.ogr, osgeo.osr

driver = osgeo.ogr.GetDriverByName('ESRI Shapefile')
dSource = driver.CreateDataSource ('sample.shp')
layer = dSource.CreateLayer('TestLayer',  geom_type=osgeo.ogr.wkbPoint)
fieldDefn = osgeo.ogr.FieldDefn('Elev',  osgeo.ogr.OFTReal)
layer.CreateField(fieldDefn)
layerDefn = layer.GetLayerDefn()
newPoint = osgeo.ogr.Geometry(osgeo.ogr.wkbPoint)
newPoint.SetPoint(0, 1.1,  1.2)
newFeature = osgeo.ogr.Feature(layer.GetLayerDefn())
newFeature.SetGeometry(newPoint)
newFeature.SetField('Elev',  1.3)
layer.CreateFeature(newFeature)
newFeature.Destroy()
dSource.Destroy()


*Now, Python and CodeLite code work fine, and produce the value 1.3 for the
Elev field, Qt Creator code don't and the saved value is read as 1 (because
is codified as "1,3" and not as "1.3")!!!*

Now, I took a look inside the source code of the GDAL library.
The method that writes real values to the dbf file is
DBFWriteDoubleAttribute (inside the ogr/ogr_frmts/shape/dbfopen.c file).
I think that there must be a sprintf that writes values using local settings
while it shouldn't, but I don't know the library and I'm not able to check.
I think it's a bug to correct.

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/OFTReal-values-truncated-to-integer-tp4933952p4936894.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list