示例#1
0
        // Öppnar shapefil.
        public void AddShapefileUsingOpenFileDialog(ESRI.ArcGIS.Carto.IActiveView activeView)
        {
            //parameter check
            if (activeView == null)
            {
                return;
            }

            // Use the OpenFileDialog Class to choose which shapefile to load.
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            openFileDialog.InitialDirectory = @"H:\VT2019\GIS_App\Projekt\Program\Data\InData\Vektor";
            openFileDialog.Filter           = "Shapefiles (*.shp)|*.shp";
            openFileDialog.FilterIndex      = 2;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect      = false;


            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // The user chose a particular shapefile.

                // The returned string will be the full path, filename and file-extension for the chosen shapefile. Example: "C:\test\cities.shp"
                string shapefileLocation = openFileDialog.FileName;

                if (shapefileLocation != "")
                {
                    // Ensure the user chooses a shapefile

                    // Create a new ShapefileWorkspaceFactory CoClass to create a new workspace
                    ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();

                    // System.IO.Path.GetDirectoryName(shapefileLocation) returns the directory part of the string. Example: "C:\test\"
                    ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(System.IO.Path.GetDirectoryName(shapefileLocation), 0); // Explicit Cast

                    // System.IO.Path.GetFileNameWithoutExtension(shapefileLocation) returns the base filename (without extension). Example: "cities"
                    ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(System.IO.Path.GetFileNameWithoutExtension(shapefileLocation));

                    ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = new ESRI.ArcGIS.Carto.FeatureLayerClass();
                    featureLayer.FeatureClass = featureClass;
                    featureLayer.Name         = featureClass.AliasName;
                    featureLayer.Visible      = true;
                    activeView.FocusMap.AddLayer(featureLayer);

                    // Zoom the display to the full extent of all layers in the map
                    activeView.Extent = activeView.FullExtent;
                    activeView.PartialRefresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography, null, null);
                }
                else
                {
                }
            }
            else
            {
            }
        }
示例#2
0
        public static IFeatureClass ReadFeatureClass(string path)
        {
            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            // workspaceFactory.OpenFromFile,传入的参数是Shapefile文件所在的文件夹路径
            string pathwithoutname   = path.Substring(0, path.LastIndexOf("\\"));
            string namewithextention = path.Substring(path.LastIndexOf("\\") + 1);
            string name = namewithextention.Substring(0, namewithextention.LastIndexOf("."));

            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspaceFactory.OpenFromFile(pathwithoutname, 0);
            // OpenFeatureClass传入的参数是shape文件的文件名,不带后缀
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(name);
            return(featureClass);
        }
示例#3
0
        /// <summary>
        /// 获得一个shapefile文件的的要素类对象
        /// </summary>
        /// <params name="sFilePath"></params>
        /// <returns></returns>
        public static IFeatureClass GetShapefileWorkspaceFeatureClass(string sFilePath)
        {
            try
            {
                IWorkspaceFactory pWSF = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();

                string     sPath = System.IO.Path.GetDirectoryName(sFilePath);
                IWorkspace pWS   = pWSF.OpenFromFile(sPath, 0);

                string sFileName = System.IO.Path.GetFileNameWithoutExtension(sFilePath);

                IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;

                IFeatureClass pFC = pFWS.OpenFeatureClass(sFileName);

                return(pFC);
            }
            catch (Exception ex) { return(null); }
        }
示例#4
0
        /// <summary>
        /// Get the FeatureClass from a Shapefile on disk (hard drive).
        /// </summary>
        /// <param name="string_ShapefileDirectory">A System.String that is the directory where the shapefile is located. Example: "C:\data\USA"</param>
        /// <param name="string_ShapefileName">A System.String that is the shapefile name. Note: the shapefile extension's (.shp, .shx, .dbf, etc.) is not provided! Example: "States"</param>
        /// <returns>An IFeatureClass interface. Nothing (VB.NET) or null (C#) is returned if unsuccessful.</returns>
        /// <remarks></remarks>
        public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassFromShapefileOnDisk(System.String string_ShapefileDirectory, System.String string_ShapefileName)
        {
            System.IO.DirectoryInfo directoryInfo_check = null;
            try
            {
                directoryInfo_check = new System.IO.DirectoryInfo(string_ShapefileDirectory);
                if (directoryInfo_check.Exists)
                {
                    //We have a valid directory, proceed

                    System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(string_ShapefileDirectory + "\\" + string_ShapefileName + ".shp");
                    if (fileInfo_check.Exists)
                    {
                        //We have a valid shapefile, proceed

                        ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                        ESRI.ArcGIS.Geodatabase.IWorkspace        workspace        = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0);
                        ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
                        ESRI.ArcGIS.Geodatabase.IFeatureClass     featureClass     = featureWorkspace.OpenFeatureClass(string_ShapefileName);

                        return(featureClass);
                    }
                    else
                    {
                        //Not valid shapefile
                        return(null);
                    }
                }
                else
                {
                    // Not valid directory
                    return(null);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                directoryInfo_check = null;
            }
        }
示例#5
0
        public static IFeatureWorkspace getWorkSpace()
        {
            // 运行时环境的绑定
            //ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);

            // AO的许可级别
            IAoInitialize     aoInitialize  = new AoInitializeClass();
            esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeStandard);

            if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
            {
                Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus);
                return(null);
            }

            //string path = System.AppDomain.CurrentDomain.BaseDirectory + "..\\MapFiles";
            string path = System.Configuration.ConfigurationSettings.AppSettings["GisPath"].ToString();

            IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(path, 0);

            return(featureWorkspace);
        }
示例#6
0
        public static IFeature FindNextStartingFeature(IGeometry RegionGeometry)
        {
            List <DistanceFeatureClass> DistFeatureList = new List <DistanceFeatureClass>();
            IEnumLayer        pEnumLayer = Form1.pMap.Layers;
            ILayer            pLayer;
            IFeatureSelection pFeatureSelection = null;

            IWorkspaceFactory workspaceFactory       = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            IWorkspace        workspace              = workspaceFactory.OpenFromFile(System.IO.Path.GetTempPath(), 0);
            IFeatureWorkspace featureWorkspace       = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
            IFeatureClass     featureClass           = featureWorkspace.OpenFeatureClass("Temp_ConcaveHull");
            IFeatureCursor    AllRegionFeatureCursor = featureClass.Search(null, false);
            IFeature          AllRegionFeature       = AllRegionFeatureCursor.NextFeature();
            IGeometry         AllRegionGeometry      = AllRegionFeature.Shape;

            IPolygon            polygon           = (IPolygon)AllRegionGeometry;
            IGeometryCollection pGeomColl         = (IGeometryCollection)polygon;
            IGeometryCollection AllRegionPolyline = new PolylineClass();
            ISegmentCollection  PsegPath          = null;

            object obj = Type.Missing;

            for (int k = 0; k < pGeomColl.GeometryCount; k++)
            {
                PsegPath = new ESRI.ArcGIS.Geometry.PathClass();
                PsegPath.AddSegmentCollection((ISegmentCollection)pGeomColl.get_Geometry(k));
                AllRegionPolyline.AddGeometry((IGeometry)PsegPath, ref obj, ref obj);
            }

            IPolyline pPolyline = (IPolyline)AllRegionPolyline;

            //MessageBox.Show("..." + pPolyline.Length, "hi");
            //polygon.

            for (int i = 0; i < Form1.pMap.LayerCount; i++)
            {
                pLayer = pEnumLayer.Next();


                if (pLayer.Name.Equals(Form1.OperationLayer))
                {
                    IFeatureLayer pFeatureLayer = (IFeatureLayer)pLayer;
                    pFeatureSelection = (IFeatureSelection)pFeatureLayer;
                }
            }


            ISpatialFilter pSpatialFilter = new SpatialFilter();

            pSpatialFilter.GeometryField = "SHAPE";
            pSpatialFilter.SpatialRel    = esriSpatialRelEnum.esriSpatialRelIntersects;
            pSpatialFilter.WhereClause   = "SpltGropus = 0";

            ITopologicalOperator pTopologicalOperator = (ITopologicalOperator)RegionGeometry;
            IPolygon             pPolygon             = (IPolygon)pTopologicalOperator.Buffer(5);

            pSpatialFilter.Geometry = pPolygon;

            pFeatureSelection.SelectFeatures(pSpatialFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);

            ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet;

            IQueryFilter pQueryFilter = new QueryFilter();

            pQueryFilter.WhereClause = "SpltGropus = 0";
            ICursor pCursor;

            pSelectionSet.Search(pQueryFilter, false, out pCursor);
            IFeatureCursor pFeatureCursor = (IFeatureCursor)pCursor;

            IFeature pFeature = pFeatureCursor.NextFeature();


            //sorting the feature according to the distance of the RerionGeometry

            while (pFeature != null)
            {
                IProximityOperator pProxOper = (IProximityOperator)pFeature.Shape;
                double             distance  = pProxOper.ReturnDistance(RegionGeometry);

                DistanceFeatureClass DistFeature = new DistanceFeatureClass(distance, pFeature);
                DistFeatureList.Add(DistFeature);

                pFeature = pFeatureCursor.NextFeature();
            }


            DistFeatureList = DistFeatureList.OrderBy(a => a.getDistance()).ToList();

            IRelationalOperator AllRegionFeatureGeom = (IRelationalOperator)pPolyline;
            IFeature            ReturnFeature        = null;

            for (int j = 0; j < DistFeatureList.Count; j++)
            {
                //IRelationalOperator RegionFeatureGeom = (IRelationalOperator)DistFeatureList[j].getFeature().Shape;
                //MessageBox.Show("..." + AllRegionFeatureGeom.Touches(DistFeatureList[j].getFeature().Shape), "hi");
                if (AllRegionFeatureGeom.Touches(DistFeatureList[j].getFeature().Shape) == true)
                {
                    ReturnFeature = DistFeatureList[j].getFeature();
                    break;
                }
            }
            //MessageBox.Show("..." + ReturnFeature.OID, "hi");
            return(ReturnFeature);
        }
