python - 是否可以使用 Python 和 cx_Oracle 进行空间查询?

标签 python cx-oracle nearest-neighbor sdo

我正在尝试使用 cx_Oracle 包通过 python 对 Oracle 空间表执行空间查询。

我可以成功进行通用查询,但是当我尝试空间查询时,会导致错误。

这是我尝试过的:

import cx_Oracle
...

lon = -120.494352
lat = 36.585289

# open a connection to oracle
con = cx_Oracle.connect('myuser/mypass@spatialdb')

# create a cursor
cur = con.cursor()

# Create and populate Oracle objects
typeObj = con.gettype("MDSYS.SDO_GEOMETRY")
elementInfoTypeObj = con.gettype("MDSYS.SDO_ELEM_INFO_ARRAY")
ordinateTypeObj = con.gettype("MDSYS.SDO_ORDINATE_ARRAY")
obj = typeObj.newobject()
obj.SDO_GTYPE = 2001
obj.SDO_SRID = 8307
obj.SDO_ELEM_INFO = elementInfoTypeObj.newobject()
obj.SDO_ELEM_INFO.extend([1, 1, 1])
obj.SDO_ORDINATES = ordinateTypeObj.newobject()
obj.SDO_ORDINATES.extend([lon, lat])
print("Created object", obj)

# set up a distance-calculating sql statement
sql = "select id into :id from spatialtbl s where sdo_nn(s.geometry, :obj, 'sdo_num_res=1', 1) = 'TRUE'"

try:
    # execute the distance sql
    cur.execute(sql, id=id, obj=obj)
    print(f'The id is {id.getvalue()}')
except cx_Oracle.Error as error:
    print(error)

这会导致错误:

ORA-01036: illegal variable name/number

谁能告诉我我在代码方面可能做错了什么,或者是否可以使用 Python 和 cx_Oracle 进行空间查询?据我所知/发现,cx_Oracle 文档没有具体解决这个问题。

最佳答案

文档中有一个简短的提及:

以下是 cx_Oracle 源代码存储库中的两个示例:

以下是最近 Oracle session 的演示文稿:

在您的示例中,您可能至少需要执行id =cursor.var(int),请参阅Bind Direction因此 cx_Oracle 知道如何处理从数据库获取的值。

关于python - 是否可以使用 Python 和 cx_Oracle 进行空间查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60302762/

相关文章:

金融中的计算几何算法?

python - 在 Python 中过滤数据框

python - 与厚模式相比,python-oracledb 瘦模式是否对性能有任何影响?

python - 在 Windows 中部署 cx_Oracle

Windows 7 上的 Python 3.5.3 和 Oracle 11.2 : cannot locate oracle include files

matlab - 使用 VLFeat 创建集群后,将描述符分配给集群中心

r - R中点要素到最近多边形的距离

python - 删除单链表中的最后一个节点

python - 访问 django oscar 产品属性

python - 优化 numpy 点云创建脚本