OGR-MSSQL-Spatial and ReprojectLayersOnAdding ->AccessViolatonException

Dec 15, 2015 at 7:27 AM
Hi All,

using GlobalSettings.ReprojectLayersonAdding with an OGR SpatialDatabase-Layer throws an ugly AccessViolationException.

Maybe I missed something?

Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim connectionString As String = "MSSQL:Server=DELL-4-2015\SQLEXPRESS12; Database=DBNAME; User Id=USERNAME;Password=PASSWORD;"
        Dim layer = New OgrLayer()
        If layer.OpenFromDatabase(connectionString, "plz_de") Then
            ' only layer handle for the first layer being added is returned
            Dim layerHandle As Integer = AxMap1.AddLayer(layer, True) 'Exception thrown here!!!
        Else
            Debug.WriteLine("Failed to open: " + layer.ErrorMsg(layer.LastErrorCode))
        End If
 End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        AxMap1.Projection = tkMapProjection.PROJECTION_GOOGLE_MERCATOR
        AxMap1.TileProvider = tkTileProvider.OpenStreetMap
        AxMap1.KnownExtents = tkKnownExtents.keGermany
        Dim gs As New GlobalSettings
        gs.ReprojectLayersOnAdding = True
    End Sub
I'm using a fresh installed 4.9.3 with VS2013 and MSSQL2012SP2 on a WIN7/64 machine.
Any suggestions welcome.

Cheers
Burkhart
Dec 15, 2015 at 8:24 AM
Edited Dec 15, 2015 at 9:21 AM
Adding a shapefile to the map with reprojection works so far...
but trying to change the DrawingOptions of the reprojected shapefile fails (it seems the DefaultDrawingOptions are ignored).

Changing all datasources to the same projection everything works fine.

Cheers
Burkhart

EDIT: Okay, I've got that. Understanding reprojection helps...
If the layer is reprojected while adding, it is in fact a copy of the original shapefile in memory...

But the MSSQL-problem is still open.
Developer
Dec 15, 2015 at 10:04 AM
Hi Burkhart, I've just checked the code below with my current version (dev494 branch) and it worked:
string connection = @"MSSQL:server=.\;database=spatial;trusted_connection=yes";

var layer = new OgrLayer();
if (layer.OpenFromDatabase(connection, "buildings"))
{
    axMap1.AddLayer(layer, true);
}
For now I can't suggesting anything but to try it with different parameters (database, layers, etc.) to narrow down the problem.
Dec 15, 2015 at 12:24 PM
Edited Dec 15, 2015 at 12:26 PM
Hi Sergej,

thank you for your fast reply.

I'm stuck. I always get the AccessViolationException whenever the map is set to reprojectlayersonadd.
I checked the SRIDs of the spatial table, comes otu to 4326 which is quite right for wgs84.
I checked the feature count after open (layer.featurcount) which brings a correct number of features. So I think my connectionstring is correct (the data loads into wgs84 if ReprojectLayeronAdding is false).

I even tried testdata in UTM (so it is geometry not geography). Same thing happening.
I've tried another server (SQL-Server 2008). Same procedure.

Converting all data to pseudo mercator is no option.

What else can i check/test?
Any ideas?

Cheers
Burkhart

The complete exception stack is here:
System.AccessViolationException wurde nicht behandelt.
  HResult=-2147467261
  Message=Es wurde versucht, im geschützten Speicher zu lesen oder zu schreiben. Dies ist häufig ein Hinweis darauf, dass anderer Speicher beschädigt ist.
  Source=mscorlib
  StackTrace:
       bei System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
       bei MapWinGIS._DMap.AddLayer(Object Object, Boolean Visible)
       bei AxMapWinGIS.AxMap.AddLayer(Object object, Boolean visible)
       bei MapWinGIS_Test.Form1.Button1_Click(Object sender, EventArgs e) in C:\Entwicklung\VS\MapWinGIS_Test\MapWinGIS_Test\Form1.vb:Zeile 16.
       bei System.Windows.Forms.Control.OnClick(EventArgs e)
       bei System.Windows.Forms.Button.OnClick(EventArgs e)
       bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       bei System.Windows.Forms.Control.WndProc(Message& m)
       bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
       bei System.Windows.Forms.Button.WndProc(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.Run(ApplicationContext context)
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       bei MapWinGIS_Test.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 81.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Dec 15, 2015 at 12:51 PM
Digging further into this problem (maybe)...

Using QGIS I can access all spatial data on the server(s).

Now I've installed the latest MapWindow 5 and trying to connect I get a working connection but no databases layers are found!!
I'm clueless to this.
Maybe there is some 'metadata' on the server missinG? I've just tables with geometry or geograph columns (and tables with none-spatial-data) besides the standard system tables...

Still without any clue...

Cheers
Burkhart
Dec 15, 2015 at 2:15 PM
Importing a WGS-Shapefile to MSSQL via Mawindow5 results in a spatial table of geometry type, even if the srid is 4326 which should be geography. Such a layer throws the same exception as any other.

Loading a spatial layer with web mercator projection (with mapwingis) works like a charm.
So there seems to be something wrong with the reprojection (here).

Cheers
Burkhart
Dec 15, 2015 at 3:18 PM
Maybe it's possible to have a workaround?
Maybe I can load the data from the ogrlayer into a in-memory-shapefile and load the shapefile to the map?
Didnt't find a clever way yet.
Any recommendations?

Cheers
Burkhart
Coordinator
Jan 21, 2016 at 2:50 PM
We've made some changes for the geodatabase handling in the new installer which will be published in two weeks time.
Perhaps you can then try again with the latest version?

Regards,

Paul