read, manipulate, and display geometry field in c# .net

2,635 views
Skip to first unread message

Richard Males

unread,
Jun 27, 2012, 6:57:49 PM6/27/12
to spatiali...@googlegroups.com
I have previously successfully read non-geometry data from a spatialite database in a VS 2010 C# application (simple transportation network routing), and obtained points in an xy coordinate system that I use for display and animation in my program.  I obtain my x and y coordinates through the sql statement below, so the transformation happens on the spatialite side, not in my program.:

                using (SQLiteCommand command = new SQLiteCommand(@"SELECT LinkIdentifier,LinkType, NAME, AppCodes, Speed, Distance, TTimeAB, TTimeBA, FTimeAB, FTimeBA, CapacityAB, CapacityBA, StartNodeID, EndNodeID, EndNodeName, StartNodeName,
x(transform(StartPoint(geometry),ProjectSRID)) as startEasting,
y(transform(StartPoint(geometry),ProjectSRID)) as startNorthing,
x(transform(EndPoint(geometry),ProjectSRID)) as endEasting,
y(transform(EndPoint(geometry),ProjectSRID)) as endNorthing FROM NetworkLinks,Project", connection))


I am interested in doing something similar, but this time by directly using the geometry data for manipulation and display within the C# program, that is, I would just grab the geometry fields via SQL statements.  I will have point, linestring, and polygon data.  My questions:
  1. What is the optimal C# datatype for storing geometry?
  2. Are there libraries that support this kind of application, with the relevant needed spatial transformations?
  3. Is DotSpatial a good option for display of such data?  Any others recommended?

And of course, any example code or code snippets would be most welcome.

Thanks in advance.

Dick

Vittorio Maniezzo

unread,
Jun 28, 2012, 3:49:10 AM6/28/12
to spatiali...@googlegroups.com
Just to contribute, I routinely work in .net 4.0. Used to rely on mapscript/postgis now moved to geoserver/spatialite.
No need for special data structures.
Vittorio


Brad Hards

unread,
Jun 28, 2012, 5:52:30 AM6/28/12
to spatiali...@googlegroups.com, Richard Males
On Thursday 28 June 2012 08:57:49 Richard Males wrote:
> I am interested in doing something similar, but this time by directly using
> the geometry data for manipulation and display within the C# program, that
> is, I would just grab the geometry fields via SQL statements. I will have
> point, linestring, and polygon data. My questions:
>
> 1. What is the optimal C# datatype for storing geometry?
binary is probably the only thing sensible for the blob. I'd suggest a custom
type for each of the geomety types.

> 2. Are there libraries that support this kind of application, with the
> relevant needed spatial transformations?
I have the code to translate between the binary blob geometry and a C# object
(without libspatialite). I expect to release it on the weekend - I just need
to sort out the required functions. You'll find it on the normal SpatiaLite
website when its available.
No transformations though - you'd need to do those with another tool.

> 3. Is DotSpatial a good option for display of such data? Any others
> recommended?
I confess I haven't ever heard of it before. Looks like it does have some
spatialite support.

I'm not convinced that I've really answered the question though.

At some point you need to convert the spatialite-specific binary into something
you can use. What you use depends on what that interface looks like. Maybe you
can ask a more specific question?

Brad

Vittorio Maniezzo

unread,
Jun 28, 2012, 7:56:08 AM6/28/12
to spatiali...@googlegroups.com, Richard Males
Coming to think about it, the best data structure for storing a spatialite geometry is probably an in-memory spatialite database.

Vittorio

Brad Hards

unread,
Jun 29, 2012, 6:05:46 AM6/29/12
to spatiali...@googlegroups.com
On Thursday 28 June 2012 19:52:30 Brad Hards wrote:
> I have the code to translate between the binary blob geometry and a C#
> object (without libspatialite). I expect to release it on the weekend - I
> just need to sort out the required functions. You'll find it on the normal
> SpatiaLite website when its available.
This hit a showstopper issue today. It turns out that you can write SQLite
functions in C#, but that they can't do SQL. That means I can't write the
RTreeAlign function.

I'm researching options at the moment.

Brad

Felix Obermaier

unread,
Jun 29, 2012, 6:15:57 AM6/29/12
to spatiali...@googlegroups.com
For reading and writing spatialite geometries you might want to take a look at
NetTopologySuite.IO.SpatiaLite project on
http://code.google.com/p/nettopologysuite


http://code.google.com/p/nettopologysuite/source/browse/#svn%2Ftrunk%2FNetTopologySuite.IO%2FNetTopologySuite.IO.SpatiaLite

Hth FObermaier


Brad Hards <br...@frogmouth.net> hat am 29. Juni 2012 um 12:05 geschrieben:
> --
> 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.
>

Richard Males

unread,
Jun 29, 2012, 11:03:24 AM6/29/12
to spatiali...@googlegroups.com, Felix Obermaier
Thank you for your reply.  I believe that you also post on DotSpatial.   Can you compare the advantages/disadvantages of each as regards to reading, displaying, and manipulating data drawn from spatialite geometry?   Can you possible point me to any code examples (did not find any for NTS).

Thanks.

Dick

FObermaier

unread,
Jun 29, 2012, 11:26:45 AM6/29/12
to spatiali...@googlegroups.com, Felix Obermaier
I don't have much experience using DotSpatial, I believe they have their own provider.

There is a test project that should get you started:
http://code.google.com/p/nettopologysuite/source/browse/trunk/NetTopologySuite.IO/NetTopologySuite.IO.Tests/

especially SpatiaLiteFixture.cs
Hth FObermaier

Jim

unread,
Jul 16, 2012, 12:13:20 PM7/16/12
to spatiali...@googlegroups.com, Felix Obermaier


It may be crude, but I convert geometries to and from strings using the SQLite functions provided by spatialite.

Lookup  'ST_Astext' and 'ST_GeometryFromText' (Not sure of the spelling on this one).

Regards,
Jim

Richard Males

unread,
Jul 17, 2012, 9:54:21 AM7/17/12
to spatiali...@googlegroups.com, Felix Obermaier
Thank you for your response.   I am assuming that you do this in the SQL command that reads the spatialite database.   I am interested in reading the geometry into a c# construct of some kind, then being able to work with it in c#, primarily for display.  If I cannot get that to work, then I will explore what I take to be your approach.

Dick

bohne

unread,
Jul 17, 2012, 10:32:37 AM7/17/12
to spatiali...@googlegroups.com, Felix Obermaier
Hi,

I think I created a small library which does what you want. Perhaps you want to have look at it. 
Small example:

SQLiteConnection con=new SQLiteConnection(@"Data Source =D:\vector.sqlite");
            SQLiteDataReader reader = GetDataReader("Select Geometry from Table", con);
            while(reader.Read())
            {
            SpatiaLiteNET.Geometry geom = SpatiaLiteNET.Geometry.CreateGeometry(reader.GetValue(0) as byte[]);
            if(geom is SpatiaLiteNET.Polygon)
            {
                
            }
            
            
            }
SpatiaLiteNET.zip

Felix Obermaier

unread,
Jul 17, 2012, 10:55:06 AM7/17/12
to spatiali...@googlegroups.com
Dick,

if you only want to display the geometry, you can use either SharpMap or
DotSpatial.
Both have providers that can read SpatiaLite geometries and display them on a
map.

Hth FObermaier


Richard Males <rbm...@gmail.com> hat am 17. Juli 2012 um 15:54 geschrieben:
> --
> You received this message because you are subscribed to the Google Groups
> "SpatiaLite Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/spatialite-users/-/41c5C76gYpkJ
> <https://groups.google.com/d/msg/spatialite-users/-/41c5C76gYpkJ> .

Richard Males

unread,
Jul 17, 2012, 11:37:01 AM7/17/12
to spatiali...@googlegroups.com, Felix Obermaier
Thank you, this looks like it will be quite useful.  I have attempted to implement this in a test application.  However, GetDataReader is not found.   Can you tell me where this might be located?

Richard Males

unread,
Jul 17, 2012, 12:07:31 PM7/17/12
to spatiali...@googlegroups.com, Felix Obermaier
Thank you for your response.   My current need is just to read from the spatialite database and display on screen, but in the future I will want to incorporate the additional geospatial manipulation capabilities that seem to be provided by NTS.  Full confession, I am an engineer by training, not a computer scientist, and my work is primarily in Monte Carlo simulation of water resources efforts.

I would like to choose a combination of platforms that will best serve me over the next few years, I have definitely settled on spatialite as opposed to Postgres/PostGIS or MS Sql server, QGIS, and c# .NET 4.0 (I am debating using WPF instead of Windows Forms).   I have satisfactorily developed Monte Carlo simulation models in c# reading from a spatialite database (a highway network, with locational information already in planar coordinates within the database, so no spatial manipulation required).   So now the question comes down to the preferred additional environment for handling spatial data.   Perhaps I can prevail upon you to describe the advantages/disadvantages of NTS, SharpMap, and DotSpatial, if you are willing to do so.

Frankly, I have been struggling to follow the code sample in SpatialLiteFixture.cs, I am only recently acquainted with NUnit testing.    I am continuing to try to work my way through this, but if you happen to have a simple example code snippet of reading an IGeometry from spatialite, I will work on the issues of display based on what is provided in BasicTests.cs

Dick
>  To post to this group, send email to spatialite-users@googlegroups.com.
>  To unsubscribe from this group, send email to
> spatialite-users+unsubscribe@googlegroups.com.

Thomas Bonsiepen

unread,
Jul 17, 2012, 12:26:24 PM7/17/12
to spatiali...@googlegroups.com
Its a function which executes the DataReader s.th like this

SqLiteCommad cmd=new SqLiteCommand("Select * from Table,myConnection);
SQLiteReader reader =cmd.ExecuteReader();
while(reader.Read())
{
}

2012/7/17 Richard Males <rbm...@gmail.com>
To view this discussion on the web visit https://groups.google.com/d/msg/spatialite-users/-/7M8YWE4fd9wJ.

To post to this group, send email to spatiali...@googlegroups.com.
To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.

Richard Males

unread,
Jul 17, 2012, 3:16:00 PM7/17/12
to spatiali...@googlegroups.com
Thank you.  This gets me half-way to where I want to go.  This is what worked for me, to this point:

  private void TestSQLiteDLLReader()
        {
            SQLiteConnection con = new SQLiteConnection(@"Data Source =" + fileName);
            con.Open();

            SQLiteCommand cmd = new SQLiteCommand("Select * from Reaches",con);
            DbDataReader reader = cmd.ExecuteReader();
            byte[] blob;
            LineString lineGeometry = new LineString();

            while (reader.Read())
            {
                blob=(byte [])reader.GetValue(12);
               
                 SpatiaLiteNET.Geometry geom = SpatiaLiteNET.Geometry.CreateGeometry(blob);
                  if (geom is SpatiaLiteNET.LineString)
                  {
                    
                      lineGeometry= (LineString) SpatiaLiteNET.LineString.CreateGeometry(blob);

                      MessageBox.Show(lineGeometry.NumberOfPoints.ToString());
                     
                  }

                }
        }
My particular database has a linestring geometry in position 12 of the Reaches table, and it reads correctly.

I do note that I was getting an error associated with SpatiaLiteNET, running against .NET 4.0 as follows:
'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information

I was able to follow some instructions on the internet to revise app.config to handle this problem, for a windows forms application, I have not yet resolved it for the WPF application I am testing.

At this point, I am interested in doing something with the geom object that is now available having been read from the database, in particular using NTS functions, but I am unclear as to how to convert either the blob or SpatiaLiteNET geometry into something that can be used by NTS.

Dick

Thomas Bonsiepen

unread,
Jul 17, 2012, 3:26:43 PM7/17/12
to spatiali...@googlegroups.com

If you Like, i can send you my Visual Studio Solution, so you can Compiler it for your needs...?

To view this discussion on the web visit https://groups.google.com/d/msg/spatialite-users/-/V9QwQh_S6dIJ.
To post to this group, send email to spatiali...@googlegroups.com.
To unsubscribe from this group, send email to spatialite-use...@googlegroups.com.

Richard Males

unread,
Jul 17, 2012, 3:30:34 PM7/17/12
to spatiali...@googlegroups.com
I will be happy to do so, test it, and repost for you and others.

Thank you.

bohne

unread,
Jul 18, 2012, 1:37:42 AM7/18/12
to spatiali...@googlegroups.com
No problem. Here is my VS2008 Solution.
SpatiaLiteNET.zip

Felix Obermaier

unread,
Jul 18, 2012, 7:33:30 AM7/18/12
to spatiali...@googlegroups.com
Hello Dick,

I've setup a quick-and-dirty sample project that should guide you to using
SpatiaLite database with GeoAPI/NTS geometries.
Due to my ignorance of WPF and all that is to it, I hesitate to add it to the
NTS repository. Instead you can download it from here:
http://www.ivv-aachen.de/webdaten/NetTopologySuite.Samples.Wpf.zip
<http://www.ivv-aachen.de/webdaten/NetTopologySuite.Samples.Wpf.zip>


It will stay there for a month.



Instructions:
1. Checkout latest source from NTS
2. Download the above file
3. Extract the contents to the NTS checkout folder
4. Open NetTopologySuite.Net.VS2010.sln, add NetTopologySuite.Samples.Wpf
project


The use of GaiaGeoReader and GaiaGeoWriter is illustrated in the FeatureGroup
class.



Hth FObermaier



Richard Males <rbm...@gmail.com> hat am 17. Juli 2012 um 18:07 geschrieben:


> Thank you for your response. My current need is just to read from the
> > Richard Males < rbm...@gmail.com <mailto:rbm...@gmail.com> > hat am 17.
> > > <https://groups.google.com/d/msg/spatialite-users/-/41c5C76gYpkJ>
> > > spatiali...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > <mailto:spatiali...@googlegroups.com>
> > > spatialite-use...@googlegroups.com.
> > > For more options, visit this group at
> > > <mailto:spatialite-users%2Bunsu...@googlegroups.com>
> > > http://groups.google.com/group/spatialite-users?hl=en.
> > >
> > <http://groups.google.com/group/spatialite-users?hl=en>
> > >
> --
> You received this message because you are subscribed to the Google Groups
> "SpatiaLite Users" group.
> To view this discussion on the web visit
> <http://groups.google.com/group/spatialite-users?hl=en>
> https://groups.google.com/d/msg/spatialite-users/-/7M8YWE4fd9wJ
> <https://groups.google.com/d/msg/spatialite-users/-/7M8YWE4fd9wJ> .
> To post to this group, send email to spatiali...@googlegroups.com.
> To unsubscribe from this group, send email to
> spatialite-use...@googlegroups.com.

Richard Males

unread,
Jul 18, 2012, 10:28:25 AM7/18/12
to spatiali...@googlegroups.com
Thank you very much, this is very useful, understandable.   I have recompiled using Visual Studio 2010 Express, debug, targeting .net 4.0, the dll is attached for those who are interested.  Using it avoids the version conflict noted previously.
SpatiaLiteNET4.zip

Richard Males

unread,
Jul 18, 2012, 1:15:47 PM7/18/12
to spatiali...@googlegroups.com, Felix Obermaier

Thank you.   I followed the steps you suggest.   I am only recently working in wpf, so not very familiar with it, but when I attempt to run, after getting everything going, I get the following error on attempting to launch the sample:
'Initialization of 'NetTopologySuite.Samples.Wpf.MainWindow' threw an exception.' Line number '3' and line position '64'.
and this apparently refers to the line:
xmlns:Wpf="clr-namespace:NetTopologySuite.Samples.Wpf" in MainWindow.xaml.

I will attempt to sort this out on my own, learn something more about WPF in the process, but if something obvious occurs to you, please advise.

Thanks.

Dick
Reply all
Reply to author
Forward
0 new messages