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---

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

  1. 在微服务架构下基于 Prometheus 构建一体化监控平台的最佳实践

    欢迎关注方志朋的博客,回复"666"获面试宝典 随着 Prometheus 逐渐成为云原生时代的可观测事实标准,那么今天为大家带来在微服务架构下基于 Prometheus 构建一体 ...

  2. Linux下基于密钥的安全验证实现方法

    Linux下基于密钥的安全验证实现方法 -------OpenSSH+WinSCP+putty密钥生成器+putty 实验背景: 小诺公司目前已使用Linux搭建了各个服务器(FTP.DNS.Apac ...

  3. 在windows下基于visual studio2017和CMake的安装Google glog

    这里简单记录安装google glog在windows下基于visual studio2017的安装过程. 下载 https://github.com/google/glog 使用cmake编译,编译 ...

  4. Ubuntu下基于 Cilium CNI 的 Kubernetus集群环境搭建

    Ubuntu下基于 Cilium CNI 的 Kubernetus集群环境搭建 1. 前言 2. 安装三个Ubuntu 2.1 三个机器都关闭防火墙 2.2 三个机器都关闭swap 2.3 三个机器都 ...

  5. 科学计算机撤销,云计算环境下基于属性的撤销方案-计算机科学.PDF

    云计算环境下基于属性的撤销方案-计算机科学.PDF 第 卷 第 期 计 算 机 科 学 45 8 Vol.45No.8 年 月 2018 8 COMPUTER SCIENCE Au.2018 g 云计 ...

  6. 深度学习核心技术精讲100篇(五十一)-Spark平台下基于LDA的k-means算法实现

    本文主要在Spark平台下实现一个机器学习应用,该应用主要涉及LDA主题模型以及K-means聚类.通过本文你可以了解到: 文本挖掘的基本流程 LDA主题模型算法 K-means算法 Spark平台下 ...

  7. iis 7 php_Windows server 2008 下基于IIS7配置php7.2运行环境

    在windows环境下搭建php开发环境也有很多方法,比如apache,nginx,IIS 都可以,或者图方便集成套件安装非常简单.但是如果这台机器还有其他的需要,比如要同时运行asp和asp.net ...

  8. linux 下基于jrtplib库的实时传送实现

    linux 下基于jrtplib库的实时传送实现 一.RTP 是进行实时流媒体传输的标准协议和关键技术 实时传输协议(Real-time Transport Protocol,PRT)是在 Inter ...

  9. C++:Windows环境下基于Eclipse配置C/C++开发环境

    C++:Windows环境下基于Eclipse配置C/C++开发环境 目录 Windows下的MinGW下载.安装和配置 1.MinGW下载 2.MinGW安装与配置 3.基于Eclipse配置 Wi ...

最新文章

  1. TensorFlow基础10-(误差反向传播算法以及实现多层神经网络)
  2. 服务器中使用Com组建处理Excel 常见问题
  3. 问题分析探讨 -- 大约有700W数据的表,把当天的10W数据select导入新表,整个原来的表就锁死...
  4. Matlab:成功解决引用了已清除的变量 handles
  5. 项目: 用C语言写一个图形化的音乐播放器 【C++ / C】
  6. Ubuntu16.04 安装 docker
  7. Css中实现一个盒子固定宽度,另一个盒子宽度自适应的方法
  8. 求链表是否有环,及环入口,环长度
  9. 安卓智能手机运行iFIX组态软件
  10. 一款很漂亮的一天只弹窗一次的公告
  11. 【转】对程序中常出现的EINT、DINT、ERTM、DRTM的理解
  12. JQuery监听页面滚动总结
  13. android打鸭子代码,打鸭子安卓版
  14. 微信小程序开发日记1
  15. 计算机信息管理 日语,2017年北京科技大学高职计算机信息管理(第一外语:日语)...
  16. matlab实现手绘风格(简笔画风格、漫画风格)的曲线绘图
  17. 从软件工程师到IT猎头续:告诉你如何写简历
  18. 常用密码加密md5值,123456,admin,admin888
  19. 强制页面运行于IE8模式下
  20. SSH连接远程服务器自动断开解决方案

热门文章

  1. Laravel dcat-admin 用户头像显示不出的原因及解决方法
  2. php 按键连击,写了一个独立按键 支持组合键、单键长按,连发功能的例子
  3. 让群晖“文本编辑器”支持更多文件扩展名
  4. RPC-BDY(2)-注册多个服务
  5. 自助式分析是数据组织的一种状态
  6. eclipse jee配置tomcat
  7. 「角」毫米波雷达前装增速放缓?哪些供应商位居TOP10
  8. feign 服务启动后加载 避免第一次调用超时 及第一次调用耗时长
  9. 智能建筑计算机网络系统设计的主要内容及遵循的原则,小区智能化系统设计设计原则及功能需求...
  10. 求解Python 爬取百度翻译手机版{errno:997,from:zh,to:en,query:\u4eba\u751f\u82e6\u77ed}怎么办