ThinkGeo.com    |     Documentation    |     Premium Support

Sql Server Spatial Features

I am suddenly having trouble getting SQL server Spatial features to display.  The features are loaded into an Express database as geometry.  They have been displaying properly, and I don't now what changed to cause them to stop.  I have developed a simple app to test the problem and they still don't display.  The application is a single form with the WinFormMaps control added.  Code is as follows:


 

<code:lang="csharp">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
using ThinkGeo.MapSuite.DesktopEdition;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            OpenOverViewMap();
        }
        private void OpenOverViewMap()
        {
            winformsMap1.MapUnit = GeographyUnit.Feet;
            winformsMap1.CurrentExtent = new RectangleShape(395000.1, 390000.1, 590000.1, 199000.1);
            winformsMap1.BackgroundOverlay.BackgroundBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
            MsSql2008FeatureLayer worldLayer = new MsSql2008FeatureLayer(@"Server=CHARLESRLAPTOP\SQLEXPRESS;Database=Lakeland;Trusted_Connection=True;", "msiOverview", "ID");
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen.Width = 1;
            worldLayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen.Color = GeoColor.StandardColors.Black;
            worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
            LayerOverlay staticOverlay = new LayerOverlay();
            staticOverlay.Layers.Add("WorldLayer", worldLayer);
            winformsMap1.Overlays.Add(staticOverlay);
            winformsMap1.Refresh();
        }
    }
}

 


Any help will be appreciated.


Charles


 



Just to clarify, the code does not error out.  The form appears without any features on it.  Strangely enough, if I open worldLayer, GetAllFeatures returns 552 features, but GetFeaturesInsideBoundingBox and GetFeaturesOutsideBoundingBox both return 0 features.  I triple checked and the CurrentExtent should include all of the features in the data set.

Charles, 
  
   I think the sample code you attached looks fine.  I would suspect that something in SQL has changed.  If you have not upgraded the dll etc.  If the GetAllFeatures works but the GetFeaturesInsideBoundingBox does not it points to possibly a spatial index issue.  If you have a pretty recent version  of the the MapSuiteCore then you can wire up an event to help troubleshoot this.  You will need to wire it up on the Mssql2008FeatureLayer.FeatureSource directly.  To do that you need to get to it like below with a cast. 
  
 MsSql2008FeatureSource msSql2008FeatureSource = (MsSql2008FeatureSource)msSql2008FeatureLayer.FeatureSource; 
  
 Next hook up an event on the msSql2008FeatureSource.ExecutingSqlStatement 
  
 That event will get raised every t ime before we execute a SQL statement.  I suggest you put a break point in there and when you get to the GetFeaturesInsideBoundingBox call you extract the SQL out and run it directly against the SQL machine using some kind of viewer. 
  
 David

David, 
  
 I implemented the code you suggested.  The sql statement when getting features inside bounding box is as follows: 
  
 "SELECT Location.STAsBinary() as Location,ID FROM msiOverview WHERE (geometry::STGeomFromWKB(@geometry,4326).STIntersects(Location)=1);" 
  
 This project uses an unusual projection for which there is no SRID.  It uses a nonstandard projection string, when I am doing conversions, but for my test project I am not using Proj4Projection.  When I stored the data it was with an SRID of 0.  I see the 4326 SRID (Lat/Lon?) for the bounding box in the query.  How can it use a projected value for @geometry if Location has no SRID? 
  
 Charles

David, 
  
 I set worldLayer.SRID = 0 and everything works. 
  
 Thanks. 
 Charles

Charles, 
  
   I am glad you were able to get it to work.  That event is something we added recently and is really handy for debugging and also tweaking the SQL to get exactly what you want.  I am not all that familiar with the geometry versus geographic, the Srid, and way that SQL stores and indexes stuff.  We have some experts here but I am not one of them. :-)  If you run into anything else let us know and I can get someone more knowledgeable to help. 
  
 David