This project has moved and is read-only. For the latest updates, please go here.

PostGIS connection, VectorRenderer exception

Topics: Data Access, SharpMap v0.9 / v1.x, WinForms Controls
Apr 1, 2009 at 9:59 PM
Hi all, after successfully testing with SHP files I added the PostGIS data provider. The dll was created successfully with no errors and now when I try to use that DLL file in a project (the same was as I did with previous version of dll) with the following code:

_sharpMap =

new SharpMap.Map(new Size(600, 300));



VectorLayer layer = new SharpMap.Layers.VectorLayer("layer");



String connection = "Server=ip;Port=5432;User Id=user;Password=pass;Database=table";


layer.DataSource =

new SharpMap.Data.Providers.PostGIS(connection, "points_layer", "wkb_geometry");



picMap.Image = _sharpMap.GetMap();

I get the following exception:
System.TypeInitializationException was unhandled
  Message="The type initializer for 'SharpMap.Rendering.VectorRenderer' threw an exception."
       at SharpMap.Rendering.VectorRenderer.DrawPoint(Graphics g, Point point, Bitmap symbol, Single symbolscale, PointF offset, Single rotation, Map map)
       at SharpMap.Layers.VectorLayer.RenderGeometry(Graphics g, Map map, Geometry feature, VectorStyle style)
       at SharpMap.Layers.VectorLayer.Render(Graphics g, Map map)
       at SharpMap.Map.GetMap()
       at yyyy.Form1..ctor() in C:\Documents and Settings\xxxx\My Documents\Visual Studio 2008\Projects\Project1\yyyy\yyyy\Form1.cs:line 42
       at yyyy.Program.Main() in C:\Documents and Settings\xxxx\My Documents\Visual Studio 2008\Projects\Project1\yyyy\yyyy\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.ArgumentException
       Message="Value of 'null' is not valid for 'stream'."
            at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
            at System.Drawing.Image.FromStream(Stream stream)
            at SharpMap.Rendering.VectorRenderer..cctor()

The connection seems to be working since when the wrong login data is entered, I get an exception message from Postgres server about the wrong login attempt.


Any help will be very apreciated, thank you in advance,


Apr 1, 2009 at 11:15 PM
Hi Matej not sure if it is an obfuscation error but... in your connection string above you are setting db to table.. db should be set to the db catalog name.. The error message however looks more like it is due to the defaultSymbol param failing to be set or accessed (perhaps you changed the image but didn't set the embedded resource property?)... run it as a debug  build and the error message should be clearer.. hth jd
Apr 2, 2009 at 10:33 AM
Hi John, that was an obfuscation error, as you thought, I connect to the db catalog. When running in debug build, I get the bold line as problematic:


case "SharpMap.Geometries.Point":


SharpMap.Rendering.VectorRenderer.DrawPoint(g, (Point)feature, style.Symbol, style.SymbolScale, style.SymbolOffset, style.SymbolRotation, map);



with the message: "The type initializer for 'SharpMap.Rendering.VectorRenderer' threw an exception."

Is it possible that something is wrong with my compiled SharpMap.dll? This PostGIS database is being used every day by Mapserver and other software I don't think the problem can be on its' side. I would really like to get this thing working, if you need any more information, please inform me.



Apr 2, 2009 at 11:01 AM
Edited Apr 2, 2009 at 11:08 AM
I don't think it is an error with the Database, I suspect the default image used for point symbolizing is not embedded in the dll.. Try explicitly setting a style for the layer and setting the Symbol property to a bitmap .

(layer.Style as VectorStyle).Symbol = Bitmap.FromFile(path);

hth jd

EDIT.. That still may not work... because the image will still not be there when the VectorRenderer class is first initialized.. look at line 31 of the VectorRenderer class and make sure the embedded resource at the given path exists in the project at the correct location and has the Embedded Resource file setting
Apr 2, 2009 at 12:13 PM
I tried with

(layer.Style as SharpMap.Styles.VectorStyle).Symbol = (Bitmap)Bitmap.FromFile(@"c:\symbol.bmp");

and after adding layer and calling ZoomToExtents and then picMap.Image = _sharpMap.GetMap(); I get the same exception.



Apr 2, 2009 at 12:17 PM
Are you buildings SharpMap from source? or are you using the version on the Release tab?
Apr 2, 2009 at 12:30 PM
I have the version from (source v.09) and then I have added PostGIS.cs (which I downloaded from somewhere on the SharpMap web page where the solution on how to use SharpMap with PostGIS db) and Npgsql.* and used that new .dll.
Are there any other better steps?
Thanks for your time!
Apr 2, 2009 at 12:38 PM
Personally I would go for the Trunk on the Source Code Tab.. Use Tortoise to get the code, that way you can keep it up to date very easily..
Apr 2, 2009 at 12:49 PM
just recompiled with the one from Trunk and it works!
----- Original Message -----
From: [email removed]
To: [email removed]
Sent: Thursday, April 02, 2009 1:38 PM
Subject: Re: PostGIS connection, VectorRenderer exception [SharpMap:52017]

From: johndiss

Personally I would go for the Trunk on the Source Code Tab.. Use Tortoise to get the code, that way you can keep it up to date very easily..