Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
622
Spatial data with SqlShapeReader, SQL geometry type and LinQ
posted

Hi,

I'm trying to implements a xamMap using a SqlShapeReader to retrieve spatial datas from a database.

The thing is, the requests to the database are implements with LinQ and I put the results into an ObservableCollection that I convert to an IEnumerable for the DataSource property of the SqlShapeReader.
But LinQ doesn't support the SQL geometry type, so I converted the SQL geometry field in varbinary(max) and linked my LinQ requests - and the Data field in the DataSource property - on it.

I tried to stock the result of my request into a CLR Binary field.
First, I noticed that the CLR Binary values and the SQL Binary values was completly differents, seems like, during the mapping LinQ between CLR Types and SQL Types, values are changed.
Then, the SqlShapeReader's DataSource property - where I put my IEnumerable containing the spatial data - seems to not recognize the CLR Types I tried to put in.

My question is: I need to know what kind of CLR Type the control SqlShapeReader need to have in its DataSource property (and particulary in the Data field in this property). I already tried CLR Types like: SqlGeometry, SqlChars, SqlString, byte[], Binary... Nothing worked.

Hoping my demand is clear,
Many thanks for your answers.

PS: to clarify:
View.xaml:

<ig:XamMap        x:Name="xamMap" 
                       Loaded="XamMap_Loaded" >
            <ig:XamMap.Layers>
                <ig:MapLayer Name="World" FillMode="RandomInterpolate" VisibleFromScale="0">
                    <ig:MapLayer.Reader>
                        <ig:SqlShapeReader DataMapping="Data=CoordGET_Polygon; Name=Department; Value=GETregion; Caption=CODEGET; ToolTip=GETregion">
                        </ig:SqlShapeReader>
                    </ig:MapLayer.Reader>
                </ig:MapLayer>
            </ig:XamMap.Layers>
        </ig:XamMap>



View.xaml.cs:

 private void XamMap_Loaded(object sender, RoutedEventArgs e)
        {
            XamMap map = sender as XamMap;
            xamMapViewModel mapVM = this.DataContext as xamMapViewModel;

            SqlShapeReader sqlReader = xamMap.Layers["World"].Reader as SqlShapeReader;
            if (sqlReader != null)
            {
                sqlReader.DataSource = mapVM.InitializeMap();
                xamMap.Layers["World"].ImportAsync();
            }
        }



ViewModel.cs (convert the ObservableCollection into an IEnumerable<Map> collection):

public IEnumerable<Map> InitializeMap()
        {
            try
            {
                xamMapController mapController = new xamMapController();
                DispatcherNotifiedObservableCollection<Map> data = mapController.ReadMap();

                IEnumerable<Map> ret = data.ToList();

                return ret;
            }
            catch (Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString(), "Erreur lors de la lecture de la carte");
            }
            return null;
        }



Controller.cs (put the request's answer into an ObservableCollection):


public DispatcherNotifiedObservableCollection<Map> ReadMap()
        {
            DispatcherNotifiedObservableCollection<Map> collection = new DispatcherNotifiedObservableCollection<Map>();
            DataContextController dtc = DataContextController.Instance;
            try
            {
                if (dtc.MapInfo == null)
                {
                    foreach (var map in (GetManager<IManageRead<GETs_RTE>>()).ReadAll())
                    {
                        collection.Add(new Map { InternalEntity = map });
                    }
                    dtc.MapInfo = collection;

                    return dtc.MapInfo;
                }
            }
            catch (Exception ex)
            {
                TreatException("DispatcherNotifiedObservableCollection<Map> GetAll()", ex);
            }
            return null;
            
        }


Manager.cs (request LinQ):


public IQueryable<GETs_RTE> ReadAll()
        {
            return getAllMap(DataContext);
        }




Map.cs:

Representing the instance of the SQL table containing spatial data




  • 34510
    Verified Answer
    Offline posted

    Hi dupond,

    First I recommend that you take a look at this help topic if you haven't already: http://help.infragistics.com/Help/NetAdvantage/Silverlight/2013.1/CLR4.0/html/xamWebMap_Geospatial_Database_Integration.html

    This demonstrates a way to do what you're looking for, however, it doesn't use LINQ.  The reason I'm recommending that you look at it is because it shows you how the datasource needs to be formatted.  If you look in the GetData method halfway down the page, you will see that it returns a collection of dictionaries.  (IEnumerable<Dictionary<string, string>>)  This type of collection is what needs to be set as the DataSource for the SqlShapeReader.

    Each dictionary in the collection represents a shape and each entry in the dictionary represents the property and it's value (key/value pair) for the shape.  For example, if we used countries as an example our datasource might look something like this:

    Collection Entry 1:

        Dictionary for 'United Kingdom'

            Key: "Name" / Value: "United Kingdom"

            Key: "CountryCode" / Value: "UK"

            Key: "SpatialValue" / Value: "MULTIPOLYGON(((-0.835...."

    Collection Entry 2:

        Dictionary for 'Albania'

            Key: "Name" / Value: "Albania"

            Key: "CountryCode" / Value: "AL"

            Key: "SpatialValue" / Value: "POLYGON((20.9834...."

    In this example, SpatialValue represents the actual shape geometry.  This is then hooked up via the DataMapping property in the SqlShapeReader.

    <ig:SqlShapeReader DataMapping="Name=Name;Data=SpatialValue;Caption=CountryCode" />

    You can see an example of this in samples browser online: http://ko.infragistics.com/products/silverlight/sample/map/#/binding-to-spatial-data

    I've also attached the xml file used by the sample that contains the data.  This way you can see how the data is organized and how the code loads it into the SqlShapeReader.

    Let me know if you have any questions on this.

    MapSpatialData.zip