New to SharpMap and C#

Dec 5, 2008 at 5:02 PM

Hi

What is the procedure for referencing the SharpMap library to a new .exe application in order to open SharpMap.

Do I use 'Add Reference' and add all of the .dll's in the SharpMap library?

Is there some other method?

Thanks in advance for any help.

Bob

Coordinator
Dec 6, 2008 at 3:33 PM
Hi Bob, it depends which bits of functionality you need. I am not sure which version of sharpmap you are using (I am assuming 0.9) but you will always reference the sharpmap.dll

To create an exe using the win forms controls you will also reference the sharpmap.ui.dll

Then if you require any of the extra provider or layer types you would also reference sharpmap.extensions.dll

hth jd
Dec 7, 2008 at 6:00 PM
Thanks John
I can now compile the application which has been assembled to show a shape file and references the .ui and .extensions dll.
What I want to do is make a viewer (SRID = -1) looking directly at Postgis WKB tables.
If I understand correctly I need to include the GeometryFromWKB file, and the layers folder??
What function does the Geometries folder provide? Are the geometries needed to form WKB or to form geometries from WKB?
Bob
----- Original Message -----
From: [email removed]
To: [email removed]
Sent: Saturday, December 06, 2008 8:33 AM
Subject: Re: New to SharpMap and C# [SharpMap:41603]

From: johndiss

Hi Bob, it depends which bits of functionality you need. I am not sure which version of sharpmap you are using (I am assuming 0.9) but you will always reference the sharpmap.dll

To create an exe using the win forms controls you will also reference the sharpmap.ui.dll

Then if you require any of the extra provider or layer types you would also reference sharpmap.extensions.dll

hth jd
Dec 8, 2008 at 7:07 AM
Hi Bob
If you have referenced the dll's, all you need to do in order to display data from PostGis is something like the following:

VectorLayer layDistricts = new VectorLayer("Districts"); //define layer

layDistricts.DataSource = new SharpMap.Data.Providers.PostGIS(Configuration.AppConfiguration.ConnectionString, "vv_get_adm_area_district", "polygon", "gid"); //set the datasource

//set some style
layDistricts.Style.Fill = new SolidBrush(Color.AntiqueWhite);

layDistricts.Style.EnableOutline = true;

layDistricts.Style.Outline = new Pen(Color.LightBlue, 1.5f);

layDistricts.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

nb: i have my connectionstring in separate class.

Best Peter

Coordinator
Dec 8, 2008 at 9:14 AM
Hi Bob, Peter is right, you shouldn't need to reference folders or files per se, occasionally you may want to do this if you only need a very small subset of functionality from a library which is easily extracted - but usually you would just reference the dll.
In the case of the PostGis provider all conversion from the db wkb into the live Geometry objects should be built in.

In the case of the Geometries folder,  the contents are compiled into the classes in the SharpMap.Geometries namespace - these are the actual 'models' representing a geometry, whereas wkb is a storage/interchange format which needs to be parsed and built into a model before any meaningful operation can be done on it. hth jd
Dec 11, 2008 at 4:30 PM
Hi

I am getting an error message during compile.

“InvalidOperationException("No layers to render");”
(Layers == Null; LayerCount == 0)

Following is the code that I have assembled so far.
If the message is not misleading it appears that I may be missing something.

Help would be greatly appreciated.

Bob

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 SharpMap.Layers;
using System.Collections.ObjectModel;