示例#7
0
        /// <summary>
        /// 获得一个shapefile文件的的要素类对象
        /// </summary>
        /// <params name="sFilePath"></params>
        /// <returns></returns>
        public static IFeatureClass GetShapefileWorkspaceFeatureClass(string sFilePath)
        {
            try
            {
                IWorkspaceFactory pWSF = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();

                string sPath = System.IO.Path.GetDirectoryName(sFilePath);
                IWorkspace pWS = pWSF.OpenFromFile(sPath, 0);

                string sFileName = System.IO.Path.GetFileNameWithoutExtension(sFilePath);

                IFeatureWorkspace pFWS = pWS as IFeatureWorkspace;

                IFeatureClass pFC = pFWS.OpenFeatureClass(sFileName);

                return pFC;
            }
            catch (Exception ex) { return null; }
        }
示例#8
0
 /// <summary>
 /// 复制导入要素(ConvertFeatureDataset)
 /// </summary>
 /// <param name="pDataSet"></param>
 /// <param name="strFeatFileDir"></param>
 /// <param name="strFeatFileName"></param>
 /// <param name="strOutName"></param>
 /// <param name="isWorkspace"></param>
 public void FeatureClassToFeatureClass(IDataset pDataSet, string strFeatFileDir, string strFeatFileName, string strOutName, bool isWorkspace)
 {
     try
     {
         IWorkspaceFactory pWSF            = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
         IFeatureWorkspace pFeatureWK      = (IFeatureWorkspace)pWSF.OpenFromFile(strFeatFileDir, 0);
         IFeatureClass     pInFeatureClass = pFeatureWK.OpenFeatureClass(strFeatFileName);
         if (pInFeatureClass == null || pDataSet == null)
         {
             MessageBox.Show("创建失败");
             return;
         }
         IFeatureClassName pInFeatureclassName;
         IDataset          pIndataset = (IDataset)pInFeatureClass;
         pInFeatureclassName = (IFeatureClassName)pIndataset.FullName;
         //如果名称已存在
         IWorkspace2 pWS2 = null;
         if (isWorkspace)
         {
             pWS2 = pDataSet as IWorkspace2;
         }
         else
         {
             pWS2 = pDataSet.Workspace as IWorkspace2;
         }
         if (pWS2.get_NameExists(esriDatasetType.esriDTFeatureClass, strOutName))
         {
             MessageBoxResult result = MessageBox.Show(null, "矢量文件名  " + strOutName + "  在数据库中已存在!" + "/r是否覆盖?", "相同文件名", MessageBoxButton.YesNo, MessageBoxImage.Information, MessageBoxResult.Yes);
             //覆盖原矢量要素
             if (result == MessageBoxResult.Yes)
             {
                 IFeatureWorkspace pFWS     = (IFeatureWorkspace)pWS2;
                 IDataset          pDataset = pFWS.OpenFeatureClass(strOutName) as IDataset;
                 pDataset.Delete();
                 pDataset = null;
             }
         }
         IFields         pInFields, pOutFields;
         IFieldChecker   pFieldChecker = new FieldCheckerClass();
         IEnumFieldError pError;
         pInFields = pInFeatureClass.Fields;
         pFieldChecker.Validate(pInFields, out pError, out pOutFields);
         IField geoField = null;
         for (int i = 0; i < pOutFields.FieldCount; i++)
         {
             IField pField = pOutFields.get_Field(i);
             if (pField.Type == esriFieldType.esriFieldTypeOID)
             {
                 IFieldEdit pFieldEdit = (IFieldEdit)pField;
                 pFieldEdit.Name_2 = pField.AliasName;
             }
             if (pField.Type == esriFieldType.esriFieldTypeGeometry)
             {
                 geoField = pField;
                 break;
             }
         }
         IGeometryDef          geometryDef          = geoField.GeometryDef;
         IFeatureDataConverter one2another          = new FeatureDataConverterClass();
         IFeatureClassName     pOutFeatureClassName = new FeatureClassNameClass();
         IDatasetName          pOutDatasetName      = (IDatasetName)pOutFeatureClassName;
         if (isWorkspace)
         {
             pOutDatasetName.WorkspaceName = (IWorkspaceName)pDataSet.FullName;
         }
         else
         {
             pOutDatasetName.WorkspaceName = (IWorkspaceName)((IDataset)pDataSet.Workspace).FullName;
         }
         pOutDatasetName.Name = strOutName;
         if (isWorkspace)
         {
             one2another.ConvertFeatureClass(pInFeatureclassName, null, null, pOutFeatureClassName, geometryDef, pOutFields, "", 1000, 0);
         }
         else
         {
             IFeatureDataset     pFeatDS        = (IFeatureDataset)pDataSet;
             IFeatureDatasetName pOutFeatDSName = pFeatDS.FullName as IFeatureDatasetName;
             one2another.ConvertFeatureClass(pInFeatureclassName, null, pOutFeatDSName, pOutFeatureClassName, geometryDef, pOutFields, "", 1000, 0);
             pOutFeatDSName = null;
             pFeatDS        = null;
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
         return;
     }
 }
示例#9
0
        public static IPolygon GetPolyGonFromFile(string path)
        {
            IPolygon pGon = null;

            if (path.EndsWith(".mdb"))
            {
                string            errmsg       = "";
                IWorkspaceFactory pwf          = new ESRI.ArcGIS.DataSourcesGDB.AccessWorkspaceFactoryClass();
                IWorkspace        pworkspace   = pwf.OpenFromFile(path, 0);
                IEnumDataset      pEnumdataset = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
                pEnumdataset.Reset();
                IDataset pDataset = pEnumdataset.Next();
                while (pDataset != null)
                {
                    IFeatureClass pFeatureclass = pDataset as IFeatureClass;
                    if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        pDataset = pEnumdataset.Next();
                        continue;
                    }
                    else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon)
                    {
                        IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                        IFeature       pFeature = pCursor.NextFeature();
                        if (pFeature != null)
                        {
                            pGon = pFeature.Shape as IPolygon;
                            break;
                        }
                        else
                        {
                            pDataset = pEnumdataset.Next();
                            continue;
                        }
                    }
                    else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline)
                    {
                        IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                        IFeature       pFeature = pCursor.NextFeature();
                        if (pFeature != null)
                        {
                            IPolyline pPolyline = pFeature.Shape as IPolyline;
                            pGon = GetPolygonFormLine(pPolyline);
                            if (pGon.IsClosed == false)
                            {
                                errmsg   = "选择的要素不能构成封闭多边形!";
                                pGon     = null;
                                pDataset = pEnumdataset.Next();
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                if (pGon == null)
                {
                    IEnumDataset pEnumdataset1 = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                    pEnumdataset1.Reset();
                    pDataset = pEnumdataset1.Next();
                    while (pDataset != null)
                    {
                        IFeatureDataset pFeatureDataset = pDataset as IFeatureDataset;
                        IEnumDataset    pEnumDataset2   = pFeatureDataset.Subsets;
                        pEnumDataset2.Reset();
                        IDataset pDataset1 = pEnumDataset2.Next();
                        while (pDataset1 != null)
                        {
                            if (pDataset1 is IFeatureClass)
                            {
                                IFeatureClass pFeatureclass = pDataset1 as IFeatureClass;
                                if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline)
                                {
                                    pDataset1 = pEnumDataset2.Next();
                                    continue;
                                }
                                else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon)
                                {
                                    IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                                    IFeature       pFeature = pCursor.NextFeature();
                                    if (pFeature != null)
                                    {
                                        pGon = pFeature.Shape as IPolygon;
                                        break;
                                    }
                                    else
                                    {
                                        pDataset1 = pEnumDataset2.Next();
                                        continue;
                                    }
                                }
                                else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline)
                                {
                                    IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                                    IFeature       pFeature = pCursor.NextFeature();
                                    if (pFeature != null)
                                    {
                                        IPolyline pPolyline = pFeature.Shape as IPolyline;
                                        pGon = GetPolygonFormLine(pPolyline);
                                        if (pGon.IsClosed == false)
                                        {
                                            errmsg    = "选择的要素不能构成封闭多边形!";
                                            pGon      = null;
                                            pDataset1 = pEnumDataset2.Next();
                                            continue;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (pGon != null)
                        {
                            break;
                        }
                        pDataset = pEnumdataset1.Next();
                    }
                }
                if (pGon == null)
                {
                    if (errmsg != "")
                    {
                        System.Windows.Forms.MessageBox.Show(errmsg, "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("请选择一个包含面要素和线要素的文件", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    }
                    return(pGon);
                }
            }
            else if (path.EndsWith(".shp"))
            {
                IWorkspaceFactory pwf               = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                string            filepath          = System.IO.Path.GetDirectoryName(path);
                string            filename          = path.Substring(path.LastIndexOf("\\") + 1);
                IFeatureWorkspace pFeatureworkspace = (IFeatureWorkspace)pwf.OpenFromFile(filepath, 0);
                IFeatureClass     pFeatureclass     = pFeatureworkspace.OpenFeatureClass(filename);
                if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon)
                {
                    IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                    IFeature       pFeature = pCursor.NextFeature();
                    if (pFeature != null)
                    {
                        pGon = pFeature.Shape as IPolygon;
                    }
                }
                else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline)
                {
                    IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                    IFeature       pFeature = pCursor.NextFeature();
                    if (pFeature != null)
                    {
                        IPolyline pPolyline = pFeature.Shape as IPolyline;
                        pGon = GetPolygonFormLine(pPolyline);
                        if (pGon.IsClosed == false)
                        {
                            System.Windows.Forms.MessageBox.Show("选择的线要素不能构成封闭多边形!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
                            return(null);
                        }
                    }
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("请选择一个面或者线要素文件!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    return(null);
                }
            }
            else if (path.EndsWith(".txt"))
            {
                string txtpath = path;
                System.IO.StreamReader smRead = new System.IO.StreamReader(txtpath, System.Text.Encoding.Default); //设置路径
                string line;

                IPointCollection pc = pGon as IPointCollection;
                double           x, y;
                while ((line = smRead.ReadLine()) != null)
                {
                    if (line.IndexOf(",") > 0)
                    {
                        try
                        {
                            x = double.Parse(line.Substring(0, line.IndexOf(",")));
                            y = double.Parse(line.Substring(line.IndexOf(",") + 1));
                        }
                        catch
                        {
                            System.Windows.Forms.MessageBox.Show("文本文件格式不正确!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
                            smRead.Close();
                            return(null);
                        }
                        IPoint tmpPoint = new ESRI.ArcGIS.Geometry.Point();
                        tmpPoint.X = x;
                        tmpPoint.Y = y;
                        object ep = System.Reflection.Missing.Value;

                        pc.AddPoint(tmpPoint, ref ep, ref ep);
                    }
                }
                smRead.Close();
                ICurve pCurve = pGon as ICurve;
                if (pCurve.IsClosed == false)
                {
                    System.Windows.Forms.MessageBox.Show("导入点坐标不能构成封闭多边形!", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation);
                    return(null);
                }
            }
            else if (path.EndsWith("gdb"))
            {
                string            errmsg       = "";
                IWorkspaceFactory pwf          = new ESRI.ArcGIS.DataSourcesGDB.FileGDBWorkspaceFactoryClass();
                IWorkspace        pworkspace   = pwf.OpenFromFile(path.Substring(0, path.LastIndexOf("\\")), 0);
                IEnumDataset      pEnumdataset = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureClass);
                pEnumdataset.Reset();
                IDataset pDataset = pEnumdataset.Next();
                while (pDataset != null)
                {
                    IFeatureClass pFeatureclass = pDataset as IFeatureClass;
                    if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline)
                    {
                        pDataset = pEnumdataset.Next();
                        continue;
                    }
                    else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon)
                    {
                        IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                        IFeature       pFeature = pCursor.NextFeature();
                        if (pFeature != null)
                        {
                            pGon = pFeature.Shape as IPolygon;
                            break;
                        }
                        else
                        {
                            pDataset = pEnumdataset.Next();
                            continue;
                        }
                    }
                    else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline)
                    {
                        IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                        IFeature       pFeature = pCursor.NextFeature();
                        if (pFeature != null)
                        {
                            IPolyline pPolyline = pFeature.Shape as IPolyline;
                            pGon = GetPolygonFormLine(pPolyline);
                            if (pGon.IsClosed == false)
                            {
                                errmsg   = "选择的要素不能构成封闭多边形!";
                                pGon     = null;
                                pDataset = pEnumdataset.Next();
                                continue;
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
                if (pGon == null)
                {
                    IEnumDataset pEnumdataset1 = pworkspace.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                    pEnumdataset1.Reset();
                    pDataset = pEnumdataset1.Next();
                    while (pDataset != null)
                    {
                        IFeatureDataset pFeatureDataset = pDataset as IFeatureDataset;
                        IEnumDataset    pEnumDataset2   = pFeatureDataset.Subsets;
                        pEnumDataset2.Reset();
                        IDataset pDataset1 = pEnumDataset2.Next();
                        while (pDataset1 != null)
                        {
                            if (pDataset1 is IFeatureClass)
                            {
                                IFeatureClass pFeatureclass = pDataset1 as IFeatureClass;
                                if (pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolygon && pFeatureclass.ShapeType != esriGeometryType.esriGeometryPolyline)
                                {
                                    pDataset1 = pEnumDataset2.Next();
                                    continue;
                                }
                                else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolygon)
                                {
                                    IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                                    IFeature       pFeature = pCursor.NextFeature();
                                    if (pFeature != null)
                                    {
                                        pGon = pFeature.Shape as IPolygon;
                                        break;
                                    }
                                    else
                                    {
                                        pDataset1 = pEnumDataset2.Next();
                                        continue;
                                    }
                                }
                                else if (pFeatureclass.ShapeType == esriGeometryType.esriGeometryPolyline)
                                {
                                    IFeatureCursor pCursor  = pFeatureclass.Search(null, false);
                                    IFeature       pFeature = pCursor.NextFeature();
                                    if (pFeature != null)
                                    {
                                        IPolyline pPolyline = pFeature.Shape as IPolyline;
                                        pGon = GetPolygonFormLine(pPolyline);
                                        if (pGon.IsClosed == false)
                                        {
                                            errmsg    = "选择的要素不能构成封闭多边形!";
                                            pGon      = null;
                                            pDataset1 = pEnumDataset2.Next();
                                            continue;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                        if (pGon != null)
                        {
                            break;
                        }
                        pDataset = pEnumdataset1.Next();
                    }
                }
                if (pGon == null)
                {
                    if (errmsg != "")
                    {
                        System.Windows.Forms.MessageBox.Show(errmsg, "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show("请选择一个包含面要素和线要素的文件", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
                    }
                    return(pGon);
                }
            }
            return(pGon);
        }
示例#10
0
        private void AddShapesFromDirToFocusMap(string dir, string grouplayername)
        {
            // Start tool for download from WFS service
            try
            {
                IWorkspaceFactory workspaceFactoryShape = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactoryShape.OpenFromFile(dir, 0);

                IGroupLayer groupLayer = new GroupLayerClass();
                groupLayer.Name = grouplayername;
                IEnumDatasetName enumDatasetName = ((IWorkspace)featureWorkspace).get_DatasetNames(esriDatasetType.esriDTFeatureClass);
                IDatasetName featureDatasetName = (IDatasetName)enumDatasetName.Next();
                while (featureDatasetName != null)
                {
                    IFeatureLayer featureLayer = new FeatureLayerClass();
                    featureLayer.FeatureClass = featureWorkspace.OpenFeatureClass(featureDatasetName.Name);
                    featureLayer.Name = featureDatasetName.Name;
                    groupLayer.Add(featureLayer);
                    featureDatasetName = (IDatasetName)enumDatasetName.Next();
                }
                IMxDocument mxDoc = (IMxDocument)m_application.Document;
                IMap map = (IMap)mxDoc.FocusMap;
                IActiveView activeView = (IActiveView)map;
                map.AddLayer(groupLayer);
                mxDoc.UpdateContents();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
示例#11
0
    /// <summary>
    /// Get the FeatureClass from a Shapefile on disk (hard drive).
    /// </summary>
    /// <param name="string_ShapefileDirectory">A System.String that is the directory where the shapefile is located. Example: "C:\data\USA"</param>
    /// <param name="string_ShapefileName">A System.String that is the shapefile name. Note: the shapefile extension's (.shp, .shx, .dbf, etc.) is not provided! Example: "States"</param>
    /// <returns>An IFeatureClass interface. Nothing (VB.NET) or null (C#) is returned if unsuccessful.</returns>
    /// <remarks></remarks>
    public ESRI.ArcGIS.Geodatabase.IFeatureClass GetFeatureClassFromShapefileOnDisk(System.String string_ShapefileDirectory, System.String string_ShapefileName)
    {

      System.IO.DirectoryInfo directoryInfo_check = null;
      try
      {
        directoryInfo_check = new System.IO.DirectoryInfo(string_ShapefileDirectory);
        if (directoryInfo_check.Exists)
        {

          //We have a valid directory, proceed

          System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(string_ShapefileDirectory + "\\" + string_ShapefileName + ".shp");
          if (fileInfo_check.Exists)
          {

            //We have a valid shapefile, proceed

            ESRI.ArcGIS.Geodatabase.IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            ESRI.ArcGIS.Geodatabase.IWorkspace workspace = workspaceFactory.OpenFromFile(string_ShapefileDirectory, 0);
            ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(string_ShapefileName);

            return featureClass;
          }
          else
          {
            //Not valid shapefile
            return null;
          }

        }
        else
        {
          // Not valid directory
          return null;
        }
      }
      catch (Exception ex)
      {
        throw ex;
      }
      finally
      {
        directoryInfo_check = null;
      }
      

    }