Make Microsoft SQL Server geospatial

by Morten 5/14/2006 5:08:02 PM

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.

Tags:

SharpMap

Comments

5/17/2006 4:18:11 AM

Gustaf

Very cool! It works great. I had trouble with comma as decimal separator, but with a point it works great.

Gustaf

5/20/2006 1:04:24 AM

NotSoSharpGIS

Fantastic! I'm just guessing, but I should be able to edit information in the SQLServer database. I haven't tried yet but if this method is as fast as you say it is, it'll be the one I use in my application, especially with the added ability for the user to upload shapefile data. I hope you're enjoying this work because I know there are alot of us out here that are in debt to you for putting such a great product out. I'm learning quickly so hopefully I'll be of more assistance shortly.

Everyone else, KEEP CLICKING ON THOSE GOOGLE ADS ON THE SHARPMAP PAGE.

NotSoSharpGIS

6/24/2006 12:23:05 AM

Dao Lam Tung

HI.
I can not set up database. It show err.
Pls, help me run this demo.
Thank you.

Dao Lam Tung

6/24/2006 12:34:54 AM

Diego Guidi

Storing geodata in binary format it's the same way i've used for my tests with Sql Server 2005... i've integrated NTS as CLR assembly and from T-SQL it's possible to call topologycal functions on gis data, such calculate polygons area and buffer... and even aggregates are possible.

Diego Guidi

6/24/2006 8:34:27 PM

Diego: The problem with the custom datatypes in the CLR is that they can only contain 8kb, which is approximately 500 vertices

Morten

7/21/2006 10:16:36 AM

Ricardo Stuven

Oops... I had not read Diego's comment on the same thing I worked today: sharpmap.iter.dk/Forum/default.aspx?threadid=383

Ricardo Stuven

Comments are closed

Powered by BlogEngine.NET 1.6.1.0

RecentComments

Comment RSS

About the author

Morten Nielsen

Morten Nielsen
<--That's me
E-mail me Send mail

Twitter @SharpGIS 

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009