This project has moved. For the latest updates, please go here.

How to add a PostGIS datasource to the Map

The PostGIS provider is used for rendering geometry data from an PostgreSQL database. You will need to install the PostGIS extension in PostgreSQL to use it with SharpMap.

To get proper performance, make sure you have applied a spatial index to the geometry column.

// Connectionstring
string connStr = "Server=;Port=5432;User Id=postgres;Password=pass;Database=myMapDB";
// Name of table in database, you may prepend the schema (e.g. \"public\".\"Roads\")
string tablename = "Roads";
// Name of object ID column - MUST be integer and unique!
string idColumn = "gid";

// Initialize map object
SharpMap.Map myMap = new SharpMap.Map(new System.Drawing.Size(500,250));

//Create layer
SharpMap.Layers.VectorLayer layRoads= new SharpMap.Layers.VectorLayer("Roads");
//Set the datasource to the PostgreSQL table
layRoads.DataSource = new SharpMap.Providers.PostGis(connStr, tablename, idColumn); 
 * Note: Additionally you can specifiy the name of the geometry column to use, in case you 
 * have more than one.
 * layRoads.DataSource = 
       new SharpMap.Providers.PostGis(connStr, tablename, "geom3857", idColumn); 

//Add layer to map

 //Zoom to extents
//Render map
Image img = myMap.GetMap();

You can find more help at the PostGIS website on how to load map data into the PostGreSQL database, and how to install PostGIS properly on you database.

Note: In earlier versions PostGIS was included in SharpMap, but is now a seperate extension that needs to be added to SharpMap. See PostGIS

Last edited Aug 31, 2012 at 1:05 PM by FObermaier, version 5