Fork me on GitHub

ArcGIS Engine 之 GP 工具实现矢量转栅格

做项目遇到需要将矢量数据转换成栅格数据的问题,走了很多弯路,后来发现使用 GP 工具很轻松就解决了,记录一下。

ArcGIS 软件中 ArcToolbox 中的工具几乎都可以使用 GP 工具来完成。矢量转栅格的工具主要在 ArcToolbox --> Conversion Tools --> To Raster 下,包括 Feature to RasterPoint to RasterPolygon to RasterPolyline to Raster。其中 Feature to Raster 与其他三种效果相同。

ArcGIS 中 Feature to Raster 方法如下图所示:

官方文档给出的工具语法如下:

  • FeatureToRaster_conversion (in_features, field, out_raster, {cell_size})
Parameter Explanation Data Type
in_features The input feature dataset to be converted to a raster dataset. Feature Layer
field The field used to assign values to the output raster.
It can be any field of the input feature dataset’s attribute table.
If the Shape field of a point or multipoint dataset contains z or m values, then either of these can be used.
Field
out_raster The output raster dataset to be created.
When not saving to a geodatabase, specify .tif for a TIFF file format, .img for an ERDAS IMAGINE file format, or no extension for an Esri Grid raster format.
Raster Dataset
cell_size
(Optional)
The cell size for the output raster dataset.
The default cell size is the shortest of the width or height of the extent of the input feature dataset, in the output spatial reference, divided by 250.
Analysis Cell Size

AE 代码见 Github:PointToRaster (AE 版本 10.1,VS 版本 2017)

若代码不能运行,请添加如下引用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.AnalysisTools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.DataSourcesRaster;

我主要实现了 点转栅格 的方法,核心代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
private void toRaster_Click(object sender, EventArgs e)
{
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true;

int pLayerId = -1;
ILayer pLayer = null;
for (int i = 0; i < axMapControl1.LayerCount; i++)
{
pLayer = axMapControl1.get_Layer(i);
//if (pLayer is IFeatureLayer && pLayer.Name == "质点")
if (pLayer is IFeatureLayer)
{
pLayerId = i;
}
}
if (pLayerId == -1)
{
MessageBox.Show("找不到点图层,请重新加载", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}


try
{
ESRI.ArcGIS.ConversionTools.FeatureToRaster ptr = new ESRI.ArcGIS.ConversionTools.FeatureToRaster();

// 获得点图层
IFeatureLayer pfeatureLayer = axMapControl1.get_Layer(pLayerId) as IFeatureLayer;
IFeatureClass feaureClass = pfeatureLayer.FeatureClass;

ptr.in_features = pfeatureLayer;
string filepath = @"d:\gis\";
ptr.out_raster = filepath + pfeatureLayer.Name + ".tif"; // 将转换结果保存为tif格式
ptr.field = "id"; // 设置根据那个字段进行转换
ptr.cell_size = 64; // 设置转换后的栅格像元大小
gp.Execute(ptr, null); // 执行 GP 工具

MessageBox.Show("转换成功!", "恭喜你!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

// 加载栅格
IWorkspaceFactory pWorkspaceFactory = new RasterWorkspaceFactory();
IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(filepath, 0);
IRasterWorkspace pRasterWorkspace = pWorkspace as IRasterWorkspace;
IRasterDataset pRasterDataset = pRasterWorkspace.OpenRasterDataset(pfeatureLayer.Name + ".tif");
IRaster pRaster= pRasterDataset.CreateDefaultRaster();
IRasterLayer pRasterLayer = new RasterLayerClass();
pRasterLayer.CreateFromRaster(pRaster);
ILayer layer = pRasterLayer as ILayer;
axMapControl1.AddLayer(layer, 0);
}
catch (Exception ex)
{
MessageBox.Show("转换失败!", "很遗憾!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}

运行结果:

经测试,该代码同样适用与 面数据转换栅格

--- The End    Thanks For Your Reading ---

本文标题:ArcGIS Engine 之 GP 工具实现矢量转栅格

文章作者:RenKai

发布时间:2018年03月21日 - 17:03

最后更新:2018年08月10日 - 14:08

原始链接:http://yoursite.com/ArcGIS-Engine-PointToRaster.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

宠辱不惊,看庭前花开花落;去留无意,望天上云卷云舒。