Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have a street layer and I only need to export a subset of features to a new shape file where the "TestDone" attribute field is not '0' (Zero).

My current code ( which is pasted below ) exports the total street layer.

I can use a query filter to get the non zero values out and assign it to an ICursor. But then not sure how to conenter code herevert it to a IfeatureClass. ( Not really sure whether the ICursor and then trying to convert to IFeatureClass is the correct way to go even )

How can I just get features that have a non zero value in the "TestDone" attribute field and then convert that to a feature layer as the second argument in the ExportFeatureClass method ( please see below code) needs a IFeatureClass.

Any help with some code guidance will be greatly appreciated.

*CODE ***

private void btnExport_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Shape file|*.shp";
saveFileDialog1.DefaultExt = "shp";
saveFileDialog1.AddExtension = true;
saveFileDialog1.InitialDirectory = @"C:/";
saveFileDialog1.Title = "Check data";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
string strFile = System .IO.Path.GetFileName ( saveFileDialog1.FileName) ;
IMap pMap = default(IMap);
IWorkspaceFactory pWorkspaceFactory = default(IWorkspaceFactory);
IWorkspace pWorkspace = default(IWorkspace);
IFileName pFileName = default(IFileName);
IFeatureLayer2 pFeatureLayer = default(IFeatureLayer2);
IFeatureClass pFeatureClass = default(IFeatureClass);
IPropertySet pPropertySet = default(IPropertySet);
IGeometryDef pGeometryDef = default(IGeometryDef);
IGeometryDefEdit pGeometryDefEdit = default(IGeometryDefEdit);
string deletefile = null;

pMap = clsFunctions.pMxDocument.FocusMap;
pFileName = new FileNameClass();
pFileName.Path = System .IO.Path.GetDirectoryName (saveFileDialog1.FileName);
pPropertySet = new PropertySet();
pPropertySet.SetProperty("DATABASE", pFileName.Path);
pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFact ory();
pWorkspace = pWorkspaceFactory.Open(pPropertySet, 0);

IGeographicCoordinateSystem pGCS = default(IGeographicCoordinateSystem);
SpatialReferenceEnvironment pSpatialReferenceEnv = default(SpatialReferenceEnvironment);
ISpatialReference2 pSpatialReference = default(ISpatialReference2);
pSpatialReferenceEnv = new SpatialReferenceEnvironment();
pGCS = pSpatialReferenceEnv.CreateGeographicCoordinateSys tem(Convert.ToInt16(esriSRGeoCSType.esriSRGeoCS_WG S1984));
pSpatialReference = (ISpatialReference2)pGCS;

ILayer pLayer;
int intI = 0;

    for (intI = 0; intI <= pMap.LayerCount - 1; intI++)
    {
    pLayer = pMap.get_Layer(intI);
    if (pLayer.Name == cmbStreets.Text)
    {
    pFeatureLayer = (IFeatureLayer2)pLayer;
    }
    }

pFeatureClass = pFeatureLayer.FeatureClass;
pGeometryDef = new GeometryDef();
pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef;
pGeometryDefEdit.GeometryType_2 = pFeatureClass.ShapeType;
pGeometryDefEdit.SpatialReference_2 = pSpatialReference;

ExportFeatureClass(strFile, pFeatureClass, pWorkspace, pSpatialReference, pGeometryDef);
}



public void ExportFeatureClass(string strLayerName, IFeatureClass pFeatureClass, IWorkspace pOutWorkspace, ISpatialReference pSpatialReference, IGeometryDef pGeometryDef)
{
IFeatureClassName pInFeatureClassName = default(IFeatureClassName);
IDataset pDataset = default(IDataset);
IWorkspaceName pOutWorkspaceName = default(IWorkspaceName);
IFeatureClassName pOutFeatureClassName = default(IFeatureClassName);
IDatasetName pDatasetName = default(IDatasetName);
IExportOperation pExportOperation = default(IExportOperation);
IWorkspace pInWorkspace = default(IWorkspace);

pDataset = (IDataset)pFeatureClass;
pInFeatureClassName = (IFeatureClassName)pDataset.FullName;
pInWorkspace = pDataset.Workspace;
pDataset = (IDataset)pOutWorkspace;
pOutWorkspaceName = (IWorkspaceName)pDataset.FullName;

pOutFeatureClassName = new FeatureClassNameClass();
pDatasetName = (IDatasetName)pOutFeatureClassName;
pDatasetName.Name = strLayerName;
pDatasetName.WorkspaceName = pOutWorkspaceName;

pExportOperation = new ExportOperation();
pExportOperation.ExportFeatureClass((IDatasetName) pInFeatureClassName, null, null, pGeometryDef, pOutFeatureClassName, 0);

//Cleanup
pInFeatureClassName = null;
pDataset = null;
pOutWorkspaceName = null;
pOutFeatureClassName = null;
pDatasetName = null;
pExportOperation = null;
pInWorkspace = null;
}
share|improve this question
1  
You should reformulate this to a question. –  MathiasWestin Dec 17 '10 at 9:16
1  
What is the deal with default() all over the place? default() is a C# operator introduced in C# 2.0 and it returns null for reference types. –  Petr Krebs Dec 17 '10 at 11:16
add comment

2 Answers 2

up vote 3 down vote accepted

You are using the IExportOperation interface, it allows you to specify the query filter when exporting data so that you do not need to deal with cursors yourself.

You can modify your code by first creating the query filter:

var queryFilter = new QueryFilterClass();
queryFilter.SubFields = "*";
queryFilter.WhereClause = "NOT TestDone=0";

Then adding the query filter as a parameter to your export function and using it in your IExportOperation.ExportFeatureClass call:

public void ExportFeatureClass(string strLayerName, IFeatureClass pFeatureClass, IWorkspace pOutWorkspace, ISpatialReference pSpatialReference, IGeometryDef pGeometryDef, IQueryFilter pQueryFilter)
{
    ...
    pExportOperation.ExportFeatureClass((IDatasetName)pInFeatureClassName, pQueryFilter, null, pGeometryDef, pOutFeatureClassName, 0);
    ...
}
share|improve this answer
    
You saved my day. Just what the doctor ordered. Works great. Thanks a lot –  user1473 Dec 17 '10 at 20:17
add comment

Well something like this should do it:

Dim featureLayer As IFeatureLayer = CType(layer, IFeatureLayer)
Dim queryFilter As IQueryFilter = New QueryFilter()
queryFilter.SubFields = "*"
queryFilter.WhereClause = "TestDone<>0"

Dim featCur As IFeatureCursor = featureLayer.Search(queryFilter, False)

See the SQL reference

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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