Tuesday, January 13, 2015

Create standalone feature class and feature class inside feature dataset on Geodatabase using ArcObjects

 public void StartUp()   
    {   
     try   
     {   
       //create gdb   
      const string path = "D:/SampleDatasets/NewShp";   
      const string fileGDBName = "sample.gdb";   
      const string fileGDBAddress = path + "/" + fileGDBName;   
      const string featureDatasetname="polygonFeatureClasses";   
      const string featureClassname = "MytestPolygons";   
      if (System.IO.Directory.Exists(fileGDBAddress))   
      {   
       Console.WriteLine("already exist");   
       // return;   
      }   
      Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");   
      IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);   
      IWorkspaceName workspaceName = workspaceFactory.Create(path, fileGDBName, null,ArcMap.Application.hWnd);   
      MessageBox.Show("gbd created!");   
      
      //Create feature dataset   
      IFeatureWorkspace fws = workspaceFactory.OpenFromFile(fileGDBAddress, ArcMap.Application.hWnd) as IFeatureWorkspace;   
      
     //check the existance of FeatureDataset   
      IWorkspace2 ws = fws as IWorkspace2;   
      if(ws.get_NameExists(esriDatasetType.esriDTFeatureDataset,featureDatasetname)==true)   
      {   
       MessageBox.Show("Feature dataset is already exists");   
      }  

      #region feature Class inside feature Dataset : Region_1 (Choose either Region_1 or Region_2)  
     
      //Create a spatial refrence/extract spatial refrence from map   
      //Infuture will access through the arcmap   
      ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();   
      int coordinateSystemID = (int) esriSRGeoCSType.esriSRGeoCS_WGS1984;   
      ISpatialReference spatialReference =spatialReferenceFactory.CreateGeographicCoordinateSystem(coordinateSystemID);   
      
     //Create featuredataset   
      IFeatureDataset fds = fws.CreateFeatureDataset(featureDatasetname, spatialReference);   
           
      //Create featureClass   
      IFieldsEdit fields = new FieldsClass();   
      fields.FieldCount_2 = 3;   
            IFieldEdit field= new FieldClass();   
      field.Name_2 = "ObjectID";   
      field.Type_2=esriFieldType.esriFieldTypeOID;   
      fields.Field_2[0] = field;   
          
      IGeometryDefEdit geometryDef= new GeometryDefClass();   
      geometryDef.GeometryType_2=esriGeometryType.esriGeometryPolygon;   
      geometryDef.SpatialReference_2 = spatialReference;   
      field= new FieldClass();   
            
      field.Name_2 = "Shape";   
      field.Type_2 = esriFieldType.esriFieldTypeGeometry;   
      field.GeometryDef_2 = geometryDef;   
      fields.Field_2[1] = field;   
            
      field = new FieldClass();   
      field.Name_2 = "Name_Test";   
      field.Type_2 = esriFieldType.esriFieldTypeString;   
      fields.Field_2[2] = field;   
      
      fds.CreateFeatureClass(featureClassname+"2", fields, null, null, esriFeatureType.esriFTSimple, "shape", null);    
            
     #endregion feature Class inside feature class  
            
    #region standalone featureClass : Region_2  
            /*   
      IFieldsEdit fields = new FieldsClass();   
      IFieldEdit field = new FieldClass();   
      field.Name_2 = "ObjectID";   
      field.Type_2 = esriFieldType.esriFieldTypeOID;   
      fields.AddField(field);   
            
      // Create a geometry definition (and spatial reference) for the feature class.   
      IGeometryDefEdit geometryDefEdit = new GeometryDefClass();   
      geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;   
      ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironmentClass();   
      int coordinateSystemID = (int)esriSRGeoCSType.esriSRGeoCS_WGS1984;   
      ISpatialReference spatialReference = spatialReferenceFactory.CreateGeographicCoordinateSystem(coordinateSystemID);   
      geometryDefEdit.SpatialReference_2 = spatialReference;   
            
      field = new FieldClass();   
      field.Name_2 = "Shape";   
      field.Type_2 = esriFieldType.esriFieldTypeGeometry;   
      field.GeometryDef_2 = geometryDefEdit as IGeometryDef;   
      fields.AddField(field);   
          
      field = new FieldClass();   
      field.Name_2 = "Name";   
      field.Type_2 = esriFieldType.esriFieldTypeString;   
      fields.AddField(field);   
            
      fws.CreateFeatureClass(featureClassname + "2", fields, null, null, esriFeatureType.esriFTSimple, "shape", null);   
           
      */   
            
      #endregion standalone featureClass  
     }    
     catch(Exception e)   
     {   
      MessageBox.Show(e.Message);   
     }   
    }   

ArcMap , arcobject

0 comments :

Post a Comment

 

© 2011 GIS and Remote Sensing Tools, Tips and more .. ToS | Privacy Policy | Sitemap

About Me