OTL 4.0, Example 769 (Oracle 11.2 and higher, SDO Spatial Geometry)

This example demonstrates how to use OTL with oci_spatial_geometry for reading/writing MDSYS.SDO_GEOMETRY values from/to the otl_stream.

Source Code

#include <iostream>
using namespace std;

#include <stdio.h>
#define OTL_STL // enables OTL/STL integration
#define OTL_ORA_SDO_GEOMETRY // enables support for Oracle MDSYS.SDO_GEOMETRY
#define OTL_ORA_OCI_ENV_CREATE // enables OCI mode that supports "OCI object mode"
#define OTL_ORA_OCI_ENV_CREATE_MODE (OCI_THREADED|OCI_OBJECT) // enables OCI threaded and object modes
#define OTL_ORA11G_R2 // OCI 11.2

#include <otlv4.h> // include the OTL 4.0 header file

otl_connect db; // connect object

void insert()
{
otl_stream o(5, // buffer size
"insert into test_tab "
"values(:f1/*int*/,:f2/*sdo_geometry*/)",
// SQL statement
db // connect object
);
// Point
{
oci_spatial_geometry geom;
geom.isNull = false;
geom.x = 10;
geom.y = 10;
geom.gtype = 2001;//gType of Point2D
o<<1<<geom;
}

// Line
{
oci_spatial_geometry geom;
geom.isNull = false;
geom.gtype = 2002;//gType of Line2D
geom.eleminfo.push_back(1);
geom.eleminfo.push_back(2);
geom.eleminfo.push_back(1);
// array of coordinates, every pair = (x,y).
// for such example line is {(100,100),(50,80),(40,40)}
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(50.0);
geom.ordinates.push_back(80.0);
geom.ordinates.push_back(40.0);
geom.ordinates.push_back(40.0);
o<<2<<geom;
}

// Polygon
{
oci_spatial_geometry geom;
geom.isNull = false;
geom.gtype = 2003; //gType of Polygon2D
std::vector<int> v1;
geom.eleminfo.push_back(1);
geom.eleminfo.push_back(1003);
geom.eleminfo.push_back(1);
// array of coordinates, every pair = (x,y).
// for such example polygon is {(100,100),(100,0),(0,0),(0,100),(100,100)}
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(0.0);
geom.ordinates.push_back(0.0);
geom.ordinates.push_back(0.0);
geom.ordinates.push_back(0.0);
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(100.0);
geom.ordinates.push_back(100.0);
o<<3<<geom;
}

}

void select()
{
otl_stream i(5, // buffer size
"select id,geoloc from test_tab",
// SQL statement
db // connect object
);
while(!i.eof()){
int id;
oci_spatial_geometry geom;
i>>id>>geom;
switch(geom.gtype){
case 2001://read Point2D
{
cout<<"Point2D: X="<<geom.x<<", Y="<<geom.y<<endl;
break;
}
case 2002://read line2D
{
cout<<"Line2D: ";
cout<<"eleminfo(";
for(size_t i2=0; i2<geom.eleminfo.size();++i2){
cout<<geom.eleminfo[i2];
if(i2<geom.eleminfo.size()-1)
cout<<", ";
else
cout<<"), ";
}
cout<<"ordinates(";
for(size_t i2=0; i2<geom.ordinates.size();++i2){
cout<<geom.ordinates[i2];
if(i2<geom.ordinates.size()-1)
cout<<", ";
else
cout<<") ";
}
cout<<endl;
break;
}
case 2003://read Polygon2D
{
cout<<"Polygon2D: ";
cout<<"eleminfo(";
for(size_t i2=0; i2<geom.eleminfo.size();++i2){
cout<<geom.eleminfo[i2];
if(i2<geom.eleminfo.size()-1)
cout<<", ";
else
cout<<"), ";
}
cout<<"ordinates(";
for(size_t i2=0; i2<geom.ordinates.size();++i2){
cout<<geom.ordinates[i2];
if(i2<geom.ordinates.size()-1)
cout<<", ";
else
cout<<") ";
}
cout<<endl;
break;
}
}
}

}

int main()
{
 try{

  db.rlogon("scott/tiger"); // connect to Oracle
db.direct_exec("drop table test_tab",otl_exception::disabled);
db.direct_exec
("create table test_tab "
"(id integer not null,"
" geoloc mdsys.sdo_geometry not null, "
" primary key (id))");
db.direct_exec("delete from user_sdo_geom_metadata where table_name = 'TEST_TAB'");
db.direct_exec
("insert into user_sdo_geom_metadata (table_name , column_name , diminfo , srid) "
"values('TEST_TAB', 'GEOLOC', mdsys.sdo_dim_array(mdsys.sdo_dim_element('X', -100000, 100000, 0.001), "
"mdsys.sdo_dim_element('Y', -100000, 100000, 0.001)), "
"(select srid from mdsys.cs_srs where cs_name = 'Non-Earth (meters)')) ");
insert();
select();
db.logoff();
 }

 catch(otl_exception& p){ // intercept OTL exceptions
  cerr<<p.msg<<endl; // print out error message
  cerr<<p.stm_text<<endl; // print out SQL that caused the error
  cerr<<p.var_info<<endl; // print out the variable that caused the error
 }

 db.logoff(); // disconnect from Oracle

 return 0;

}

Output

Point2D: X=10, Y=10
Line2D: eleminfo(1, 2, 1), ordinates(100, 100, 50, 80, 40, 40)
Polygon2D: eleminfo(1, 1003, 1), ordinates(100, 100, 100, 0, 0, 0, 0, 100, 100, 100)


Examples ContentsGo Home

Copyright 1996-2017, Sergei Kuchin, email: skuchin@gmail.com, skuchin@gmail.com .

Permission to use, copy, modify and redistribute this document for any purpose is hereby granted without fee, provided that the above copyright notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.