Oracle Spatial Space Data Reading operations based on oo4o, FME, and ODP. Net in. net
Using system;
Using system. Collections. Generic;
Using system. text;
Using system. reflection;
Using safe. fmeobjects;
Using system. Collections. Specialized;
Using system. xml;
Using Oracle. dataaccess. client;
Using oracleinprocserver;
Namespace csharpreader_writer
{
Class Program
{
//
Static void main (string [] ARGs)
{
// Readoraclespatial ();
// Writemapinfo ();
Oo4o ();
String T = console. Readline ();
}
// Write MapInfo
Static void writemapinfo ()
{
String apppath = assembly. getexecutingassembly (). location;
Int Index = apppath. lastindexof ("\", apppath. Length-1 );
String appfolder = apppath. substring (0, index );
String appname = apppath. substring (index + 1 );
Index = appname. lastindexof (".", appname. Length-1 );
Appname = appname. substring (0, index );
Const string releasefoldername = "bin \ release ";
Const string debugfoldername = "bin \ debug ";
Index = appfolder. Length-releasefoldername. length;
If (string. Equals (appfolder. substring (INDEX), releasefoldername ))
{
Appfolder = appfolder. substring (0, index );
}
Else
{
Index = appfolder. Length-debugfoldername. length;
If (string. Equals (appfolder. substring (INDEX), debugfoldername ))
{
Appfolder = appfolder. substring (0, index );
}
}
String logfilename = appname + ". log ";
// Datasetname shocould be one of MIF or MapInfo or sde30 or gml2 or DWG or Oracle8i
String datasetname = "shape"; // "MapInfo"; // ARGs [0];
// Log File Path
String logfilepath = appfolder + logfilename;
// Dataset path
String datasetfilepath = appfolder + datasetname;
//
// Create the session object
Ifmeosession fmesession = fmeobjects. createsession ();
// Initialize the session
Fmesession. INIT (null );
// Get the log file object from the session
Ifmeologfile fmelogfile = fmesession. logfile ();
// Set the path of the log file
Fmelogfile. setfilename (logfilepath, false );
//
Try
{
// Now let's create a writer to write out features
Console. writeline ("Creating" + datasetname + "writer ...");
Ifmeowriter fmewriter = fmesession. createwriter (datasetname, null );
// Open the writer
Console. writeline ("Opening destination Dataset:" + datasetfilepath );
Stringcollection writerparams = new stringcollection ();
Writerparams. Add ("bounds ");
Writerparams. Add ("121 48 124 50 ");
Fmewriter. Open (datasetfilepath, writerparams );
// First, add the schema features to the writer
Console. writeline ("adding Schema Features ...");
Ifmeofeature schemafeature = fmesession. createfeature ();
Schemafeature. featuretype = "point ";
Fmelogfile. logfeature (schemafeature, fmeomessagelevel. Inform,-1 );
Fmewriter. addschema (schemafeature );
// Now, create the first data feature, and write it out
Console. writeline ("writing data features ...");
Ifmeofeature fmefeature = fmesession. createfeature ();
Fmefeature. featuretype = "point ";
Fmefeature. setstringattribute ("fme_type", "fme_point ");
Fmefeature. add2dcoordinate (123.0, 49.0 );
// Log the feature
Fmelogfile. logfeature (fmefeature, fmeomessagelevel. Inform,-1 );
// Write out the feature
Fmewriter. Write (fmefeature );
// Modify the feature and write it out as the second data feature
Fmefeature. featuretype = "point ";
Fmefeature. setstringattribute ("fme_type", "fme_point ");
Fmefeature. add2dcoordinate (122.0, 49.0 );
// Log the feature
Fmelogfile. logfeature (fmefeature, fmeomessagelevel. Inform,-1 );
// Write out the feature
Fmewriter. Write (fmefeature );
Console. writeline ("Writing complete .");
Console. writeline ("see" + logfilepath + "for log of features written .");
// Cleanup
Schemafeature. Dispose ();
Fmefeature. Dispose ();
// Close the write and destroy it
Fmewriter. Close ();
Fmewriter. Dispose ();
}
Catch (fmeoexception ex)
{
Console. writeline ("an fmeoexception has occurred. See the log file for more details .");
// Log errors to log file
Fmelogfile. logmessagestring (ex. fmeerrormessage, fmeomessagelevel. Error );
Fmelogfile. logmessagestring (ex. fmestacktrace, fmeomessagelevel. Error );
Fmelogfile. logmessagestring (ex. fmeerrornumber. tostring (), fmeomessagelevel. Error );
}
// Destroy the session
Fmesession. Dispose ();
}
// FME write Oracle8i
Static void writeexample ()
{
// Creates the session
Ifmeosession fmesession = fmeobjects. createsession ();
Fmesession. INIT (null );
// Creates the Oracle Spatial writer
Ifmeowriter fmewriter = fmesession. createwriter ("Oracle8i", null );
Stringcollection writerparms = new stringcollection ();
Writerparms. Add ("user_name ");
Writerparms. Add ("FME ");
Writerparms. Add ("password ");
Writerparms. Add ("FME ");
Fmewriter. Open ("khangdb", writerparms );
// Adds schema information for writer
Ifmeofeature schemafeature = fmesession. createfeature ();
Schemafeature. featuretype = "Drawing ";
Schemafeature. setsequencedattriature ("Geom", "Geometry ");
Fmewriter. addschema (schemafeature );
Schemafeature. Dispose ();
// Creates a feature to write out
Ifmeofeature fmefeature = fmesession. createfeature ();
Fmefeature. featuretype = "Drawing ";
Fmefeature. setsequencedattriature ("fme_type", "fme_line ");
Fmefeature. geometrytype = fmeogeometry. line;
Fmefeature. Dimension = fmeodimension. Two;
Fmefeature. add2dcoordinate (5, 5 );
Fmefeature. add2dcoordinate (5, 10 );
Fmefeature. add2dcoordinate (10, 10 );
// Writes the created feature. A table named drawing will be created.
Fmewriter. Write (fmefeature );
// Closes the writer
Fmewriter. Close ();
Fmewriter. Dispose ();
// Clean up
Fmefeature. Dispose ();
Fmesession. Dispose ();
}
// FME read Oracle8i
Static void readerexample ()
{
// Creates the session
Ifmeosession fmesession = fmeobjects. createsession ();
Fmesession. INIT (null );
// Creates the Oracle Spatial Reader
Ifmeoreader fmeread = fmesession. createreader ("Oracle8i", true, null );
Stringcollection writerparms = new stringcollection ();
Writerparms. Add ("user_name ");
Writerparms. Add ("cdbfsgdb ");
//
Writerparms. Add ("password ");
Writerparms. Add ("1 ");
//
Writerparms. Add ("server ");
Writerparms. Add ("xpserver ");
Fmeread. Open ("xpserver_oraclespatail", writerparms );
/// Adds schema information for Reader
// Ifmeofeature schemafeature = fmesession. createfeature ();
// Schemafeature. featuretype = "Drawing ";
// Schemafeature. setsequencedattriature ("Geom", "Geometry ");
// Fmeread. addschema (schemafeature );
// Schemafeature. Dispose ();
/// Creates a feature to write out
// Ifmeofeature fmefeature = fmesession. createfeature ();
// Fmefeature. featuretype = "Drawing ";
// Fmefeature. setsequencedattriature ("fme_type", "fme_line ");
// Fmefeature. geometrytype = fmeogeometry. line;
// Fmefeature. Dimension = fmeodimension. Two;
// Fmefeature. add2dcoordinate (5, 5 );
// Fmefeature. add2dcoordinate (5, 10 );
// Fmefeature. add2dcoordinate (10, 10 );
/// Writes the created feature. A table named drawing will be created.
// Fmewriter. Write (fmefeature );
/// Closes the writer
// Fmewriter. Close ();
// Fmewriter. Dispose ();
// Clean up
// Fmefeature. Dispose ();
Fmesession. Dispose ();
}
// ODP. Net Method for accessing the Oracle Spatial Database
Static xmldocument getspatialdata (string connectionstring, string SQL)
{
Oracleconnection con = new oracleconnection (connectionstring );
Try
{
Con. open ();
Oraclecommand cmd = new oraclecommand (SQL, con );
Cmd. xmlcommandtype = oraclexmlcommandtype. query;
Cmd. bindbyname = true;
Int rows = cmd. executenonquery ();
Xmlreader = cmd. executexmlreader ();
Xmldocument = new xmldocument ();
Xmldocument. preservewhitespace = true;
Xmldocument. Load (xmlreader );
Return xmldocument;
}
Catch (exception E)
{
Throw E;
}
Finally
{
If (con. State = system. Data. connectionstate. Open) con. Close ();
Con. Dispose ();
}
}
Static void readoraclespatial ()
{
// Call:
// String constr = "User ID = Scott; Password = tiger; Data Source = Apollo ";
// String SQL = "select * From interstates where rownum <2 ";
// Xmldocument = getspatialdata (constr, SQL );
// Console. writeline (xmldocument! = NULL )? Xmldocument. innerxml: "fail to read ");
// --- The --- end;
String constr = "User ID = cdbfsgdb; Password = 1; Data Source = xpserver ";
String SQL = "select * From v_tdlyxz_dltb_h where zldwdm like '000000 '";
Xmldocument = getspatialdata (constr, SQL );
If (xmldocument! = NULL)
{
Xmldocument. Save ("C: \ v_tdlyxz_dltb_h_510113106.xml ");
Console. writeline ("dltb record count:" + xmldocument. childnodes. Count. tostring () + ");
}
Console. writeline (xmldocument! = NULL )? Xmldocument. childnodes. Count. tostring (): "fail to read ");
// Constr = "User ID = cdbfsgdb; Password = 1; Data Source = xpserver ";
SQL = "select * From v_tdlyxz_dltb_h_yd where zldwdm like '20170101 '";
Xmldocument = getspatialdata (constr, SQL );
If (xmldocument! = NULL)
{
Xmldocument. Save ("C: \ v_tdlyxz_dltb_h_yd_510113106.xml ");
Console. writeline ("dltb_yd record count:" + xmldocument. childnodes. Count. tostring () + ");
}
Console. writeline (xmldocument! = NULL )? Xmldocument. childnodes. Count. tostring (): "fail to read ");
//
SQL = "select * From v_tdlygh_ytfq_xz_e where xzqdm like '20140901 '";
Xmldocument = getspatialdata (constr, SQL );
If (xmldocument! = NULL)
{
Xmldocument. Save ("C: \ v_tdlygh_ytfq_xz_e_510113.xml ");
Console. writeline ("ytfq record count:" + xmldocument. childnodes. Count. tostring () + ");
}
Console. writeline (xmldocument! = NULL )? Xmldocument. childnodes. Count. tostring (): "fail to read ");
}
// Oo4o Method for accessing the Oracle Spatial Database
Static void oo4o ()
{
Orasessionclass ORAS = new orasessionclassclass ();
Oradatabase oradb = ORAS. get_opendatabase ("r61", "cdbfsgdb/1", 0) as oradatabase;
Object snapshort = new object ();
Oradynaset dltb = oradb. get_createdynaset ("select * From v_tdlyxz_dltb_h where zldwdm like '000000' and rownum <= 4", 0, ref snapshort) as oradynaset;
Orafields orafds = dltb. fields as orafields;
Int fdsnums = orafds. count;
Int recordnums = dltb. recordcount;
Console. writeline ("output field list:" + fdsnums + ");
Orafield FD = NULL;
// For (INT I = 0; I <fdsnums; I ++)
//{
// FD = orafds [I] As orafield;
// Console. writeline (FD. Name + "," + FD. oraidatatype. tostring () + "," + FD. oramaxsize. tostring ());
//}
String T = "";
Int index_coor = 1;
Console. writeline ("output record:" + recordnums + ");
Oraobject Geom = NULL;
While (dltb. EOF = false)
{
Orafds = dltb. fields as orafields;
FD = orafds ["FID"] As orafield;
Console. writeline ("FID =" + FD. value. tostring ());
FD = orafds ["zldwdm"] As orafield;
Console. writeline ("zldwdm =" + FD. value. tostring ());
//
FD = orafds ["zldwmc"] As orafield;
Console. writeline ("zldwmc =" + FD. value. tostring ());
//
FD = orafds ["Geometry"] As orafield;
Geom = FD. value as oraobject;
// Sdo_gtype
Object gtype = (object) "sdo_gtype ";
Oraattribute gtype_attr = (oraattribute) Geom [gtype];
Int sdo_gtype_type = gtype_attr.type;
String sdo_gtype_value = (string) gtype_attr.value;
//
Console. writeline ("Geometry in (gtype =" + sdo_gtype_value + ")");
// Sdo_srid
Object SRID = (object) "sdo_srid ";
Oraattribute srid_attr = (oraattribute) Geom [SRID];
Int sdo_srid_type = srid_attr.type;
String sdo_srid_value = (string) srid_attr.value;
//
Console. writeline ("SRID =" + sdo_srid_value + "");
// Sdo_elem_info
Object einfo = (object) "sdo_elem_info ";
Oraattribute einfo_attr = (oraattribute) Geom [einfo];
Int sdo_einfo_type = einfo_attr.type;
Oracollection A = (oracollection) einfo_attr.value;
String [] SA = (string []) A. safearray;
//
T = "";
Foreach (string TMP in SA)
T + = "," + TMP;
Console. writeline ("sdo_elem_info =" + T + "");
// Sdo_ordinates coordinate set
Object ordi = (object) "sdo_ordinates ";
Oraattribute ordi_attr = (oraattribute) Geom [ordi];
Int sdo_ordi_type = ordi_attr.type;
Oracollection B = (oracollection) ordi_attr.value;
String [] sb = (string []) B. safearray;
T = "";
Index_coor = 1;
Console. writeline ("points =" + sb. Length/2 );
Foreach (string TMP in SB)
{
T + = "," + TMP;
If (index_coor % 2 = 0)
{
Console. writeline ("X, Y = (" + T + "),");
T = "";
}
Index_coor + = 1;
}
Console. writeline ("End FID ");
//
Dltb. movenext ();
}
}
}
}
---- The -- end ---