2

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:

enter image description here

3
+50

First note that IExportOperation interface doesn't export featureclasses or tables that containt a BloB field. So if a blob field exists in your featurelayer you should use "FeatureClassToFeatureClass" instead.

In your question you mentioned you want to export each row to a shapefile but in your export path you set a path to a File Geodatabase!

Here are some bugs with your code:

You must use

IDatasetName dsName = new FeatureClassNameClass();

instead of this

IDatasetName dsName = new FeatureDatasetNameClass();

and it is best to instantiate your objects outside of your loop.

Another problem with your code (main problem) is that you are missing to set the workspaceName of your output feature class:

dsName.Name = nameOfLGA;
dsName.WorkspaceName = pWorkspaceName;

I modified your code and now it works fine:

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);

                            IWorkspaceName pWorkspaceName = new WorkspaceNameClass();
                            pWorkspaceName.PathName = "C:\\Users\\user\\Documents\\ArcGIS\\Default.gdb";
                            pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.FileGDBWorkspaceFactory";

                            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;

                            IFeature lga = lgaCursor.NextFeature();
                            while (lga != null)
                            {
                                string nameOfLGA = lga.Value[lgaNameFieldIndex].ToString();

                                IDatasetName dsName = new FeatureClassNameClass();

                                dsName.Name = nameOfLGA;
                                dsName.WorkspaceName = pWorkspaceName;
                                IFeatureClassName outfcName = dsName as IFeatureClassName; 

                                IQueryFilter qFilt = new QueryFilterClass();
                                qFilt.WhereClause = "name = '" + nameOfLGA + "'"; 

                                IExportOperation exportOp = new ExportOperationClass();
                                exportOp.ExportFeatureClass(pDatasetName, qFilt, null, geoDef, outfcName, 0);

                                lga = lgaCursor.NextFeature();
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n\n" + ex.Source + "\n\n" + ex.StackTrace);
            } 

Just make sure that the data in your lga_name field is unique because then you will have an already exported feature class replaced with a new one with the same name

  • Could you tell me what a blob field is? Also, I am actually using a feature class, I was initially using a shape file but converted it to a FC. I mustn't have updated the earlier paragraph. Thank you for your help and for pointing that out. – TheLastGIS Feb 22 '15 at 10:49
  • blob stands for binary of large objects. it's a type of field that you can define in your Feature Class to hold any binary object for example Image,Video,Document,... – Farid Cheraghi Feb 22 '15 at 17:23
3

In your code you have:

pWorkspaceName.PathName = "C:\\Users\\username\\Documents\\ArcGIS\\Default.gdb";
pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";

As you have defined the path name to be a file geodatabase then the workspacefactory should be:

pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.FileGDBWorkspaceFactory";
  • Good point. I actually picked up on this after I posted the code. I did change the PathName property to "C:\\Users\\username\\Documents\\ArcGIS". Is there something I need to change to the WorkspaceFactoryProgID property? Note: I will change the code to reflect my changes now. – TheLastGIS Feb 17 '15 at 1:54
  • OK a closer look at your code shows you are not using the IExportOperation correctly, for example you are specifying the GeometryDef parameter, you have set it to null. Have a look at this Q&A. – Hornbydd Feb 17 '15 at 10:06
  • I have amended the code with your suggestions (my interpretation of them at least) and now I have a new error. Also it was my understanding that these parameters could be left as null. – TheLastGIS Feb 18 '15 at 7:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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