做系統開發或者資料處理的時候,我一般還是喜歡使用檔案資料來源,例如向量用.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()函式即可。