C# reading SpatiaLite geometry

174 Views Asked by At

I am trying to read GIS geometry data from a SpatiaLite database with C# .NET Framework. I have downloaded the SpatialLite extensions from here: http://www.gaia-gis.it/gaia-sins/windows-bin-x86/ and added them to my output folder. I use the LoadExtension() function to load it.

According to this tutorial the GIS geometry in a SpatiaLite database is stored as blob storage and can be readed with the AsText() or GeomFromWKB() functions. If I retrieve the GEOMETRY column from the table COVERAGES it is returning a byte[]. But when I try these functions on my database, it is returning null. It seems that the SpatiaLite functions are not working. If I load the database in QGIS it is displaying the GIS geometry fine.

Any ideas how to resolve this?

string connectString = "Data Source=" + @"{myFile}";

SQLiteConnection connection = new SQLiteConnection(connectString);

// Open the database and load in the Spatialite extension
connection.Open();
connection.EnableExtensions(true);
connection.LoadExtension("mod_spatialite");

string sql = "SELECT GEOMETRY from COVERAGES";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                //returns byte[]
                Console.WriteLine(reader.GetValue(i));
            }

        }
    }
}

sql = "SELECT AsText(GEOMETRY) from COVERAGES";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                //returns null
                Console.WriteLine(reader.GetValue(i));
            }

        }
    }
}

sql = "SELECT GeomFromWKB(GEOMETRY) from COVERAGES";
using (SQLiteCommand command = new SQLiteCommand(sql, connection))
{
    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                //returns null
                Console.WriteLine(reader.GetValue(i));
            }

        }
    }
}
0

There are 0 best solutions below