I am trying to create a shapefile with Python GDAL/OGR library. I add to it a real number field, but when I write into it doesn't appear as it should but rounded. I will explain better with the code:

theDriver = driver = ogr.GetDriverByName("ESRI Shapefile")
theShp = driver.CreateDataSource(theFileName)
layer = theShp.CreateLayer(theFileName, geom_type=ogr.wkbPoint)

#create the field
fldN = ogr.FieldDefn('Num', ogr.OFTReal)

#add a point
pt = ogr.Geometry(ogr.wkbPoint)
pt.AddPoint(lng, lat)
featureDefn = layer.GetLayerDefn()
feature = ogr.Feature(featureDefn)

#write the field
feature.SetField('Num', 5.123456)

When I open the shapefile, the field "Num" is set to 5.00000. The extrange thing is that if I change the Driver to "GeoJSON" it works perfect. I tryed to set the precision of the field, but it didn't work.

  • What happens if you set the field width at 24 (normal default for Esri Shapefile OFTReal) Feb 8, 2016 at 18:40
  • Nop, doesn't work.. Feb 8, 2016 at 18:50
  • What precision did you use?
    – AndreJ
    Feb 8, 2016 at 19:14
  • 1
    what are you using to open the shapefile? With some additions to your posted code I could get the 'num' field to show up as 5.1234500000 opening this in QGIS. The field shows up as length=23, precision=15.
    – Steven Kay
    Feb 8, 2016 at 19:23

1 Answer 1


I don't understand your problem

import osgeo.ogr as ogr
import osgeo.osr as osr
driver = ogr.GetDriverByName("ESRI Shapefile")
srs = osr.SpatialReference()
# create the data source
data_source = driver.CreateDataSource("test_ogr.shp")
# create the layer
layer = data_source.CreateLayer("test_ogr", srs, ogr.wkbPoint)

# Add the field
layer.CreateField(ogr.FieldDefn("Num", ogr.OFTReal))
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetField("Num", 5.123456)
pt = ogr.Geometry(ogr.wkbPoint)

Control with Fiona (another GDAL/OGR Python binding)

import fiona
shape = fiona.open("test_ogr.shp")
first = shape.next()
print first
{'geometry': {'type': 'Point', 'coordinates': (5.0, 50.0)}, 'type': 'Feature', 'id': '0', 'properties': OrderedDict([(u'Num', 5.123456)])}
print first['properties']['Num']

Control with ogr

from osgeo import ogr
data_source = ogr.Open("test_ogr.shp")
layer = data_source.GetLayer(0)
feature = layer.GetFeature(0)
print feature.ExportToJson()
{"geometry": {"type": "Point", "coordinates": [5.0, 50.0]}, "type": "Feature", "properties": {"Num": 5.123456}, "id": 0}

And it is easier to create a shapefile with Fiona (look at Problem intersecting shapefiles with OGR in Python)

import fiona
from fiona.crs import from_epsg
# schema of the shapefile
schema = {'geometry': 'Point', 'properties': {'Num' : 'float:9.6'}}
crs = from_epsg(4326)
with fiona.open('test_fiona.shp','w',driver='ESRI Shapefile', crs=crs,schema= schema) as output:
    geom = {'type': 'Point', 'coordinates': (5.0, 50.0)}
    prop = {'Num': 5.123456}
    output.write({'geometry':geom, 'properties': prop})

Schema in QGIS

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.