namespace WindowsFormsApplication1
{
public partial class frmGraphics : Form
{
//--> Define the SharpMap object
SharpMap.Map _sharpMap;

//--> Zoom Factor
const float ZOOM_FACTOR = 0.3f;

public frmGraphics()
{
InitializeComponent();

//--> Initialize the Map
_sharpMap = new SharpMap.Map(new Size(1300, 750));
_sharpMap.BackColor = Color.Silver;



//--> Zoom to extent
_sharpMap.ZoomToExtents();
RefreshMap();



SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
string ConnStr = "Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=XXX;";
myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "P_ID.P_ID", "the_geom");

//Setup linestyle 
myLayer.Style.Line.Width = 2;
myLayer.Style.Line.Color = Color.Black;
myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end


myLayer.Style.EnableOutline = true;
myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines
myLayer.MaxVisible = 40000;

_sharpMap.Layers.Add(myLayer);




Coordinator
Dec 11, 2008 at 6:05 PM
Hi Bob, I think it is just the order in which things are happening: You are calling map.ZoomToExtents before you have added a layer hence the extents are null.

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 SharpMap.Layers;
using System.Collections.ObjectModel;


namespace WindowsFormsApplication1
{
public partial class frmGraphics : Form
{
//--> Define the SharpMap object
SharpMap.Map _sharpMap;

//--> Zoom Factor
const float ZOOM_FACTOR = 0.3f;

public frmGraphics()
{
InitializeComponent();

//--> Initialize the Map
_sharpMap = new SharpMap.Map(new Size(1300, 750));
_sharpMap.BackColor = Color.Silver;

//--> Add the first Layer
SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
string ConnStr = "Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=XXX;";
myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "P_ID.P_ID", "the_geom");

//Setup linestyle 
myLayer.Style.Line.Width = 2;
myLayer.Style.Line.Color = Color.Black;
myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end


myLayer.Style.EnableOutline = true;
myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines
myLayer.MaxVisible = 40000;

_sharpMap.Layers.Add(myLayer);


//--> Zoom to extent
_sharpMap.ZoomToExtents();
RefreshMap();





Dec 11, 2008 at 10:37 PM
Thanks John
That seems to work.
However I now get a message "Function AsBinary does not exist"
The AsBinary works in PostgreSQL.
Any thoughts would be appreciated.
Bob
----- Original Message -----
From: [email removed]
To: [email removed]
Sent: Thursday, December 11, 2008 11:05 AM
Subject: Re: New to SharpMap and C# [SharpMap:41603]

From: johndiss

Hi Bob, I think it is just the order in which things are happening: You are calling map.ZoomToExtents before you have added a layer hence the extents are null.

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 SharpMap.Layers;
using System.Collections.ObjectModel;


namespace WindowsFormsApplication1
{
public partial class frmGraphics : Form
{
//--> Define the SharpMap object
SharpMap.Map _sharpMap;

//--> Zoom Factor
const float ZOOM_FACTOR = 0.3f;

public frmGraphics()
{
InitializeComponent();

//--> Initialize the Map
_sharpMap = new SharpMap.Map(new Size(1300, 750));
_sharpMap.BackColor = Color.Silver;

//--> Add the first Layer
SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
string ConnStr = "Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=XXX;";
myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "P_ID.P_ID", "the_geom");

//Setup linestyle
myLayer.Style.Line.Width = 2;
myLayer.Style.Line.Color = Color.Black;
myLayer.Style.Line.EndCap = System.Drawing.Drawing2D.LineCap.Round; //Round end


myLayer.Style.EnableOutline = true;
myLayer.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //Render smooth lines
myLayer.MaxVisible = 40000;

_sharpMap.Layers.Add(myLayer);


//--> Zoom to extent
_sharpMap.ZoomToExtents();
RefreshMap();





Coordinator
Dec 11, 2008 at 10:53 PM
Hi Bob, unfortunately I dont really use PostGIS so I cant really help.. I'm sure somebody else will though.. cheers jd
Coordinator
Dec 12, 2008 at 7:23 AM
Hello Bob,
you are sure that the database you are connecting to is either the postgis database or made up from the postgis_template.
Ordinary postgresql databases do not have the AsBinary function.
If you are using a different schema than 'public' you must set the search path for that schema to include public.
hth
FObermaier
Dec 12, 2008 at 4:19 PM
My Postgis geometry tables were not set up quite properly.
I can now access a table in the public schema.
If I want to access a table (P_ID) in the schema (P_ID) -as below- how do I add the public schema to the search path.

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

string ConnStr = "Server=localhost;Port=5432;User Id=postgres;Password=*****;Database=PDW;";

myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "p_id.p_id", "the_geom");

Bob

----- Original Message -----
From: [email removed]
To: [email removed]
Sent: Friday, December 12, 2008 12:23 AM
Subject: Re: New to SharpMap and C# [SharpMap:41603]

From: fobermaier

Hello Bob,
you are sure that the database you are connecting to is either the postgis database or made up from the postgis_template.
Ordinary postgresql databases do not have the AsBinary function.
If you are using a different schema than 'public' you must set the search path for that schema to include public.
hth
FObermaier
Coordinator
Dec 13, 2008 at 8:04 AM
Hi Bob,
to set the search path  to your schema you have connet to your database (with pgAdmin3 or psql) and
run the following command.
SET search_path TO p_id, public;

I'm not sure though, whether the postgis provider for sharpmap v1 does support schemas. I havn't tried that. hth FObermaier
Dec 13, 2008 at 8:40 PM

The following seems to be working.

 myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "p_id.p_id", "the_geom", "public");

Thanks all for your help.

Bob

Dec 19, 2008 at 11:44 AM
Hi, I'm having problems working with SharpMap. I've basically pulled in the SharpMap and SharpMap.UI projects, as well as all the App_Data and App_Code files into my application. I tried replicating Simple.aspx from the demo, but it doesn't display the map image at all. I think the problem might be in constructing the ImageUrl:

this.ImageButton1.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);

There isn't a 'getmap.aspx' file anywhere from the download that I could find. Does anyone know what could be wrong? Thanks.

P.S. the demo worked just fine, tho.

Here's my Page_Load code snippet (Thanks in advance!!):

myMap = new SharpMap.Map(new Size((int)this.ImageButton1.Width.Value, (int)this.ImageButton1.Height.Value));

//Set up the countries layer

SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");

//Set the datasource to a shapefile in the App_data folder

layCountries.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~\App_data\countries.shp"), true);

//Set fill-style to green

layCountries.Style.Fill = new SolidBrush(Color.Green);

//Set the polygons to have a black outline

layCountries.Style.Outline = System.Drawing.Pens.Black;

layCountries.Style.EnableOutline = true;

layCountries.SRID = 4326;

HttpContext.Current.Trace.Write("Initializing map...");

//Add the layers to the map object.

//The order we add them in are the order they are drawn, so we add the rivers last to put them on top

myMap.Layers.Add(layCountries);

//limit the zoom to 360 degrees width

myMap.MaximumZoom = 360;

myMap.BackColor = Color.LightBlue;

myMap.Zoom = 360;

myMap.Center = new SharpMap.Geometries.Point(0, 0);

HttpContext.Current.Trace.Write("Map initialized");

if (Page.IsPostBack)

{

//Page is post back. Restore center and zoom-values from viewstate

myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"];

myMap.Zoom = (double)ViewState["mapZoom"];

}

else

{

//Save the current mapcenter and zoom in the viewstate

ViewState.Add("mapCenter", myMap.Center);

ViewState.Add("mapZoom", myMap.Zoom);

//Render map

System.Drawing.Image img = myMap.GetMap();

string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);

              this.ImageButton1.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);

}

 

Coordinator
Dec 19, 2008 at 12:26 PM
Hi shanahime, GetMap.aspx is an ashx handler mapped through the web.config file hth jd

<

 

httpHandlers>

 

<

 

add verb="*" path="GetMap.aspx" type="SharpMap.Web.HttpHandler,SharpMap"/>

 

</

 

httpHandlers>

 

Jan 8, 2009 at 1:07 PM
Cannot view the map (POSTGIS layer) using SharpMap

I am new to GIS. I am using POSTGIS with pgrouting. I created a dijsktra_result table and viewed it through Quantum GIS, it worked fine.

But when I tried to work with sharpMap 0.9 and Visual Studio 2005, it dont display the map on windows form. But it dont give errors. I did included POSTGIS.cs in the trunk folder too.

Please provide me with details on my error. Thanks.

Herewith I have attached the code segment :

private void onPaint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SharpMap.Map myMap = new SharpMap.Map(new Size(400, 400));
            myMap.MinimumZoom = 100; //Minimum zoom allowed
            myMap.BackColor = Color.White; //Set background
            myMap.Center = new SharpMap.Geometries.Point(2511300, 7041040); //Set center of map
            myMap.Zoom = 1200; //Set zoom level
            
            myMap.Size = new System.Drawing.Size(400, 400); //Set output size

            //Add PostGIS layer:
            SharpMap.Layers.VectorLayer myLayer1 = new SharpMap.Layers.VectorLayer("My layer1");
            string ConnStr1 = "Server=127.0.0.1;Port=5432;UserId=postgres;Password=pass;Database=testdb;";

            myLayer1.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr1, "edges", "the_geom", "id");
            myLayer1.MaxVisible = 4000;
            myMap.Layers.Add(myLayer1);

            //Add PostGIS layer:
            SharpMap.Layers.VectorLayer myLayer = new SharpMap.Layers.VectorLayer("My layer");
            string ConnStr = "Server=127.0.0.1;Port=5432;UserId=postgres;Password=pass;Database=testdb;";
                      
            myLayer.DataSource = new SharpMap.Data.Providers.PostGIS(ConnStr, "dijsktra_result", "the_geom","gid");
            myLayer.MaxVisible = 4000;
            myLayer.Style.Fill = Brushes.Aquamarine;
            myLayer.Style.Outline = System.Drawing.Pens.Black;
            myLayer.Style.EnableOutline=true;
            myMap.Layers.Add(myLayer);

            //Render the map
            System.Drawing.Image imgMap = myMap.GetMap();
            //myMap.ZoomToExtents();
            g.DrawImage(imgMap,3,10);


Coordinator
Jan 8, 2009 at 2:08 PM
hi supunmali, it looks like you were nearly there.. you need to call :

myMap.ZoomToExtents();
System.Drawing.Image imgMap = myMap.GetMap();
g.DrawImage(imgMap,3,10);


hth jd

Jan 10, 2009 at 4:06 AM
I tried that, but it don't work. Still it wont display any map, but don't give errors also. I did put as below after adding POSTGIS layers.

myMap.ZoomToExtents();
System.Drawing.Image imgMap = myMap.GetMap();
g.DrawImage(imgMap,3,10);

Pls provide details on what I have done wrong. (thanks for the quick response).
Coordinator
Jan 10, 2009 at 9:13 AM
Perhaps you need to increase the layer.MaxVisible value (or comment them out completely) hth jd
Jan 10, 2009 at 1:02 PM
Thank u so much. When maxVisible value is commented, it works fine.