SharpGIS

#GIS from a .NET developer's perspective

Using OGR with SharpMap

Christian Graefe recently posted an update on his additions to the GDAL/OGR  C# wrapper and his OGR provider for SharpMap.

Apart from the data sources that SharpMap already supports, Christians OGR provider adds support for "MapInfo File", "TIGER", "S57", "DGN", "Memory", "CSV", "GML", "Interlis 1", "Interlis 2", "SQLite" and "ODBC". If I'm not completely wrong he is also working on a GDAL raster layer as well...

Look at http://www.volley-boy.de/home/sharpmap.aspx

You will need to install FWTools to use it.

Great work Christian !

Using NetTopologySuite in SharpMap

I've created a small demo showing how to utilize NetTopologySuite in SharpMap. The great thing about NetTopologySuite is that is has a bunch of geoprocessing methods that SharpMap doesn't have (yet anyway). The demo uses the NetTopologySuite library for the following:

  1. Apply some geoprocessing to the geometries (in this case it puts a buffer on all rivers)
  2. Uses NTS for making exact intersection queries. By clicking on an object the buffered feature(s) are highlighted and attribute data shown beneath.

The basics are:

Create a NTS datasource based on another datasource which takes a delegate to apply to all geometries:

NtsProvider ntsDataSource = new NtsProvider(myShapeFileDatasource, CreateBuffers);

Create the geometry-operation delegate method which should be applied to all features:

private void CreateBuffers(List<GisSharpBlog.NetTopologySuite.Features.Feature> features)
{
   foreach (GisSharpBlog.NetTopologySuite.Features.Feature feature in
features)
   feature.Geometry = feature.Geometry.Buffer(0.5);
}

The 'ntsDataSource' object can then be assigned as datasource to a vectorlayer.

Clicking and highlighting features are done by reacting to a click-event on an ImageButton (in ASP.NET) or a PictureBox (in Windows.Forms).

protected void imgMap_Click(object sender, ImageClickEventArgs e)
{
   //Convert click-point in image coordinates to world coordinates
   Point ClickPnt = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
  
SharpMap.Data.FeatureDataSet ds = new SharpMap.Data.FeatureDataSet();
  
//Execute click-query on first layer in layers collection
  
(myMap.Layers[0] as SharpMap.Layers.VectorLayer).DataSource.ExecuteIntersectionQuery(ClickPnt, ds);
   if (ds.Tables.Count > 0) //We have a result
  
{
     
//Add clicked features to a new selection layer
     
SharpMap.Layers.VectorLayer laySelected = new SharpMap.Layers.VectorLayer("Selection");
      laySelected.DataSource =
new GeometryProvider(ds.Tables[0]);
      laySelected.Style.Fill =
new System.Drawing.SolidBrush(System.Drawing.Color.Yellow);
      myMap.Layers.Add(laySelected);
   }
   //Call method that renders a new map and displays it to the user
  
GenerateMap();
}

 

Download NetTopologySuite_Demo.zip (544,32 KB)

SharpMap v0.9 RC1 online !

SharpMap v0.9RC1 is now available for download from the SharpMap website.

Thanks to all the people who kept sending feedback and bug reports!

I've resolved a lot of bugs since beta 1, as well as added some obviously missing properties and methods in the API. Especially the Shapefile reader has been enhanced to handle a lot of special cases (in one case it was even able to render a file that ArcGIS claimed to be corrupt). On-the-fly transformation has also been completed and now supports transformation between geographic and projected coordinate systems.

Thanks to Humberto Ferreira for providing us with an Oracle Spatial provider as well. SharpMap now supports data from ESRI Shapefiles, PostGIS, Oracle, Microsoft SQL Server, Access and ECW raster.

Last but not least, I've spent a great deal of time enhancing the documentation, so go take a look at that as well. There are lots of code-samples hidden in there as well.

Make Microsoft SQL Server geospatial

