Пример #1
0
        private IFeatureClass getShapeFile(string inDir, string infile)
        {
            System.IO.DirectoryInfo directoryInfo_check = new System.IO.DirectoryInfo(inDir);
            if (directoryInfo_check.Exists)
            {
                //We have a valid directory, proceed

                System.IO.FileInfo fileInfo_check = new System.IO.FileInfo(inDir + "\\" + infile + ".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(inDir, 0);
                    ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explict Cast
                    ESRI.ArcGIS.Geodatabase.IFeatureClass     featureClass     = featureWorkspace.OpenFeatureClass(infile);

                    return(featureClass);
                }
                else
                {
                    //Not valid shapefile
                    return(null);
                }
            }
            else
            {
                // Not valid directory
                return(null);
            }
        }
Пример #2
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 = 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);
            }
        }
Пример #3
0
        //裁剪图层要素   打开  按钮
        private void button2_Click(object sender, EventArgs e)
        {
            // Use the OpenFileDialog Class to choose which shapefile to load.
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            //openFileDialog.InitialDirectory = "c:\\";
            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;
                txtClipsFile.Text = shapefileLocation;

                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
                {
                    // The user did not choose a shapefile.
                    // Do whatever remedial actions as necessary
                    System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1",
                                                         System.Windows.Forms.MessageBoxButtons.OK,
                                                         System.Windows.Forms.MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                // The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button.
                // Do whatever remedial actions as necessary.
                System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2",
                                                     System.Windows.Forms.MessageBoxButtons.OK,
                                                     System.Windows.Forms.MessageBoxIcon.Exclamation);
            }
        }
        public IFeatureClass CreatePolygonFile(String strFolder, String filename, IEnvelope pEnvBorder, ISpatialReference pSR)
        {
            // Open the folder to contain the shapefile as a workspace
            IWorkspaceFactory pWF = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace pfws = (IFeatureWorkspace)pWF.OpenFromFile(strFolder, 0);

            //Set up a simple fields collection
            IField pField = new FieldClass();
            IFieldEdit pFieldEdit = (IFieldEdit)pField;
            IFields pFields = new FieldsClass();
            IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

            // Make the shape field
            //it will need a geometry definition, with a spatial reference

            pFieldEdit.Name_2 = "Shape";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;

            IGeometryDef pGeomDef = new GeometryDef();
            IGeometryDefEdit pGeomDefEdit = (IGeometryDefEdit)pGeomDef;

            pGeomDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
            pGeomDefEdit.SpatialReference_2 = pSR;

            pFieldEdit.GeometryDef_2 = pGeomDef;
            pFieldsEdit.AddField(pField);

            // Add ID field
            IField pFieldOID = new Field();
            pFieldEdit = (IFieldEdit)pFieldOID;
            pFieldEdit.Name_2 = "OBJECTID";
            pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;
            pFieldEdit.IsNullable_2 = false;

            pFieldsEdit.AddField(pFieldOID);

            //EulerPoleReFrmgwn eulerfrm=new EulerPoleReFrmgwn();

            if (File.Exists(strFolder + filename + ".shp") == true)
            {
                DialogResult result = MessageBox.Show("There is a shapefile have the same name in this foler, do you want to overwrite it?", "", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    File.Delete(strFolder + filename);

                }
                else
                {
                    return null;
                }
            }

            IFeatureClass pFeatclass = pfws.CreateFeatureClass(filename, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
            return pFeatclass;
        }
Пример #5
0
        protected override void OnClick()
        {
            try
            {
                string shpFilePath = string.Format(@"C:\temp\contour datas");
                string shpFileName = "contour2.shp"; // height data

                IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                IWorkspace workspace = workspaceFactory.OpenFromFile(shpFilePath, 0);
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
                IFeatureClass pFeatureClass = featureWorkspace.OpenFeatureClass(shpFileName);
                IFeatureLayer pFeatureLayer = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureClass;
                IQueryFilter pQueryFilter = new QueryFilterClass();
                pQueryFilter.WhereClause = "";

                IEnvelope pEnv = pFeatureLayer.AreaOfInterest;
                IGeoDataset pGDS = (IGeoDataset)pFeatureLayer.FeatureClass;
                ISpatialReference pSR = pGDS.SpatialReference;
                IGeometry pGeom = pEnv;
                pGeom.Project(pSR);
                ITinEdit pTinEdit = new TinClass();
                pTinEdit.InitNew(pEnv);
                IFields pFields = pFeatureClass.Fields;
                IField pFiled = pFields.get_Field(pFields.FindField("HSL"));
                object Missing = Type.Missing;
                esriTinSurfaceType pTinSurface = esriTinSurfaceType.esriTinHardLine;
                pTinEdit.AddFromFeatureClass(pFeatureClass, pQueryFilter, pFiled, pFiled, pTinSurface, ref Missing);
                string outputFolderPath = @"C:\temp\Output\";
                string tinFolderName = "tin_data";
                pTinEdit.SaveAs(outputFolderPath + tinFolderName, ref Missing);
                MessageBox.Show("end");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            ArcMap.Application.CurrentTool = null;
        }
Пример #6
0
        protected override void OnClick()
        {
            try
            {
                string shpFilePath = string.Format(@"C:\temp\contour datas");
                string shpFileName = "contour2.shp"; // height data

                IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
                IWorkspace        workspace        = workspaceFactory.OpenFromFile(shpFilePath, 0);
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
                IFeatureClass     pFeatureClass    = featureWorkspace.OpenFeatureClass(shpFileName);
                IFeatureLayer     pFeatureLayer    = new FeatureLayerClass();
                pFeatureLayer.FeatureClass = pFeatureClass;
                IQueryFilter pQueryFilter = new QueryFilterClass();
                pQueryFilter.WhereClause = "";

                IEnvelope         pEnv  = pFeatureLayer.AreaOfInterest;
                IGeoDataset       pGDS  = (IGeoDataset)pFeatureLayer.FeatureClass;
                ISpatialReference pSR   = pGDS.SpatialReference;
                IGeometry         pGeom = pEnv;
                pGeom.Project(pSR);
                ITinEdit pTinEdit = new TinClass();
                pTinEdit.InitNew(pEnv);
                IFields            pFields     = pFeatureClass.Fields;
                IField             pFiled      = pFields.get_Field(pFields.FindField("HSL"));
                object             Missing     = Type.Missing;
                esriTinSurfaceType pTinSurface = esriTinSurfaceType.esriTinHardLine;
                pTinEdit.AddFromFeatureClass(pFeatureClass, pQueryFilter, pFiled, pFiled, pTinSurface, ref Missing);
                string outputFolderPath = @"C:\temp\Output\";
                string tinFolderName    = "tin_data";
                pTinEdit.SaveAs(outputFolderPath + tinFolderName, ref Missing);
                MessageBox.Show("end");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            ArcMap.Application.CurrentTool = null;
        }
Пример #7
0
        /// <summary>Create a ESRI shapefile </summary>
        /// <param name="shapeFilePath">the path to the shapefile</param>
        /// <param name="field2add">List of Ifields</param>
        /// <param name="srs">the srs of the shapefile</param>
        /// <param name="geomType">the type geometry: point, polyline, polygon, ...</param>
        /// <param name="deleteIfExists">Overwrite existing FeatureClass</param>
        /// <returns>the shapefile loaded in a IFeatureClass</returns>
        public static IFeatureClass createShapeFile(string shapeFilePath, List<IField> field2add , 
                                  ISpatialReference srs, esriGeometryType geomType, bool deleteIfExists = true)
        {
            FileInfo shapeInfo = new FileInfo(shapeFilePath);

            // Instantiate a feature class description to get the required fields.
            IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();
            IObjectClassDescription ocDescription = fcDescription as IObjectClassDescription;

            IFields fields = ocDescription.RequiredFields;
            IFieldsEdit fieldsEdit = fields as IFieldsEdit;

            // Find the shape field in the required fields and modify its GeometryDef to
            // use wanted geometry and to set the spatial reference.
            int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);
            IField geofield = fields.get_Field(shapeFieldIndex);
            IGeometryDef geometryDef = geofield.GeometryDef;
            IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
            geometryDefEdit.GeometryType_2 = geomType;
            geometryDefEdit.SpatialReference_2 = srs;

            foreach (IField field in field2add)
            {
                fieldsEdit.AddField(field);
            }

            IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFactoryClass();
            IWorkspace workspace = workspaceFactory.OpenFromFile( shapeInfo.DirectoryName , 0);
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspace; // Explict Cast

            if ( shapeInfo.Exists )
            {
                if (deleteIfExists)
                {
                    if (!deleteFeatureClass(shapeInfo.FullName))
                    {
                        throw new Exception(shapeInfo.Name + " exists and cannot be deleted");
                    }
                }
                else
                {
                    throw new Exception(shapeInfo.Name + " exists");
                }
            }

            IFields validatedFields = validateFields((IWorkspace)workspace, fields);

            IFeatureClass featureClass = featureWorkspace.CreateFeatureClass(shapeInfo.Name, validatedFields,
                                               ocDescription.InstanceCLSID,  ocDescription.ClassExtensionCLSID,
                                               esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, "");

            return featureClass;
        }
Пример #8
0
        //�ü�ͼ��Ҫ��   ��  ��ť
        private void button2_Click(object sender, EventArgs e)
        {
            // Use the OpenFileDialog Class to choose which shapefile to load.
            System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
            //openFileDialog.InitialDirectory = "c:\\";
            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;
                txtClipsFile.Text = shapefileLocation;

                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
                {
                    // The user did not choose a shapefile.
                    // Do whatever remedial actions as necessary
                    System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #1",
                                                        System.Windows.Forms.MessageBoxButtons.OK,
                                                        System.Windows.Forms.MessageBoxIcon.Exclamation);
                }
            }
            else
            {
                // The user did not choose a shapefile. They clicked Cancel or closed the dialog by the "X" button.
                // Do whatever remedial actions as necessary.
                System.Windows.Forms.MessageBox.Show("No shapefile chosen", "No Choice #2",
                                                     System.Windows.Forms.MessageBoxButtons.OK,
                                                     System.Windows.Forms.MessageBoxIcon.Exclamation);
            }
        }