I'm trying to export each row in a shapefile, but I get an error I haven't seen before (see below). I feel like it might be the labyrinth of interfaces that I have to cast between that may be causing the error. I must be getting close though because I get an export feature class progress bar (see below) which then gets overridden by the error.
Code:
try
{
IMxDocument mxdoc = ArcMap.Application.Document as IMxDocument;
IMap map = mxdoc.FocusMap;
for (int i = 0; i < map.LayerCount; i++)
{
ILayer layer = map.Layer[i];
if (layer.Name == "lga_polygon")
{
if (layer is IFeatureLayer)
{
IFeatureLayer flayer = layer as IFeatureLayer;
IFeatureClass fc = flayer.FeatureClass;
IDataset pDataset = flayer as IDataset;
IDatasetName pDatasetName = pDataset.FullName as IDatasetName;
string lgaName = "LGA_NAME";
IFields fields = fc.Fields;
int lgaNameFieldIndex = fields.FindField(lgaName);
IField lgaNameField = fields.Field[lgaNameFieldIndex];
IFeatureCursor lgaCursor = fc.Search(null, true);
IFeature lga = lgaCursor.NextFeature();
while (lga != null)
{
string nameOfLGA = lga.Value[lgaNameFieldIndex].ToString();
IDatasetName dsName = new FeatureDatasetNameClass();
dsName.Name = nameOfLGA;
IFeatureClassName fcName = dsName as IFeatureClassName;
IWorkspaceName pWorkspaceName = new WorkspaceNameClass();
pWorkspaceName.PathName = "C:\\Users\\username\\Documents\\ArcGIS\\Default.gdb";
pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.FileGDBWorkspaceFactory";
IQueryFilter qFilt = new QueryFilterClass();
qFilt.WhereClause = "LGA_NAME = '" + nameOfLGA + "'";
IGeographicCoordinateSystem pGCS = new GeographicCoordinateSystemClass();
SpatialReferenceEnvironment spatRefEnv = new SpatialReferenceEnvironmentClass();
ISpatialReference spatRef;
pGCS = spatRefEnv.CreateGeographicCoordinateSystem(Convert.ToInt16(esriSRGeoCSType.esriSRGeoCS_GDA1994));
spatRef = pGCS as ISpatialReference;
IGeometryDef geoDef = new GeometryDefClass();
IGeometryDefEdit geoDefEdit = geoDef as IGeometryDefEdit;
geoDefEdit.SpatialReference_2 = spatRef;
geoDefEdit.GeometryType_2 = fc.ShapeType;
IGeometryDef geoDef2 = geoDefEdit as IGeometryDef;
IExportOperation exportOp = new ExportOperationClass();
exportOp.ExportFeatureClass(pDatasetName, qFilt, null, geoDef2, fcName, ArcMap.Application.hWnd);
lga = lgaCursor.NextFeature();
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" + ex.Source + "\n\n" + ex.StackTrace);
}
Error:
