Skip to content Skip to sidebar Skip to footer

Python Pyodbc Sql Server Native Client 11.0 Cannot Return Geometry Column

I have SQL Server Native Client 11.0 and pyodbc installed using python 2.7. I am able to set the connection correctly within python import pyodbc conn = pyodbc.connect('DRIVER={SQ

Solution 1:

Based on the GitHub issue here, the following output converter function seems to do the trick:

def unpack_geometry(raw_bytes):
    # adapted from SSCLRT information at
    #   https://docs.microsoft.com/en-us/openspecs/sql_server_protocols/ms-ssclrt/dc988cb6-4812-4ec6-91cd-cce329f6ecda
    tup = struct.unpack('<i2b3d', raw_bytes)
    # tup contains: (unknown, Version, Serialization_Properties, X, Y, SRID)
    return tup[3], tup[4], tup[5]

# ...

cnxn.add_output_converter(-151, unpack_geometry)
crsr.execute("SELECT CAST('POINT(-79.528874 43.648533 12345)' AS geometry)")
print(crsr.fetchval())  # (-79.528874, 43.648533, 12345.0)

Post a Comment for "Python Pyodbc Sql Server Native Client 11.0 Cannot Return Geometry Column"