GP工具柵格重分類
阿新 • • 發佈:2018-12-12
GP工具可以較為簡單的實現柵格資料重分類,要注意引數設定準確。
重分類時要用到獲取柵格資料最大最小值,
等間距重分類:
步驟:
- 獲取柵格資料最大最小值
- 定義與初始化GP工具
- 定義重分類工具
- 重分類工具初始化
- 執行重分類
- 載入執行結果
獲取柵格資料最大最小值
//獲取取值範圍 IRaster pRaster = pRasterLayer.Raster; IRasterBandCollection pRBC = (IRasterBandCollection)pRaster; IRasterBand pRasterband = pRBC.Item(0); //第一坡道 IRasterStatistics pRS = pRasterband.Statistics; double max = pRS.Maximum; double min = pRS.Minimum;
定義與初始化GP工具
Geoprocessor GP = new Geoprocessor();
GP.OverwriteOutput = true;
定義重分類工具
Reclassify pRec = new Reclassify();
重分類工具初始化
pRec.in_raster = pRasterLayer; pRec.reclass_field = "Value"; string remap = ""; double dis = (max + min) / 10; for(double i = 0; i < 10; i++) //此處注意,一定要為double型別 { //注意數字和數字一起算 //注意字元跟字元一起算 double from = min + dis * i/10; double to = min + dis * (i + 1)/10; remap += from + " " + to + " " + i + ";"; } pRec.remap = remap;
執行重分類、載入執行結果
try { GP.Execute(pRec, null); IRasterLayer resRasterLayer = new RasterLayer(); resRasterLayer.CreateFromFilePath(Application.StartupPath + "\\reclass.tif"); axMapControl1.AddLayer(resRasterLayer); object sev = null; MessageBox.Show(GP.GetMessages(ref sev)); } catch { object sev = null; MessageBox.Show(GP.GetMessages(ref sev)); }
注意:
int型資料運算後會被取整,涉及小數運算時們一定要將資料定義為double或float
資料加減運算與字串加減要分開,防止混淆,無法執行。
全部程式碼(函式):
private void 重分類ToolStripMenuItem_Click(object sender, EventArgs e)
{
//獲取輸入資料
ILayer pLayer = axMapControl1.get_Layer(3);
IRasterLayer pRasterLayer = pLayer as IRasterLayer;
//獲取取值範圍
IRaster pRaster = pRasterLayer.Raster;
IRasterBandCollection pRBC = (IRasterBandCollection)pRaster;
IRasterBand pRasterband = pRBC.Item(0); //第一坡道
IRasterStatistics pRS = pRasterband.Statistics;
double max = pRS.Maximum;
double min = pRS.Minimum;
Geoprocessor GP = new Geoprocessor();
GP.OverwriteOutput = true;
//定義重分類工具
Reclassify pRec = new Reclassify();
pRec.in_raster = pRasterLayer;
pRec.reclass_field = "Value";
string remap = "";
double dis = (max + min) / 10;
for(double i = 0; i < 10; i++) //此處注意,一定要為double型別
{
//注意數字和數字一起算
//注意字元跟字元一起算
double from = min + dis * i/10;
double to = min + dis * (i + 1)/10;
remap += from + " " + to + " " + i + ";";
}
pRec.remap = remap;
MessageBox.Show(remap);
pRec.out_raster = Application.StartupPath + "\\reclass.tif";
try
{
GP.Execute(pRec, null);
IRasterLayer resRasterLayer = new RasterLayer();
resRasterLayer.CreateFromFilePath(Application.StartupPath + "\\reclass.tif");
axMapControl1.AddLayer(resRasterLayer);
object sev = null;
MessageBox.Show(GP.GetMessages(ref sev));
}
catch
{
object sev = null;
MessageBox.Show(GP.GetMessages(ref sev));
}
}