Truncated values when accessing decimal fields when using the GeoDjango DataSource

35 views
Skip to first unread message

khoobks

unread,
Aug 24, 2012, 8:34:43 AM8/24/12
to django...@googlegroups.com
Hi Everybody,

I noticed what appeared to be a truncation issue today while attempting to retrieve decimal fields from a Map Info TAB file using the GeoDjango datasource. 

I'd like to find out if 
1) This is replicable for other people (I have replicated it successfully on two machines)
2) If this is a known issue and
3) If there are any workarounds besides falling back to 'normal' OGR.

I have attached a sample TAB file and a python file to test the problem. The output on my machine is as follows,


Python 2.7.2+ (default, Oct  4 2011, 20:06:09) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> filepath = '/tmp/test_datatypes/test_datatypes.TAB'
>>> 
>>> # Using Django
>>> import django
>>> print django.VERSION
(1, 4, 1, 'final', 0)
>>> from django.contrib.gis.gdal import DataSource
>>> ds = DataSource(filepath)
>>> lyr = ds[0]
>>> for feat in lyr:
...     print '----------'
...     print 'Feature ID: ', feat.fid
...     for field_name in feat.fields:
...         print 'Field Name: ', field_name
...         print 'Field Data:', type(feat.get(field_name)), feat.get(field_name)
...         print
... 
----------
Feature ID:  1
Field Name:  char
Field Data: <type 'str'> test

Field Name:  deci
Field Data: <type 'float'> 1.234

Field Name:  int
Field Data: <type 'int'> 5

Field Name:  date
Field Data: <type 'datetime.date'> 2012-08-24

Field Name:  float
Field Data: <type 'int'> 987

----------
Feature ID:  2
Field Name:  char
Field Data: <type 'str'> abc

Field Name:  deci
Field Data: <type 'float'> 987.6

Field Name:  int
Field Data: <type 'int'> 3

Field Name:  date
Field Data: <type 'datetime.date'> 2012-08-26

Field Name:  float
Field Data: <type 'int'> 12345

>>> # Using OGR
>>> import ogr
>>> ds = ogr.Open(filepath)
>>> lyr = ds[0]
>>> for feat in lyr:
...     feat.items()
... 
{'char': 'test', 'int': 5, 'float': 987.65, 'date': '2012/08/24', 'deci': 1.234}
{'char': 'abc', 'int': 3, 'float': 12345.6789, 'date': '2012/08/26', 'deci': 987.6}
>>> 


The problem is that for the field named "float" the expected value is 987.65 for the first feature and 12345.6789 for the second field. Using geodjango however I am getting 987 and 12345 respectively. This is of course a truncation of some sort making the number into an int. 

Has anyone else experienced this problem? Is this a known issue and is the appropriate work around simply to fall back to 'normal' OGR?
On a separate note purely for my own edification, does anyone know why the design decision was made to wrap OGR and GDAL and not simply reuse the python bindings that they provide?

Thanks in advance for any help that you can provide.
Ben

test_datatypes.zip
test.py
Reply all
Reply to author
Forward
0 new messages