码迷,mamicode.com
首页 > 其他好文 > 详细

SDE与shapefile之间的数据导入与导出

时间:2014-08-20 12:22:12      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   文件   数据   ar   2014   

一、SDE要素导出到shapefile中。

 1、创建一个新的shapefile文件。

 private bool CreateShapefile(string filepath, string name)
        {
            bool isSuccssed = false;
            try
            {
                IFeatureWorkspace pFWS = null;
                IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
                pFWS = pWSF.OpenFromFile(filepath, 0) as IFeatureWorkspace;

                IFields pFields;
                IFieldsEdit pFieldsEdit;
                pFields = new FieldsClass();
                pFieldsEdit = pFields as IFieldsEdit;
                IField pField;
                IFieldEdit pFieldEdit;
                pField = new FieldClass();
                pFieldEdit = pField as IFieldEdit;
                pFieldEdit.Name_2 = "Shape";
                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
                IGeometryDef pGeomDef;
                IGeometryDefEdit pGeomDefEdit;
                pGeomDef = new GeometryDefClass();
                pGeomDefEdit = pGeomDef as IGeometryDefEdit;
                pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
                pGeomDefEdit.SpatialReference_2 = new UnknownCoordinateSystemClass();
                pFieldEdit.GeometryDef_2 = pGeomDef;
                pFieldsEdit.AddField(pField);
                IFeatureClass pFeatureClass;
                pFeatureClass = pFWS.CreateFeatureClass(name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
                isSuccssed = true;
            }
            catch
            {
                isSuccssed = false;
            }
            return isSuccssed;
        }

2、从SDE中读取要素,得到Geometry图形。

private IWorkspace GetSdeWorkspace(string Server, string Instance, string database, string user, string password, string version)
        {
            IWorkspace pWS = null;
            IWorkspaceFactory pWSF = null;
            IPropertySet pPropertySet = new PropertySet();
            //sde数据库连接的各个参数
            pPropertySet.SetProperty("Server", Server);
            pPropertySet.SetProperty("Instance", Instance);
            pPropertySet.SetProperty("Database", database);
            pPropertySet.SetProperty("user", user);
            pPropertySet.SetProperty("password", password);
            pPropertySet.SetProperty("version", version);
            try
            {
                pWSF = new SdeWorkspaceFactory();
                pWS = pWSF.Open(pPropertySet, 0);
            }
            catch
            {
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pPropertySet);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(pWSF);
                pPropertySet = null;
                pWSF = null;
            }
            return pWS;
        }
private List<IGeometry> GetGeometryFromSde(string pFeaClassName,string objectid)
        {
            List<IGeometry> pGeometryList = new List<IGeometry>();
            IWorkspace pWS = null;
            IFeatureWorkspace pFWS = null;
            IFeatureCursor pFeatCursor = null;
            IFeature pFeature = null;
            IFeatureClass pFeatClss = null;
            IQueryFilter pQueryFilter =new QueryFilterClass();
            pWS = GetSdeWorkspace("172.30.242.185", "5151", "server185_SDE", "sde", "hiway2014", "DEFAULT");
            try
            {
                pFWS = pWS as IFeatureWorkspace;
                pFeatClss = pFWS.OpenFeatureClass(pFeaClassName);
                pQueryFilter.WhereClause = "OBJECTID=" + objectid;
                IFeatureCursor featureCursor = pFeatClss.Search(pQueryFilter, false);
                while ((pFeature = featureCursor.NextFeature()) != null)
                {
                    if (pFeature.Shape.GeometryType == esriGeometryType.esriGeometryPolygon)
                    {
                        pGeometryList.Add(pFeature.ShapeCopy);
                    }
                }
            }
            catch
            {
            }
            finally
            {
                if (pFeatCursor != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatCursor);
                    pFeatCursor = null;
                }
                if (pWS != null)
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pWS);
                }
            }
            return pGeometryList;
        }

3、将sde中获得的要素,加到shapefile中。

 private void AddFeature2Shapefile(IGeometry geometry, string filepath, string name)
        {
            IFeatureWorkspace pFWS = null;
            IFeatureClass pFeaCls = null;
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            pFWS = pWSF.OpenFromFile(filepath, 0) as IFeatureWorkspace;
            pFeaCls = pFWS.OpenFeatureClass(name);
            IDataset dataset = pFeaCls as IDataset;
            IWorkspace workspace = dataset.Workspace;
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();
            IFeatureBuffer featureBuffer = pFeaCls.CreateFeatureBuffer();
            IFeatureCursor featureCursor = pFeaCls.Insert(true);
            featureBuffer.Shape = geometry;
            featureCursor.InsertFeature(featureBuffer);
            featureCursor.Flush();
            Marshal.ReleaseComObject(featureCursor);
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);
        }

二、将一个shapefile中的要素加到另一个shapefile中方法与上面类似。只是获得的要素源不同。

 private IGeometry GetGeometryFromShapefile(string filepath, string name)
        {
            IGeometry pGeometry = null;
            IFeatureWorkspace pFWS = null;
            IFeatureClass pFeaCls = null;
            IFeature pFeature = null;
            IWorkspaceFactory pWSF = new ShapefileWorkspaceFactoryClass();
            pFWS = pWSF.OpenFromFile(filepath, 0) as IFeatureWorkspace;
            pFeaCls = pFWS.OpenFeatureClass(name);
            IFeatureCursor featureCursor = pFeaCls.Search(null, false);
            while ((pFeature = featureCursor.NextFeature()) != null)
            {
                pGeometry = pFeature.ShapeCopy;
            }
            return pGeometry;
        }

 

SDE与shapefile之间的数据导入与导出,布布扣,bubuko.com

SDE与shapefile之间的数据导入与导出

标签:style   blog   color   io   文件   数据   ar   2014   

原文地址:http://www.cnblogs.com/aegisada/p/3924226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!