niedziela, 6 lutego 2011

cx_Oracle - how to get SDO_GEOMETRY data

#Let's connect to the database
import cx_Oracle
orcl = cx_Oracle.connect ('scott/tiger@orcl')
c = orcl.cursor()

#we need some spatial data
sql = "SELECT geom FROM geometry_tab WHERE (...)"
res = c.execute(sql)
obj = res.fetchone()[0]

#ok, what do we have?
obj
<cx_Oracle.OBJECT object at 0x0158A4C0>
#if you type dir(obj) you will not get the attributes names - try..
# you MUST know (or read here) the you can use attributes from SDO_GEOMETRY object in ORCL, like:
#'SDO_GTYPE','SDO_SRID','SDO_POINT','SDO_ELEM_INFO','SDO_ORDINATES'

obj.SDO_GTYPE
2003.0

obj.SDO_ELEM_INFO
[1.0, 1003.0, 1.0]

obj.SDO_ORDINATES
[20.623358189, 52.88806347, 20.623203441, 52.887897231000004, 20.623488043000002, 52.887861954, (...) , 52.888026894, 20.623358189, 52.88806347]

3 komentarze:

  1. Nice very nice! Thx a lot!

    OdpowiedzUsuń
  2. Hello
    Do you know how insert SDO_GEOMETRY with cx_oracle and using parameter like this?

    sql = 'INSERT INTO poly(geom) VALUES (:1)'
    cursor.execute(sql, "MDSYS"."SDO_GEOMETRY"(2003,NULL,NULL,"MDSYS"."SDO_ELEM_INFO_ARRAY"(1,1003,1),"MDSYS"."SDO_ORDINATE_ARRAY"(0,0,1,0,1,1,0,1,0,0))') '

    I don´t know how create cx_oracle.OBJECT from geometry, which I can insert

    OdpowiedzUsuń
  3. fillippoo, if you have problems with inserting directly SDO_GEOMETRY try to insert WKT and then execute SDO_UTIL.FROM_WKTGEOMETRY by Cursor.callproc(). Good Luck!

    OdpowiedzUsuń