ThinkGeo.com    |     Documentation    |     Premium Support

Using sql view as basis for MsSQLFeatureLayer?

Hello everyone,


I found a post about this, but the one that seemed most detailed is dated 4/20. I'm wondering if there is an update to that story.


I'd like to be able to have a feature layer based on a sql view. I created the view and tried referencing its name in place of the table name, but it came up with an error about not being able to find the geometry column. Incidentally, that's the same error you get if you don't have permissions to the table and if the table doesn't exist. In any case, I verifed that the name in the code matches the name on the server and that doesn't seem to be correct.


Has this functionality been implemented since the thread on 4/20? If so, is there something different I need to do to make this work?


Thank you,


Kimberly



Kimberly,


We have discussed this problem recently in several posts as following, please take a look if you are interested.


gis.thinkgeo.com/Support/Dis...fault.aspx

gis.thinkgeo.com/Support/Dis...fault.aspx


Any more questions please feel free to let me know.


Thanks.


Yale

 



Both of the links you posted above are the same. I didn't find anything newer in my searching, I'm sure I just didn't have the keywords right to find those specifically. 



So are you saying that I can't reference a view directly? I have a view on the server saved. I don't need to be writing the query into my code as it's already defined. I don't want to write the query into my code because unless there is a very solid reason, I think that writing SQL code in my C# code is bad programming practice. (Charles example is probably a good example, I won't have that issue at all, I need about 15 views total I think.)



Can I use a view that is already defined on the server without having to catch the event and write my own SQL? 



Thank you. 

Kimberly



Kimberly, 
  
   As far as I know you can access a view just as you would a normal table.  There is nothing special you need to do.  If I remember correctly we have a number of people doing this currently.  (If you are a customer on the forum doing this give us a shout)  The system can’t tell what is a table or what is a view, you just have to make sure the view returns the geometry column just like it was a spatial table.  
  
   The technique with the events are for people who want to tweak things a step further.  For example views are good in replacement for tables when you have some small number of pre-defined scenarios but some people want to dynamically change up filter criteria.  Their only option before we put in the support for the events was to create a bunch of views for every scenario.  Based on feedback that some customers have literally thousands of potential combination we allowed them to have the finest grain control over the SQL itself.  They can manipulate the SQL statement to add additional where clause items, joins, sub queries etc.  This means they have allot of control but at the cost of having to know exactly what they are doing and having to write some extra code. 
  
 David 


David, thanks for the response. I have gone back over everything I could think of to check. The error I'm getting is still that it can't find a geometry column. I have verified that I have permissions to the table, that the connection string is working. The view itself is working and does return a geoGraphy column.


Could that be an issue? I have several tables that I'm referencing now and they each have a geography column.


Thanks for the help.


Kimberly



David, I thought I should mention as few more things I have tested. My view is only limiting the results from a single table, meaning there are no joins in the view. If I reference the table directly from the code (in place of the view), I get a layer that has all the records in the table and no error. I changed the connection string to that of a different database that doesn't have the view, and I still get the same error message. (Sorry to keep harping on the error message not being helpful.)


Error message: 'Can not find the geometry column.'


Stack trace: "   at ThinkGeo.MapSuite.Core.MsSql2008FeatureSource.OpenCore()\r\n   at ThinkGeo.MapSuite.Core.FeatureSource.Open()\r\n   at ThinkGeo.MapSuite.Core.FeatureLayer.OpenCore()\r\n   at ThinkGeo.MapSuite.Core.Layer.Open()\r\n   at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.DrawCore(GeoCanvas canvas)\r\n   at ThinkGeo.MapSuite.DesktopEdition.Overlay.Draw(GeoCanvas canvas)\r\n   at ThinkGeo.MapSuite.DesktopEdition.WinformsMap.x5e1f8125aa040824(Object x2680f14bfcc5c488)\r\n   at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)\r\n   at System.Threading.ExecutionContext.runTryCode(Object userData)\r\n   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal(_ThreadPoolWaitCallback tpWaitCallBack)\r\n   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)"


