在 C# 中創建和讀取 Shapefile

Shapefile 格式是一種地理空間矢量數據格式,用於顯示地理信息。我們可以在 Shapefile 中存儲點、線和多邊形要素的位置、幾何形狀和屬性。在本文中,我們將學習如何在 C# 中創建和讀取 Shapefile。

本文應涵蓋以下主題:

  1. 什麼是 Shapefile
  2. 如何使用 Shapefile
  3. 用於創建和讀取 Shapefile 的 C# API – .NET GIS 庫
  4. 使用 C# 創建 Shapefile
  5. 在 C# 中向現有 ShapeFile 添加特徵
  6. 使用 C# 讀取 Shapefile

什麼是形狀文件

shapefile 以矢量數據的形式表示地理空間信息,供 GIS 應用程序使用。它由 ESRI 作為開放規範開發,以促進 ESRI 與其他軟件產品之間的互操作性。它包含在地圖上繪製點、線或多邊形的幾何數據和數據屬性。 Shapefile 可以直接被多個 GIS 軟件程序讀取,例如 ArcGISQGIS

如何使用形狀文件

軟件應用程序不能使用獨立的 shapefile (.shp)。但是,可在 GIS 軟件中使用的有效 shapefile 應包含以下附加強製文件:

用於創建和讀取 Shapefile 的 C# API – .NET GIS 庫

要創建或讀取 shapefile,我們將使用 Aspose.GIS for .NET API。它允許渲染地圖、創建、讀取和轉換地理數據而無需額外的軟件。它支持使用 shapefile 以及其他幾種支持的文件格式

API 的 VectorLayer 類表示矢量圖層。它提供了各種屬性和方法來處理存儲在文件中的地理特徵集合。此類的 Create() 方法允許創建支持的矢量圖層。 Drivers 類為所有支持的格式提供驅動程序。 API 的 Feature 類表示由幾何和用戶定義的屬性組成的地理特徵。

下載 API 的 DLL 或使用 NuGet 安裝它。

PM> Install-Package Aspose.GIS

使用 C# 創建 Shapefile

我們可以按照以下步驟輕鬆地以編程方式創建 shapefile:

  1. 首先,使用 VectorLayer.Create() 方法創建一個圖層。
  2. 接下來,將 FeatureAttributes 添加到圖層的屬性集合中。
  3. 然後,使用 ConstructFeature() 方法創建 Feature 類的實例。
  4. 之後,設置不同屬性的值。
  5. 最後,使用 Add() 方法添加功能。

以下代碼示例顯示瞭如何在 C# 中創建 shapefile:

// 此代碼示例演示如何創建新的 shapefile。
// 創建一個新的形狀文件
using (VectorLayer layer = VectorLayer.Create(@"C:\Files\GIS\NewShapeFile_out.shp", Drivers.Shapefile))
{
    // 在添加功能之前添加屬性
    layer.Attributes.Add(new FeatureAttribute("name", AttributeDataType.String));
    layer.Attributes.Add(new FeatureAttribute("age", AttributeDataType.Integer));
    layer.Attributes.Add(new FeatureAttribute("dob", AttributeDataType.DateTime));

    // 添加功能並設置值
    Feature firstFeature = layer.ConstructFeature();
    firstFeature.Geometry = new Point(33.97, -118.25);
    firstFeature.SetValue("name", "John");
    firstFeature.SetValue("age", 23);
    firstFeature.SetValue("dob", new DateTime(1982, 2, 5, 16, 30, 0));
    layer.Add(firstFeature);

    // 添加另一個功能並設置值
    Feature secondFeature = layer.ConstructFeature();
    secondFeature.Geometry = new Point(35.81, -96.28);
    secondFeature.SetValue("name", "Mary");
    secondFeature.SetValue("age", 54);
    secondFeature.SetValue("dob", new DateTime(1984, 12, 15, 15, 30, 0));
    layer.Add(secondFeature);
}
使用 CSharp 創建形狀文件

使用 C# 創建 Shapefile。

使用 C# 向現有 ShapeFile 添加特徵

我們還可以按照以下步驟向現有 shapefile 添加新功能:

  1. 首先,使用 Drivers.Shapefile.EditLayer() 方法加載一個 shapefile。
  2. 接下來,使用 ConstructFeature() 方法創建 Feature 類的實例。
  3. 之後,設置不同屬性的值。
  4. 最後,使用 Add() 方法添加功能。

以下代碼示例展示瞭如何使用 C# 向現有 shapefile 添加要素。

// 此代碼示例演示如何編輯 shapefile。
// 文件路徑
string path = Path.Combine(dataDir, "NewShapeFile_out.shp");

// 編輯 Shapefile 圖層
using (var layer = Drivers.Shapefile.EditLayer(path))
{
    // 添加功能
    var feature = layer.ConstructFeature();
    feature.Geometry = new Point(34.81, -92.28);
    object[] data = new object[3] { "Alex", 25, new DateTime(1989, 4, 15, 15, 30, 0) };
    feature.SetValues(data);
    layer.Add(feature);
}

使用 C# 讀取 Shapefile

我們可以按照以下步驟從 shapefile 中讀取屬性:

  1. 首先,使用 Drivers.Shapefile.OpenLayer() 方法加載一個 shapefile。
  2. 遍歷層中的每個特徵。
  3. 遍歷屬性並顯示屬性詳細信息。
  4. 最後,檢查點幾何並讀取點。

以下代碼示例顯示瞭如何使用 C# 讀取 shapefile。

// 此代碼示例演示如何讀取新的 shapefile。
// 文件路徑
string path = Path.Combine(dataDir, "NewShapeFile_out.shp");

// 打開一個圖層
var layer = Drivers.Shapefile.OpenLayer(path);

foreach (Feature feature in layer)
{
    foreach (var attribute in layer.Attributes)
    {
        // 顯示屬性詳細信息
        Console.WriteLine(attribute.Name + " : " + feature.GetValue(attribute.Name));
    }

    // 檢查點幾何
    if (feature.Geometry.GeometryType == GeometryType.Point)
    {
        // 讀點
        Point point = (Point)feature.Geometry;
        Console.WriteLine(point.AsText() + " X: " + point.X + " Y: " + point.Y);
        Console.WriteLine("---------------------");
    }
}
name : John
age : 23
dob : 1982-02-05T16:30:00
POINT (33.97 -118.25) X: 33.97 Y: -118.25
---------------------
name : Mary
age : 54
dob : 1984-12-15T15:30:00
POINT (35.81 -96.28) X: 35.81 Y: -96.28
---------------------
name : Alex
age : 25
dob : 04/15/1989 15:30:00
POINT (34.81 -92.28) X: 34.81 Y: -92.28

獲得免費許可證

您可以 獲得免費的臨時許可證 來試用該庫,而沒有評估限制。

結論

在本文中,我們學習瞭如何

  • 以編程方式創建一個新的 shapefile;
  • 向 shapefile 添加新功能;
  • 編輯 shapefile 圖層;
  • 打開 shapefile 圖層並使用 C# 讀取屬性。

此外,您可以探索如何使用其他幾種 GIS 文件格式,並使用 文檔 了解有關 Aspose.GIS for .NET API 的更多信息。如有任何歧義,請隨時在我們的 論壇 上與我們聯繫。

也可以看看