I have a street layer and I only need to export a subset of features to a new shapefile 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 convert 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;
}
Answer
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);
...
}
No comments:
Post a Comment