Reading Geometry using C#

977 views
Skip to first unread message

Joe

unread,
Jan 11, 2011, 10:30:58 AM1/11/11
to SpatiaLite Users
How do I read the 'Geometry' from a spatialite table so I can display
it as text?

I eventually want to add these features to a map using DotSpatial. If
anyone has code they could share I'd really appreciate it.

Thanks, Joe

Shep

unread,
Jan 11, 2011, 10:42:17 AM1/11/11
to SpatiaLite Users
Joe,

Here's a snippet of our code that shows you how to do what I think you
are asking. AirBoundCache is just a custom object of ours.

Matt


string sql = "SELECT Class, Name, Type, Id, Placemark, AsText(Geom) as
geom FROM AirBoundCache as point; "

using (SQLiteCommand cmd = new SQLiteCommand(sql, _conn))
{
using (SQLiteDataReader reader = cmd.ExecuteReader())
{

while (reader.Read())
{

AirBoundCache a = new AirBoundCache();

a.Class = reader["Class"] == DBNull.Value ? (string)null :
reader["Class"].ToString();
a.Name = reader["Name"] == DBNull.Value ? (string)null :
reader["Name"].ToString();
a.Type = reader["Type"] == DBNull.Value ? (string)null :
reader["Type"].ToString();
a.Id = Convert.ToInt32(reader["Id"]);
a.Placemark = reader["Placemark"] == DBNull.Value ?
(string)null : reader["Placemark"].ToString();
a.Geom =
reader["geom"] == DBNull.Value ? (string)null :
reader["geom"].ToString();
AirBoundCaches.Add(a);

Joe

unread,
Jan 11, 2011, 11:55:30 AM1/11/11
to SpatiaLite Users
Matt,

Your snippet helped a lot - Thanks.

What are you using to display your layers?

We are having difficulty with the .Net version of SQLite, spatiaLite,
DotSpatial, Windows 7, and VS2010 (.Net 4.0) 32bit app. When we close
the SQLite connection there are numerous errors.

Any ideas.

--------
Windows has triggered a breakpoint in TestDotSpatial.exe.

This may be due to a corruption of the heap, which indicates a bug in
TestDotSpatial.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while TestDotSpatial.exe
has focus.

The output window may have more diagnostic information.

Thanks, Joe

Matthew Shepherd

unread,
Jan 11, 2011, 12:36:28 PM1/11/11
to spatiali...@googlegroups.com
We are using the Google Earth plugin.  We haven't moved to 4.0 yet and we are using the SharpMap library instead of DotSpatial, everything else is the same as what you are using.  We don't have any issues creating or closing connections.  Here are the methods we use to create our connection and load the Spatialite extension.  The connectionstring is pretty standard and stored in an app.config.

private void CreateDBConnection()
        {
            // Assume failure.
            _conn = null;

            // Create the DbConnections.
            try
            {
                _conn = new SQLiteConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ToString());
                _conn.Open();
                Object retval = LoadSpatialite();
            }
            catch (Exception ex)
            {
                // Set the connection to null if it was created.
                if (_conn != null)
                    _conn = null;

                Logger.WriteError(ex.Message);
            }
        }

private Object LoadSpatialite()
        {
            return new SQLiteCommand("SELECT load_extension('" + _libSpatialitePath + "');", _conn).ExecuteScalar();
        }


Thanks, Joe

--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To post to this group, send email to spatiali...@googlegroups.com.
To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spatialite-users?hl=en.


Joe

unread,
Jan 11, 2011, 12:59:37 PM1/11/11
to SpatiaLite Users
Matt,

That is pretty much what we do to connect also. We didn't have any
close connection problems until we moved to .Net
SQLite(System.Data.SQLite.dll) and VS2010 .Net 4.0.

I'm not sure the .Net SQLite is the latest version of SQLite and it
may not be compatible with the latest spatialite
('libspatialite-2.dll').

Maybe I'll back off to 3.5 and see what happens?

Thanks,
Joe

Matthew Shepherd

unread,
Jan 11, 2011, 1:05:00 PM1/11/11
to spatiali...@googlegroups.com
It's possible.  We too are using System.Data.Sqlite.  Let me know if your move back to 3.5 works.


Thanks,
Joe

FObermaier

unread,
Jan 13, 2011, 3:57:44 AM1/13/11
to SpatiaLite Users
To work correctly with VS2010, .Net40 you need version 1.0.66 of
SQLite.Net
HTH FObermaier

On 11 Jan., 19:05, Matthew Shepherd <shepdaw...@gmail.com> wrote:
> It's possible.  We too are using System.Data.Sqlite.  Let me know if your
> move back to 3.5 works.
>
> On Tue, Jan 11, 2011 at 10:59 AM, Joe <joe.wol...@gmail.com> wrote:
> > Matt,
>
> > That is pretty much what we do to connect also.  We didn't have any
> > close connection problems until we moved to .Net
> > SQLite(System.Data.SQLite.dll) and VS2010 .Net 4.0.
>
> > I'm not sure the .Net SQLite is the latest version of SQLite and it
> > may not be compatible with the latest spatialite
> > ('libspatialite-2.dll').
>
> > Maybe I'll back off to 3.5 and see what happens?
>
> > Thanks,
> > Joe
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "SpatiaLite Users" group.
> > To post to this group, send email to spatiali...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > spatialite-use...@googlegroups.com<spatialite-users%2Bunsu...@googlegroups.com>
> > .

Joe

unread,
Jan 13, 2011, 9:56:31 AM1/13/11
to SpatiaLite Users
Thanks for the link.
I was using the 1.0.66 version of SQLite.Net

I think my original problem was that I was using the latest not stable
libspatialite-2.dll 2.4 from 12/17/2010.

After installing the projects from the DotSpatial download I used the
libspatialite-2 DLL from those projects (1/8/2010) and everything
worked fine.

Now I'll try the new DotSpatial.Data.Database and
DotSpatial.Data.Database.SpatiaLite2 DLLs.

Thanks again,
Joe


On Jan 12, 5:15 pm, FObermaier <felix.oberma...@netcologne.de> wrote:
> Joe,
>
> have a look at:http://dotspatial.codeplex.com/workitem/206
>
> Hth FObermaier
>

Adam Estrada

unread,
Jan 13, 2011, 9:59:06 AM1/13/11
to spatiali...@googlegroups.com
Like Matt mentioned, SharpMap does have a really good provider for Spatialite. http://geobabble.wordpress.com/2009/04/11/spatialite-provider-code-for-sharpmap-09/

Adam

--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To post to this group, send email to spatiali...@googlegroups.com.
To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages