Transfer data and C# / Silverlight utilities RRS feed

  • Question

  • Hey Guys,

    I want to show spatial data inside my Silverlight application: http://codeplex.com/deepearth . Since there could be a lot of data I want to minimise the transmission size and have the best performance possible. I also would love to support all of the great features of SQL 2008 spatial, for example holes in polygons, accurate area and length measurements.


    After a quick read it sounds like I should look at using WKB or the internal binary representation?


    What tools are available to deal with WKB or the internal binary format inside Silverlight / C#? Clearly we can't use the Microsoft.SqlServer.Types.dll inside silverlight (correct me if i'm wrong).


    We plan to add support for KML. Would a better direction be working with one of the SQL 2008 -> KML projects and just deal with KML on the Silverlight client?


    Are there C# source libraries anywhere?




    Wednesday, November 19, 2008 1:36 AM


  • i found : http://code.google.com/p/spatialsharp/
    read ,write WKB .
    Monday, November 24, 2008 3:07 AM

All replies

  • Hi John,

    I'm no silverlight expert, so what follows is just a general comment about some of the techniques I've tried - maybe it's of some use:

    All of the examples that I could find currently on the internet that move spatial data from SQL Server -> Virtual Earth retrieve data as WKT (using toString(), STAsText(), etc.) and then perform string manipulation in .NET to create the equivalent Pushpin / Polyline / Polygon javascript constructor methods. This is good for instructional purposes, and doesn't depend on any external libraries, but passing/parsing text is obviously not going to be very efficient for your needs.

    The only library I'm aware of that provides methods to deal with SQL Server's native binary format is SQLServer.Types.dll. If you can't use the methods provided by this in Sliverlight then I can't see any advantage of working with SQL Server binary over WKB. (The main difference between the two formats is that SQL Server native binary also serializes the SRID of the data, but if you're working with the virtual earth API you'll need this to be 4326 in every case anyway...)
    WKB, in contrast, is an industry standard, so if you built your design based on this then you could benefit from existing libraries (although you'll probably have to do some rewrites), and you could make your code database-neutral to make it easy to port the backend to any other spatial database that supports WKB: SQL Server -> Oracle -> PostGIS etc.

    KML, being an XML-based language, is clearly well suited for serialization over the internet - I don't think there are many mature projects to reliably create KML from SQL Server 2008 yet, but I'm sure they are coming. One significant advantage of KML is that, not only does it express the purely geometrical properties of features, but it can also contain styling / descriptive elements (line widths / fills / extrude to ground etc.) that specify how that feature should be displayed on the map. This differentiates it from GML, or from any of the other text- or binary- based formats.

    If it were me, I'd wait to see if somebody else can confirm whether it's possible to use SQLServerTypes.dll in Sliverlight. If so, use SQL Server native binary, and the methods provided by the dll. If not, use WKB (unless you want to take advantage of the additional styling elements provided in KML).

    Sorry - there's probably nothing new that you didn't already know there, but maybe it's of some help!

    Wednesday, November 19, 2008 9:02 AM
  • Thanks for your thoughts. Yes it will just be 4326.

    To use a dll in silverlight2 that dll must be compiled agaist silverlight2 (subset of the full .net framework). The SQL dll is probably a little large anyway (250KB ish).


    Do you recommend any good open source WKB libraries?


    If I had unlimited reasources the ultimate solution would be a cut down version of the SQL dll with just the ability to get access to the point, line, polygon and collection objects so we can draw these on the Map. Additionally Area, Length, Intersections, Buffer etc would be very valuable.


    We will impliment the KML import/export options and it all comes down to how small the code would be and how much faster / smaller the transmission size is to do something else like impliment WKB.


    thanks for your help,





    Wednesday, November 19, 2008 10:35 PM
  • i found : http://code.google.com/p/spatialsharp/
    read ,write WKB .
    Monday, November 24, 2008 3:07 AM
  • Nice Find!

    That looks like exactly what I'm after, thanks.

    I'll report back when I have something to show.


    Monday, November 24, 2008 11:31 AM
  • Hi,
    sorry my english Smile
    i have 1
    1. use WCF
    2. PathGeometry (in namespace System.Windows.MediaWink - same http://www.codeplex.com/SpatialViewer

    SqlGeography -> PathGeometry -> string (M98,89 L79,122 L117,119 L150,108 L161,58 L102,58 z .. )  

    use WCF send silverlight
    use http://www.codeplex.com/StringToPathGeometry

    cover string to Geometry -> show Silverlight ....

    ex: ...
    code : http://cid-6e8d6ce706d70065.skydrive.live.com/self.aspx/silverlight/SViewGis.zip


    so ,I'm no silverlight expert . 

    Code Snippet

    private Geometry Decode(SqlGeography g)
    PathGeometry result = new PathGeometry();
    switch (g.STGeometryType().Value.ToLower())
    case "point":
    PathFigure pointFig = new PathFigure();
    pointFig.StartPoint = new Point(g.Long.Value - 2, g.Lat.Value - 2);
    LineSegment pointLs = new LineSegment(new Point(g.Long.Value + 2, g.Lat.Value + 2), true);

    pointFig = new PathFigure();
    pointFig.StartPoint = new Point(g.Long.Value - 2, g.Lat.Value + 2);
    pointLs = new LineSegment(new Point(g.Long.Value + 2, g.Lat.Value - 2), true);

    return result;
    case "polygon":
    string cmd = new string(g.STAsText().Value).Trim().Substring(8);
    string[] polyArray = (cmd.Substring(1, cmd.Length - 2) + ", ").Split('(');

    var polys = from s in polyArray
    where s.Length > 0
    select s.Trim().Substring(0, s.Length - 3);

    PathFigure fig;
    foreach (var item in polys)
    fig = new PathFigure();
    var polyPoints = from p in item.Split(',')
    select p.Trim().Replace(" ", ",");
    fig.StartPoint = Point.Parse(polyPoints.ElementAt(0));
    for (int i = 1; i < polyPoints.Count(); i++)
    LineSegment ls = new LineSegment(Point.Parse(polyPoints.ElementAt(i)), true);


    return result;
    case "linestring":
    PathFigure lsfig = new PathFigure();
    lsfig.StartPoint = new Point(g.STPointN(1).Long.Value, g.STPointN(1).Lat.Value);

    for (int i = 1; i <= g.STNumPoints(); i++)
    LineSegment ls = new LineSegment();
    ls.Point = new Point(g.STPointN(i).Long.Value, g.STPointN(i).Lat.Value);

    return result;
    case "multipoint":
    case "multilinestring":
    case "multipolygon":
    case "geometrycollection":
    GeometryGroup mpG = new GeometryGroup();

    for (int i = 1; i <= g.STNumGeometries().Value; i++)

    return mpG;
    return Geometry.Empty;

    Thursday, December 4, 2008 2:55 PM
  • This is a very useful snippet of code, and seems to work nicely. However, I am not clear why you add/subtract 2 from the Long/Lat of the geometry. Is there a particular reason for this?

    Tuesday, July 28, 2009 1:27 AM