You read that right! In Q3 2010 OpenAccess ORM introduced new SQL Spatial Type support. Which is awesome for those of us that love playing with spatial data :) I started playing around with the new OA spatial support, and decided what better way to to demonstrate the capabilities than to output the data on to a map? This blog covers how I accomplished this task :)
First We Need Spatial Data
Some of you may already have spatial data sitting in a SQL Server DB, but if not never fear! You can use the great Shp2Sql utility, made by Morten Nielsen, to import a Shapefile to MS SQL.
You can just run the included SQL SpatialData.sql to create the database I use for this example(includes data).
Now For Our Model
So now it is time to create the OpenAccess domain model we will use.
1. Create a new class library in VS
2. Add a new telerik OpenAccess domain model.
3. Select Populate from existing database.
4. Select Your newly created DB, and click next.
5. Check the 2 tables we just added, and click finished.
Your visual designer should now look something like this:
As you can see the two properties in red are SqlGeography…Sweet!
On to the Application
1. Add a new Telerik WPF Application to your solution, and make sure to include Telerik.Windows.Controls.DataVisualization.dll.
2. Add a references to: Microsoft.SqlServer.Types, Telerik.OpenAccess.dll, Telerik.OpenAccess.35.Extensions.dll, Telerik.OpenAccess.40.Extensions.dll, and your model project.
3. Drag out a RadMap, add a map provider, and an information layer:
Also, make sure to add these name spaces:
Now we are ready for loading the data!
Using the Code
For this example I created a simple helper script that converts SqlGeography to RadMap shapes. It is an extension method for the Information layer, which makes it really easy to use.
In this code, we query for our entities, then pass them to the LoadSpatial() extension method, along with a Func to specify which property contains SqlGeography.
To load data into the ExtendedData property for a MapShape, I provide an overload that allows you to specify the mapping:
Here we tell the MapShape how to populate it’s ExtendedData property using the provided entity, that way we can use it in the map where needed. For example, in this demo I use the ExtendedData to populate tooltips:
As you can see I am displaying all of the counties in America in this example. In addition, I ran a query to load a few points of interest from my Locations table. Each MapShape shows additional information in it’s tool tip, which is pulled from the ExtendedData.
1. WPF only for this post at least. Although it should be quite easy to do something similar for Silverlight. Maybe next blog :)
2. Currently my extension method only handles OpenGisGeometryType: Point, MultiPoint, Polygon, and MultiPolygon. Feel free to add support for more!
Code used in this blog