做系统开发或者数据处理的时候,我一般还是喜欢使用文件数据源,例如矢量用.shp文件存储,栅格数据用.tif或者.img文件存储。ArcGIS Pro SDK中对数据源操作的API和ArcObjects SDK中差别还是比较大的。
(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);
得到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中差别还是挺大的。
添加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()函数即可。