You are here: > ESRI Forums > arcgis desktop discussion forums > Thread Replies

ArcGIS Desktop Discussion Forums

ArcGIS Desktop - ArcObjects General forum

Create a new shapefile and export it to a G...   Jiacheng Chiou Dec 08, 2002
Re: Create a new shapefile and export it to...   Olivier Damanet Dec 09, 2002
Re: Create a new shapefile and export it to...   Gavin OLeary Nov 08, 2004
Re: Create a new shapefile and export it to...   Charles Haynes Nov 08, 2004
Re: Create a new shapefile and export it to...   Gavin OLeary Nov 08, 2004
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Create a new shapefile and export it to a Geodatabase 
Author Jiacheng Chiou 
Date Dec 08, 2002 
Message How to create a new shapefile and specify its feature type and add a predefine coordinate system (file) to it? Then how to export this shapefile to a existing Geodatabase as a new feature class and specify it's new name? Thanks a lot. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Create a new shapefile and export it to a Geodatabase 
Author Olivier Damanet 
Date Dec 09, 2002 
Message The following sample shows you how to create a new shapefile with an unknow coordinate system: http://arconline.esri.com/arcobjectsonline/default.asp?URL=/ArcObjectsOnline/Samples/Geodatabase/Creating%20Data/CreateNewShapefile.htm
Use the function below to create a ISpatialReference from a .prj file.
To show how to export the shapefile to PGDB, I have adapted the 'Converting Shapefile to an ArcSDE Geodatabase' sample (see http://arconline.esri.com/arcobjectsonline/default.asp?URL=/ArcObjectsOnline/Samples/Geodatabase/Converting%20Data/ShapefileToSDE.htm ), and have pasted the code below (Sub LoadShps).
Good luck. 
 
Private Function GetSpatialReference(prjFile As String) As ISpatialReference
  Dim pSpatRefFact As ISpatialReferenceFactory 
  Set pSpatRefFact = New SpatialReferenceEnvironment 

  Set GetSpatialReference = pSpatRefFact.CreateESRISpatialReferenceFromPRJFile(prjFile) 
End Function

Sub LoadShps()
     
     ' +++ Create a new feature datset name object for the output feature dataset, call
     ' +++ it "USA"
     Dim pOutWorkspaceName As IWorkspaceName
     Set pOutWorkspaceName = New WorkspaceName
     pOutWorkspaceName.WorkspaceFactoryProgID = "esriCore.AccessWorkspaceFactory.1"
   
     Dim pOutFeatDSName As IFeatureDatasetName
     Set pOutFeatDSName = New FeatureDatasetName
     
     Dim pDSname As IDatasetName
     Set pDSname = pOutFeatDSName
     Set pDSname.WorkspaceName = pOutWorkspaceName
     pDSname.Name = "USA"

     ' +++ Get the name object for the input shapefile workspace
     Dim pInShpWorkspaceName As IWorkspaceName
     Set pInShpWorkspaceName = New WorkspaceName
     pInShpWorkspaceName.PathName = "C:\arcgis\arcexe82\ArcObjects Developer Kit\Samples\Data\Usa"
     pInShpWorkspaceName.WorkspaceFactoryProgID = "esriCore.ShapefileWorkspaceFactory.1"

     Dim pInShpFeatCLSNm As IFeatureClassName
     Set pInShpFeatCLSNm = New FeatureClassName
     Dim pShpDatasetName As IDatasetName
     Set pShpDatasetName = pInShpFeatCLSNm
     pShpDatasetName.Name = "counties"
     Set pShpDatasetName.WorkspaceName = pInShpWorkspaceName

     ' +++ create the new output FeatureClass name object that will be passed
     ' +++ into the conversion function
     Dim pOutputDSName As IDatasetName
     Dim pOutputFCName As IFeatureClassName
     Set pOutputFCName = New FeatureClassName
     Set pOutputDSName = pOutputFCName
     Dim pInDSNAme As IDatasetName
      
     ' +++ Set the new FeatureClass name to be the same as the input FeatureClass name
     Set pInDSNAme = pInShpFeatCLSNm
     pOutputDSName.Name = "counties"
     ' +++ Open the input Shapefile FeatureClass object, so that we can get its fields
     Dim pName As IName
     Dim pInShpFeatCls As IFeatureClass
     Set pName = pInShpFeatCLSNm
     Set pInShpFeatCls = pName.Open
      
     ' +++ Get the fields for the input feature class and run them through
     ' +++ field checker to make sure there are no illegal or duplicate field names
     Dim pOutFlds As IFields
     Dim pInShpFlds As IFields
     Dim pFldChk As IFieldChecker
     Dim i As Long
     Dim pGeoField As IField
     Dim pOutGeoDef As IGeometryDef
     Dim pOutGeoDefEdit As IGeometryDefEdit
     Set pInShpFlds = pInShpFeatCls.Fields
     Set pFldChk = New FieldChecker
     pFldChk.Validate pInShpFlds, Nothing, pOutFlds

     ' +++ Loop through the output fields to find the geometry field
     For i = 0 To pOutFlds.FieldCount
       If pOutFlds.Field(i).Type = esriFieldTypeGeometry Then
         Set pGeoField = pOutFlds.Field(i)
         Exit For
       End If
     Next i

     ' +++ Get the geometry field's geometry definition
     Set pOutGeoDef = pGeoField.GeometryDef

     ' +++ Give the geometry definition a spatial index grid count and grid size
     Set pOutGeoDefEdit = pOutGeoDef
     pOutGeoDefEdit.GridCount = 1
     pOutGeoDefEdit.GridSize(0) = 1500000
      
     ' +++ Now use IFeatureDataConverter::Convert to create the output FeatureDataset and
     ' +++ FeatureClass.
     Dim pShpToFc As IFeatureDataConverter
     Set pShpToFc = New FeatureDataConverter
     pShpToFc.ConvertFeatureClass pInShpFeatCLSNm, Nothing, pOutFeatDSName, _
                        pOutputFCName, Nothing, pOutFlds, "", 1000, 0
          
     MsgBox "Load operation complete!", vbInformation
End Sub
 
  Olivier. 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Create a new shapefile and export it to a Geodatabase 
Author Gavin OLeary 
Date Nov 08, 2004 
Message This code is very helpful, however if I try to run it more than once it crashed, assuming because it cannot over write the previous file with the same name. What is the best way in VBA to write over the existing file with a new file having the same name? Any hints would be great.
Thanks
Gavin 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Create a new shapefile and export it to a Geodatabase 
Author Charles Haynes 
Date Nov 08, 2004 
Message if you are using dot net then this works 
 
	string shapePath = System.IO.Path.GetDirectoryName(geoPath);
					string checkfile = shapePath + @"\" + "cutPolygona";
					if (File.Exists(checkfile + ".shp") == true)
					{
						string message = "This Cross Section profile already exist. Would you like to delete it?";
						string caption = "Cuttting Profile";
						MessageBoxButtons buttons = MessageBoxButtons.YesNo;
						DialogResult result;

						// Displays the MessageBox.

						result = MessageBox.Show(this, message, caption, buttons);

						if(result == DialogResult.Yes)
						{

							File.Delete(checkfile + ".shp");	
							File.Delete(checkfile + ".dbf");	
							File.Delete(checkfile + ".prj");	
							File.Delete(checkfile + ".shx");	

						}
						else
						{
						
						}
 
   
Report Inappropriate Content • Top • Print • This Forum is closed for replies.    
Subject Re: Create a new shapefile and export it to a Geodatabase 
Author Gavin OLeary 
Date Nov 08, 2004 
Message Thanks Charles, but I'm not much of a VB translator...but it has the concept right. I just can't figure out how to call the file name and either 'delete' or 'kill' the file then replace it with the new file same name.

Gavin