1

我有一个很高兴返回数据的 cx_Oracle 连接。然而,我在几何方面遇到了麻烦。它作为 cx_Oracle 对象返回,例如 0x3afc320 处的 cx_Oracle.OBJECT。但我无法访问它的属性如下

import cx_Oracle
query = '''
select geometry from table
'''
cx_Oracle.makedsn(...)
db_conn = cx_Oracle.connect(...)
cursor = db_conn.cursor()
cursor.execute(query)
columns = [i[0].lower() for i in cursor.description]
results = []
for row in cursor:
    results.append(dict(zip(columns, row)))
db_conn.close()
print (results[0]['geometry'])
print results[0]['geometry'].SDO_ORDINATES
print results[0]['geometry'].SDO_GTYPE
print results[0]['geometry'].SDO_ELEM_INFO

因为?这些属性不可用?

import inspect
inspect.getmembers(results[0]['geometry'])
[('__class__', cx_Oracle.OBJECT),
('__delattr__',
<method-wrapper '__delattr__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__doc__', None),
('__format__', <function __format__>),
('__getattribute__',
<method-wrapper '__getattribute__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__hash__',
<method-wrapper '__hash__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__init__',
<method-wrapper '__init__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__new__', <function __new__>),
('__reduce__', <function __reduce__>),
('__reduce_ex__', <function __reduce_ex__>),
('__repr__',
<method-wrapper '__repr__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__setattr__',
<method-wrapper '__setattr__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__sizeof__', <function __sizeof__>),
('__str__',
<method-wrapper '__str__' of cx_Oracle.OBJECT object at 0x03AFC320>),
('__subclasshook__', <function __subclasshook__>),
('type', <cx_Oracle.ObjectType MDSYS.SDO_GEOMETRY>)]

当我使用 sql developer 查看有问题的表时,“几何”字段的类型为“SDO_GEOMETRY()”。任何帮助表示赞赏。

4

2 回答 2

2

如果您有一个如下所示的表:

create table TestGeometry (
    IntCol number(9) not null,
    Geometry sdo_geometry not null
);

并使用如下数据填充它:

insert into TestGeometry
values (1, sdo_geometry(2003, null, null, sdo_elem_info_array(1, 1003, 3),
    sdo_ordinate_array(1, 1, 5, 7)));

那么以下脚本将访问您要查找的数据:

connection = cx_Oracle.Connection("user/pw@tns")
cursor = connection.cursor()
cursor.execute("""
        select Geometry
        from TestGeometry
        where IntCol = 1""")
obj, = cursor.fetchone()
print(obj.SDO_ORDINATES)
print(obj.SDO_GTYPE)
print(obj.SDO_ELEM_INFO)

您始终可以使用以下代码找出可用的属性:

for attr in obj.type.attributes:
    print(attr.name)
于 2016-02-23T21:58:44.877 回答
0

我遇到了类似的问题。sdo_geometry 对象不会传输到请求客户端。我相信原因是内部几何图形存储为 LOB。

在我的例子中,我需要简单地访问几何,所以我将 sdo_geometry (SDO_UTIL.TO_WKTGEOMETRY) 转换为字符数据,然后它就下来了。

可能还有其他方法可以将对象转换为子组件。

希望有帮助,J

于 2013-10-11T17:51:18.940 回答