Geometric Data

1

I'm calculating the geometric data from the latitude / longitude.

  

link

consultaGeom = ('''select geometry::STGeomFromText('point('+left(?,12)+' '+left(?,12)+')' , 4326)''');
                 param = (latCIE,lngCIE)
                 query = ConexionProyecto.execute(consultaGeom,param)
                 row = query.fetchone()
                 print(row)

The drawback that the data that returns the query and is displayed is

(b'\xe6\x10\x00\x00\x01\x0c\xbdR\x96!\x8e\xa5;\xc0\x1c\xeb\xe26\x1a\P\xc0', )

When it should be

0xE6100000010C00000000004050C00000000000003CC0

Why does this happen?

I need the data returned to be correct in order to insert it in a geometric field.

    
asked by Sebastian 07.03.2018 в 15:57
source

2 answers

1

What returns the function fetchone() is a tuple of a single element. Extracting its first element ( [0] ), you get a string of bytes , which can be converted to its hexadecimal representation with bytes.hex() .

If you need to also be exactly as in the example you have shown (that is, with a 0x in front and hexadecimal digits in upper case), you can use str.upper() and concatenate the 0x . So:

consultaGeom = ('''select geometry::STGeomFromText('point('+left(?,12)+' '+left(?,12)+')' , 4326)''');
param = (latCIE,lngCIE)
query = ConexionProyecto.execute(consultaGeom,param)
row = query.fetchone()

row = "0x{}".format(row[0].hex().upper())
print(row)
  

0xE6100000010C00000000004050C00000000000003CC0

    
answered by 07.03.2018 / 16:24
source
0

What you are receiving is an object of type bytes :

>>> type(b'\xe6\x10\x00\x00\x01\x0c\xbdR\x96!\x8e\xa5;\xc0\x1c\xeb\xe26\x1a\P\xc0')
<class 'bytes'>

Therefore, you can simply call their method hex() :

>>> b'\xe6\x10\x00\x00\x01\x0c\xbdR\x96!\x8e\xa5;\xc0\x1c\xeb\xe26\x1a\P\xc0'.hex()
'e6100000010cbd5296218ea53bc01cebe2361a5c50c0'

You can also use binascii :

>>> import binsacii
>>> hex_value = binascii.hexlify(b'\xe6\x10\x00\x00\x01\x0c\xbdR\x96!\x8e\xa5;\xc0\x1c\xeb\xe26\x1a\P\xc0')
>>> hex_value
b'e6100000010cbd5296218ea53bc01cebe2361a5c50c0'
>>> binascii.unhexlify(hex_value)
b'\xe6\x10\x00\x00\x01\x0c\xbdR\x96!\x8e\xa5;\xc0\x1c\xeb\xe26\x1a\P\xc0'
    
answered by 07.03.2018 в 16:18