Stack trace when running in single threaded mode: (Not sure, but since it's different, maybe it will help.)


"   at ThinkGeo.MapSuite.Core.MsSql2008FeatureSource.OpenCore()\r\n   at ThinkGeo.MapSuite.Core.FeatureSource.Open()\r\n   at ThinkGeo.MapSuite.Core.FeatureLayer.OpenCore()\r\n   at ThinkGeo.MapSuite.Core.Layer.Open()\r\n   at ThinkGeo.MapSuite.DesktopEdition.LayerOverlay.DrawCore(GeoCanvas canvas)\r\n   at ThinkGeo.MapSuite.DesktopEdition.Overlay.Draw(GeoCanvas canvas)\r\n   at ThinkGeo.MapSuite.DesktopEdition.WinformsMap.x5e1f8125aa040824(Object x2680f14bfcc5c488)\r\n   at ThinkGeo.MapSuite.DesktopEdition.WinformsMap.x958228b8a7148f83()\r\n   at ThinkGeo.MapSuite.DesktopEdition.WinformsMap.x5c390bc16fa1868b(Object xd9272088e65bd176, EventArgs xc2fd4c0ed406cdb7)\r\n   at System.Windows.Forms.Timer.OnTick(EventArgs e)\r\n   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)\r\n   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)\r\n   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)\r\n   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)\r\n   at System.Windows.Forms.Application.Run(Form mainForm)\r\n   at AWARE.Program.Main() in C:\\Repository\\AWARE\\Program.cs:line 20\r\n   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)\r\n   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n   at System.Threading.ExecutionContext.runTryCode(Object userData)\r\n   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ThreadHelper.ThreadStart()"


Thanks!


Kimberly



Kimberly,


Just want to let you know that when using the Geography data, there was a bug, this will probably cause your problem.

gis.thinkgeo.com/Support/Dis...aspx#11230


Any more questions just feel free to let me know.

Thanks.


Yale


 



I’m sorry, but I’m absolutely confused by this. In the past, I had some tables that had geometry columns and when I debugged, all the records showed up, but they weren’t showing on the map. I converted them all to geography and they all started appearing just fine. That’s still true today. The only time it’s not working for me is when trying to reference that same column through a view. Now you’re saying this won’t work unless I convert it all to geometry? What’s the deal?  
 Kimberly

Kimberly, 
  
 I think all he is saying is that we have a bug.  I will ensure we get this working with views and not with geometry and geography. 
  
 David

Thank you David! I appreciate the quick and precise answer!


Kimberly



Kimberly, 
  
   Don’t thank me just yet we still have to pull it off! :-) 
  
 David

Kimberly, 
  
   The changes have been integrated in the latest build.  Please contact support and ask for build 418.  The changes should cover the Geometry vs Geography and also the views usage for SQL 2008.  It also handles the Postgre schema issues. 
  
 David

Hello, I was able to get build 418 from support. Thank you for that. It did fix the issue with views not showing and giving the error about the geometry column. But now I have another issue. Just by installing the new version, with no other changes, the map load time went from less than 10 seconds (usually about 5) to now somewhere between 3 minutes and 15 seconds to 3 minutes and 45 seconds. Panning now, which before was nearly instantaneous, takes a full minute to show the updated areas of the map control. I implemented the tile caching based on the example in the developer reference, but that didn't seem to make much different. I haven't had much time to play with that yet though. I'm wondering if there's just some random other issue that accidentally got through in the dll set or something. (Maybe that doesn't even make sense, but I can't imagine this being a known issue.) Is there something I need to do drastically different with the new version? What are your thoughts on the matter?


Thank you,


Kimberly



Kimberly,


For this public release(3.0.415), our main enhancement is on the performance improvement compare to last public release. I was embrassed to see it would take 3 minutes to load the map. Could you give me more information to recreate your problem? How many layers are you loading when this case happened? And could you find the one which take the majority of time to load and create a simple sample for us? 


Following code shows the way to use the Geography view, here the States1 is a Geography datatable imported from States.shp.

string connectString = "Data Source=192.168.0.191;Initial Catalog=InternalDB;Persist Security Info=True;User ID=sa;Password=akeypmon1";
MsSql2008FeatureLayer sql2008Layer = new MsSql2008FeatureLayer(connectString, "States1", "ID");
sql2008Layer.Open();
sql2008Layer.ExecuteNonQuery("create view Statesview as select RECID, geom from States1 where FEMALES > '1000000' ;");
winformsMap1.CurrentExtent = sql2008Layer.GetBoundingBox();
winformsMap1.CurrentExtent.ScaleDown(50);
sql2008Layer.Close();

sql2008Layer = new MsSql2008FeatureLayer(connectString, "Statesview", "RECID");
sql2008Layer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Country1;
sql2008Layer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
winformsMap1.StaticOverlay.Layers.Add("Sql2008Layer", sql2008Layer);
       
winformsMap1.Refresh();

 

Sorry for the inconvinience.
 
Thanks.
 
Yale