.NET C# 使用GDAL读取FileGDB要素类

.NET C# 使用GDAL读取FileGDB要素类

目录

  • [.NET C# 使用GDAL读取FileGDB要素类](# 使用GDAL读取FileGDB要素类)
    • [1 环境](#1 环境)
    • [2 Nuget](#2 Nuget)
    • [3 Code](#3 Code)

1 环境

VisualStudio2022 + .NET6 + GDAL 3.7.5

2 Nuget

3 Code

csharp 复制代码
using OSGeo.OGR;
using OSGeo.OSR;

namespace TestGDAL
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string strDataPath = @".\Data\Default.gdb";

            GdalConfiguration.ConfigureGdal();
            GdalConfiguration.ConfigureOgr();

            // 打开 FileGDB
            using (var driver = Ogr.GetDriverByName("OpenFileGDB"))
                using (var dataSource = driver.Open(strDataPath, 0))
            {
                if (dataSource == null)
                {
                    Console.WriteLine("Failed to open FileGDB.");
                    return;
                }
				
                List<Layer> layers = new List<Layer>();
                int layerCount = dataSource.GetLayerCount();
                for (int i = 0; i < layerCount; i++)
                {
                	// 读取要素类
                    Layer layer = dataSource.GetLayerByIndex(i);
                    layers.Add(layer);
                }

                Console.WriteLine("GDB Layers: ");
                foreach (var layer in layers)
                {
                    string layerName = layer.GetName();
                    // 要素类集合类型
                    wkbGeometryType wkbGeometryType = layer.GetGeomType();
                    // 空间引用(坐标系)
                    SpatialReference spatialReference = layer.GetSpatialRef();
                    Console.WriteLine($"LayerName: {layerName}\tGeometryType: {wkbGeometryType}\tSpatialReference: {spatialReference.GetName()}");
                }
                Console.WriteLine();

                Console.WriteLine("Geometry: ");
                foreach (var layer in layers)
                {
                    Console.WriteLine(layer.GetName());
                    Console.WriteLine("Geometry Wkt");
					// 将读指针设置初始位置
                    layer.ResetReading();
                    Feature feature;
                    while ((feature = layer.GetNextFeature()) != null)
                    {
                        try
                        {
                        	// 几何图形
                            Geometry geometry = feature.GetGeometryRef();
                            int v = geometry.ExportToWkt(out string wkt);
                            Console.WriteLine(wkt);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                        finally
                        {
                            feature.Dispose();
                        }
                    }
                }
                Console.WriteLine();

                Console.WriteLine("Attribute Table: ");
                foreach (var layer in layers)
                {
                    Console.WriteLine(layer.GetName());
                    FeatureDefn featureDefn = layer.GetLayerDefn();
                    List<FieldDefn> fieldDefns = new List<FieldDefn>();
                    int fieldCount = featureDefn.GetFieldCount();
                    for (int i = 0; i < fieldCount; i++)
                    {
                    	// 获取字段
                        FieldDefn fieldDefn = featureDefn.GetFieldDefn(i);
                        fieldDefns.Add(fieldDefn);
                    }
                    Console.WriteLine(string.Join(",", fieldDefns.Select(f => f.GetName())));

                    layer.ResetReading();
                    Feature feature;
                    while ((feature = layer.GetNextFeature()) != null)
                    {
                        try
                        {
                            List<string> values = new List<string>();
                            for (int i = 0; i < fieldDefns.Count; i++)
                            {
                                FieldDefn fieldDefn = fieldDefns[i];
                                string fieldName = fieldDefn.GetName();
                                // 字段类型
                                FieldType fieldType = fieldDefn.GetFieldType();
                                // 获取字段值
                                switch (fieldType)
                                {
                                    case FieldType.OFTInteger:
                                        int valInt = feature.GetFieldAsInteger(fieldName);
                                        values.Add(valInt + "");
                                        break;
                                    case FieldType.OFTString:
                                        string valStr = feature.GetFieldAsString(fieldName);
                                        values.Add(valStr);
                                        break;
                                    case FieldType.OFTWideString:
                                        string valWidStr = feature.GetFieldAsString(fieldName);
                                        values.Add(valWidStr);
                                        break;
                                    case FieldType.OFTDate:
                                        feature.GetFieldAsDateTime(
                                            fieldName,
                                            out int dyear,
                                            out int dmonth,
                                            out int dday,
                                            out int dhour,
                                            out int dminute,
                                            out float dsecond,
                                            out int dtzFlag);
                                        DateTime valDate = new DateTime(dyear, dmonth, dday, dhour, dminute, Convert.ToInt32(dsecond));
                                        values.Add(valDate.ToString());
                                        break;
                                    case FieldType.OFTTime:
                                        feature.GetFieldAsDateTime(
                                            fieldName,
                                            out int tyear,
                                            out int tmonth,
                                            out int tday,
                                            out int thour,
                                            out int tminute,
                                            out float tsecond,
                                            out int ttzFlag);
                                        DateTime valTime = new DateTime(tyear, tmonth, tday, thour, tminute, Convert.ToInt32(tsecond));
                                        values.Add(valTime.ToString());
                                        break;
                                    case FieldType.OFTDateTime:
                                        feature.GetFieldAsDateTime(
                                            fieldName,
                                            out int dtyear,
                                            out int dtmonth,
                                            out int dtday,
                                            out int dthour,
                                            out int dtminute,
                                            out float dtsecond,
                                            out int dttzFlag);
                                        DateTime valDateTime = new DateTime(dtyear, dtmonth, dtday, dthour, dtminute, Convert.ToInt32(dtsecond));
                                        values.Add(valDateTime.ToString());
                                        break;
                                    case FieldType.OFTInteger64:
                                        long valLon = feature.GetFieldAsInteger64(fieldName);
                                        values.Add(valLon + "");
                                        break;
                                    default:
                                        break;
                                }
                            }
                            Console.WriteLine(string.Join(",", values));
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex);
                        }
                        finally
                        {
                            feature.Dispose();
                        }
                    }
                }
            }
        }
    }
}
相关推荐
向宇it2 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
数据的世界012 小时前
.NET开发人员学习书籍推荐
学习·.net
向宇it4 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎
paixiaoxin4 小时前
CV-OCR经典论文解读|An Empirical Study of Scaling Law for OCR/OCR 缩放定律的实证研究
人工智能·深度学习·机器学习·生成对抗网络·计算机视觉·ocr·.net
坐井观老天8 小时前
在C#中使用资源保存图像和文本和其他数据并在运行时加载
开发语言·c#
supermapsupport10 小时前
iClent3D for Cesium 实现无人机巡检飞行效果
gis·cesium·supermap·webgis
pchmi10 小时前
C# OpenCV机器视觉:模板匹配
opencv·c#·机器视觉
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭12 小时前
C#都可以找哪些工作?
开发语言·c#
19004312 小时前
.NET重点
.net
m0_6632340112 小时前
在 .NET 5.0 运行 .NET 8.0 教程:使用 ASP.NET Core 创建 Web API
前端·asp.net·.net