Convert from text file to shape file

9666
2
08-04-2013 12:54 AM
ababreddy
New Contributor
Hi, can anybody help me to write a C# program which converts text file(it is having X,Y coordinates) to shape file using arcobjects
0 Kudos
2 Replies
BarbaraSchneider1
New Contributor III
You could do the following:
- First, you create a layer based on your xy data: http://help.arcgis.com/en/sdk/10.0/vba_desktop/conceptualhelp/index.html#//00010000010r000000
- Then, you export your layer to a shapefile: http://support.esri.com/en/knowledgebase/techarticles/detail/28109

Does this help?
0 Kudos
FreddieGibson
Occasional Contributor III
The programmatic workflow for this would be the same as when using ArcGIS Desktop.  You'll want to 1) Convert the text file to an XY Event Layer and 2) export the Event Layer to a valid feature class format.  The hardest part will be part one, as such I've written a quick example of how I'm able to accomplish this with a text file.

The below code will show you how to create the XY event layer and get a reference to IFeatureClass.  After doing this you'll want to decide on if you want to use pure ArcObjects or the Geoprocessing framework to export the feature class to your needed format.  For example, you could use IFeatureDataConvertor or you use use Geoprocessing tools like FeatureClassToFeatureClass_conversion or CopyFeatures_management.  I have pasted links that discuss this workflows below the code.

private static void Main()
{
    ISpatialReference pSpatialReference = CreateSpatialReference(esriSRGeoCSType.esriSRGeoCS_WGS1984);
    ITable pTable = OpenTextTable(TxtFile);
    IFeatureClass pFeatureClass = CreateXyEventFeature(pTable, "LONGITUDE_X", "LATITUDE_Y", pSpatialReference);
}

private static ITable OpenTextTable(string txtPath)
{
    string tablePath = System.IO.Path.GetDirectoryName(txtPath);
    string tableName = System.IO.Path.GetFileName(txtPath);

    IWorkspaceFactory pWorkspaceFactory = new TextFileWorkspaceFactoryClass();
    IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace) pWorkspaceFactory.OpenFromFile(tablePath, 0);
    return pFeatureWorkspace.OpenTable(tableName);
}

private static IFeatureClass CreateXyEventFeature(ITable table, String xField, String yField, ISpatialReference spatialReference)
{
    IXYEvent2FieldsProperties pXyEvent2FieldsProperties = new XYEvent2FieldsPropertiesClass()
    {
        XFieldName = xField,
        YFieldName = yField
    };

    IName pName = ((IDataset) table).FullName;
    IXYEventSourceName pXyEventSourceName = new XYEventSourceNameClass()
    {
        EventProperties = pXyEvent2FieldsProperties,
        EventTableName = pName,
        SpatialReference = spatialReference
    };

    pName = (IName) pXyEventSourceName;
    IXYEventSource pXyEventSource = (IXYEventSource) pName.Open();

    IFeatureClass pFeatureClass = (IFeatureClass) pXyEventSource;
    return pFeatureClass;
}

private static ISpatialReference CreateSpatialReference(esriSRGeoCSType coordSystem)
{
    ISpatialReferenceFactory pSpatialReferenceFactory = new SpatialReferenceEnvironmentClass();
    ISpatialReferenceResolution pSpatialReferenceResolution =
        pSpatialReferenceFactory.CreateGeographicCoordinateSystem(Convert.ToInt32(coordSystem)) as ISpatialReferenceResolution;
    pSpatialReferenceResolution.ConstructFromHorizon();
    ISpatialReferenceTolerance pSpatialReferenceTolerance =
        pSpatialReferenceResolution as ISpatialReferenceTolerance;
    pSpatialReferenceTolerance.SetDefaultXYTolerance();
    ISpatialReference pSpatialReference = pSpatialReferenceResolution as ISpatialReference;
    return pSpatialReference;
}


*** Additional Resources ***
Converting simple data
http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#//00010000028w000000

How to run a geoprocessing tool
http://resources.arcgis.com/en/help/arcobjects-net/conceptualhelp/index.html#//0001000003rr000000
0 Kudos