Search code examples
c#sql-serversilverlightbing-mapssqlgeography

silverlight c# sql geography to xaml


i'm having some trouble when converting from Sql Server Geography data to Silverlight XAML.

/* Database query spatial data structure for the SQL Server spatial data types object */

var geo = SqlGeography.STGeomFromText (new SqlChars(new SqlString(polygon.ToString())), 4326);

/* Spatial data structure for the Bing Maps graphical objects (polygons) XAML text, to resolve Xaml directly returned to the client in the Silverlight application object. */

for (int j =   0; j < geo.NumRings(); j++)

The problem: geo.NumRings() method returns null, but i have 2 rings inside my polygon object.

the print screen below should explain better

link print screen source code and geo object data


Solution

  • Your image is showing a MultiPolygon which is an array of Polygons. Each polygon has an array of rings. As such, when the geometry is a Multi-shape, you will need to loop through each of its child geometries and parse it. You can loop through the child geometries by using the STNumGeometries and STGeometryN methods.

    That said, I don't recommend doing any development with Silverlight. The Bing Maps Silverlight control will be deprecated in November as noted here.

    The latest Bing Maps JavaScript control (V8) would be much easier to connect to your SqlGeography objects too and is also a lot faster than the Silverlight control. The V8 control has a built in Well Known Text module, which means you simply need to do geom.STAsText and return that data to this module in JavaScript and you would be able to render the shape on the map really easily. The Bing Maps Silverlight control can handle about 1MB or 2MB of polygon data before it becomes slow, where as the V8 control has been tested with over 40MB of data and performed even better than the Silverlight control when loaded with 2MB of data.