做系统开发或者数据处理的时候,我一般还是喜欢使用文件数据源,例如矢量用.shp文件存储,栅格数据用.tif或者.img文件存储。ArcGIS Pro SDK中对数据源操作的API和ArcObjects SDK中差别还是比较大的。

1、打开数据文件

打开文件数据的步骤如下。

(1)使用Shape文件所在的目录,创建FileSystemConnectionPath对象,该对象是文件系统连接路径对象。

(2)使用FileSystemConnectionPath对象创建FileSystemDatastore,该对象是文件系统数据存储器对象。

(3)使用FileSystemDatastore,打开指定数据名称的数据源。打开数据源函数是一个模板函数,如果是Shape文件,则返回FeatureClass,如果是栅格数据,返回RasterDataset,如果是dbf数据,则返回Table。

代码如下。

string myFileName = System.IO.Path.GetFileName(pShapeFile);
var myFileSystemConnectionPath = new FileSystemConnectionPath(new Uri(myFolderPath), FileSystemDatastoreType.Shapefile); FileSystemDatastore myFileSystemDatastore = null;
FeatureClass myFeatureClass = null;
try
{
await Task.Run(() =>
{
myFileSystemDatastore = new FileSystemDatastore(myFileSystemConnectionPath);
myFeatureClass = myFileSystemDatastore.OpenDataset<FeatureClass>(myFileName);
});
myFileSystemDatastore?.Dispose();
}
catch (Exception ex)
{
myFileSystemDatastore?.Dispose();
throw new ArgumentException("打开文件失败。" + pShapeFile + "," + ex.Message);
}
return myFeatureClass;

需要注意的是,ArcGIS Pro SDK中,很多函数都是异步函数,需要添加到await Task.Run(() =>{}里面执行。开发的时候,把鼠标放到函数上,会有提示。如下图所示。

如果要打开栅格数据,代码思路是一样的,只是在初始化FileSystemConnectionPath的时候,传入Raster,知识在OpenDataset的时候,传入RasterDataset即可。代码如下所示。

string myFileName = System.IO.Path.GetFileName(pRasterFile);
var myFileSystemConnectionPath = new FileSystemConnectionPath(new Uri(myFolderPath), FileSystemDatastoreType.Raster);
FileSystemDatastore myFileSystemDatastore = null;
RasterDataset myRasterDataset = null;
try
{
await Task.Run(() =>
{
myFileSystemDatastore = new FileSystemDatastore(myFileSystemConnectionPath);
myRasterDataset = myFileSystemDatastore.OpenDataset<RasterDataset>(myFileName);
});
myFileSystemDatastore?.Dispose();
}
catch (Exception ex)
{
myFileSystemDatastore?.Dispose();
throw new ArgumentException("打开文件失败。" + pRasterFile + "," + ex.Message);
}
return myRasterDataset;

打开dbf的时候,FileSystemConnectionPath还是使用FileSystemDatastoreType.Shapefile,在Open的时候,返回Table。代码如下所示。

myTable = myFileSystemDatastore.OpenDataset<Table>(myFileName);

2、读取FeatureClass

得到FeatureClass后,我们可以遍历里面要要素,读取其中的信息。方法和ArcObjects SDK中的流程类似,也是使用了Search方法,返回Cursor变量。具体使用方法如下面代码所示。

await Task.Run(() =>
{
var myDefinition = myFeatureClass.GetDefinition();
this._SpatialReference = myDefinition.GetSpatialReference();
int my_gridcode_FileIndex = myDefinition.FindField("gridcode");
RowCursor myRowCursor = myFeatureClass.Search(null, true);
while (myRowCursor.MoveNext())
{
Feature myFeature = myRowCursor.Current as Feature;
DraExtBasin myNewBasin = new DraExtBasin
{
FID = myFeature.GetObjectID(),
SnapPourPointFID = Convert.ToInt64(myFeature.GetOriginalValue(my_gridcode_FileIndex)),
Polygon = PolygonBuilderEx.CreatePolygon(myFeature.GetShape() as Polygon)
};
myFeature.Dispose();
myBasinList.Add(myNewBasin);
}
myRowCursor.Dispose();
});

我们通过FeatureClass.GetDefinition()函数,得到FeatureClass的一些定义信息,通过定义信息,可以获取数据的空间参考、字段等,这点和ArcObjects SDK中差别还是挺大的。

3、添加和编辑Feature

添加Feature和ArcObjects SDK类似,也是使用RowBuffer进行添加,代码如下所示。

await Task.Run(() =>
{
var myDefinition = myFeatureClass.GetDefinition();
int my_P_FID_FI = myDefinition.FindField("P_FID");
int my_D_Length_FI = myDefinition.FindField("D_Length");
int my_P_Dis_FI = myDefinition.FindField("P_Dis");
RowBuffer myRowBuffer = myFeatureClass.CreateRowBuffer();
foreach (DraExtFullBasin myFullBasin in pFullBasinList)
{
double myDLength = 0;
double myPDis = 0;
if (myMainDrainageD.ContainsKey(myFullBasin.SnapPourPointFID))
{
var myDraExtMainDrainage = myMainDrainageD[myFullBasin.SnapPourPointFID];
myDLength = myDraExtMainDrainage.DrainageLength;
myPDis = myDraExtMainDrainage.SnapPourPointDistance;
}
myRowBuffer[1] = myFullBasin.Polygon;
myRowBuffer[my_P_FID_FI] = myFullBasin.SnapPourPointFID;
myRowBuffer[my_D_Length_FI] = myDLength;
myRowBuffer[my_P_Dis_FI] = myPDis;
myFeatureClass.CreateRow(myRowBuffer);
}
myRowBuffer.Dispose();
});

编辑的话,就是获取Row或者Feature之后,修改信息,最后调用对象的Store()函数即可。

最新文章

  1. Building the Testing Pipeline
  2. Bootstap datetimepicker报错TypeError: intermediate value
  3. android 第三方登录---新浪微博
  4. html,css命名规范 (转)
  5. 学习笔记:CSS3的filter属性
  6. 使用HTML5新支持的搭建WebRtc环境来作为视频通讯
  7. BZOJ1004: [HNOI2008]Cards
  8. 更改appstore开发商名字
  9. 构造函数模式自己定义js对象
  10. struts+spring action应配置为scope=&quot;prototype&quot;
  11. 更改Apache默认网站根目录
  12. Mysql学习(慕课学习笔记8)插入、更新、删除记录
  13. 使用脚本管理IIS
  14. java public protect default private
  15. VS2008通过 map 和 cod 文件定位崩溃代码行
  16. “幸福企业”定义-参观“MES项目”有感
  17. javascript中的apply,call,bind详解
  18. 浅谈卷积神经网络及matlab实现
  19. Leetcode_263_Ugly Number
  20. 复制命令(ROBOCOPY)

热门文章

  1. i春秋Backdoor
  2. 说一下 ArrayDeque 和 LinkedList 的区别?
  3. table 动态隐藏tr行
  4. C++编程笔记(GPU并行编程)
  5. 【每日一题】【双端降序队列Deque】2021年12月28日-239. 滑动窗口最大值
  6. Mysql-delete语句
  7. python 错误之TypeError: XXXXX() takes no keyword arguments
  8. Linux安装&amp;卸载mysql5.7
  9. python之路22 hashlib、subprocess、logging模块
  10. [WPF]项目整合Metro和MaterialDesignInXamlToolkit UI框架