NET下基于OO4O,FME,ODP.NET的Oracle Spatial空间数据读取操作

  • Post author:
  • Post category:其他


NET下基于OO4O,FME,ODP.NET的Oracle Spatial空间数据读取操作

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 should be one of MIF or MAPINFO or SDE30 or GML2 or DWG or ORACLE8I

string datasetName = “SHAPE”;// “MAPINFO”;// args[0];

//日志文件路径

string logFilePath = appFolder + logFileName;

//数据集路径

string datasetFilePath = appFolder + datasetName;

//

//Create the session object

IFMEOSession fmeSession = FMEObjects.CreateSession();

//初始化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.SetSequencedAttribute(“GEOM”, “GEOMETRY”);

fmeWriter.AddSchema(schemaFeature);

schemaFeature.Dispose();

// Creates a feature to write out

IFMEOFeature fmeFeature = fmeSession.CreateFeature();

fmeFeature.FeatureType = “drawing”;

fmeFeature.SetSequencedAttribute(“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.SetSequencedAttribute(“GEOM”, “GEOMETRY”);

//fmeRead.AddSchema(schemaFeature);

//schemaFeature.Dispose();

Creates a feature to write out

//IFMEOFeature fmeFeature = fmeSession.CreateFeature();

//fmeFeature.FeatureType = “drawing”;

//fmeFeature.SetSequencedAttribute(“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访问Oracle spatial 数据库的方法

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 xmlReader = cmd.ExecuteXmlReader();

XmlDocument 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()

{

//调用:

//string constr = “User Id=scott;Password=tiger;Data Source=apollo”;

//    string sql = “select * from interstates where rownum<2”;

//    XmlDocument 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 ‘510113106%'”;

XmlDocument xmlDocument = GetSpatialData(constr, sql);

if (xmlDocument != null)

{


xmlDocument.Save(“c:\\v_tdlyxz_dltb_h_510113106.xml”);

Console.WriteLine(“DLTB 记录数:”+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 ‘510113106%'”;

xmlDocument = GetSpatialData(constr, sql);

if (xmlDocument != null)

{


xmlDocument.Save(“c:\\V_TDLYXZ_DLTB_H_YD_510113106.xml”);

Console.WriteLine(“DLTB_YD 记录数:” + 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 ‘510113%'”;

xmlDocument = GetSpatialData(constr, sql);

if (xmlDocument != null)

{


xmlDocument.Save(“c:\\V_TDLYGH_YTFQ_XZ_E_510113.xml”);

Console.WriteLine(“YTFQ 记录数:” + xmlDocument.ChildNodes.Count.ToString() + “个”);

}

Console.WriteLine((xmlDocument != null) ? xmlDocument.ChildNodes.Count.ToString() : “fail to read”);

}

//oo4o 访问Oracle spatial数据库的方法

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 ‘510113106%’ and  rownum<=4 “,0,ref snapshort) as OraDynaset;

OraFields orafds = dltb.Fields as OraFields;

int fdsNums = orafds.Count;

int recordNums = dltb.RecordCount;

Console.WriteLine(“输出字段列表:” + 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(“输出记录:” + 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 坐标集合

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—

转载于:https://www.cnblogs.com/sqlite3/archive/2012/06/28/2566673.html