GP tool , Resample, Mask, Clip
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.SpatialAnalystTools;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//构造Geoprocessor
ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
//设置参数
ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect();
intersect.in_features = @"E:\数据\temp\New_Shapefile.shp;E:\数据\temp\New_Shapefile_Intersect.shp";
intersect.out_feature_class = @"E:\数据\temp\intersectOne.shp";
intersect.join_attributes = "ONLY_FID";
//执行Intersect工具
RunTool(gp, intersect, null);
}
private void RunTool(Geoprocessor geoprocessor, IGPProcess process, ITrackCancel TC)
{
// Set the overwrite output option to true
geoprocessor.OverwriteOutput = true;
try
{
geoprocessor.Execute(process, null);
ReturnMessages(geoprocessor);
}
catch (Exception err)
{
Console.WriteLine(err.Message);
ReturnMessages(geoprocessor);
}
}
// Function for returning the tool messages.
private void ReturnMessages(Geoprocessor gp)
{
string ms = "";
if (gp.MessageCount > 0)
{
for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
{
ms += gp.GetMessage(Count);
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//构造Geoprocessor
//遍历得到所有N开头的Featureclass
ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
gp.SetEnvironmentValue("workspace",@"E:\数据\temp");
IGpEnumList featureClasses = gp.ListFeatureClasses("N*", "", "");
String featureClass = featureClasses.Next();
while (featureClass != "")
{
Console.WriteLine(featureClass.ToString());
featureClass = featureClasses.Next();
}
MessageBox.Show("Done!");
}
private void button4_Click(object sender, EventArgs e)
{
// List all TIF files in the workspace and build pyramids
ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
gp.SetEnvironmentValue("workspace", @"E:\数据\temp");
IGpEnumList rasters = gp.ListRasters("", "img");
String raster = rasters.Next();
while (raster != "")
{
Console.WriteLine(raster.ToString());
raster = rasters.Next();
}
MessageBox.Show("Done!");
}
private void button5_Click(object sender, EventArgs e)
{
//合并某个工作空间下所有N开头的polygon Featureclass
ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
gp.SetEnvironmentValue("workspace", @"E:\数据\temp");
IGpEnumList featureClasses = gp.ListFeatureClasses("N*", "polygon", "");
String featureClass = featureClasses.Next();
String polygonsToUnion = "";
while (featureClass != "")
{
Console.WriteLine(featureClass.ToString());
polygonsToUnion += featureClass;
featureClass = featureClasses.Next();
if (featureClasses != null)
{
polygonsToUnion += ";";
}
}
Union union = new Union(polygonsToUnion, @"E:\数据\temp" + "\\" + "BIGUnioned.shp");
gp.Execute(union, null);
MessageBox.Show("Done!");
}
private void button6_Click(object sender, EventArgs e)
{
//调用IRasterGeometryProc,resample raster
IWorkspaceFactory workspaceFactory = new RasterWorkspaceFactory();
IWorkspace workspace;
workspace = workspaceFactory.OpenFromFile(@"E:\数据\GTDSSJ", 0); //inPath栅格数据存储路径
IRasterWorkspace rastWork = (IRasterWorkspace)workspace;
IRasterDataset rastDataset;
rastDataset = rastWork.OpenRasterDataset("12.img") as IRasterDataset;//inName栅格文件名
IRaster raster = rastDataset.CreateDefaultRaster();
IRasterGeometryProc rastGeoProc = new RasterGeometryProcClass();
rastGeoProc.Resample(rstResamplingTypes.RSP_NearestNeighbor,10.0, raster);
ISaveAs2 saveAs = (ISaveAs2)raster;
//saveAs.SaveAs("newnewnew.jpg", workspace, "JPG");
//第三个参数有严格的格式要求 "IMAGINE Image" "TIFF" "HDF4" 具体见帮助
saveAs.SaveAs("NewResampled3.img", workspace, "IMAGINE Image");
//IRasterStorageDef rastStorageDef = new RasterStorageDefClass();
//rastStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionLZ77;
//rastStorageDef.CompressionQuality = 1;
//IRasterDataset newRastDataSet = null;
//newRastDataSet=saveAs.SaveAsRasterDataset("GenNew22.img", workspace, "IMAGE", rastStorageDef);
MessageBox.Show("Done!");
}
private void button7_Click(object sender, EventArgs e)
{
//构造Geoprocessor
ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new ESRI.ArcGIS.Geoprocessor.Geoprocessor();
//设置参数
ESRI.ArcGIS.SpatialAnalystTools.ExtractByMask mask = new ExtractByMask();
mask.in_raster = @"E:\数据\A2000049.h26v04.005.img";
mask.in_mask_data = @"E:\数据\hebei";
mask.out_raster = @"E:\数据\A2000049.h26v04.005_Marked";
RunTool(gp, mask, null);
//ESRI.ArcGIS.AnalysisTools.Intersect intersect = new ESRI.ArcGIS.AnalysisTools.Intersect();
//intersect.in_features = @"E:\数据\temp\New_Shapefile.shp;E:\数据\temp\New_Shapefile_Intersect.shp";
//intersect.out_feature_class = @"E:\数据\temp\intersectOne.shp";
//intersect.join_attributes = "ONLY_FID";
//执行Intersect工具
//RunTool(gp, intersect, null);
MessageBox.Show("Done!");
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
标题: [已解决] 影像数据的裁切和保存(源代码) [打印本页]
作者: lsbocai 时间: 2008-5-28 11:11 标题: 影像数据的裁切和保存(源代码)
因为问题已经解决,现将代码整理给大家。
影像数据的裁切我是参考的scrsgjh的帖子(vb的代码),原帖地址http://bbs.esrichina-bj.cn/ESRI/viewthread.php?
tid=4152&highlight=%B6%E0%B1%DF%D0%CE%2Bscrsgjh
//影像裁切
IGeometry clipGeo = axMapControl1.TrackPolygon(); //
ILayer layer = axMapControl1.get_Layer(i); //要裁切的影像图层
IRasterLayer pRasterLayer = layer as IRasterLayer;
IRaster pRaster = pRasterLayer.Raster;
IRasterProps pProps = pRaster as IRasterProps;
object cellSizeProvider = pProps.MeanCellSize().X;
IGeoDataset pInputDataset = pRaster as IGeoDataset;
IExtractionOp pExtractionOp = new RasterExtractionOpClass();
IRasterAnalysisEnvironment pRasterAnaEnvir = pExtractionOp as IRasterAnalysisEnvironment;
pRasterAnaEnvir.SetCellSize(esriRasterEnvSettingEnum.esriRasterEnvValue, ref cellSizeProvider);
object extentProvider = clipGeo.Envelope;
object snapRasterData = Type.Missing;
pRasterAnaEnvir.SetExtent(esriRasterEnvSettingEnum.esriRasterEnvValue, ref extentProvider, ref snapRasterData);
IGeoDataset pOutputDataset = pExtractionOp.Polygon(pInputDataset, clipGeo as IPolygon, true);
IRaster clipRaster; //裁切后得到的IRaster
if (pOutputDataset is IRasterLayer)
{
IRasterLayer rasterLayer = pOutputDataset as IRasterLayer;
clipRaster = rasterLayer.Raster;
}
else if (pOutputDataset is IRasterDataset)
{
IRasterDataset rasterDataset = pOutputDataset as IRasterDataset;
clipRaster = rasterDataset.CreateDefaultRaster();
}
else if (pOutputDataset is IRaster)
{
clipRaster = pOutputDataset as IRaster;
}
else
{
return;
}
//保存裁切后得到的clipRaster
//如果直接保存为img影像文件
IWorkspaceFactory pWKSF = new RasterWorkspaceFactoryClass();
IWorkspace pWorkspace = pWKSF.OpenFromFile(@"C:\temp", 0);
ISaveAs pSaveAs = clipRaster as ISaveAs;
pSaveAs.SaveAs("test.img", pWorkspace, "IMAGINE Image");
//如果保存在mdb中
IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass();
pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000;
pRasterStorageDef.CompressionQuality = 50;
pRasterStorageDef.TileHeight = 128;
pRasterStorageDef.TileWidth = 128;
IWorkspaceFactory pWKSF = new AccessWorkspaceFactoryClass();
IWorkspace pWorkspace = pWKSF.OpenFromFile(@"C:\temp\test.mdb", 0);
ISaveAs2 pSaveAs = clipRaster as ISaveAs2;
pSaveAs.SaveAsRasterDataset("test", pWorkspace, "gdb", pRasterStorageDef);
//也可以使用IRasterWorkspaceEx的SaveAsRasterDataset方法保存到mdb中
IWorkspaceFactory pWKSF = new AccessWorkspaceFactoryClass();
IWorkspace pWorkspace = pWKSF.OpenFromFile(@"C:\temp\test.mdb", 0);
IRasterWorkspaceEx pRasterWKS = pWorkspace as IRasterWorkspaceEx;
IRasterStorageDef pRasterStorageDef = new RasterStorageDefClass();
pRasterStorageDef.CompressionType = esriRasterCompressionType.esriRasterCompressionJPEG2000;
pRasterStorageDef.CompressionQuality = 50;
pRasterStorageDef.PyramidLevel = 2;
pRasterStorageDef.PyramidResampleType = rstResamplingTypes.RSP_BilinearInterpolation;
pRasterStorageDef.TileHeight = 128;
pRasterStorageDef.TileWidth = 128;
IRasterDef pRasterDef = new RasterDefClass();
pRasterDef.Description = "rasterdataset";
pRasterDef.SpatialReference = axMapControl1.SpatialReference;
IGeometryDef pGeoDef = new GeometryDefClass();
IGeometryDefEdit pGeoDefEdit = pGeoDef as IGeometryDefEdit;
pGeoDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolygon;
pGeoDefEdit.AvgNumPoints_2 = 4;
pGeoDefEdit.GridCount_2 = 1;
pGeoDefEdit.set_GridSize(0, 1000);
pGeoDefEdit.SpatialReference_2 = axMapControl1.SpatialReference;
IRasterDataset pRasterDataset = pRasterWKS.SaveAsRasterDataset("test", clipRaster, pRasterStorageDef, "", pRasterDef,
pGeoDef);
//其实参数可以不用设置,直接:
IRasterDataset pRasterDataset = pRasterWKS.SaveAsRasterDataset("test", clipRaster, null, "", null, null);