Using C# code, convert the XYEvent layer to a shapefile. Add the created shapefile to the map.

I have already created the XY event layer with the following code:

public void XYEvent()

    IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
    IMap map = mxdoc.FocusMap;

    // Get the table named XYSample.txt
    IStandaloneTableCollection stTableCollection = map as IStandaloneTableCollection;
    IStandaloneTable standaloneTable = null;
    ITable table = null;

    //Error check to make sure the table has been added before the event is created
    for (int i = 0; i < stTableCollection.StandaloneTableCount; i++)
    standaloneTable = stTableCollection.StandaloneTable[i];
    if (standaloneTable.Name == "ONT_Stations")
    table = standaloneTable.Table;

    if (table == null)
    MessageBox.Show("ONT_Stations table was not found in this map.");

    // Get the table name object
    IDataset dataset = table as IDataset;
    IName tableName = dataset.FullName;

    // Specify the X and Y fields
    IXYEvent2FieldsProperties xyEvent2FieldsProperties = new XYEvent2FieldsProperties() as IXYEvent2FieldsProperties;
    xyEvent2FieldsProperties.XFieldName = "LATITUDE_DECIMAL_DEGREES";
    xyEvent2FieldsProperties.YFieldName = "LONGITUDE_DECIMAL_DEGREES";
    xyEvent2FieldsProperties.ZFieldName = "ELEVATION";

    // Specify the projection
    //Projection found at: http://edndoc.esri.com/arcobjects/9.1/ComponentHelp/esriGeometry/esriSRGeoCSType.htm
    ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment() as ISpatialReferenceFactory;
    IGeographicCoordinateSystem geographicCoordinateSystem = spatialReferenceFactory.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_NAD1983);

    // Create the XY name object as set it's properties
    IXYEventSourceName xyEventSourceName = new XYEventSourceName() as IXYEventSourceName;
    xyEventSourceName.EventProperties = xyEvent2FieldsProperties;
    xyEventSourceName.SpatialReference = geographicCoordinateSystem;
    xyEventSourceName.EventTableName = tableName;
    IName xyName = xyEventSourceName as IName;
    IXYEventSource xyEventSource = xyName.Open() as IXYEventSource;
    IFeatureLayer featureLayer = new FeatureLayer() as IFeatureLayer;
    featureLayer.FeatureClass = xyEventSource as IFeatureClass;
    featureLayer.Name = "ONT_Stations_Events";

    //Add layer to map          

1 Answer 1


Here's something I put together to show how to create a shapefile (just the basics) and iterate through a feature class writing to an output as you go:

void NewShapefile(string FullName, ISpatialReference SR, ILayer FromLayer )
    System.IO.FileInfo pFInfo = new System.IO.FileInfo(FullName);
    IWorkspaceFactory pWSfact = new ShapefileWorkspaceFactoryClass();
    IWorkspace pWS = pWSfact.OpenFromFile(pFInfo.DirectoryName, 0); // open the folder as a 'workspace'
    IFeatureWorkspace pFWS = (IFeatureWorkspace)pWS;// make that a feature workspace
    IFeatureLayer pFtLayer = (IFeatureLayer)FromLayer;// layer as feature layer

    IFields pFields = new FieldsClass();
    IFieldsEdit pFieldsEdit = (IFieldsEdit)pFields;

    // create the FID field
    IField pField = new FieldClass();
    IFieldEdit pFieldEdit = (IFieldEdit)pField;
    pFieldEdit.Name_2 = "FID";
    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;

    // create the Shape field
    pField = new FieldClass();
    pFieldEdit = (IFieldEdit)pField;
    pFieldEdit.Name_2 = "Shape";
    pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
    // geometry def: point, spatial reference
    IGeometryDef pGeomDef = new GeometryDefClass();
    IGeometryDefEdit pGeomDefEd = (IGeometryDefEdit)pGeomDef;
    pGeomDefEd.GeometryType_2 = esriGeometryType.esriGeometryPoint;
    pGeomDefEd.SpatialReference_2 = SR;
    pFieldEdit.GeometryDef_2 = pGeomDef;

    // add more fields here if you like

    // create the feature class
    IFeatureClass OutFC =  pFWS.CreateFeatureClass(pFInfo.Name, pFields, null, null, esriFeatureType.esriFTSimple, "Shape", "");

    // get ready to start writing the new features
    IFeatureBuffer pFtBuff = OutFC.CreateFeatureBuffer();
    IFeatureCursor pOutCur = OutFC.Insert(true);
    IFeatureCursor pFromCur = pFtLayer.FeatureClass.Search(null, true); // get a cursor on the 'from' features, the xy event layer
    IFeature fromFt = pFromCur.NextFeature();
    // loop through all the features and put them in the out feature class
    while (fromFt != null)
        pFtBuff.Shape = fromFt.ShapeCopy;

        // use pFtBuff.set_Value(index,value) to set the values of any new fields
        fromFt = pFromCur.NextFeature();
    pOutCur.Flush(); // write pending edits..

but compare that to using the IGeoprocessor interface:

        IGeoProcessor pGP = new GeoProcessorClass();
        IVariantArray gpParams = new VarArrayClass();
        gpParams.Add("ONT_Stations_Events"); // 'from features'
        pGP.Execute("CopyFeatures_management", gpParams, null);

Yes, you get more control over doing it manually but if all you want to do is write to a shapefile, whatever is there, then I'd do it in 5 lines with the IGeoProcessor.

  • Neither of these methods worked, but that's possibly because I wasn't clear enough in my question. In one class I have created the XYEvent() class and called it in a windows form which is activated through a button. In a separate class I have created the CreateShapefile() class which is called through a separate button (no form, only button) which when clicked would get the in memory reference to the TOC where the event layer is, take that layer and save it to file, and then delete the event layer and table from the TOC only leaving the shapefile.
    – gisgirl
    Commented Mar 23, 2015 at 18:49
  • NEVERMIND!!! The IGeoProcessor is golden, I just had a little mistake in it that wasn't allowing it to work. Setting the environments in the map document you are using also allows you to control the output space. Thank you so much!!!!!
    – gisgirl
    Commented Mar 23, 2015 at 19:05
  • You're welcome. I provided both sets of code to show the difference between the methods; the long way is more flexible, you can add new fields or skip fields you don't want, project the points to a different spatial reference or optionally skip points that don't fall within an extent.. there's so many reasons why you would consider doing it the long way, so it's worth at least a reference. If this has ultimately helped please accept the question by clicking on the tick on the top left of the answer to show future readers that this helped. Commented Mar 23, 2015 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.