[gdal-dev] OGR python : read binary data in layer

Guilhain Averlant g.averlant at gmail.com
Fri Dec 18 01:12:05 PST 2020


Hello everyone.

Same layer new problem when I try to get value stored in my feature my
binary value (transformed to hex value) is truncated to 75 char maximum.
I tried to add the 'SetWidth' variable when creating the field, but it
doesn't change anything.

from osgeo import ogr

# create layer
driver = ogr.GetDriverByName('Memory')
data_source = driver.CreateDataSource('foo')
layer = data_source.CreateLayer('bar')

# create binary field
field_name = 'bin_field'
binary_field = ogr.FieldDefn(field_name, ogr.OFTBinary)
layer.CreateField(binary_field)

# add feature that contains binary value
bytes_value = bytes(b'\x0e=p\xa3\xd7\n=@"\x8f\\(\xf5\xc2\x8f\xc0`]\x1e\xb8Q\xeb\x85?\xf8Q\xeb\x85\x1e\xb8R at A\x8f\\(\xf5\xc2\x8f at R\x0e\xf9\xdb"\xd0\xe5@\x0e=p\xa3\xd7\n=@"\x8f\\(\xf5\xc2\x8f\x00\x00\x00\x01\x00\x00\x00\x04 at 7.\x14z\xe1G\xae\xc0A%\x1e\xb8Q\xeb\x85\xbf\xf4\xf5\xc2\x8f\\(\xf6\xc0\x12p\xa3\xd7\n=q@\x0bG\xae\x14z\xe1H at Sz=p\xa3\xd7\n at 7.\x14z\xe1G\xae\xc0A%\x1e\xb8Q\xeb\x85')
binary_as_hex_string = bytes_value.hex()
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetFieldBinaryFromHexString(field_name, binary_as_hex_string)
layer.CreateFeature(feature)

# get new feature in layer to read it
read_feature = layer.GetFeature(0)
# get 'bin_field' field value
print(read_feature.GetField(field_name))

>>> "0E3D70A3D70A3D40228F5C28F5C28FC0605D1EB851EB853FF851EB851EB85240418F5C28..."


Any idea to get the entire value ?

This should return this value normally :
0e3d70a3d70a3d40228f5c28f5c28fc0605d1eb851eb853ff851eb851eb85240418f5c28f5c28f40520ef9db22d0e5400e3d70a3d70a3d40228f5c28f5c28f000000010000000440372e147ae147aec041251eb851eb85bff4f5c28f5c28f6c01270a3d70a3d71400b47ae147ae14840537a3d70a3d70a40372e147ae147aec041251eb851eb85

-- 
Guilhain AVERLANT
119 rue Manin
75019 PARIS
06 28 22 83 05
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20201218/3f3b9349/attachment.html>


More information about the gdal-dev mailing list