Arcengine+C#实现矢量数据的裁剪
最近工作需要完成图像的裁剪,因为有很多需要裁剪的图像,用arcgis做的话工作量太大,而且后期工作涉及到自动化处理,所以写了段代码用engine实现图像的裁剪功能。首先要获得你所需要被裁剪的图层的IFeatureLayer和裁剪图层的IFeatureLayer,然后调用IBasicGeoprocessor.Clip方法进行裁剪,我把该方法单独封装了一下,代码如下:
public IFeatureClass Clip(IFeatureClass pInputFeatureClass, IFeatureClass pClipFeatureClass, string pfilePath, string pfileName)
{
IFeatureClassName pOutput = new FeatureClassNameClass();
pOutput.ShapeFieldName = pInputFeatureClass.ShapeFieldName;
pOutput.ShapeType = pInputFeatureClass.ShapeType;
pOutput.FeatureType = esriFeatureType.esriFTSimple;
//获取shapefile数据工作空间
IWorkspaceName pWsN = new WorkspaceNameClass();
pWsN.WorkspaceFactoryProgID = “esriDataSourcesFile.ShapefileWorkspaceFactory”;
pWsN.PathName = pfilePath;
//通过IDatasetName设置输出结果相关参数
IDatasetName pDatasetName = pOutput as IDatasetName;
pDatasetName.Name = pfileName;
pDatasetName.WorkspaceName = pWsN;
//初始化IBasicGeoprocessor对象,调用clip方法
IBasicGeoprocessor pBasicGeo = new BasicGeoprocessorClass();
pBasicGeo.SpatialReference = axMapControl1.SpatialReference;
//进行裁剪运算
IFeatureClass pFeatureClass = pBasicGeo.Clip(pInputFeatureClass as ITable, true , pClipFeatureClass as ITable, true , 0.01, pOutput);
return pFeatureClass;
}
然后我添加了一个button和三个mapcontrol控件,在axmapcontrol1和axmapcontrol2的属性里直接添加了图层,代码如下:
private void button1_Click(object sender, EventArgs e)
{
ILayer pInputLayer = axMapControl1.get_Layer(0);//获取在axmapcontrol1中加载的图层,是需要被裁剪的图层
ILayer pClipLayer = axMapControl2.get_Layer(0);//获取在axmapcontrol2中加载的图层,是裁剪图层
if (pInputLayer != null && pClipLayer != null)
{
IFeatureLayer pInputFeatureLayer = pInputLayer as IFeatureLayer;
IFeatureLayer pClipFeatureLayer = pClipLayer as IFeatureLayer;
IFeatureClass outFeatureClass = Clip(pInputFeatureLayer.FeatureClass, pClipFeatureLayer.FeatureClass, @”C:\Users\Administrator\Desktop\”, “裁剪分析结果”);
if (outFeatureClass != null)
{
IFeatureLayer pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = outFeatureClass;
pFeatureLayer.Name = outFeatureClass.AliasName;
this.axMapControl3.AddLayer(pFeatureLayer);
}
}
转载自:https://blog.csdn.net/u014627099/article/details/52151534