1

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)
layer.CreateField(fldN)

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

#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) – blord-castillo Feb 8 '16 at 18:40
  • Nop, doesn't work.. – Rodrigo E. Principe Feb 8 '16 at 18:50
  • What precision did you use? – AndreJ Feb 8 '16 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 '16 at 19:23
4

I don't understand your problem

import osgeo.ogr as ogr
import osgeo.osr as osr
driver = ogr.GetDriverByName("ESRI Shapefile")
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
# 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)
pt.AddPoint(5,50)
feature.SetGeometry(pt)
layer.CreateFeature(feature)
feature.Destroy()
data_source.Destroy()

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']
5.123456

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.