I’ve always thought that on the spatial support, MSSQL was way behind many of the other database servers, in its lack of supporting storage of geometry. With the new .NET CLR you can actually add your own .NET-based object types and I’ve also tried implementing the Simple Features Specification in SQL Server. There are some limitations that made me give this up though. First of all, a user data type cannot be more than 8000 bytes. That is at most no more than 500 vertices in a geometry object, which is far too little for an accurate coastline for instance. Another problem is that SQL Server doesn’t support inheritance chains, so you can’t make a good object-oriented implementation of your datatype either.

…so yesterday I went for a completely different and much simpler approach. I decided to just store the geometry as Well-Known Binary in an image column. The reason for using an image column is that it can hold up to 2Gb of data, which should be sufficient for most geometry objects ;-). A binary field has the same 8000 byte limitation as UDT so this is no good. In addition to the geometry field, I create four real-type fields, holding the min/max values of the geometry envelope. This makes it efficient to do boundingbox based queries on the data. Any additional fields would be attributes for the geometry object.

I implemented the whole thing using SharpMap. First I created a small upload application that takes a shapefile, creates a table in the database and uploads the geometry and attributes to it. SharpMap has the necessary data readers and WKB formatters for this. The second part was to create a data provider from which SharpMap could draw from. I more or less based this on the PostGreSQL/PostGIS data provider for SharpMap, by changing the boundingbox query to use the four envelope fields. All this was not much more than an hour’s work, so it is very simple to accomplish.

I must say I was very surprised by the performance of the approach. It is just slightly faster than the shapefile data provider, which used to be the fastest data provider for SharpMap. In comparison the PostGreSQL/PostGIS is generally 4-6 times slower.

I have created a small demo web-application you can download from here. It contains two pages: one for uploading to the database, and one for rendering a layer from the database. All you need to do is to add an empty SQL Server 2005 Express database to the \App_Data\ folder and name id "GeoDatabase.mdf".

Download SharpMapSqlServer.zip (181,74 KB) (updated May 20, 2006)

Update: The MsSqlProvider is now also included in v0.9RC1, including a method for uploading from any of the SharpMap datasources to MS SQL.

Transformation problems with NetTopologySuite and GeoTools.NET

The last couple of days I've been working on doing on-the-fly transformations in SharpMap. I started out by implementing all the interfaces in the OGC "Coordinate Transformation Services" specification. These specifications are just great and makes my work of designing APIs so much eaier. Anyway when I started at the actual implementation I also looked at NetTopologySuite (which is a .NET port of Java Topology Suite and GeoTools.NET for a little help on the more complex transformation formulas.

It didn't really work out. All my results was either wrong, resulted in Overflow and DivisionByZero exceptions and NaN values. All I could then do was working through the EPSG transformation formulas, which was I was trying to avoid in the first place (oh by the way, EPSG recently released v7.10 of their coordinate system database).

Well I finally hacked it, and my conclusion is that there are several problems in the NTS code that causes very inaccurate (if not completely wrong) transformations. Problems like an incorrect constant for converting between radians and degrees, transformation formulas that are wrong (at least the Mercator projection - haven't checked the others yet), transformation-factories that doesn't respect the order of source and target coordinate systems (it will always transform from geographic to projected coordsys), and using the TransformList(array-of-points) method in many cases gives a different result than Transform(point).

I emailed my findings to Diego and Andrew who's doing a great job on NTS, so that they can benefit from this work well, but this is just to warn you people out there that NTS is probably giving your incorrect or inaccurate transformation results, until the bug is corrected. This is by no means Diego's fault; I guess he just copied it from GeoTools.NET (since the code is more or less the same), but this reminds me why we always use the national cadastras transformation libraries for transforming data...

Oh well as you can probably understand from this post, SharpMap now supports on-the-fly transforrmation! How cool is that? :-) If you can't wait for the next release, grap the source from the GotDotNet workspace. Warning though: I haven't checked the rest of the formulas thoroughly yet, but Mercator projection is checked and corrected and UTM seems to be working...