ArcGIS Pro SDK 002 对数据文件的读取和操作
做系统开发或者数据处理的时候,我一般还是喜欢使用文件数据源,例如矢量用.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()函数即可。
相关文章
- 从本体论开始说起——运营商关系图谱的构建及应用
- 如何成为一名数据科学家?
- 从未见过的堂兄杀了人,你的DNA是关键证据
- 20个安全可靠的免费数据源,各领域数据任你挑
- 20个安全可靠的免费数据源,各领域数据任你挑
- 阿里云李飞飞:All in Cloud时代,云原生数据库优势明显
- 基于Hadoop生态系统的一高性能数据存储格式CarbonData(性能篇)
- 大数据告诉你:10年漫威,到底有多少角色
- TigerGraph:实时图数据库助力金融风控升级
- Splunk利用Splunk Connected Experiences和Splunk Business Flow 扩大数据访问
- 大数据开发常见的9种数据分析手段
- 以免在景区看人,我爬了5W条全国景点门票数据...
- 【实战解析】基于HBase的大数据存储在京东的应用场景
- 数据科学家告诉你哪些计算机科学书籍是你应该看的
- Kafka作为大数据的核心技术,你了解多少?
- Spring Boot 整合 Redis 实现缓存操作
- 大数据学习必须掌握的五大核心技术有哪些?
- 基于Antlr在Apache Flink中实现监控规则DSL化的探索实践
- 甲骨文再次被Gartner评为分析型数据管理解决方案魔力象限领导者
- 爬取吴亦凡微博102118条转发数据,扒一扒流量的真假