Opened 7 years ago

Closed 7 years ago

#5882 closed defect (fixed)

GeoJSON driver truncate numbers

Reported by: emanueledigiacomo Owned by: warmerdam
Priority: normal Milestone: 1.11.3
Component: OGR_SF Version: 1.11.2
Severity: normal Keywords: geojson
Cc:

Description (last modified by emanueledigiacomo)

Some numeric values in GeoJSON file (e.g. 4.1e-8, 0.00000001) are truncated.

I think the problem is in the conversion from JSON double to JSON string (function json_object_double_to_json_string in ogr/ogrsf_frmts/geojson/libjson/json_object.c): the conversion is done with a %f format (by default, 6 digits after the decimal number):

size = snprintf(buf, 128, "%f", jso->o.c_double);

See attached file "data.json", where the properties are

"properties" : {
            "value1" : 4.1e-9,
            "value2": 0.00000001,
            "value3": 0.0000012
}

But ogrinfo show truncated values:

$ ogrinfo -geom=NO -ro data.json OGRGeoJSON
INFO: Open of `data.json'
      using driver `GeoJSON' successful.

Layer name: OGRGeoJSON
Geometry: Point
Feature Count: 1
Extent: (10.330000, 44.800000) - (10.330000, 44.800000)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
value1: Real (0.0)
value2: Real (0.0)
value3: Real (0.0)
OGRFeature(OGRGeoJSON):0
  value1 (Real) = 0
  value2 (Real) = 0
  value3 (Real) = 1e-06

And a conversion to another format has the same result:

$ ogr2ogr -f CSV data.csv data.json
$ cat data.csv
value1,value2,value3
0,0,1e-06

Attachments (1)

data.json (391 bytes) - added by emanueledigiacomo 7 years ago.

Download all attachments as: .zip

Change History (5)

Changed 7 years ago by emanueledigiacomo

Attachment: data.json added

comment:1 Changed 7 years ago by Jukka Rahkonen

I made a test with GDAL 2.0-dev I can confirm your results. By reading the document page http://www.gdal.org/drv_geojson.html the driver does not support doubles:

"Schema detection will recognized fields of type String, Integer, Real, StringList?, IntegerList? and RealList?. Starting with GDAL 2.0, Integer(Boolean), Date, Time and DateTime? fields are also recognized."

comment:2 Changed 7 years ago by emanueledigiacomo

Description: modified (diff)

comment:3 in reply to:  1 Changed 7 years ago by emanueledigiacomo

I thought that Real type was the OFTReal, one of the values of the enum type OGRFieldType. From ogr_core.h:

  /** Double Precision floating point */        OFTReal = 2,

Furthermore, the GeoJSON driver should support doubles, because it declares only double values in the code (and not float).

I updated the bug description, I think the problem is the snprintf inside the function json_object_double_to_json_string.

Replying to jratike80:

I made a test with GDAL 2.0-dev I can confirm your results. By reading the document page http://www.gdal.org/drv_geojson.html the driver does not support doubles:

"Schema detection will recognized fields of type String, Integer, Real, StringList?, IntegerList? and RealList?. Starting with GDAL 2.0, Integer(Boolean), Date, Time and DateTime? fields are also recognized."

comment:4 Changed 7 years ago by Even Rouault

Keywords: geojson added
Milestone: 1.11.3
Resolution: fixed
Status: newclosed

trunk r28748, branches/1.11 r28749 "GeoJSON reader: avoid truncation of real numbers (#5882)"

Note: See TracTickets for help on using tickets.