将 Geopackage PolyLines 加载到 ThinkGeo InMemoryFeatureLayer 中,以便与 C# 中的 ThinkGeo.UI.WinForms.MapView 一起使用

问题描述 投票:0回答:1

我正在尝试找到最有效的方法将 Geopackage polyLines 几何图形加载到 ThinkGeo InMemoryFeatureLayer 中。我有一个利用 Microsoft Sqlite 和 NetTopologySuite 套件/块的工作解决方案。我正在寻找将 geopackage 数据库直接加载到 InMemoryFeatureLayer 中的解决方案。

...
...
...

using Microsoft.Data.Sqlite;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;
using ThinkGeo.Core;
using ThinkGeo.UI.WinForms;

...
...
...

GeoPackageGeoReader gpkgReader = new GeoPackageGeoReader(); // for reading geopackage blob
WKBWriter wkbWriter = new WKBWriter();                      // for converting the GeoPackageGeoReader to standard wkb  

using (SqliteConnection SQLiteConnection = new SqliteConnection(@"Data Source=:memory:"))
{
    using (SqliteCommand SQLiteCommand = new SqliteCommand())
    {
        SQLiteCommand.CommandText =
            $@"

            ATTACH 'c:\temp\lines.gpkg' as lines_;
            CREATE TEMP TABLE IF NOT EXISTS lines
            AS SELECT * FROM lines_.lines;
            DETACH lines_;

            SELECT
                fid,
                _ogr_geometry_,
                street
            FROM lines;

            ";

        SQLiteCommand.Connection = SQLiteConnection;
        SQLiteConnection.Open();

        using (SqliteDataReader SQLiteDataReader = SQLiteCommand.ExecuteReader())
        {
            while (SQLiteDataReader.Read())
            {
                string id = SQLiteDataReader["fid"].ToString();
                byte[] geomData = (byte[])SQLiteDataReader.GetValue("_ogr_geometry_");

                Geometry geometry = gpkgReader.Read(geomData);      // get the geometry from the GeoPackage blob
                byte[] standardWkb = wkbWriter.Write(geometry);     // convert the gpkg blob to standard WKB

                Feature polyLineFeature = new Feature(standardWkb, id);
                polyLineFeature.Id = id;

                for (int i = 0; i < SQLiteDataReader.FieldCount; i++)
                {
                    string columnName = SQLiteDataReader.GetName(i);
                    object value = SQLiteDataReader.GetValue(i);

                    string valueAsString = value switch
                    {
                        null or DBNull => string.Empty, // handling null and DBNull values
                        string str => str,              // use it as is
                        _ => value.ToString()           // default - convert to string
                    };

                    polyLineFeature.ColumnValues.Add(columnName, valueAsString); // adding columns to feature
                }
                InMemoryFeatureLayer.InternalFeatures.Add(polyLineFeature); // adding row to InMemoryFeatureLayer
            }
            InMemoryFeatureLayer.BuildIndex();
        }
    }
}

...
...
...
geospatial geopackage
1个回答
0
投票

ThinkGeo 最近使用 GdalFeatureLayer 正式化了 GeoPackage 支持。 有一篇博客文章和示例这里

© www.soinside.com 2019 - 2024. All rights reserved.