Get SDO_GEOMETRY from CX-Oracle in Python/GeoDjango

Being pretty new to Python and Django, I spent a while trying to figure out how to get the vertices out of an Oracle Geometry object using python and cx-oracle. I came across a blog post which helped me realise that it was simpler that I had been expecting.

It’s pretty simple.You just need to remember how the elements of SDO_GEOMETRY are put together.

Once you have selected the oracle object into a variable, if you print it you’ll get:

Not that useful, but you can just access the structure of the SDO_GEOMETRY object directly. So, if you just want the SDO_POINT, you can use:

Note that the above only works if your point is stored in the SDO_POINT attribute. With SDO_GEOMETRY, you can also store a point in the SDO_ORDINATES attribute, so be careful which one you use (or check both if you don’t know). Interestingly, you can actually store a linestring or any other kind of geometry in the SDO_ORDINATES attribute, while also storing a point in the SDO_POITN field. I guess that might be useful for some applications.

If you want to access the coordinates for geometry stored in SDO_ORDINATES, it’s just:

Of course you should know what kind of geometry is stored there, but you can use the information in geom.SDO_ELEM_INFO to figure that out (refer to the Oracle docs).

Buffer this pageTweet about this on TwitterShare on FacebookShare on Google+Email this to someone

Leave a Reply

Your email address will not be published. Required fields are marked *