ArcGIS Pro SDK (八)地理数据库 3 数据
文章目录
- ArcGIS Pro SDK (八)地理数据库 3 数据
- 1 从地理数据库打开数据集
- 2 检查表是否存在
- 3 检查要素类是否存在
- 4 打开关系两个表之间的类
- 5 从关系类获取相关要素类
- 6 从 shapefile 数据存储打开要素类
- 7 打开 CAD 数据存储
环境:Visual Studio 2022 + .NET6 + ArcGIS Pro SDK 3.0
1 从地理数据库打开数据集
public async Task OpeningDatasetsFromGeodatabase()
{await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>{using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde")))){using (Table table = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.EmployeeInfo")){}// 打开一个要素类(在要素数据集内或要素数据集外)。using (FeatureClass featureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.AddressPoint")){}// 你可以将要素类作为表打开,这将为你提供一个表引用。using (Table featureClassAsTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.AddressPoint")){// 但它实际上是一个要素类对象。FeatureClass featureClassOpenedAsTable = featureClassAsTable as FeatureClass;}// 打开一个要素数据集。using (FeatureDataset featureDataset = geodatabase.OpenDataset<FeatureDataset>("LocalGovernment.GDB.Address")){}// 打开一个关系类。就像你可以将要素类作为表打开一样,你也可以将带有属性的关系类作为关系类打开。using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.AddressPointHasSiteAddresses")){}}});
}
2 检查表是否存在
// 必须在 QueuedTask.Run() 中调用
public bool TableExists(Geodatabase geodatabase, string tableName)
{try{TableDefinition tableDefinition = geodatabase.GetDefinition<TableDefinition>(tableName);tableDefinition.Dispose();return true;}catch{// 如果定义不存在,GetDefinition 会抛出异常return false;}
}
3 检查要素类是否存在
// 必须在 QueuedTask.Run() 中调用
public bool FeatureClassExists(Geodatabase geodatabase, string featureClassName)
{try{FeatureClassDefinition featureClassDefinition = geodatabase.GetDefinition<FeatureClassDefinition>(featureClassName);featureClassDefinition.Dispose();return true;}catch{// 如果定义不存在,GetDefinition 会抛出异常return false;}
}
4 打开关系两个表之间的类
// 必须在 QueuedTask.Run() 中调用。
// 当与文件或企业级地理数据库一起使用时,此例程需要两个表名。
// 当与要素服务一起使用时,此例程需要图层 ID 或服务中暴露的表的名称(例如 "L0States")。
public IReadOnlyList<RelationshipClass> OpenRelationshipClassFeatureServices(Geodatabase geodatabase, string originClass, string destinationClass)
{return geodatabase.OpenRelationshipClasses(originClass, destinationClass);
}
5 从关系类获取相关要素类
public async Task GetFeatureClassesInRelationshipClassAsync()
{await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>{using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb")))){IReadOnlyList<RelationshipClassDefinition> relationshipClassDefinitions = geodatabase.GetDefinitions<RelationshipClassDefinition>();foreach (var relationshipClassDefinition in relationshipClassDefinitions){IReadOnlyList<Definition> definitions = geodatabase.GetRelatedDefinitions(relationshipClassDefinition,DefinitionRelationshipType.DatasetsRelatedThrough);foreach (var definition in definitions){System.Diagnostics.Debug.WriteLine($"关系类中的要素类是:{definition.GetName()}");}}}});
}
6 从 shapefile 数据存储打开要素类
public async Task OpenShapefileFeatureClass()
{await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>{var fileConnection = new FileSystemConnectionPath(new Uri("path\\to\\folder\\containing\\shapefiles"), FileSystemDatastoreType.Shapefile);using (FileSystemDatastore shapefile = new FileSystemDatastore(fileConnection)){FeatureClass taxLotsFeatureClass = shapefile.OpenDataset<FeatureClass>("TaxLots");FeatureClass manHolesFeatureClass = shapefile.OpenDataset<FeatureClass>("ManHoles.shp"); // 可以使用 .shp 扩展名,但不是必需的。Table taxDetailsTableWithoutExtension = shapefile.OpenDataset<Table>("TaxDetails");Table taxDetailsTable = shapefile.OpenDataset<Table>("TaxDetails.dbf");}});
}
7 打开 CAD 数据存储
public async Task OpenCADFeatureClass()
{await ArcGIS.Desktop.Framework.Threading.Tasks.QueuedTask.Run(() =>{var fileConnection = new FileSystemConnectionPath(new Uri("path\\to\\folder\\containing\\CAD"), FileSystemDatastoreType.Cad);using (FileSystemDatastore cadDatastore = new FileSystemDatastore(fileConnection)){// 注意 - 扩展名是必需的var cadDataset = cadDatastore.OpenDataset<FeatureClass>("hatchplayboundaries.dwg");// 注意引用要素类的模式。var cadfeatureClass = cadDatastore.OpenDataset<FeatureClass>("hatchplayboundaries.dwg:Polyline");int numRows = 0;using (var cursor = cadfeatureClass.Search()){while (cursor.MoveNext())numRows++;}}});
}