方法一, 图层对图层裁剪,输出图层
ILayer pLayer;
IFeatureLayer pFeatureLayer;
IFeatureClass pFeatureClass;
IWorkspaceName pNewWSName;
IBasicGeoprocessor pBasicGeop;
IFeatureClassName pFeatureClassName;
IDatasetName pDatasetName;
IFeatureClass pOutputFeatureClass;
IFeatureLayer pOutputFeatureLayer;
ITable pinputTable, pclipTable;
//定义输出图层的FIELD表
pLayer = this.axMapControl1.get_Layer(0);
pinputTable = (ITable)pLayer;
pFeatureLayer = (IFeatureLayer)pLayer;
pFeatureClass = pFeatureLayer.FeatureClass;
IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
IFeatureRenderer pFeatureRenderer = pGeoFeatureLayer.Renderer;
//定义相交图层(面)
pLayer = this.axMapControl1.get_Layer(1);
pclipTable = (ITable)pLayer;
//输出文件类型
pFeatureClassName = new FeatureClassNameClass();
pFeatureClassName.FeatureType = esriFeatureType.esriFTSimple;
pFeatureClassName.ShapeFieldName = "Shape";
pFeatureClassName.ShapeType = pFeatureClass.ShapeType;
//输出图层的名称和位置
saveFileDialog1.Filter = "shapefile文件(*.shp)|*.shp";
saveFileDialog1.InitialDirectory = @"C:";
DialogResult pDialogRuselt = saveFileDialog1.ShowDialog();
if (pDialogRuselt != DialogResult.OK)
return;
string pPath = saveFileDialog1.FileName;
string pFolder = System.IO.Path.GetDirectoryName(pPath);
string pFileName = System.IO.Path.GetFileName(pPath);
pNewWSName = new WorkspaceNameClass();
pNewWSName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";
pNewWSName.PathName = pFolder;
pDatasetName = (IDatasetName)pFeatureClassName;
pDatasetName.Name = pFileName;
pDatasetName.WorkspaceName = pNewWSName;
pBasicGeop = new BasicGeoprocessorClass();
pOutputFeatureClass = pBasicGeop.Clip(pinputTable, false, pclipTable, false, 0.0001, pFeatureClassName);
pOutputFeatureLayer = new FeatureLayerClass();
pOutputFeatureLayer.FeatureClass = pOutputFeatureClass;
pOutputFeatureLayer.Name = pOutputFeatureClass.AliasName;
IGeoFeatureLayer pGeoFeatureLayer2 = pOutputFeatureLayer as IGeoFeatureLayer;
pGeoFeatureLayer.Renderer = pFeatureRenderer;
IFeatureCursor pFeatureCursor=pGeoFeatureLayer.Search(null,false);
if (pFeatureRenderer.CanRender(pOutputFeatureClass,new SimpleDisplayClass()))
{
pFeatureRenderer.Draw(pFeatureCursor, esriDrawPhase.esriDPGeography, new SimpleDisplayClass(), new TrackCancelClass());
}
this.axMapControl1.AddLayer(pOutputFeatureLayer as ILayer, 0);
方法二,图层对图层裁剪,输出图层:
IFeatureLayer pFeatureLayer;
IFeatureClass inputfeatureclass;
IFeatureClass cliplayerClass;
//定义输出图层
pFeatureLayer = this.axMapControl1.get_Layer(0) as IFeatureLayer ;
inputfeatureclass = pFeatureLayer.FeatureClass;
//定义相交图层(面)
pFeatureLayer = this.axMapControl1.get_Layer(1 ) as IFeatureLayer ;
cliplayerClass = pFeatureLayer.FeatureClass;
//输出图层的名称和位置
saveFileDialog1.Filter = "shapefile文件(*.shp)|*.shp";
saveFileDialog1.InitialDirectory = @"C:";
DialogResult pDialogRuselt = saveFileDialog1.ShowDialog();
if (pDialogRuselt != DialogResult.OK)
return;
string pPath = saveFileDialog1.FileName;
string pFolder = System.IO.Path.GetDirectoryName(pPath);
string pFileName = System.IO.Path.GetFileName(pPath);
IFeatureWorkspace pFWS;
IWorkspaceFactory pWorkspaceFactor = new ShapefileWorkspaceFactoryClass();
pFWS = pWorkspaceFactor.OpenFromFile(pFolder, 0) as IFeatureWorkspace;
IFeatureClass outfeatureclass = pFWS.CreateFeatureClass(pFileName,
inputfeatureclass.Fields , null, null, esriFeatureType.esriFTSimple, "Shape", "");
Clip clipTool =new Clip(inputfeatureclass, cliplayerClass, outfeatureclass);
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true;
gp.Execute(clipTool, null);
IFeatureLayer outlayer = new FeatureLayerClass();
outlayer.FeatureClass = outfeatureclass;
outlayer.Name = outfeatureclass.AliasName;
//将裁剪输出的加载到当前Mapcontrol上
axMapControl1.AddLayer((ILayer)outlayer);
方法三,在地图上拉框裁剪
ILayer pLayer;
IFeatureLayer pFeatureLayer;
IFeatureClass pFeatureClass;
IWorkspaceName pNewWSName;
IBasicGeoprocessor pBasicGeop;
IFeatureClassName pFeatureClassName;
IDatasetName pDatasetName;
IFeatureClass pOutputFeatureClass;
IFeatureLayer pOutputFeatureLayer;
ITable pinputTable, pclipTable;
//定义输出图层的FIELD表
pLayer = this.axMapControl1.get_Layer(0);
pinputTable = (ITable)pLayer;
pFeatureLayer = (IFeatureLayer)pLayer;
pFeatureClass = pFeatureLayer.FeatureClass;
//输出文件类型
pFeatureClassName = new FeatureClassNameClass();
pFeatureClassName.FeatureType = esriFeatureType.esriFTSimple;
pFeatureClassName.ShapeFieldName = "Shape";
pFeatureClassName.ShapeType = pFeatureClass.ShapeType;
//输出图层的名称和位置
saveFileDialog1.Filter = "shapefile文件(*.shp)|*.shp";
saveFileDialog1.InitialDirectory = @"C:";
DialogResult pDialogRuselt = saveFileDialog1.ShowDialog();
if (pDialogRuselt != DialogResult.OK)
return;
string pPath = saveFileDialog1.FileName;
string pFolder = System.IO.Path.GetDirectoryName(pPath);
string pFileName = System.IO.Path.GetFileName(pPath);
pNewWSName = new WorkspaceNameClass();
pNewWSName.WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory";
pNewWSName.PathName = pFolder;
pDatasetName = (IDatasetName)pFeatureClassName;
pDatasetName.Name = pFileName;
pDatasetName.WorkspaceName = pNewWSName;
//定义相交图层(面)
IGeometry polygon = axMapControl1.TrackPolygon();
IFeatureClass pFeatureClass2 = ToFeatureClass(pinputTable.Fields, polygon);
pclipTable = (ITable)pFeatureClass2;
pBasicGeop = new BasicGeoprocessorClass();
pOutputFeatureClass = pBasicGeop.Clip(pinputTable, false, pclipTable, false, 0.0001, pFeatureClassName);
pOutputFeatureLayer = new FeatureLayerClass();
pOutputFeatureLayer.FeatureClass = pOutputFeatureClass;
pOutputFeatureLayer.Name = pOutputFeatureClass.AliasName;
this.axMapControl1.AddLayer(pOutputFeatureLayer as ILayer, 0);
private IFeatureClass ToFeatureClass(IFields _fields, IGeometry pGeometry)
{
IClone pClone = _fields as IClone;
IFields fields = pClone.Clone() as IFields;
IFieldEdit pFieldEdit = fields.get_Field(fields.FindField("shape")) as IFieldEdit ;
IGeometryDefEdit pGeometryDefEdit = pFieldEdit.GeometryDef as IGeometryDefEdit;
pGeometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
IWorkspaceFactory2 pWorkspaceFactory = new InMemoryWorkspaceFactoryClass();
IWorkspaceName pWorkspaceName = pWorkspaceFactory.Create("", "temp", null, 0);
IFeatureWorkspace pFeatureWorkspace = (IFeatureWorkspace)(((IName)pWorkspaceName).Open());
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass("tempShp",
fields, null, null, esriFeatureType.esriFTSimple, "shape", "");
IFeature pFeature = pFeatureClass.CreateFeature();
pFeature.Shape = pGeometry;//类型应该相同
pFeature.Store();
return pFeatureClass;
}